1 |
54479
|
myrto.kouk
|
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 |
54747
|
myrto.kouk
|
private _storage: Storage = sessionStorage;
|
21 |
54479
|
myrto.kouk
|
|
22 |
61435
|
spyroukon
|
private cookie: string = null;
|
23 |
|
|
|
24 |
54479
|
myrto.kouk
|
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 |
|
|
const 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 |
58109
|
stefania.m
|
sessionStorage.setItem('state.location', '/join');
|
36 |
54479
|
myrto.kouk
|
}
|
37 |
|
|
console.log('redirect location', sessionStorage.getItem('state.location'));
|
38 |
|
|
window.location.href = this.loginUrl;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
public logout() {
|
42 |
|
|
deleteCookie('AccessToken');
|
43 |
|
|
sessionStorage.clear();
|
44 |
|
|
this.isLoggedIn = false;
|
45 |
|
|
|
46 |
|
|
console.log('logging out, calling:');
|
47 |
|
|
console.log(`${this.apiUrl}/openid_logout`);
|
48 |
|
|
|
49 |
|
|
/*window.location.href = `${this.apiUrl}/openid_logout`;*/
|
50 |
|
|
window.location.href = `https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.apiUrl}/openid_logout`;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
public tryLogin() {
|
54 |
61435
|
spyroukon
|
this.cookie = getCookie('AccessToken');
|
55 |
|
|
if (this.cookie && this.cookie !== '') {
|
56 |
|
|
// console.log(`I got the cookie!`);
|
57 |
|
|
// console.log(`in tryLogin -> document.cookie is: ${document.cookie.toString()}`);
|
58 |
54479
|
myrto.kouk
|
/* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTDOWN */
|
59 |
|
|
setInterval(() => {
|
60 |
|
|
this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe(
|
61 |
|
|
userInfo => {
|
62 |
61435
|
spyroukon
|
// console.log('User is still logged in');
|
63 |
|
|
// console.log(userInfo);
|
64 |
54479
|
myrto.kouk
|
this.isLoggedIn = true;
|
65 |
|
|
},
|
66 |
|
|
() => {
|
67 |
|
|
this.logout();
|
68 |
|
|
},
|
69 |
|
|
() => {
|
70 |
61435
|
spyroukon
|
this.cookie = getCookie('AccessToken');
|
71 |
|
|
if ( !this.cookie || this.cookie === '') {
|
72 |
54479
|
myrto.kouk
|
this.logout();
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
);
|
76 |
|
|
/*this.redirectUrl = window.location.pathname;
|
77 |
|
|
this.loginWithState();*/
|
78 |
|
|
|
79 |
|
|
}, 1000 * 60 * 5);
|
80 |
|
|
if (!this.getIsUserLoggedIn()) {
|
81 |
61435
|
spyroukon
|
// console.log(`session.name wasn't found --> logging in via repo-service!`);
|
82 |
54479
|
myrto.kouk
|
this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe(
|
83 |
|
|
userInfo => {
|
84 |
61435
|
spyroukon
|
// console.log(userInfo);
|
85 |
54747
|
myrto.kouk
|
sessionStorage.setItem('name', userInfo['name']);
|
86 |
|
|
sessionStorage.setItem('email', userInfo['email'].trim());
|
87 |
|
|
sessionStorage.setItem('role', userInfo['role']);
|
88 |
54479
|
myrto.kouk
|
this.isLoggedIn = true;
|
89 |
61435
|
spyroukon
|
// console.log(`the current user is: ${sessionStorage.getItem('name')},
|
90 |
|
|
// ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
|
91 |
54479
|
myrto.kouk
|
},
|
92 |
|
|
error => {
|
93 |
|
|
sessionStorage.clear();
|
94 |
|
|
console.log('Error!');
|
95 |
|
|
console.log(error);
|
96 |
|
|
deleteCookie('AccessToken');
|
97 |
61435
|
spyroukon
|
deleteCookie('AccessToken');
|
98 |
54479
|
myrto.kouk
|
this.isLoggedIn = false;
|
99 |
58110
|
stefania.m
|
this.router.navigate(['/home']);
|
100 |
54479
|
myrto.kouk
|
},
|
101 |
|
|
() => {
|
102 |
|
|
if ( sessionStorage.getItem('state.location') ) {
|
103 |
|
|
const state = sessionStorage.getItem('state.location');
|
104 |
54747
|
myrto.kouk
|
sessionStorage.removeItem('state.location');
|
105 |
54479
|
myrto.kouk
|
console.log(`tried to login - returning to state: ${state}`);
|
106 |
|
|
if ( !this.getIsUserLoggedIn() ) {
|
107 |
61435
|
spyroukon
|
// console.log('user hasn\'t logged in yet -- going to home');
|
108 |
58110
|
stefania.m
|
this.router.navigate(['/home']);
|
109 |
54479
|
myrto.kouk
|
} else {
|
110 |
|
|
this.router.navigate([state]);
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
);
|
115 |
|
|
} else {
|
116 |
|
|
this.isLoggedIn = true;
|
117 |
61435
|
spyroukon
|
// console.log(`the current user is: ${sessionStorage.getItem('name')},
|
118 |
|
|
// ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
|
119 |
54479
|
myrto.kouk
|
if (this.redirectUrl) {
|
120 |
|
|
const url = this.redirectUrl;
|
121 |
|
|
this.redirectUrl = null;
|
122 |
|
|
this.router.navigate([url]);
|
123 |
61435
|
spyroukon
|
// console.log('route is', url);
|
124 |
54479
|
myrto.kouk
|
}
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
public getIsUserLoggedIn() {
|
130 |
61435
|
spyroukon
|
// todo: probably not all of them are needed
|
131 |
|
|
return this.isLoggedIn && this.cookie && this.cookie !== '' && sessionStorage.getItem('email') !== null;
|
132 |
54479
|
myrto.kouk
|
}
|
133 |
|
|
|
134 |
|
|
public getUserName() {
|
135 |
|
|
if (this.isLoggedIn) {
|
136 |
54747
|
myrto.kouk
|
return sessionStorage.getItem('name');
|
137 |
54479
|
myrto.kouk
|
} else {
|
138 |
|
|
return '';
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
public getUserEmail() {
|
143 |
57094
|
stefania.m
|
if (this.getIsUserLoggedIn()) {
|
144 |
54747
|
myrto.kouk
|
return sessionStorage.getItem('email');
|
145 |
54479
|
myrto.kouk
|
} else {
|
146 |
|
|
return '';
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
public getUserRole() {
|
151 |
|
|
if (this.isLoggedIn) {
|
152 |
54747
|
myrto.kouk
|
return sessionStorage.getItem('role');
|
153 |
54479
|
myrto.kouk
|
} else {
|
154 |
|
|
return '';
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
}
|