|
1 | 1 | import { Injectable } from '@angular/core'; |
2 | | -import { HttpClient } from '@angular/common/http'; |
3 | | -import { TokenStorageService } from './token-storage.service'; |
4 | | -import jwt_decode from 'jwt-decode'; |
5 | 2 | import { environment } from '../../../environments/environment'; |
| 3 | +import { AuthConfig, OAuthService } from 'angular-oauth2-oidc'; |
6 | 4 | import { Router } from '@angular/router'; |
7 | 5 |
|
8 | 6 | @Injectable({ |
9 | 7 | providedIn: 'root' |
10 | 8 | }) |
11 | 9 | export class AuthService { |
12 | 10 |
|
13 | | - constructor(private http: HttpClient, private tokenStorageService: TokenStorageService, private router: Router) { |
14 | | - |
15 | | - } |
16 | | - |
17 | | - loggedIn() { |
18 | | - return !!this.tokenStorageService.getToken(); |
| 11 | + constructor(public oauthService: OAuthService, private router: Router) { |
19 | 12 | } |
20 | 13 |
|
21 | 14 | isCurator() { |
22 | | - if (environment.WHITELISTED_CURATORS.indexOf(this.getDecodedToken()?.email) > -1) { |
| 15 | + if (environment.WHITELISTED_CURATORS.indexOf(this.getEmail()) > -1) { |
23 | 16 | return true; |
24 | 17 | } |
25 | | - return this.getDecodedToken()?.domains?.indexOf('self.GWAS_Curator') > -1; |
26 | | - } |
27 | | - |
28 | | - getDecodedToken() { |
29 | | - const jwtToken = this.tokenStorageService.getToken(); |
30 | | - const decoded = jwt_decode(jwtToken); |
31 | | - if (Date.now() > decoded.exp * 1000) { |
32 | | - this.tokenStorageService.signOut(); |
33 | | - } |
34 | | - return decoded; |
| 18 | + return this.getDomains().indexOf('self.GWAS_Curator') > -1; |
35 | 19 | } |
36 | 20 |
|
37 | 21 | login() { |
38 | | - const width = 650; |
39 | | - const height = 1000; |
40 | | - let left = -1; |
41 | | - let top = -1; |
42 | | - |
43 | | - if (left < 0) { |
44 | | - const screenWidth = window.screen.width; |
45 | | - if (screenWidth > width) { |
46 | | - left = Math.round((screenWidth - width) / 2); |
47 | | - } |
48 | | - } |
49 | | - if (top < 0) { |
50 | | - const screenHeight = window.screen.height; |
51 | | - if (screenHeight > height) { |
52 | | - top = Math.round((screenHeight - height) / 2); |
53 | | - } |
54 | | - } |
| 22 | + this.oauthService.initLoginFlow(); |
| 23 | + } |
55 | 24 |
|
56 | | - const windowOptions = [ |
57 | | - `width=${width}`, |
58 | | - `height=${height}`, |
59 | | - `left=${left}`, |
60 | | - `top=${top}`, |
61 | | - 'personalbar=no', |
62 | | - 'toolbar=no', |
63 | | - 'scrollbars=yes', |
64 | | - 'resizable=yes', |
65 | | - 'directories=no', |
66 | | - 'location=no', |
67 | | - 'menubar=no', |
68 | | - 'titlebar=no', |
69 | | - 'toolbar=no' |
70 | | - ]; |
71 | | - const loginWindow = window.open(environment.AAPURL + '/sso?from=' + location.origin + '&ttl=180', |
72 | | - 'Sign in to Elixir', windowOptions.join(',')); |
| 25 | + isLoggedIn(): boolean { |
| 26 | + return this.oauthService.hasValidAccessToken(); |
| 27 | + } |
73 | 28 |
|
74 | | - if (loginWindow) { |
75 | | - loginWindow.focus(); |
76 | | - } |
77 | | - return loginWindow; |
| 29 | + logout() { |
| 30 | + this.oauthService.logOut(); |
78 | 31 | } |
79 | 32 |
|
80 | | - saveToken(token: string) { |
81 | | - this.tokenStorageService.saveToken(token); |
| 33 | + getEmail() { |
| 34 | + return this.oauthService.getIdentityClaims['email']; |
82 | 35 | } |
83 | 36 |
|
84 | | - logout() { |
85 | | - window.localStorage.removeItem('id_token'); |
| 37 | + getDomains() { |
| 38 | + return this.oauthService.getIdentityClaims['domains']; |
86 | 39 | } |
87 | 40 | } |
0 commit comments