1
|
import { Injectable } from "@angular/core";
|
2
|
import { ActivatedRoute, Router } from '@angular/router';
|
3
|
import { deleteCookie, getCookie } from '../domain/utils';
|
4
|
import { Http } from '@angular/http';
|
5
|
|
6
|
@Injectable()
|
7
|
export class AuthenticationService {
|
8
|
|
9
|
constructor(private route: ActivatedRoute,
|
10
|
private router: Router,
|
11
|
private http: Http) {}
|
12
|
|
13
|
private apiUrl : string = process.env.API_ENDPOINT;
|
14
|
private loginUrl: string = process.env.AAI_ENDPOINT;
|
15
|
private baseUrl: string = process.env.BASE_URL;
|
16
|
|
17
|
// store the URL so we can redirect after logging in
|
18
|
public redirectUrl: string;
|
19
|
|
20
|
private _storage: Storage = sessionStorage;
|
21
|
|
22
|
public activateFrontAuthorization: boolean = process.env.PRODUCTION;
|
23
|
|
24
|
isLoggedIn: boolean = false;
|
25
|
|
26
|
public loginWithState() {
|
27
|
console.log(`logging in with state. Current url is: ${this.router.url}`);
|
28
|
if (this.redirectUrl) {
|
29
|
let url = this.redirectUrl;
|
30
|
this.redirectUrl = null;
|
31
|
console.log('stored location', url);
|
32
|
sessionStorage.setItem("state.location", url);
|
33
|
} else {
|
34
|
/*sessionStorage.setItem("state.location", this.router.url);*/
|
35
|
sessionStorage.setItem("state.location", '/dashboard');
|
36
|
}
|
37
|
console.log('redirect location', sessionStorage.getItem('state.location'));
|
38
|
window.location.href = this.loginUrl;
|
39
|
}
|
40
|
|
41
|
public logout() {
|
42
|
deleteCookie('openAIREUser');
|
43
|
deleteCookie('AccessToken');
|
44
|
sessionStorage.removeItem('name');
|
45
|
sessionStorage.removeItem('email');
|
46
|
sessionStorage.removeItem('role');
|
47
|
this.isLoggedIn = false;
|
48
|
|
49
|
console.log('logging out, calling:');
|
50
|
console.log(`${this.apiUrl}/openid_logout`);
|
51
|
|
52
|
/*window.location.href = `${this.apiUrl}/openid_logout`;*/
|
53
|
window.location.href = `https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.apiUrl}/openid_logout`;
|
54
|
}
|
55
|
|
56
|
public tryLogin() {
|
57
|
if( getCookie('openAIREUser') && (getCookie('openAIREUser') !== '') ) {
|
58
|
console.log(`I got the cookie!`);
|
59
|
console.log(`in tryLogin -> document.cookie is: ${document.cookie.toString()}`);
|
60
|
/* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTDOWN */
|
61
|
setInterval(() => {
|
62
|
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
|
63
|
userInfo => {
|
64
|
console.log("User is still logged in");
|
65
|
console.log(userInfo.json());
|
66
|
this.isLoggedIn = true;
|
67
|
},
|
68
|
() => {
|
69
|
sessionStorage.removeItem('name');
|
70
|
sessionStorage.removeItem('email');
|
71
|
sessionStorage.removeItem('role');
|
72
|
deleteCookie('openAIREUser');
|
73
|
deleteCookie('AccessToken');
|
74
|
this.isLoggedIn = false;
|
75
|
this.router.navigate(['/landing']);
|
76
|
}
|
77
|
);
|
78
|
},1000 * 60 * 5);
|
79
|
if(!sessionStorage.getItem('name')) {
|
80
|
console.log(`session.name wasn't found --> logging in via repo-service!`);
|
81
|
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
|
82
|
userInfo => {
|
83
|
console.log(userInfo.json());
|
84
|
sessionStorage.setItem('name',userInfo.json()['name']);
|
85
|
sessionStorage.setItem('email',userInfo.json()['email']);
|
86
|
sessionStorage.setItem('role',userInfo.json()['role']);
|
87
|
this.isLoggedIn = true;
|
88
|
console.log(`the current user is: ${sessionStorage.getItem('name')}, ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
|
89
|
},
|
90
|
error => {
|
91
|
sessionStorage.removeItem('name');
|
92
|
sessionStorage.removeItem('email');
|
93
|
sessionStorage.removeItem('role');
|
94
|
console.log("Error!");
|
95
|
console.log(error);
|
96
|
deleteCookie('openAIREUser');
|
97
|
deleteCookie('AccessToken');
|
98
|
this.isLoggedIn = false;
|
99
|
this.router.navigate(['/landing']);
|
100
|
},
|
101
|
() => {
|
102
|
if ( sessionStorage.getItem("state.location") ) {
|
103
|
let state = sessionStorage.getItem("state.location");
|
104
|
sessionStorage.removeItem("state.location");
|
105
|
console.log(`tried to login - returning to state: ${state}`);
|
106
|
if ( !this.getIsUserLoggedIn() ) {
|
107
|
console.log('user hasn\'t logged in yet -- going to landing');
|
108
|
this.router.navigate(['/landing']);
|
109
|
} /*else if (this.redirectUrl) {
|
110
|
let url = this.redirectUrl;
|
111
|
this.redirectUrl = null;
|
112
|
this.router.navigate([url]);
|
113
|
} */else {
|
114
|
this.router.navigate([state]);
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
);
|
119
|
} else {
|
120
|
this.isLoggedIn = true;
|
121
|
console.log(`the current user is: ${sessionStorage.getItem('name')}, ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
|
122
|
}
|
123
|
}/* else {
|
124
|
if (this.redirectUrl) {
|
125
|
let url = this.redirectUrl;
|
126
|
this.redirectUrl = null;
|
127
|
this.router.navigate([url]);
|
128
|
console.log('route is', url);
|
129
|
}
|
130
|
}*/
|
131
|
}
|
132
|
|
133
|
public getIsUserLoggedIn() {
|
134
|
this.isLoggedIn = (getCookie('openAIREUser') && (getCookie('openAIREUser') !== '') && (this.getUserEmail() !== '' ) );
|
135
|
return this.isLoggedIn;
|
136
|
}
|
137
|
|
138
|
public getUserName() {
|
139
|
if (this.isLoggedIn)
|
140
|
return sessionStorage.getItem('name');
|
141
|
else
|
142
|
return '';
|
143
|
}
|
144
|
|
145
|
public getUserEmail() {
|
146
|
if (this.isLoggedIn)
|
147
|
return sessionStorage.getItem('email');
|
148
|
else
|
149
|
return '';
|
150
|
}
|
151
|
|
152
|
public getUserRole() {
|
153
|
if (this.isLoggedIn)
|
154
|
return sessionStorage.getItem('role');
|
155
|
else
|
156
|
return '';
|
157
|
}
|
158
|
|
159
|
}
|