-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
auth-flow-strategy.ts
138 lines (120 loc) · 4.32 KB
/
auth-flow-strategy.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Injector } from '@angular/core';
import { Params, Router } from '@angular/router';
import {
AuthConfig,
OAuthErrorEvent,
OAuthService as OAuthService2,
OAuthStorage,
} from 'angular-oauth2-oidc';
import { Observable, of } from 'rxjs';
import { filter, map, switchMap, take, tap } from 'rxjs/operators';
import {
AbpLocalStorageService,
ConfigStateService,
EnvironmentService,
HttpErrorReporterService,
LoginParams,
SessionStateService,
TENANT_KEY,
} from '@abp/ng.core';
import { clearOAuthStorage } from '../utils/clear-o-auth-storage';
import { oAuthStorage } from '../utils/oauth-storage';
import { HttpErrorResponse } from '@angular/common/http';
export abstract class AuthFlowStrategy {
abstract readonly isInternalAuth: boolean;
protected httpErrorReporter: HttpErrorReporterService;
protected environment: EnvironmentService;
protected configState: ConfigStateService;
protected oAuthService: OAuthService2;
protected oAuthConfig!: AuthConfig;
protected sessionState: SessionStateService;
protected localStorageService: AbpLocalStorageService;
protected tenantKey: string;
protected router: Router;
abstract checkIfInternalAuth(queryParams?: Params): boolean;
abstract navigateToLogin(queryParams?: Params): void;
abstract logout(queryParams?: Params): Observable<any>;
abstract login(params?: LoginParams | Params): Observable<any>;
private catchError = (err: HttpErrorResponse) => {
this.httpErrorReporter.reportError(err);
return of(null);
};
constructor(protected injector: Injector) {
this.httpErrorReporter = injector.get(HttpErrorReporterService);
this.environment = injector.get(EnvironmentService);
this.configState = injector.get(ConfigStateService);
this.oAuthService = injector.get(OAuthService2);
this.sessionState = injector.get(SessionStateService);
this.localStorageService = injector.get(AbpLocalStorageService);
this.oAuthConfig = this.environment.getEnvironment().oAuthConfig || {};
this.tenantKey = injector.get(TENANT_KEY);
this.router = injector.get(Router);
this.listenToOauthErrors();
}
async init(): Promise<any> {
if (this.oAuthConfig.clientId) {
const shouldClear = shouldStorageClear(this.oAuthConfig.clientId, oAuthStorage);
if (shouldClear) clearOAuthStorage(oAuthStorage);
}
this.oAuthService.configure(this.oAuthConfig);
this.oAuthService.events
.pipe(filter(event => event.type === 'token_refresh_error'))
.subscribe(() => this.navigateToLogin());
this.navigateToPreviousUrl();
return this.oAuthService
.loadDiscoveryDocument()
.then(() => {
if (this.oAuthService.hasValidAccessToken() || !this.oAuthService.getRefreshToken()) {
return Promise.resolve();
}
return this.refreshToken();
})
.catch(this.catchError);
}
protected navigateToPreviousUrl(): void {
const { responseType } = this.oAuthConfig;
if (responseType === 'code') {
this.oAuthService.events
.pipe(
filter(event => event.type === 'token_received' && !!this.oAuthService.state),
take(1),
map(() => {
const redirect_uri = decodeURIComponent(this.oAuthService.state);
if (redirect_uri && redirect_uri !== '/') {
return redirect_uri;
}
return '/';
}),
switchMap(redirectUri =>
this.configState.getOne$('currentUser').pipe(
filter(user => !!user?.isAuthenticated),
tap(() => this.router.navigate([redirectUri])),
),
),
)
.subscribe();
}
}
protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => clearOAuthStorage());
}
protected listenToOauthErrors() {
this.oAuthService.events
.pipe(
filter(event => event instanceof OAuthErrorEvent),
tap(() => clearOAuthStorage()),
switchMap(() => this.configState.refreshAppState()),
)
.subscribe();
}
}
function shouldStorageClear(clientId: string, storage: OAuthStorage): boolean {
const key = 'abpOAuthClientId';
if (!storage.getItem(key)) {
storage.setItem(key, clientId);
return false;
}
const shouldClear = storage.getItem(key) !== clientId;
if (shouldClear) storage.setItem(key, clientId);
return shouldClear;
}