1
|
import {StringUtils} from '../../utils/string-utils.class';
|
2
|
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
|
3
|
|
4
|
|
5
|
export class User {
|
6
|
email:string;
|
7
|
firstname: string;
|
8
|
lastname: string;
|
9
|
id: string;
|
10
|
fullname: string;
|
11
|
expirationDate: number;
|
12
|
role:string[];
|
13
|
jwt:string;
|
14
|
|
15
|
}
|
16
|
|
17
|
export class Session{
|
18
|
// public static setUser(user:User): User {
|
19
|
//
|
20
|
// localStorage.setItem("user", JSON.stringify(user));
|
21
|
//
|
22
|
// return user;
|
23
|
// }
|
24
|
public static removeUser() {
|
25
|
if(Session.isLoggedIn()){
|
26
|
localStorage.removeItem("user");
|
27
|
COOKIE.deleteCookie(COOKIE.cookieName_id)
|
28
|
COOKIE.deleteCookie(COOKIE.cookieName_u);
|
29
|
}
|
30
|
}
|
31
|
public static getUser():User {
|
32
|
if(Session.isLoggedIn()){
|
33
|
return JSON.parse(localStorage.getItem("user"));
|
34
|
}else{
|
35
|
return null;
|
36
|
}
|
37
|
}
|
38
|
public static isLoggedIn(): boolean {
|
39
|
var loggedIn:boolean = false;
|
40
|
var user:User = null;
|
41
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
42
|
if( typeof localStorage !== 'undefined') {
|
43
|
if(localStorage.getItem("user")) {
|
44
|
user = JSON.parse(localStorage.getItem("user"));
|
45
|
if(user && (user.fullname != null || user.lastname !=null || user.firstname !=null)){
|
46
|
|
47
|
loggedIn = true;
|
48
|
}else if(cookie != null && this.getUserFromCookie()!= null){
|
49
|
loggedIn = true
|
50
|
}else{
|
51
|
loggedIn = false;
|
52
|
}
|
53
|
}else{
|
54
|
if(cookie != null && this.getUserFromCookie()!= null){
|
55
|
loggedIn = true
|
56
|
}else{
|
57
|
loggedIn = false;
|
58
|
}
|
59
|
|
60
|
}
|
61
|
}else{
|
62
|
loggedIn = false;
|
63
|
}
|
64
|
return loggedIn;
|
65
|
}
|
66
|
public static getUserFromCookie():User{
|
67
|
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
68
|
if(cookie != null){
|
69
|
var user:User = MyJWT.parseUserInfo(cookie);
|
70
|
if( typeof localStorage !== 'undefined') {
|
71
|
localStorage.setItem("user", JSON.stringify(user));
|
72
|
if(user && user.email){
|
73
|
COOKIE.deleteCookie(COOKIE.cookieName_u); // delete cookie to avoid transfer through requests
|
74
|
return user;
|
75
|
}else{
|
76
|
return null;
|
77
|
}
|
78
|
}else{
|
79
|
return null;
|
80
|
}
|
81
|
}else{
|
82
|
return null;
|
83
|
}
|
84
|
}
|
85
|
public static getUserJwt():string {
|
86
|
if(Session.isLoggedIn()){
|
87
|
return Session.getUser().jwt;
|
88
|
}else{
|
89
|
return null;
|
90
|
}
|
91
|
|
92
|
}
|
93
|
public static getUserEmail():string {
|
94
|
if(Session.isLoggedIn()){
|
95
|
return Session.getUser().email;
|
96
|
}else{
|
97
|
return null;
|
98
|
}
|
99
|
|
100
|
}
|
101
|
public static getCurrentUrl():string {
|
102
|
if( typeof localStorage !== 'undefined') {
|
103
|
return localStorage.getItem("url");
|
104
|
}
|
105
|
return "";
|
106
|
|
107
|
}
|
108
|
public static setCurrentUrl(url:string) {
|
109
|
if( typeof localStorage !== 'undefined') {
|
110
|
localStorage.setItem("url", url);
|
111
|
}
|
112
|
}
|
113
|
public static getCurrentParameters():any {
|
114
|
if( typeof localStorage !== 'undefined') {
|
115
|
var params = localStorage.getItem("params");
|
116
|
console.log(params);
|
117
|
var object = null;
|
118
|
if(params.split("&").length > 0){
|
119
|
object = {};
|
120
|
}
|
121
|
for(var i=0; i<params.split("&").length; i++){
|
122
|
console.log(params.split("&")[i]);
|
123
|
console.log((params.split("&")[i]).split("=")[0]);
|
124
|
console.log((params.split("&")[i]).split("=")[1]);
|
125
|
object[(params.split("&")[i]).split("=")[0]] = (params.split("&")[i]).split("=")[1];
|
126
|
}
|
127
|
return object;
|
128
|
}
|
129
|
return {};
|
130
|
}
|
131
|
public static setCurrentParameters(params:string) {
|
132
|
if( typeof localStorage !== 'undefined') {
|
133
|
if(params.indexOf("?")==0){
|
134
|
params = params.substring(1);
|
135
|
}
|
136
|
localStorage.setItem("params",(params && params.length > 1)? params:"");
|
137
|
}
|
138
|
}
|
139
|
public static isAdminUser():boolean {
|
140
|
var isAdmin = false;
|
141
|
if(Session.isLoggedIn()){
|
142
|
var claimRoles = ["urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Curator+-+Claim","urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Portal+Administrator"]
|
143
|
for (var i = 0; i < claimRoles.length; i++) {
|
144
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
145
|
isAdmin = true;
|
146
|
break;
|
147
|
}
|
148
|
}
|
149
|
// console.log("Is admin:"+ isAdmin)
|
150
|
return (isAdmin);
|
151
|
}
|
152
|
// console.log("Is admin:"+ isAdmin)
|
153
|
return (isAdmin);
|
154
|
}
|
155
|
public static isRegisteredUser():boolean {
|
156
|
var isRegisteredUser = false;
|
157
|
if(Session.isLoggedIn()){
|
158
|
var claimRoles = ["urn:mace:openminted.eu:aai.openminted.eu:group:Registered+User"];
|
159
|
for (var i = 0; i < claimRoles.length; i++) {
|
160
|
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
161
|
isRegisteredUser = true;
|
162
|
break;
|
163
|
}
|
164
|
}
|
165
|
// console.log("Is isRegisteredUser:"+ isRegisteredUser)
|
166
|
return (isRegisteredUser);
|
167
|
}
|
168
|
// console.log("Is isRegisteredUser:"+ isRegisteredUser)
|
169
|
return (isRegisteredUser);
|
170
|
}
|
171
|
public static isUserValid() {
|
172
|
if(Session.isLoggedIn()){
|
173
|
var expires = Session.getUser().expirationDate;
|
174
|
var now = new Date().getTime() / 1000;
|
175
|
// console.log(" is still valid ? "+(now +0 < expires) +" Remaining:"+ (expires - (now+0))+ " now is:"+now + "expires at:"+expires);
|
176
|
return now +0 < expires;
|
177
|
}
|
178
|
return false;
|
179
|
}
|
180
|
public static isValidAndRemove() {
|
181
|
if(Session.isLoggedIn()){
|
182
|
if(!Session.isUserValid()){
|
183
|
Session.removeUser();
|
184
|
return false;
|
185
|
}else{
|
186
|
return true;
|
187
|
}
|
188
|
}else{
|
189
|
return false;
|
190
|
}
|
191
|
}
|
192
|
|
193
|
}
|
194
|
export class MyJWT{
|
195
|
private static validateJWTFormat(data){
|
196
|
if(data != null && (data.indexOf(".") !=-1 && data.split('.').length == 3)){
|
197
|
return true;
|
198
|
}
|
199
|
return false;
|
200
|
}
|
201
|
private static getPayload(data){
|
202
|
var payload = data.split('.')[1];
|
203
|
return StringUtils.b64DecodeUnicode(payload);
|
204
|
}
|
205
|
public static parseUserInfo(data: any): User {
|
206
|
if(this.validateJWTFormat(data)){
|
207
|
var info = JSON.parse(this.getPayload(data));
|
208
|
}else{
|
209
|
return null;
|
210
|
}
|
211
|
var user: User = new User();
|
212
|
|
213
|
user.firstname = (StringUtils.URIDecode((info.firstname && info.firstname!="")?info.firstname:"")).replace("+"," ");
|
214
|
user.lastname = (StringUtils.URIDecode((info.lastname && info.lastname!="")?info.lastname:"")).replace("+"," ");
|
215
|
user.email = info.email;
|
216
|
// user.id = info.userId;
|
217
|
user.fullname = (StringUtils.URIDecode((info.fullname && info.fullname!="")?info.fullname:"")).replace("+"," ");
|
218
|
if(user.fullname == ""){
|
219
|
if(user.firstname != ""){
|
220
|
user.fullname += user.firstname;
|
221
|
}
|
222
|
if(user.lastname !=""){
|
223
|
user.fullname += user.lastname;
|
224
|
}
|
225
|
if(user.fullname == ""){ //it is still empty set a default
|
226
|
user.fullname = "Anonymous user";
|
227
|
}
|
228
|
}
|
229
|
if(info.role && info.role != ""){
|
230
|
user.role =JSON.parse( StringUtils.URIDecode(info.role));
|
231
|
}else{
|
232
|
user.role =[];
|
233
|
}
|
234
|
|
235
|
// console.log("User Role is:");
|
236
|
// console.log(user.role)
|
237
|
user.jwt = data;
|
238
|
user.expirationDate = info.exp;
|
239
|
localStorage.setItem("user", JSON.stringify(user));
|
240
|
console.log(user)
|
241
|
return user;
|
242
|
}
|
243
|
|
244
|
}
|
245
|
export class COOKIE{
|
246
|
public static cookieName_u:string="XCsrfToken";
|
247
|
public static cookieName_id:string="AccessToken";
|
248
|
|
249
|
public static getCookie(name: string) : string {
|
250
|
if(typeof document == 'undefined'){
|
251
|
return null;
|
252
|
}
|
253
|
let ca: Array<string> = document.cookie.split(';');
|
254
|
let caLen: number = ca.length;
|
255
|
let cookieName = `${name}=`;
|
256
|
let c: string;
|
257
|
|
258
|
for (let i: number = 0; i < caLen; i += 1) {
|
259
|
c = ca[i].replace(/^\s+/g, '');
|
260
|
if (c.indexOf(cookieName) == 0) {
|
261
|
return c.substring(cookieName.length, c.length);
|
262
|
}
|
263
|
}
|
264
|
return null;
|
265
|
}
|
266
|
public static deleteCookie(name) {
|
267
|
this.setCookie(name, '', -1);
|
268
|
}
|
269
|
public static setCookie(name: string, value: string, expireDays: number, path: string = '/') {
|
270
|
let d:Date = new Date();
|
271
|
d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
|
272
|
let expires:string = `expires=${d.toUTCString()}`;
|
273
|
// let cpath:string = path ? `; path=${path}` : '';
|
274
|
document.cookie = name+'='+value+'; path='+path+'; domain='+OpenaireProperties.getCookieDomain()+';';
|
275
|
}
|
276
|
}
|