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