generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ingo Sternberg <[email protected]> #230
- Loading branch information
1 parent
23acd8e
commit c7e74f6
Showing
8 changed files
with
126 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
declare const process: { | ||
env: { | ||
XD_API_URL: string; | ||
XD_SECRET_KEY: string; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 114 additions & 107 deletions
221
libs/common/frontend/models/src/lib/services/authentication.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,120 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { inject, Injectable } from '@angular/core'; | ||
import * as CryptoJS from 'crypto-js'; | ||
|
||
import { APP_CONFIG } from '../tokens'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
providedIn: 'root', | ||
}) | ||
export class AuthenticationService { | ||
|
||
private readonly tokenKey = 'authToken'; | ||
private readonly tokenExpirationTime = 1000 * 60 * 60; | ||
|
||
// this is a demo app, don't actually store these things in the code | ||
private readonly secretKey = 'XceleratorDemoApp'; | ||
private readonly userDataBase = [ | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
{ userMail: '[email protected]', password: 'siemens' }, | ||
]; | ||
|
||
login(email: string, password: string) { | ||
|
||
if (this.checkCredentials(email, password)) { | ||
const token = this.generateToken(email, password); | ||
localStorage.setItem(this.tokenKey, token); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
logout() { | ||
localStorage.removeItem(this.tokenKey); | ||
} | ||
|
||
isLoggedIn() { | ||
const token = localStorage.getItem(this.tokenKey); | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const userData = this.decryptToken(token); | ||
if (!userData) { | ||
return false; | ||
} | ||
|
||
if (!this.checkCredentials(userData.userMail, userData.password)) { | ||
return false; | ||
} | ||
|
||
if (new Date().getTime() - userData.time > this.tokenExpirationTime) { | ||
this.logout(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
getUserMail() { | ||
const token = localStorage.getItem(this.tokenKey); | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const userData = this.decryptToken(token); | ||
if (!userData) { | ||
return false; | ||
} | ||
|
||
return userData.userMail; | ||
} | ||
|
||
private checkCredentials(email: string, password: string) { | ||
return this.userDataBase.some(user => user.userMail === email && user.password === password); | ||
} | ||
|
||
private generateToken(email: string, password: string) { | ||
// ':' is not allowed in the email, so we can use it as a separator | ||
const time = new Date().getTime(); | ||
const tokenData = `${email}:${password}:${time}`; | ||
return CryptoJS.AES.encrypt(tokenData, this.secretKey).toString(); | ||
} | ||
|
||
private decryptToken(token: string) { | ||
try { | ||
const bytes = CryptoJS.AES.decrypt(token, this.secretKey); | ||
const decryptedData = bytes.toString(CryptoJS.enc.Utf8); | ||
|
||
// the token should have the format email:password:time, where password could contain : | ||
const firstColonIndex = decryptedData.indexOf(':'); | ||
const userMail = decryptedData.slice(0, firstColonIndex); | ||
|
||
const lastColonIndex = decryptedData.lastIndexOf(':'); | ||
const password = decryptedData.slice(firstColonIndex + 1, lastColonIndex); | ||
const time = decryptedData.slice(lastColonIndex + 1); | ||
|
||
return { userMail: userMail, password: password, time: parseInt(time) }; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
private readonly tokenKey = 'authToken'; | ||
private readonly tokenExpirationTime = 1000 * 60 * 60; | ||
|
||
private readonly secretKey = inject(APP_CONFIG).secretKey; | ||
|
||
// this is a demo app, don't actually store these in the code | ||
private readonly userDataBase = [ | ||
'U2FsdGVkX1/9RSRUE4j4qGAQ48gPSr8iLF242snE0hwJmOsluUk/hPzDuXgcgkSGNa8YCrm2GW8lxAxwCCDq1w==', | ||
'U2FsdGVkX1+ifYJJA7x2MVnkOiqx8DywlVyKYOzPM6HPftzUdoGrg8X2c9i2f2ofZKF8I7sKZGFhuJQtVqBeGA==', | ||
'U2FsdGVkX18yC09eQaBPI4LllwUbBAJTYWZoDmBROU4tOaHfB6n9+FCWMOUYGhbVJCV370aVvszr6YaFywC5AQ==', | ||
'U2FsdGVkX19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| ||
|
||
login(email: string, password: string) { | ||
if (this.checkCredentials(email, password)) { | ||
const token = this.generateToken(email, password); | ||
localStorage.setItem(this.tokenKey, token); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
logout() { | ||
localStorage.removeItem(this.tokenKey); | ||
} | ||
|
||
isLoggedIn() { | ||
const token = localStorage.getItem(this.tokenKey); | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const userData = this.decryptToken(token); | ||
if (!userData) { | ||
return false; | ||
} | ||
|
||
if (!this.checkCredentials(userData.userMail, userData.password)) { | ||
return false; | ||
} | ||
|
||
if (new Date().getTime() - userData.time > this.tokenExpirationTime) { | ||
this.logout(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
getUserMail() { | ||
const token = localStorage.getItem(this.tokenKey); | ||
if (!token) { | ||
return false; | ||
} | ||
|
||
const userData = this.decryptToken(token); | ||
if (!userData) { | ||
return false; | ||
} | ||
|
||
return userData.userMail; | ||
} | ||
|
||
private checkCredentials(email: string, password: string) { | ||
return this.userDataBase.some((encryptedUser) => { | ||
const user = this.decryptToken(encryptedUser); | ||
if (!user) { | ||
return false; | ||
} | ||
|
||
return user.userMail === email && user.password === password; | ||
}); | ||
} | ||
|
||
private generateToken(email: string, password: string) { | ||
// ':' is not allowed in the email, so we can use it as a separator | ||
const time = new Date().getTime(); | ||
const tokenData = `${email}:${password}:${time}`; | ||
return CryptoJS.AES.encrypt(tokenData, this.secretKey).toString(); | ||
} | ||
|
||
private decryptToken(token: string) { | ||
try { | ||
const bytes = CryptoJS.AES.decrypt(token, this.secretKey); | ||
const decryptedData = bytes.toString(CryptoJS.enc.Utf8); | ||
|
||
// the token should have the format email:password:time, where password could contain : | ||
const firstColonIndex = decryptedData.indexOf(':'); | ||
const userMail = decryptedData.slice(0, firstColonIndex); | ||
|
||
const lastColonIndex = decryptedData.lastIndexOf(':'); | ||
const password = decryptedData.slice(firstColonIndex + 1, lastColonIndex); | ||
const time = decryptedData.slice(lastColonIndex + 1); | ||
|
||
return { userMail: userMail, password: password, time: parseInt(time) }; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
} |