forked from ionic-team/eas-2021
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.service.ts
111 lines (97 loc) · 3.5 KB
/
authentication.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { Injectable } from '@angular/core';
import { AuthConnect, AuthResult, AzureProvider, ProviderOptions, TokenType } from '@ionic-enterprise/auth';
import { Platform } from '@ionic/angular';
import { nativeIonicAuthOptions, webIonicAuthOptions } from '../../environments/environment';
import { RouteService } from './route.service';
import { VaultService } from './vault.service';
@Injectable({ providedIn: 'root' })
export class AuthenticationService {
private provider: AzureProvider;
private result: AuthResult | undefined;
private options: ProviderOptions;
constructor(
private platform: Platform,
private routeService: RouteService,
private vaultService: VaultService) {
this.options = this.platform.is('hybrid') ? nativeIonicAuthOptions : webIonicAuthOptions;
this.provider = new AzureProvider();
this.init();
}
public async init() {
await AuthConnect.setup({
platform: this.platform.is('hybrid') ? 'capacitor' : 'web',
logLevel: 'DEBUG',
ios: {
webView: 'private',
safariWebViewOptions: { dismissButtonStyle: 'close', preferredBarTintColor: '#FFFFFF', preferredControlTintColor: '#333333' }
},
android: { isAnimated: false, showDefaultShareMenuItem: false },
web: { uiMode: 'current', authFlow: 'PKCE' }
});
}
public async login() {
this.result = await AuthConnect.login(this.provider, this.options);
await this.vaultService.set(this.result);
this.routeService.goToRoot();
}
// This is call for web and takes the auth info from query parameters and gives it to auth connect to handle
// We then store the token and redirect to the main page
public async handleLogin() {
const urlParams = new URLSearchParams(window.location.search);
this.result = await AuthConnect.handleLoginCallback({ code: urlParams.get('code')!, state: urlParams.get('state')! });
await this.vaultService.set(this.result);
this.routeService.goToRoot();
}
public async logout(): Promise<void> {
if (!this.provider) {
console.error(`provider is empty`);
}
if (!this.result) {
console.error(`authResult is empty`);
}
// Hack
if (this.result.provider == undefined) {
this.result.provider = {
options: webIonicAuthOptions,
config: undefined,
authorizeUrl: undefined,
manifest: await AuthConnect.fetchManifest(webIonicAuthOptions.discoveryUrl)
};
}
try {
await AuthConnect.logout(this.provider, this.result!);
} catch (error) {
console.error('AuthConnect.logout', error);
}
this.routeService.returnToLogin();
}
public async getAccessToken(): Promise<string | undefined> {
return await AuthConnect.getToken(TokenType.access, this.result!);
}
public decodeToken() {
return AuthConnect.decodeToken(TokenType.access, this.result!);
}
public async isAuthenticated(): Promise<boolean> {
try {
const authResult = await this.vaultService.get();
if (!authResult) {
return false;
}
const { idToken } = authResult;
if (!idToken) {
throw new Error('No ID Token');
}
const expired = await AuthConnect.isAccessTokenExpired(authResult);
if (!expired) {
return true;
}
const newAuthResult = await AuthConnect.refreshSession(this.provider, authResult);
await this.vaultService.set(newAuthResult);
return true;
} catch (e) {
console.error(e);
await this.vaultService.remove();
return false;
}
}
}