@abp/ng.oauth 7.2.2 → 7.3.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/{esm2020 → esm2022}/abp-ng.oauth.mjs +4 -4
  2. package/{esm2020 → esm2022}/lib/guards/index.mjs +1 -1
  3. package/esm2022/lib/guards/oauth.guard.mjs +32 -0
  4. package/{esm2020 → esm2022}/lib/handlers/index.mjs +1 -1
  5. package/esm2022/lib/handlers/oauth-configuration.handler.mjs +37 -0
  6. package/esm2022/lib/interceptors/api.interceptor.mjs +57 -0
  7. package/{esm2020 → esm2022}/lib/interceptors/index.mjs +1 -1
  8. package/esm2022/lib/oauth.module.mjs +66 -0
  9. package/{esm2020 → esm2022}/lib/providers/index.mjs +1 -1
  10. package/{esm2020 → esm2022}/lib/providers/navigate-to-manage-profile.provider.mjs +18 -18
  11. package/{esm2020 → esm2022}/lib/services/index.mjs +1 -1
  12. package/esm2022/lib/services/oauth.service.mjs +62 -0
  13. package/esm2022/lib/strategies/auth-code-flow-strategy.mjs +40 -0
  14. package/esm2022/lib/strategies/auth-flow-strategy.mjs +81 -0
  15. package/{esm2020 → esm2022}/lib/strategies/auth-password-flow-strategy.mjs +66 -66
  16. package/{esm2020 → esm2022}/lib/strategies/index.mjs +3 -3
  17. package/{esm2020 → esm2022}/lib/tokens/auth-flow-strategy.mjs +10 -10
  18. package/{esm2020 → esm2022}/lib/tokens/index.mjs +1 -1
  19. package/{esm2020 → esm2022}/lib/utils/auth-utils.mjs +25 -25
  20. package/{esm2020 → esm2022}/lib/utils/check-access-token.mjs +10 -10
  21. package/{esm2020 → esm2022}/lib/utils/clear-o-auth-storage.mjs +18 -18
  22. package/{esm2020 → esm2022}/lib/utils/index.mjs +5 -5
  23. package/{esm2020 → esm2022}/lib/utils/oauth-storage.mjs +1 -1
  24. package/{esm2020 → esm2022}/lib/utils/storage.factory.mjs +4 -4
  25. package/{esm2020 → esm2022}/public-api.mjs +9 -9
  26. package/{fesm2020 → fesm2022}/abp-ng.oauth.mjs +436 -412
  27. package/fesm2022/abp-ng.oauth.mjs.map +1 -0
  28. package/index.d.ts +5 -5
  29. package/lib/guards/index.d.ts +1 -1
  30. package/lib/guards/oauth.guard.d.ts +13 -13
  31. package/lib/handlers/index.d.ts +1 -1
  32. package/lib/handlers/oauth-configuration.handler.d.ts +12 -12
  33. package/lib/interceptors/api.interceptor.d.ts +15 -15
  34. package/lib/interceptors/index.d.ts +1 -1
  35. package/lib/oauth.module.d.ts +10 -10
  36. package/lib/providers/index.d.ts +1 -1
  37. package/lib/providers/navigate-to-manage-profile.provider.d.ts +2 -2
  38. package/lib/services/index.d.ts +1 -1
  39. package/lib/services/oauth.service.d.ts +21 -21
  40. package/lib/strategies/auth-code-flow-strategy.d.ts +11 -11
  41. package/lib/strategies/auth-flow-strategy.d.ts +28 -26
  42. package/lib/strategies/auth-password-flow-strategy.d.ts +184 -184
  43. package/lib/strategies/index.d.ts +3 -3
  44. package/lib/tokens/auth-flow-strategy.d.ts +7 -7
  45. package/lib/tokens/index.d.ts +1 -1
  46. package/lib/utils/auth-utils.d.ts +4 -4
  47. package/lib/utils/check-access-token.d.ts +2 -2
  48. package/lib/utils/clear-o-auth-storage.d.ts +2 -2
  49. package/lib/utils/index.d.ts +5 -5
  50. package/lib/utils/oauth-storage.d.ts +1 -1
  51. package/lib/utils/storage.factory.d.ts +2 -2
  52. package/package.json +7 -13
  53. package/public-api.d.ts +9 -9
  54. package/esm2020/lib/guards/oauth.guard.mjs +0 -29
  55. package/esm2020/lib/handlers/oauth-configuration.handler.mjs +0 -36
  56. package/esm2020/lib/interceptors/api.interceptor.mjs +0 -56
  57. package/esm2020/lib/oauth.module.mjs +0 -65
  58. package/esm2020/lib/services/oauth.service.mjs +0 -61
  59. package/esm2020/lib/strategies/auth-code-flow-strategy.mjs +0 -35
  60. package/esm2020/lib/strategies/auth-flow-strategy.mjs +0 -64
  61. package/fesm2015/abp-ng.oauth.mjs +0 -469
  62. package/fesm2015/abp-ng.oauth.mjs.map +0 -1
  63. package/fesm2020/abp-ng.oauth.mjs.map +0 -1
@@ -4,451 +4,475 @@ import { CommonModule } from '@angular/common';
4
4
  import * as i1 from 'angular-oauth2-oidc';
5
5
  import { OAuthService, OAuthErrorEvent, OAuthInfoEvent, OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
6
6
  import * as i2 from '@abp/ng.core';
7
- import { HttpErrorReporterService, EnvironmentService, ConfigStateService, SessionStateService, AbpLocalStorageService, TENANT_KEY, noop, CORE_OPTIONS, IS_EXTERNAL_REQUEST, NAVIGATE_TO_MANAGE_PROFILE, AuthService, AuthGuard, ApiInterceptor, PIPE_TO_LOGIN_FN_KEY, CHECK_AUTHENTICATION_STATE_FN_KEY } from '@abp/ng.core';
8
- import { of, from, pipe, lastValueFrom } from 'rxjs';
9
- import { filter, tap, switchMap, map, take, finalize } from 'rxjs/operators';
7
+ import { HttpErrorReporterService, EnvironmentService, ConfigStateService, SessionStateService, AbpLocalStorageService, TENANT_KEY, noop, CORE_OPTIONS, IS_EXTERNAL_REQUEST, AuthService, NAVIGATE_TO_MANAGE_PROFILE, AuthGuard, ApiInterceptor, PIPE_TO_LOGIN_FN_KEY, CHECK_AUTHENTICATION_STATE_FN_KEY } from '@abp/ng.core';
8
+ import { of, from, pipe, lastValueFrom, tap as tap$1, delay } from 'rxjs';
9
+ import { filter, take, map, switchMap, tap, finalize } from 'rxjs/operators';
10
10
  import { Router } from '@angular/router';
11
11
  import { HttpHeaders, HTTP_INTERCEPTORS } from '@angular/common/http';
12
12
  import compare from 'just-compare';
13
13
 
14
14
  const oAuthStorage = localStorage;
15
15
 
16
- function clearOAuthStorage(storage = oAuthStorage) {
17
- const keys = [
18
- 'access_token',
19
- 'id_token',
20
- 'refresh_token',
21
- 'nonce',
22
- 'PKCE_verifier',
23
- 'expires_at',
24
- 'id_token_claims_obj',
25
- 'id_token_expires_at',
26
- 'id_token_stored_at',
27
- 'access_token_stored_at',
28
- 'granted_scopes',
29
- 'session_state',
30
- ];
31
- keys.forEach(key => storage.removeItem(key));
16
+ function clearOAuthStorage(storage = oAuthStorage) {
17
+ const keys = [
18
+ 'access_token',
19
+ 'id_token',
20
+ 'refresh_token',
21
+ 'nonce',
22
+ 'PKCE_verifier',
23
+ 'expires_at',
24
+ 'id_token_claims_obj',
25
+ 'id_token_expires_at',
26
+ 'id_token_stored_at',
27
+ 'access_token_stored_at',
28
+ 'granted_scopes',
29
+ 'session_state',
30
+ ];
31
+ keys.forEach(key => storage.removeItem(key));
32
32
  }
33
33
 
34
- class AuthFlowStrategy {
35
- constructor(injector) {
36
- this.injector = injector;
37
- this.catchError = (err) => {
38
- this.httpErrorReporter.reportError(err);
39
- return of(null);
40
- };
41
- this.httpErrorReporter = injector.get(HttpErrorReporterService);
42
- this.environment = injector.get(EnvironmentService);
43
- this.configState = injector.get(ConfigStateService);
44
- this.oAuthService = injector.get(OAuthService);
45
- this.sessionState = injector.get(SessionStateService);
46
- this.localStorageService = injector.get(AbpLocalStorageService);
47
- this.oAuthConfig = this.environment.getEnvironment().oAuthConfig || {};
48
- this.tenantKey = injector.get(TENANT_KEY);
49
- this.listenToOauthErrors();
50
- }
51
- async init() {
52
- if (this.oAuthConfig.clientId) {
53
- const shouldClear = shouldStorageClear(this.oAuthConfig.clientId, oAuthStorage);
54
- if (shouldClear)
55
- clearOAuthStorage(oAuthStorage);
56
- }
57
- this.oAuthService.configure(this.oAuthConfig);
58
- this.oAuthService.events
59
- .pipe(filter(event => event.type === 'token_refresh_error'))
60
- .subscribe(() => this.navigateToLogin());
61
- return this.oAuthService
62
- .loadDiscoveryDocument()
63
- .then(() => {
64
- if (this.oAuthService.hasValidAccessToken() || !this.oAuthService.getRefreshToken()) {
65
- return Promise.resolve();
66
- }
67
- return this.refreshToken();
68
- })
69
- .catch(this.catchError);
70
- }
71
- refreshToken() {
72
- return this.oAuthService.refreshToken().catch(() => clearOAuthStorage());
73
- }
74
- listenToOauthErrors() {
75
- this.oAuthService.events
76
- .pipe(filter(event => event instanceof OAuthErrorEvent), tap(() => clearOAuthStorage()), switchMap(() => this.configState.refreshAppState()))
77
- .subscribe();
78
- }
79
- }
80
- function shouldStorageClear(clientId, storage) {
81
- const key = 'abpOAuthClientId';
82
- if (!storage.getItem(key)) {
83
- storage.setItem(key, clientId);
84
- return false;
85
- }
86
- const shouldClear = storage.getItem(key) !== clientId;
87
- if (shouldClear)
88
- storage.setItem(key, clientId);
89
- return shouldClear;
34
+ class AuthFlowStrategy {
35
+ constructor(injector) {
36
+ this.injector = injector;
37
+ this.catchError = (err) => {
38
+ this.httpErrorReporter.reportError(err);
39
+ return of(null);
40
+ };
41
+ this.httpErrorReporter = injector.get(HttpErrorReporterService);
42
+ this.environment = injector.get(EnvironmentService);
43
+ this.configState = injector.get(ConfigStateService);
44
+ this.oAuthService = injector.get(OAuthService);
45
+ this.sessionState = injector.get(SessionStateService);
46
+ this.localStorageService = injector.get(AbpLocalStorageService);
47
+ this.oAuthConfig = this.environment.getEnvironment().oAuthConfig || {};
48
+ this.tenantKey = injector.get(TENANT_KEY);
49
+ this.router = injector.get(Router);
50
+ this.listenToOauthErrors();
51
+ }
52
+ async init() {
53
+ if (this.oAuthConfig.clientId) {
54
+ const shouldClear = shouldStorageClear(this.oAuthConfig.clientId, oAuthStorage);
55
+ if (shouldClear)
56
+ clearOAuthStorage(oAuthStorage);
57
+ }
58
+ this.oAuthService.configure(this.oAuthConfig);
59
+ this.oAuthService.events
60
+ .pipe(filter(event => event.type === 'token_refresh_error'))
61
+ .subscribe(() => this.navigateToLogin());
62
+ this.navigateToPreviousUrl();
63
+ return this.oAuthService
64
+ .loadDiscoveryDocument()
65
+ .then(() => {
66
+ if (this.oAuthService.hasValidAccessToken() || !this.oAuthService.getRefreshToken()) {
67
+ return Promise.resolve();
68
+ }
69
+ return this.refreshToken();
70
+ })
71
+ .catch(this.catchError);
72
+ }
73
+ navigateToPreviousUrl() {
74
+ const { responseType } = this.oAuthConfig;
75
+ if (responseType === 'code') {
76
+ this.oAuthService.events
77
+ .pipe(filter(event => event.type === 'token_received' && !!this.oAuthService.state), take(1), map(() => {
78
+ const redirect_uri = decodeURIComponent(this.oAuthService.state);
79
+ if (redirect_uri && redirect_uri !== '/') {
80
+ return redirect_uri;
81
+ }
82
+ return '/';
83
+ }), switchMap(redirectUri => this.configState.getOne$('currentUser').pipe(filter(user => !!user?.isAuthenticated), tap(() => this.router.navigate([redirectUri])))))
84
+ .subscribe();
85
+ }
86
+ }
87
+ refreshToken() {
88
+ return this.oAuthService.refreshToken().catch(() => clearOAuthStorage());
89
+ }
90
+ listenToOauthErrors() {
91
+ this.oAuthService.events
92
+ .pipe(filter(event => event instanceof OAuthErrorEvent), tap(() => clearOAuthStorage()), switchMap(() => this.configState.refreshAppState()))
93
+ .subscribe();
94
+ }
95
+ }
96
+ function shouldStorageClear(clientId, storage) {
97
+ const key = 'abpOAuthClientId';
98
+ if (!storage.getItem(key)) {
99
+ storage.setItem(key, clientId);
100
+ return false;
101
+ }
102
+ const shouldClear = storage.getItem(key) !== clientId;
103
+ if (shouldClear)
104
+ storage.setItem(key, clientId);
105
+ return shouldClear;
90
106
  }
91
107
 
92
- class AuthCodeFlowStrategy extends AuthFlowStrategy {
93
- constructor() {
94
- super(...arguments);
95
- this.isInternalAuth = false;
96
- }
97
- async init() {
98
- return super
99
- .init()
100
- .then(() => this.oAuthService.tryLogin().catch(noop))
101
- .then(() => this.oAuthService.setupAutomaticSilentRefresh({}, 'access_token'));
102
- }
103
- navigateToLogin(queryParams) {
104
- this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
105
- }
106
- checkIfInternalAuth(queryParams) {
107
- this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
108
- return false;
109
- }
110
- logout(queryParams) {
111
- return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams)));
112
- }
113
- login(queryParams) {
114
- this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
115
- return of(null);
116
- }
117
- getCultureParams(queryParams) {
118
- const lang = this.sessionState.getLanguage();
119
- const culture = { culture: lang, 'ui-culture': lang };
120
- return { ...(lang && culture), ...queryParams };
121
- }
108
+ class AuthCodeFlowStrategy extends AuthFlowStrategy {
109
+ constructor() {
110
+ super(...arguments);
111
+ this.isInternalAuth = false;
112
+ }
113
+ async init() {
114
+ return super
115
+ .init()
116
+ .then(() => this.oAuthService.tryLogin().catch(noop))
117
+ .then(() => this.oAuthService.setupAutomaticSilentRefresh({}, 'access_token'));
118
+ }
119
+ navigateToLogin(queryParams) {
120
+ let additionalState = '';
121
+ if (queryParams?.returnUrl) {
122
+ additionalState = queryParams.returnUrl;
123
+ }
124
+ const cultureParams = this.getCultureParams(queryParams);
125
+ this.oAuthService.initCodeFlow(additionalState, cultureParams);
126
+ }
127
+ checkIfInternalAuth(queryParams) {
128
+ this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
129
+ return false;
130
+ }
131
+ logout(queryParams) {
132
+ return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams)));
133
+ }
134
+ login(queryParams) {
135
+ this.oAuthService.initCodeFlow('', this.getCultureParams(queryParams));
136
+ return of(null);
137
+ }
138
+ getCultureParams(queryParams) {
139
+ const lang = this.sessionState.getLanguage();
140
+ const culture = { culture: lang, 'ui-culture': lang };
141
+ return { ...(lang && culture), ...queryParams };
142
+ }
122
143
  }
123
144
 
124
- const cookieKey = 'rememberMe';
125
- const storageKey = 'passwordFlow';
126
- const pipeToLogin = function (params, injector) {
127
- const configState = injector.get(ConfigStateService);
128
- const router = injector.get(Router);
129
- const localStorage = injector.get(AbpLocalStorageService);
130
- return pipe(switchMap(() => configState.refreshAppState()), tap(() => {
131
- setRememberMe(params.rememberMe, localStorage);
132
- if (params.redirectUrl)
133
- router.navigate([params.redirectUrl]);
134
- }));
135
- };
136
- function setRememberMe(remember, localStorageService) {
137
- removeRememberMe(localStorageService);
138
- localStorageService.setItem(storageKey, 'true');
139
- document.cookie = `${cookieKey}=true; path=/${remember ? ' ;expires=Fri, 31 Dec 9999 23:59:59 GMT' : ''}`;
140
- }
141
- function removeRememberMe(localStorageService) {
142
- localStorageService.removeItem(storageKey);
143
- document.cookie = cookieKey + '= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT';
145
+ const cookieKey = 'rememberMe';
146
+ const storageKey = 'passwordFlow';
147
+ const pipeToLogin = function (params, injector) {
148
+ const configState = injector.get(ConfigStateService);
149
+ const router = injector.get(Router);
150
+ const localStorage = injector.get(AbpLocalStorageService);
151
+ return pipe(switchMap(() => configState.refreshAppState()), tap(() => {
152
+ setRememberMe(params.rememberMe, localStorage);
153
+ if (params.redirectUrl)
154
+ router.navigate([params.redirectUrl]);
155
+ }));
156
+ };
157
+ function setRememberMe(remember, localStorageService) {
158
+ removeRememberMe(localStorageService);
159
+ localStorageService.setItem(storageKey, 'true');
160
+ document.cookie = `${cookieKey}=true; path=/${remember ? ' ;expires=Fri, 31 Dec 9999 23:59:59 GMT' : ''}`;
161
+ }
162
+ function removeRememberMe(localStorageService) {
163
+ localStorageService.removeItem(storageKey);
164
+ document.cookie = cookieKey + '= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT';
144
165
  }
145
166
 
146
- function getCookieValueByName(name) {
147
- const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
148
- return match ? match[2] : '';
149
- }
150
- class AuthPasswordFlowStrategy extends AuthFlowStrategy {
151
- constructor() {
152
- super(...arguments);
153
- this.isInternalAuth = true;
154
- this.cookieKey = 'rememberMe';
155
- this.storageKey = 'passwordFlow';
156
- }
157
- listenToTokenExpiration() {
158
- this.oAuthService.events
159
- .pipe(filter(event => event instanceof OAuthInfoEvent &&
160
- event.type === 'token_expires' &&
161
- event.info === 'access_token'))
162
- .subscribe(() => {
163
- if (this.oAuthService.getRefreshToken()) {
164
- this.refreshToken();
165
- }
166
- else {
167
- this.oAuthService.logOut();
168
- removeRememberMe(this.localStorageService);
169
- this.configState.refreshAppState().subscribe();
170
- }
171
- });
172
- }
173
- async init() {
174
- if (!getCookieValueByName(this.cookieKey) && localStorage.getItem(this.storageKey)) {
175
- this.oAuthService.logOut();
176
- }
177
- return super.init().then(() => this.listenToTokenExpiration());
178
- }
179
- navigateToLogin(queryParams) {
180
- const router = this.injector.get(Router);
181
- return router.navigate(['/account/login'], { queryParams });
182
- }
183
- checkIfInternalAuth() {
184
- return true;
185
- }
186
- login(params) {
187
- const tenant = this.sessionState.getTenant();
188
- return from(this.oAuthService.fetchTokenUsingPasswordFlow(params.username, params.password, new HttpHeaders({ ...(tenant && tenant.id && { [this.tenantKey]: tenant.id }) }))).pipe(pipeToLogin(params, this.injector));
189
- }
190
- logout(queryParams) {
191
- const router = this.injector.get(Router);
192
- return from(this.oAuthService.revokeTokenAndLogout(queryParams)).pipe(switchMap(() => this.configState.refreshAppState()), tap(() => {
193
- router.navigateByUrl('/');
194
- removeRememberMe(this.localStorageService);
195
- }));
196
- }
197
- refreshToken() {
198
- return this.oAuthService.refreshToken().catch(() => {
199
- clearOAuthStorage();
200
- removeRememberMe(this.localStorageService);
201
- });
202
- }
167
+ function getCookieValueByName(name) {
168
+ const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
169
+ return match ? match[2] : '';
170
+ }
171
+ class AuthPasswordFlowStrategy extends AuthFlowStrategy {
172
+ constructor() {
173
+ super(...arguments);
174
+ this.isInternalAuth = true;
175
+ this.cookieKey = 'rememberMe';
176
+ this.storageKey = 'passwordFlow';
177
+ }
178
+ listenToTokenExpiration() {
179
+ this.oAuthService.events
180
+ .pipe(filter(event => event instanceof OAuthInfoEvent &&
181
+ event.type === 'token_expires' &&
182
+ event.info === 'access_token'))
183
+ .subscribe(() => {
184
+ if (this.oAuthService.getRefreshToken()) {
185
+ this.refreshToken();
186
+ }
187
+ else {
188
+ this.oAuthService.logOut();
189
+ removeRememberMe(this.localStorageService);
190
+ this.configState.refreshAppState().subscribe();
191
+ }
192
+ });
193
+ }
194
+ async init() {
195
+ if (!getCookieValueByName(this.cookieKey) && localStorage.getItem(this.storageKey)) {
196
+ this.oAuthService.logOut();
197
+ }
198
+ return super.init().then(() => this.listenToTokenExpiration());
199
+ }
200
+ navigateToLogin(queryParams) {
201
+ const router = this.injector.get(Router);
202
+ return router.navigate(['/account/login'], { queryParams });
203
+ }
204
+ checkIfInternalAuth() {
205
+ return true;
206
+ }
207
+ login(params) {
208
+ const tenant = this.sessionState.getTenant();
209
+ return from(this.oAuthService.fetchTokenUsingPasswordFlow(params.username, params.password, new HttpHeaders({ ...(tenant && tenant.id && { [this.tenantKey]: tenant.id }) }))).pipe(pipeToLogin(params, this.injector));
210
+ }
211
+ logout(queryParams) {
212
+ const router = this.injector.get(Router);
213
+ return from(this.oAuthService.revokeTokenAndLogout(queryParams)).pipe(switchMap(() => this.configState.refreshAppState()), tap(() => {
214
+ router.navigateByUrl('/');
215
+ removeRememberMe(this.localStorageService);
216
+ }));
217
+ }
218
+ refreshToken() {
219
+ return this.oAuthService.refreshToken().catch(() => {
220
+ clearOAuthStorage();
221
+ removeRememberMe(this.localStorageService);
222
+ });
223
+ }
203
224
  }
204
225
 
205
- const AUTH_FLOW_STRATEGY = {
206
- Code(injector) {
207
- return new AuthCodeFlowStrategy(injector);
208
- },
209
- Password(injector) {
210
- return new AuthPasswordFlowStrategy(injector);
211
- },
226
+ const AUTH_FLOW_STRATEGY = {
227
+ Code(injector) {
228
+ return new AuthCodeFlowStrategy(injector);
229
+ },
230
+ Password(injector) {
231
+ return new AuthPasswordFlowStrategy(injector);
232
+ },
212
233
  };
213
234
 
214
- class AbpOAuthService {
215
- constructor(injector) {
216
- this.injector = injector;
217
- this.oAuthService = this.injector.get(OAuthService);
218
- }
219
- get isInternalAuth() {
220
- return this.strategy.isInternalAuth;
221
- }
222
- async init() {
223
- const environmentService = this.injector.get(EnvironmentService);
224
- const result$ = environmentService.getEnvironment$().pipe(map(env => env?.oAuthConfig), filter(Boolean), tap(oAuthConfig => {
225
- this.strategy =
226
- oAuthConfig.responseType === 'code'
227
- ? AUTH_FLOW_STRATEGY.Code(this.injector)
228
- : AUTH_FLOW_STRATEGY.Password(this.injector);
229
- }), switchMap(() => from(this.strategy.init())), take(1));
230
- return await lastValueFrom(result$);
231
- }
232
- logout(queryParams) {
233
- return this.strategy.logout(queryParams);
234
- }
235
- navigateToLogin(queryParams) {
236
- this.strategy.navigateToLogin(queryParams);
237
- }
238
- login(params) {
239
- return this.strategy.login(params);
240
- }
241
- get isAuthenticated() {
242
- return this.oAuthService.hasValidAccessToken();
243
- }
244
- loginUsingGrant(grantType, parameters, headers) {
245
- const { clientId: client_id, dummyClientSecret: client_secret } = this.oAuthService;
246
- const access_token = this.oAuthService.getAccessToken();
247
- const p = {
248
- access_token,
249
- grant_type: grantType,
250
- client_id,
251
- ...parameters,
252
- };
253
- if (client_secret) {
254
- p['client_secret'] = client_secret;
255
- }
256
- return this.oAuthService.fetchTokenUsingGrant(grantType, p, headers);
257
- }
258
- }
259
- AbpOAuthServicefac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthService, deps: [{ token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable });
260
- AbpOAuthService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthService, providedIn: 'root' });
261
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthService, decorators: [{
262
- type: Injectable,
263
- args: [{
264
- providedIn: 'root',
265
- }]
235
+ class AbpOAuthService {
236
+ get isInternalAuth() {
237
+ return this.strategy.isInternalAuth;
238
+ }
239
+ constructor(injector) {
240
+ this.injector = injector;
241
+ this.oAuthService = this.injector.get(OAuthService);
242
+ }
243
+ async init() {
244
+ const environmentService = this.injector.get(EnvironmentService);
245
+ const result$ = environmentService.getEnvironment$().pipe(map(env => env?.oAuthConfig), filter(Boolean), tap(oAuthConfig => {
246
+ this.strategy =
247
+ oAuthConfig.responseType === 'code'
248
+ ? AUTH_FLOW_STRATEGY.Code(this.injector)
249
+ : AUTH_FLOW_STRATEGY.Password(this.injector);
250
+ }), switchMap(() => from(this.strategy.init())), take(1));
251
+ return await lastValueFrom(result$);
252
+ }
253
+ logout(queryParams) {
254
+ return this.strategy.logout(queryParams);
255
+ }
256
+ navigateToLogin(queryParams) {
257
+ this.strategy.navigateToLogin(queryParams);
258
+ }
259
+ login(params) {
260
+ return this.strategy.login(params);
261
+ }
262
+ get isAuthenticated() {
263
+ return this.oAuthService.hasValidAccessToken();
264
+ }
265
+ loginUsingGrant(grantType, parameters, headers) {
266
+ const { clientId: client_id, dummyClientSecret: client_secret } = this.oAuthService;
267
+ const access_token = this.oAuthService.getAccessToken();
268
+ const p = {
269
+ access_token,
270
+ grant_type: grantType,
271
+ client_id,
272
+ ...parameters,
273
+ };
274
+ if (client_secret) {
275
+ p['client_secret'] = client_secret;
276
+ }
277
+ return this.oAuthService.fetchTokenUsingGrant(grantType, p, headers);
278
+ }
279
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthService, deps: [{ token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable }); }
280
+ static { thisprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthService, providedIn: 'root' }); }
281
+ }
282
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthService, decorators: [{
283
+ type: Injectable,
284
+ args: [{
285
+ providedIn: 'root',
286
+ }]
266
287
  }], ctorParameters: function () { return [{ type: i0.Injector }]; } });
267
288
 
268
- class OAuthConfigurationHandler {
269
- constructor(oAuthService, environmentService, options) {
270
- this.oAuthService = oAuthService;
271
- this.environmentService = environmentService;
272
- this.options = options;
273
- this.listenToSetEnvironment();
274
- }
275
- listenToSetEnvironment() {
276
- this.environmentService
277
- .createOnUpdateStream(state => state)
278
- .pipe(map(environment => environment.oAuthConfig), filter(config => !compare(config, this.options.environment.oAuthConfig)))
279
- .subscribe((config) => {
280
- this.oAuthService.configure(config);
281
- });
282
- }
283
- }
284
- OAuthConfigurationHandlerfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: OAuthConfigurationHandler, deps: [{ token: i1.OAuthService }, { token: i2.EnvironmentService }, { token: CORE_OPTIONS }], target: i0.ɵɵFactoryTarget.Injectable });
285
- OAuthConfigurationHandler.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: OAuthConfigurationHandler, providedIn: 'root' });
286
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: OAuthConfigurationHandler, decorators: [{
287
- type: Injectable,
288
- args: [{
289
- providedIn: 'root',
290
- }]
291
- }], ctorParameters: function () { return [{ type: i1.OAuthService }, { type: i2.EnvironmentService }, { type: undefined, decorators: [{
292
- type: Inject,
293
- args: [CORE_OPTIONS]
289
+ class OAuthConfigurationHandler {
290
+ constructor(oAuthService, environmentService, options) {
291
+ this.oAuthService = oAuthService;
292
+ this.environmentService = environmentService;
293
+ this.options = options;
294
+ this.listenToSetEnvironment();
295
+ }
296
+ listenToSetEnvironment() {
297
+ this.environmentService
298
+ .createOnUpdateStream(state => state)
299
+ .pipe(map(environment => environment.oAuthConfig), filter(config => !compare(config, this.options.environment.oAuthConfig)))
300
+ .subscribe((config) => {
301
+ this.oAuthService.configure(config);
302
+ });
303
+ }
304
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: OAuthConfigurationHandler, deps: [{ token: i1.OAuthService }, { token: i2.EnvironmentService }, { token: CORE_OPTIONS }], target: i0.ɵɵFactoryTarget.Injectable }); }
305
+ static { thisprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: OAuthConfigurationHandler, providedIn: 'root' }); }
306
+ }
307
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: OAuthConfigurationHandler, decorators: [{
308
+ type: Injectable,
309
+ args: [{
310
+ providedIn: 'root',
311
+ }]
312
+ }], ctorParameters: function () { return [{ type: i1.OAuthService }, { type: i2.EnvironmentService }, { type: undefined, decorators: [{
313
+ type: Inject,
314
+ args: [CORE_OPTIONS]
294
315
  }] }]; } });
295
316
 
296
- class OAuthApiInterceptor {
297
- constructor(oAuthService, sessionState, httpWaitService, tenantKey) {
298
- this.oAuthService = oAuthService;
299
- this.sessionState = sessionState;
300
- this.httpWaitService = httpWaitService;
301
- this.tenantKey = tenantKey;
302
- }
303
- intercept(request, next) {
304
- this.httpWaitService.addRequest(request);
305
- const isExternalRequest = request.context?.get(IS_EXTERNAL_REQUEST);
306
- const newRequest = isExternalRequest
307
- ? request
308
- : request.clone({
309
- setHeaders: this.getAdditionalHeaders(request.headers),
310
- });
311
- return next
312
- .handle(newRequest)
313
- .pipe(finalize(() => this.httpWaitService.deleteRequest(request)));
314
- }
315
- getAdditionalHeaders(existingHeaders) {
316
- const headers = {};
317
- const token = this.oAuthService.getAccessToken();
318
- if (!existingHeaders?.has('Authorization') && token) {
319
- headers['Authorization'] = `Bearer ${token}`;
320
- }
321
- const lang = this.sessionState.getLanguage();
322
- if (!existingHeaders?.has('Accept-Language') && lang) {
323
- headers['Accept-Language'] = lang;
324
- }
325
- const tenant = this.sessionState.getTenant();
326
- if (!existingHeaders?.has(this.tenantKey) && tenant?.id) {
327
- headers[this.tenantKey] = tenant.id;
328
- }
329
- headers['X-Requested-With'] = 'XMLHttpRequest';
330
- return headers;
331
- }
332
- }
333
- OAuthApiInterceptorfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: OAuthApiInterceptor, deps: [{ token: i1.OAuthService }, { token: i2.SessionStateService }, { token: i2.HttpWaitService }, { token: TENANT_KEY }], target: i0.ɵɵFactoryTarget.Injectable });
334
- OAuthApiInterceptor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: OAuthApiInterceptor, providedIn: 'root' });
335
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: OAuthApiInterceptor, decorators: [{
336
- type: Injectable,
337
- args: [{
338
- providedIn: 'root',
339
- }]
340
- }], ctorParameters: function () { return [{ type: i1.OAuthService }, { type: i2.SessionStateService }, { type: i2.HttpWaitService }, { type: undefined, decorators: [{
341
- type: Inject,
342
- args: [TENANT_KEY]
317
+ class OAuthApiInterceptor {
318
+ constructor(oAuthService, sessionState, httpWaitService, tenantKey) {
319
+ this.oAuthService = oAuthService;
320
+ this.sessionState = sessionState;
321
+ this.httpWaitService = httpWaitService;
322
+ this.tenantKey = tenantKey;
323
+ }
324
+ intercept(request, next) {
325
+ this.httpWaitService.addRequest(request);
326
+ const isExternalRequest = request.context?.get(IS_EXTERNAL_REQUEST);
327
+ const newRequest = isExternalRequest
328
+ ? request
329
+ : request.clone({
330
+ setHeaders: this.getAdditionalHeaders(request.headers),
331
+ });
332
+ return next
333
+ .handle(newRequest)
334
+ .pipe(finalize(() => this.httpWaitService.deleteRequest(request)));
335
+ }
336
+ getAdditionalHeaders(existingHeaders) {
337
+ const headers = {};
338
+ const token = this.oAuthService.getAccessToken();
339
+ if (!existingHeaders?.has('Authorization') && token) {
340
+ headers['Authorization'] = `Bearer ${token}`;
341
+ }
342
+ const lang = this.sessionState.getLanguage();
343
+ if (!existingHeaders?.has('Accept-Language') && lang) {
344
+ headers['Accept-Language'] = lang;
345
+ }
346
+ const tenant = this.sessionState.getTenant();
347
+ if (!existingHeaders?.has(this.tenantKey) && tenant?.id) {
348
+ headers[this.tenantKey] = tenant.id;
349
+ }
350
+ headers['X-Requested-With'] = 'XMLHttpRequest';
351
+ return headers;
352
+ }
353
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: OAuthApiInterceptor, deps: [{ token: i1.OAuthService }, { token: i2.SessionStateService }, { token: i2.HttpWaitService }, { token: TENANT_KEY }], target: i0.ɵɵFactoryTarget.Injectable }); }
354
+ static { thisprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: OAuthApiInterceptor, providedIn: 'root' }); }
355
+ }
356
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: OAuthApiInterceptor, decorators: [{
357
+ type: Injectable,
358
+ args: [{
359
+ providedIn: 'root',
360
+ }]
361
+ }], ctorParameters: function () { return [{ type: i1.OAuthService }, { type: i2.SessionStateService }, { type: i2.HttpWaitService }, { type: undefined, decorators: [{
362
+ type: Inject,
363
+ args: [TENANT_KEY]
343
364
  }] }]; } });
344
365
 
345
- class AbpOAuthGuard {
346
- constructor(oauthService, authService) {
347
- this.oauthService = oauthService;
348
- this.authService = authService;
349
- }
350
- canActivate() {
351
- const hasValidAccessToken = this.oauthService.hasValidAccessToken();
352
- if (hasValidAccessToken) {
353
- return true;
354
- }
355
- this.authService.navigateToLogin();
356
- return false;
357
- }
358
- }
359
- AbpOAuthGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthGuard, deps: [{ token: i1.OAuthService }, { token: i2.AuthService }], target: i0.ɵɵFactoryTarget.Injectable });
360
- AbpOAuthGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthGuard, providedIn: 'root' });
361
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthGuard, decorators: [{
362
- type: Injectable,
363
- args: [{
364
- providedIn: 'root',
365
- }]
366
- }], ctorParameters: function () { return [{ type: i1.OAuthService }, { type: i2.AuthService }]; } });
366
+ class AbpOAuthGuard {
367
+ constructor() {
368
+ this.oAuthService = inject(OAuthService);
369
+ this.authService = inject(AuthService);
370
+ this.httpErrorReporter = inject(HttpErrorReporterService);
371
+ }
372
+ canActivate(route, state) {
373
+ const hasValidAccessToken = this.oAuthService.hasValidAccessToken();
374
+ if (hasValidAccessToken) {
375
+ return true;
376
+ }
377
+ return of(false).pipe(tap$1(() => this.httpErrorReporter.reportError({ status: 401 })), delay(1500), tap$1(() => {
378
+ const params = { returnUrl: state.url };
379
+ this.authService.navigateToLogin(params);
380
+ }));
381
+ }
382
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthGuard, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
383
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthGuard, providedIn: 'root' }); }
384
+ }
385
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthGuard, decorators: [{
386
+ type: Injectable,
387
+ args: [{
388
+ providedIn: 'root',
389
+ }]
390
+ }] });
367
391
 
368
- const NavigateToManageProfileProvider = {
369
- provide: NAVIGATE_TO_MANAGE_PROFILE,
370
- useFactory: () => {
371
- const environment = inject(EnvironmentService);
372
- return () => {
373
- const env = environment.getEnvironment();
374
- if (!env.oAuthConfig) {
375
- console.warn('The oAuthConfig env is missing on environment.ts');
376
- return;
377
- }
378
- const { issuer } = env.oAuthConfig;
379
- const path = issuer.endsWith('/') ? issuer : `${issuer}/`;
380
- window.open(`${path}Account/Manage?returnUrl=${window.location.href}`, '_self');
381
- };
382
- },
392
+ const NavigateToManageProfileProvider = {
393
+ provide: NAVIGATE_TO_MANAGE_PROFILE,
394
+ useFactory: () => {
395
+ const environment = inject(EnvironmentService);
396
+ return () => {
397
+ const env = environment.getEnvironment();
398
+ if (!env.oAuthConfig) {
399
+ console.warn('The oAuthConfig env is missing on environment.ts');
400
+ return;
401
+ }
402
+ const { issuer } = env.oAuthConfig;
403
+ const path = issuer.endsWith('/') ? issuer : `${issuer}/`;
404
+ window.open(`${path}Account/Manage?returnUrl=${window.location.href}`, '_self');
405
+ };
406
+ },
383
407
  };
384
408
 
385
- function storageFactory() {
386
- return oAuthStorage;
409
+ function storageFactory() {
410
+ return oAuthStorage;
387
411
  }
388
412
 
389
- const checkAccessToken = function (injector) {
390
- const configState = injector.get(ConfigStateService);
391
- const oAuth = injector.get(OAuthService);
392
- if (oAuth.hasValidAccessToken() && !configState.getDeep('currentUser.id')) {
393
- clearOAuthStorage();
394
- }
413
+ const checkAccessToken = function (injector) {
414
+ const configState = injector.get(ConfigStateService);
415
+ const oAuth = injector.get(OAuthService);
416
+ if (oAuth.hasValidAccessToken() && !configState.getDeep('currentUser.id')) {
417
+ clearOAuthStorage();
418
+ }
395
419
  };
396
420
 
397
- class AbpOAuthModule {
398
- static forRoot() {
399
- return {
400
- ngModule: AbpOAuthModule,
401
- providers: [
402
- {
403
- provide: AuthService,
404
- useClass: AbpOAuthService,
405
- },
406
- {
407
- provide: AuthGuard,
408
- useClass: AbpOAuthGuard,
409
- },
410
- {
411
- provide: ApiInterceptor,
412
- useClass: OAuthApiInterceptor,
413
- },
414
- {
415
- provide: PIPE_TO_LOGIN_FN_KEY,
416
- useValue: pipeToLogin,
417
- },
418
- {
419
- provide: CHECK_AUTHENTICATION_STATE_FN_KEY,
420
- useValue: checkAccessToken,
421
- },
422
- {
423
- provide: HTTP_INTERCEPTORS,
424
- useExisting: ApiInterceptor,
425
- multi: true,
426
- },
427
- NavigateToManageProfileProvider,
428
- {
429
- provide: APP_INITIALIZER,
430
- multi: true,
431
- deps: [OAuthConfigurationHandler],
432
- useFactory: noop,
433
- },
434
- OAuthModule.forRoot().providers,
435
- { provide: OAuthStorage, useClass: AbpLocalStorageService },
436
- ],
437
- };
438
- }
439
- }
440
- AbpOAuthModulefac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
441
- AbpOAuthModulemod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthModule, imports: [CommonModule, OAuthModule] });
442
- AbpOAuthModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthModule, imports: [CommonModule, OAuthModule] });
443
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: AbpOAuthModule, decorators: [{
444
- type: NgModule,
445
- args: [{
446
- imports: [CommonModule, OAuthModule],
447
- }]
421
+ class AbpOAuthModule {
422
+ static forRoot() {
423
+ return {
424
+ ngModule: AbpOAuthModule,
425
+ providers: [
426
+ {
427
+ provide: AuthService,
428
+ useClass: AbpOAuthService,
429
+ },
430
+ {
431
+ provide: AuthGuard,
432
+ useClass: AbpOAuthGuard,
433
+ },
434
+ {
435
+ provide: ApiInterceptor,
436
+ useClass: OAuthApiInterceptor,
437
+ },
438
+ {
439
+ provide: PIPE_TO_LOGIN_FN_KEY,
440
+ useValue: pipeToLogin,
441
+ },
442
+ {
443
+ provide: CHECK_AUTHENTICATION_STATE_FN_KEY,
444
+ useValue: checkAccessToken,
445
+ },
446
+ {
447
+ provide: HTTP_INTERCEPTORS,
448
+ useExisting: ApiInterceptor,
449
+ multi: true,
450
+ },
451
+ NavigateToManageProfileProvider,
452
+ {
453
+ provide: APP_INITIALIZER,
454
+ multi: true,
455
+ deps: [OAuthConfigurationHandler],
456
+ useFactory: noop,
457
+ },
458
+ OAuthModule.forRoot().providers,
459
+ { provide: OAuthStorage, useClass: AbpLocalStorageService },
460
+ ],
461
+ };
462
+ }
463
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
464
+ static { thismod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthModule, imports: [CommonModule, OAuthModule] }); }
465
+ static { thisinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthModule, imports: [CommonModule, OAuthModule] }); }
466
+ }
467
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.1", ngImport: i0, type: AbpOAuthModule, decorators: [{
468
+ type: NgModule,
469
+ args: [{
470
+ imports: [CommonModule, OAuthModule],
471
+ }]
448
472
  }] });
449
473
 
450
- /**
451
- * Generated bundle index. Do not edit.
474
+ /**
475
+ * Generated bundle index. Do not edit.
452
476
  */
453
477
 
454
478
  export { AUTH_FLOW_STRATEGY, AbpOAuthGuard, AbpOAuthModule, AbpOAuthService, AuthCodeFlowStrategy, AuthFlowStrategy, AuthPasswordFlowStrategy, NavigateToManageProfileProvider, OAuthApiInterceptor, OAuthConfigurationHandler, checkAccessToken, clearOAuthStorage, oAuthStorage, pipeToLogin, removeRememberMe, setRememberMe, storageFactory };