@delon/auth 16.4.3 → 17.0.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.
- package/esm2022/index.mjs +2 -20
- package/esm2022/src/auth.config.mjs +1 -2
- package/esm2022/src/index.mjs +7 -0
- package/esm2022/src/provide.mjs +42 -0
- package/esm2022/src/social/social.service.mjs +6 -6
- package/esm2022/src/store/cookie-storage.service.mjs +2 -3
- package/esm2022/src/store/index.mjs +6 -0
- package/esm2022/src/store/local-storage.service.mjs +2 -3
- package/esm2022/src/store/memory.service.mjs +1 -6
- package/esm2022/src/store/session-storage.service.mjs +2 -3
- package/esm2022/src/token/base.interceptor.mjs +26 -66
- package/esm2022/src/token/helper.mjs +8 -5
- package/esm2022/src/token/index.mjs +6 -0
- package/esm2022/src/token/jwt/index.mjs +5 -0
- package/esm2022/src/token/jwt/jwt.guard.mjs +8 -9
- package/esm2022/src/token/jwt/jwt.interceptor.mjs +20 -30
- package/esm2022/src/token/simple/index.mjs +4 -0
- package/esm2022/src/token/simple/simple.guard.mjs +8 -9
- package/esm2022/src/token/simple/simple.interceptor.mjs +38 -47
- package/esm2022/src/token/token.service.mjs +6 -6
- package/fesm2022/auth.mjs +187 -223
- package/fesm2022/auth.mjs.map +1 -1
- package/index.d.ts +1 -19
- package/package.json +2 -2
- package/src/index.d.ts +6 -0
- package/src/provide.d.ts +24 -0
- package/src/store/cookie-storage.service.d.ts +1 -2
- package/src/store/index.d.ts +5 -0
- package/src/store/local-storage.service.d.ts +1 -2
- package/src/store/memory.service.d.ts +0 -5
- package/src/store/session-storage.service.d.ts +1 -2
- package/src/token/base.interceptor.d.ts +3 -14
- package/src/token/helper.d.ts +1 -2
- package/src/token/index.d.ts +5 -0
- package/src/token/jwt/index.d.ts +4 -0
- package/src/token/jwt/jwt.guard.d.ts +1 -3
- package/src/token/jwt/jwt.interceptor.d.ts +2 -18
- package/src/token/simple/index.d.ts +3 -0
- package/src/token/simple/simple.guard.d.ts +1 -3
- package/src/token/simple/simple.interceptor.d.ts +2 -18
- package/esm2022/src/auth.module.mjs +0 -12
- package/src/auth.module.d.ts +0 -6
package/fesm2022/auth.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { DOCUMENT } from '@angular/common';
|
|
2
2
|
import * as i0 from '@angular/core';
|
|
3
|
-
import { InjectionToken, inject, Injectable, Inject,
|
|
4
|
-
import { Subject, BehaviorSubject, share,
|
|
3
|
+
import { InjectionToken, inject, Injectable, Inject, makeEnvironmentProviders } from '@angular/core';
|
|
4
|
+
import { Subject, BehaviorSubject, share, map, filter, interval, Observable } from 'rxjs';
|
|
5
5
|
import * as i1 from '@delon/util/config';
|
|
6
6
|
import { AlainConfigService } from '@delon/util/config';
|
|
7
7
|
import * as i1$1 from '@angular/router';
|
|
8
8
|
import { Router } from '@angular/router';
|
|
9
|
-
import { HttpContextToken, HttpErrorResponse
|
|
9
|
+
import { HttpContextToken, HttpErrorResponse } from '@angular/common/http';
|
|
10
|
+
import { CookieService } from '@delon/util/browser';
|
|
10
11
|
|
|
11
12
|
const AUTH_DEFAULT_CONFIG = {
|
|
12
13
|
store_key: `_token`,
|
|
@@ -17,7 +18,6 @@ const AUTH_DEFAULT_CONFIG = {
|
|
|
17
18
|
token_send_place: 'header',
|
|
18
19
|
login_url: '/login',
|
|
19
20
|
ignores: [/\/login/, /assets\//, /passport\//],
|
|
20
|
-
executeOtherInterceptors: true,
|
|
21
21
|
refreshTime: 3000,
|
|
22
22
|
refreshOffset: 6000
|
|
23
23
|
};
|
|
@@ -32,8 +32,7 @@ function DA_STORE_TOKEN_LOCAL_FACTORY() {
|
|
|
32
32
|
* `localStorage` storage, **not lost after closing the browser**.
|
|
33
33
|
*
|
|
34
34
|
* ```ts
|
|
35
|
-
*
|
|
36
|
-
* { provide: DA_STORE_TOKEN, useClass: LocalStorageStore }
|
|
35
|
+
* provideAuth(withJWT(), withLocalStorage())
|
|
37
36
|
* ```
|
|
38
37
|
*/
|
|
39
38
|
class LocalStorageStore {
|
|
@@ -128,15 +127,15 @@ class TokenService {
|
|
|
128
127
|
ngOnDestroy() {
|
|
129
128
|
this.cleanRefresh();
|
|
130
129
|
}
|
|
131
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
132
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
130
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: TokenService, deps: [{ token: i1.AlainConfigService }, { token: DA_STORE_TOKEN }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
131
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: TokenService }); }
|
|
133
132
|
}
|
|
134
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
133
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: TokenService, decorators: [{
|
|
135
134
|
type: Injectable
|
|
136
|
-
}], ctorParameters:
|
|
135
|
+
}], ctorParameters: () => [{ type: i1.AlainConfigService }, { type: undefined, decorators: [{
|
|
137
136
|
type: Inject,
|
|
138
137
|
args: [DA_STORE_TOKEN]
|
|
139
|
-
}] }]
|
|
138
|
+
}] }] });
|
|
140
139
|
|
|
141
140
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
142
141
|
const DA_SERVICE_TOKEN = new InjectionToken('DA_SERVICE_TOKEN', {
|
|
@@ -231,26 +230,21 @@ class SocialService {
|
|
|
231
230
|
clearInterval(this._winTime);
|
|
232
231
|
this._winTime = null;
|
|
233
232
|
}
|
|
234
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
235
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
233
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: SocialService, deps: [{ token: DA_SERVICE_TOKEN }, { token: DOCUMENT }, { token: i1$1.Router }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
234
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: SocialService }); }
|
|
236
235
|
}
|
|
237
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
236
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: SocialService, decorators: [{
|
|
238
237
|
type: Injectable
|
|
239
|
-
}], ctorParameters:
|
|
238
|
+
}], ctorParameters: () => [{ type: undefined, decorators: [{
|
|
240
239
|
type: Inject,
|
|
241
240
|
args: [DA_SERVICE_TOKEN]
|
|
242
241
|
}] }, { type: undefined, decorators: [{
|
|
243
242
|
type: Inject,
|
|
244
243
|
args: [DOCUMENT]
|
|
245
|
-
}] }, { type: i1$1.Router }]
|
|
244
|
+
}] }, { type: i1$1.Router }] });
|
|
246
245
|
|
|
247
246
|
/**
|
|
248
247
|
* 内存存储,关掉浏览器标签后**丢失**。
|
|
249
|
-
*
|
|
250
|
-
* ```ts
|
|
251
|
-
* // global-config.module.ts
|
|
252
|
-
* { provide: DA_STORE_TOKEN, useClass: MemoryStore }
|
|
253
|
-
* ```
|
|
254
248
|
*/
|
|
255
249
|
class MemoryStore {
|
|
256
250
|
constructor() {
|
|
@@ -272,8 +266,7 @@ class MemoryStore {
|
|
|
272
266
|
* `sessionStorage` storage, **lost after closing the browser**.
|
|
273
267
|
*
|
|
274
268
|
* ```ts
|
|
275
|
-
*
|
|
276
|
-
* { provide: DA_STORE_TOKEN, useClass: SessionStorageStore }
|
|
269
|
+
* provideAuth(withJWT(), withSessionStorage())
|
|
277
270
|
* ```
|
|
278
271
|
*/
|
|
279
272
|
class SessionStorageStore {
|
|
@@ -293,8 +286,7 @@ class SessionStorageStore {
|
|
|
293
286
|
* `cookie` storage
|
|
294
287
|
*
|
|
295
288
|
* ```ts
|
|
296
|
-
*
|
|
297
|
-
* { provide: DA_STORE_TOKEN, useClass: CookieStorageStore, deps: [CookieService] }
|
|
289
|
+
* provideAuth(withJWT(), withCookie())
|
|
298
290
|
* ```
|
|
299
291
|
*/
|
|
300
292
|
class CookieStorageStore {
|
|
@@ -322,115 +314,6 @@ class CookieStorageStore {
|
|
|
322
314
|
}
|
|
323
315
|
}
|
|
324
316
|
|
|
325
|
-
function CheckSimple(model) {
|
|
326
|
-
return model != null && typeof model.token === 'string' && model.token.length > 0;
|
|
327
|
-
}
|
|
328
|
-
function CheckJwt(model, offset) {
|
|
329
|
-
try {
|
|
330
|
-
return model != null && !!model.token && !model.isExpired(offset);
|
|
331
|
-
}
|
|
332
|
-
catch (err) {
|
|
333
|
-
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
334
|
-
console.warn(`${err.message}, jump to login_url`);
|
|
335
|
-
}
|
|
336
|
-
return false;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
function ToLogin(options, injector, url) {
|
|
340
|
-
const router = injector.get(Router);
|
|
341
|
-
injector.get(DA_SERVICE_TOKEN).referrer.url = url || router.url;
|
|
342
|
-
if (options.token_invalid_redirect === true) {
|
|
343
|
-
setTimeout(() => {
|
|
344
|
-
if (/^https?:\/\//g.test(options.login_url)) {
|
|
345
|
-
injector.get(DOCUMENT).location.href = options.login_url;
|
|
346
|
-
}
|
|
347
|
-
else {
|
|
348
|
-
router.navigate([options.login_url]);
|
|
349
|
-
}
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Whether to allow anonymous login
|
|
356
|
-
*
|
|
357
|
-
* 是否允许匿名登录
|
|
358
|
-
*
|
|
359
|
-
* @example
|
|
360
|
-
* this.http.post(`login`, {
|
|
361
|
-
* name: 'cipchk', pwd: '123456'
|
|
362
|
-
* }, {
|
|
363
|
-
* context: new HttpContext().set(ALLOW_ANONYMOUS, true)
|
|
364
|
-
* })
|
|
365
|
-
*/
|
|
366
|
-
const ALLOW_ANONYMOUS = new HttpContextToken(() => false);
|
|
367
|
-
|
|
368
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
369
|
-
class HttpAuthInterceptorHandler {
|
|
370
|
-
constructor(next, interceptor) {
|
|
371
|
-
this.next = next;
|
|
372
|
-
this.interceptor = interceptor;
|
|
373
|
-
}
|
|
374
|
-
handle(req) {
|
|
375
|
-
return this.interceptor.intercept(req, this.next);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
class BaseInterceptor {
|
|
379
|
-
constructor(injector) {
|
|
380
|
-
this.injector = injector;
|
|
381
|
-
}
|
|
382
|
-
intercept(req, next) {
|
|
383
|
-
if (req.context.get(ALLOW_ANONYMOUS))
|
|
384
|
-
return next.handle(req);
|
|
385
|
-
const options = mergeConfig(this.injector.get(AlainConfigService));
|
|
386
|
-
if (Array.isArray(options.ignores)) {
|
|
387
|
-
for (const item of options.ignores) {
|
|
388
|
-
if (item.test(req.url))
|
|
389
|
-
return next.handle(req);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
if (this.isAuth(options)) {
|
|
393
|
-
req = this.setReq(req, options);
|
|
394
|
-
}
|
|
395
|
-
else {
|
|
396
|
-
ToLogin(options, this.injector);
|
|
397
|
-
// Interrupt Http request, so need to generate a new Observable
|
|
398
|
-
const err$ = new Observable((observer) => {
|
|
399
|
-
let statusText = '';
|
|
400
|
-
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
401
|
-
statusText = `来自 @delon/auth 的拦截,所请求URL未授权,若是登录API可加入 [url?_allow_anonymous=true] 来表示忽略校验,更多方法请参考: https://ng-alain.com/auth/getting-started#AlainAuthConfig\nThe interception from @delon/auth, the requested URL is not authorized. If the login API can add [url?_allow_anonymous=true] to ignore the check, please refer to: https://ng-alain.com/auth/getting-started#AlainAuthConfig`;
|
|
402
|
-
}
|
|
403
|
-
const res = new HttpErrorResponse({
|
|
404
|
-
url: req.url,
|
|
405
|
-
headers: req.headers,
|
|
406
|
-
status: 401,
|
|
407
|
-
statusText
|
|
408
|
-
});
|
|
409
|
-
observer.error(res);
|
|
410
|
-
});
|
|
411
|
-
if (options.executeOtherInterceptors) {
|
|
412
|
-
const interceptors = this.injector.get(HTTP_INTERCEPTORS, []);
|
|
413
|
-
const lastInterceptors = interceptors.slice(interceptors.indexOf(this) + 1);
|
|
414
|
-
if (lastInterceptors.length > 0) {
|
|
415
|
-
const chain = lastInterceptors.reduceRight((_next, _interceptor) => new HttpAuthInterceptorHandler(_next, _interceptor), {
|
|
416
|
-
handle: (_) => err$
|
|
417
|
-
});
|
|
418
|
-
return chain.handle(req);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return err$;
|
|
422
|
-
}
|
|
423
|
-
return next.handle(req);
|
|
424
|
-
}
|
|
425
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseInterceptor, deps: [{ token: i0.Injector, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
426
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseInterceptor }); }
|
|
427
|
-
}
|
|
428
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: BaseInterceptor, decorators: [{
|
|
429
|
-
type: Injectable
|
|
430
|
-
}], ctorParameters: function () { return [{ type: i0.Injector, decorators: [{
|
|
431
|
-
type: Optional
|
|
432
|
-
}] }]; } });
|
|
433
|
-
|
|
434
317
|
function urlBase64Decode(str) {
|
|
435
318
|
let output = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
436
319
|
switch (output.length % 4) {
|
|
@@ -519,57 +402,59 @@ class JWTTokenModel {
|
|
|
519
402
|
}
|
|
520
403
|
}
|
|
521
404
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
* ```
|
|
529
|
-
*/
|
|
530
|
-
class JWTInterceptor extends BaseInterceptor {
|
|
531
|
-
isAuth(options) {
|
|
532
|
-
this.model = this.injector.get(DA_SERVICE_TOKEN).get(JWTTokenModel);
|
|
533
|
-
return CheckJwt(this.model, options.token_exp_offset);
|
|
405
|
+
function CheckSimple(model) {
|
|
406
|
+
return model != null && typeof model.token === 'string' && model.token.length > 0;
|
|
407
|
+
}
|
|
408
|
+
function CheckJwt(model, offset) {
|
|
409
|
+
try {
|
|
410
|
+
return model != null && !!model.token && !model.isExpired(offset);
|
|
534
411
|
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
412
|
+
catch (err) {
|
|
413
|
+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
414
|
+
console.warn(`${err.message}, jump to login_url`);
|
|
415
|
+
}
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
function ToLogin(options, url) {
|
|
420
|
+
const router = inject(Router);
|
|
421
|
+
const token = inject(DA_SERVICE_TOKEN);
|
|
422
|
+
const doc = inject(DOCUMENT);
|
|
423
|
+
token.referrer.url = url || router.url;
|
|
424
|
+
if (options.token_invalid_redirect === true) {
|
|
425
|
+
setTimeout(() => {
|
|
426
|
+
if (/^https?:\/\//g.test(options.login_url)) {
|
|
427
|
+
doc.location.href = options.login_url;
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
router.navigate([options.login_url]);
|
|
540
431
|
}
|
|
541
432
|
});
|
|
542
433
|
}
|
|
543
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: JWTInterceptor, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
544
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: JWTInterceptor }); }
|
|
545
434
|
}
|
|
546
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: JWTInterceptor, decorators: [{
|
|
547
|
-
type: Injectable
|
|
548
|
-
}] });
|
|
549
435
|
|
|
550
436
|
class AuthJWTGuardService {
|
|
551
|
-
constructor(srv
|
|
437
|
+
constructor(srv) {
|
|
552
438
|
this.srv = srv;
|
|
553
|
-
this.injector = injector;
|
|
554
439
|
}
|
|
555
440
|
process(url) {
|
|
556
441
|
const cog = this.srv.options;
|
|
557
442
|
const res = CheckJwt(this.srv.get(JWTTokenModel), cog.token_exp_offset);
|
|
558
443
|
if (!res) {
|
|
559
|
-
ToLogin(cog,
|
|
444
|
+
ToLogin(cog, url);
|
|
560
445
|
}
|
|
561
446
|
return res;
|
|
562
447
|
}
|
|
563
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
564
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
448
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: AuthJWTGuardService, deps: [{ token: DA_SERVICE_TOKEN }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
449
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: AuthJWTGuardService, providedIn: 'root' }); }
|
|
565
450
|
}
|
|
566
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
451
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: AuthJWTGuardService, decorators: [{
|
|
567
452
|
type: Injectable,
|
|
568
453
|
args: [{ providedIn: 'root' }]
|
|
569
|
-
}], ctorParameters:
|
|
454
|
+
}], ctorParameters: () => [{ type: undefined, decorators: [{
|
|
570
455
|
type: Inject,
|
|
571
456
|
args: [DA_SERVICE_TOKEN]
|
|
572
|
-
}] }
|
|
457
|
+
}] }] });
|
|
573
458
|
/**
|
|
574
459
|
* JWT 路由守卫, [ACL Document](https://ng-alain.com/auth/guard).
|
|
575
460
|
*
|
|
@@ -607,77 +492,88 @@ const authJWTCanActivateChild = (_, state) => inject(AuthJWTGuardService).proces
|
|
|
607
492
|
*/
|
|
608
493
|
const authJWTCanMatch = route => inject(AuthJWTGuardService).process(route.path);
|
|
609
494
|
|
|
610
|
-
class SimpleTokenModel {
|
|
611
|
-
}
|
|
612
|
-
|
|
613
495
|
/**
|
|
614
|
-
*
|
|
496
|
+
* Whether to allow anonymous login
|
|
615
497
|
*
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
*
|
|
498
|
+
* 是否允许匿名登录
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
* this.http.post(`login`, {
|
|
502
|
+
* name: 'cipchk', pwd: '123456'
|
|
503
|
+
* }, {
|
|
504
|
+
* context: new HttpContext().set(ALLOW_ANONYMOUS, true)
|
|
505
|
+
* })
|
|
620
506
|
*/
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
const obj = {};
|
|
632
|
-
obj[token_send_key] = token;
|
|
633
|
-
req = req.clone({
|
|
634
|
-
setHeaders: obj
|
|
635
|
-
});
|
|
636
|
-
break;
|
|
637
|
-
case 'body':
|
|
638
|
-
const body = req.body || {};
|
|
639
|
-
body[token_send_key] = token;
|
|
640
|
-
req = req.clone({
|
|
641
|
-
body
|
|
642
|
-
});
|
|
643
|
-
break;
|
|
644
|
-
case 'url':
|
|
645
|
-
req = req.clone({
|
|
646
|
-
params: req.params.append(token_send_key, token)
|
|
647
|
-
});
|
|
648
|
-
break;
|
|
507
|
+
const ALLOW_ANONYMOUS = new HttpContextToken(() => false);
|
|
508
|
+
|
|
509
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
510
|
+
function isAnonymous(req, options) {
|
|
511
|
+
if (req.context.get(ALLOW_ANONYMOUS))
|
|
512
|
+
return true;
|
|
513
|
+
if (Array.isArray(options.ignores)) {
|
|
514
|
+
for (const item of options.ignores) {
|
|
515
|
+
if (item.test(req.url))
|
|
516
|
+
return true;
|
|
649
517
|
}
|
|
650
|
-
return req;
|
|
651
518
|
}
|
|
652
|
-
|
|
653
|
-
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
521
|
+
function throwErr(req, options) {
|
|
522
|
+
ToLogin(options);
|
|
523
|
+
// Interrupt Http request, so need to generate a new Observable
|
|
524
|
+
return new Observable((observer) => {
|
|
525
|
+
let statusText = '';
|
|
526
|
+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
|
527
|
+
statusText = `来自 @delon/auth 的拦截,所请求URL未授权,若是登录API可加入 new HttpContext().set(ALLOW_ANONYMOUS, true) 来表示忽略校验,更多方法请参考: https://ng-alain.com/auth/getting-started#AlainAuthConfig\nThe interception from @delon/auth, the requested URL is not authorized. If the login API can add new HttpContext().set(ALLOW_ANONYMOUS, true) to ignore the check, please refer to: https://ng-alain.com/auth/getting-started#AlainAuthConfig`;
|
|
528
|
+
}
|
|
529
|
+
const res = new HttpErrorResponse({
|
|
530
|
+
url: req.url,
|
|
531
|
+
headers: req.headers,
|
|
532
|
+
status: 401,
|
|
533
|
+
statusText
|
|
534
|
+
});
|
|
535
|
+
observer.error(res);
|
|
536
|
+
});
|
|
654
537
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
538
|
+
|
|
539
|
+
function newReq$1(req, model) {
|
|
540
|
+
return req.clone({
|
|
541
|
+
setHeaders: {
|
|
542
|
+
Authorization: `Bearer ${model.token}`
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
const authJWTInterceptor = (req, next) => {
|
|
547
|
+
const options = mergeConfig(inject(AlainConfigService));
|
|
548
|
+
if (isAnonymous(req, options))
|
|
549
|
+
return next(req);
|
|
550
|
+
const model = inject(DA_SERVICE_TOKEN).get(JWTTokenModel);
|
|
551
|
+
if (CheckJwt(model, options.token_exp_offset))
|
|
552
|
+
return next(newReq$1(req, model));
|
|
553
|
+
return throwErr(req, options);
|
|
554
|
+
};
|
|
658
555
|
|
|
659
556
|
class AuthSimpleGuardService {
|
|
660
|
-
constructor(srv
|
|
557
|
+
constructor(srv) {
|
|
661
558
|
this.srv = srv;
|
|
662
|
-
this.injector = injector;
|
|
663
559
|
}
|
|
664
560
|
process(url) {
|
|
665
561
|
const res = CheckSimple(this.srv.get());
|
|
666
562
|
if (!res) {
|
|
667
|
-
ToLogin(this.srv.options,
|
|
563
|
+
ToLogin(this.srv.options, url);
|
|
668
564
|
}
|
|
669
565
|
return res;
|
|
670
566
|
}
|
|
671
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "
|
|
672
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "
|
|
567
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: AuthSimpleGuardService, deps: [{ token: DA_SERVICE_TOKEN }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
568
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: AuthSimpleGuardService, providedIn: 'root' }); }
|
|
673
569
|
}
|
|
674
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
|
570
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: AuthSimpleGuardService, decorators: [{
|
|
675
571
|
type: Injectable,
|
|
676
572
|
args: [{ providedIn: 'root' }]
|
|
677
|
-
}], ctorParameters:
|
|
573
|
+
}], ctorParameters: () => [{ type: undefined, decorators: [{
|
|
678
574
|
type: Inject,
|
|
679
575
|
args: [DA_SERVICE_TOKEN]
|
|
680
|
-
}] }
|
|
576
|
+
}] }] });
|
|
681
577
|
/**
|
|
682
578
|
* Simple 路由守卫, [ACL Document](https://ng-alain.com/auth/guard).
|
|
683
579
|
*
|
|
@@ -715,19 +611,87 @@ const authSimpleCanActivateChild = (_, state) => inject(AuthSimpleGuardService).
|
|
|
715
611
|
*/
|
|
716
612
|
const authSimpleCanMatch = route => inject(AuthSimpleGuardService).process(route.path);
|
|
717
613
|
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
614
|
+
function newReq(req, model, options) {
|
|
615
|
+
const { token_send_template, token_send_key } = options;
|
|
616
|
+
const token = token_send_template.replace(/\$\{([\w]+)\}/g, (_, g) => model[g]);
|
|
617
|
+
switch (options.token_send_place) {
|
|
618
|
+
case 'header':
|
|
619
|
+
const obj = {};
|
|
620
|
+
obj[token_send_key] = token;
|
|
621
|
+
req = req.clone({
|
|
622
|
+
setHeaders: obj
|
|
623
|
+
});
|
|
624
|
+
break;
|
|
625
|
+
case 'body':
|
|
626
|
+
const body = req.body || {};
|
|
627
|
+
body[token_send_key] = token;
|
|
628
|
+
req = req.clone({
|
|
629
|
+
body
|
|
630
|
+
});
|
|
631
|
+
break;
|
|
632
|
+
case 'url':
|
|
633
|
+
req = req.clone({
|
|
634
|
+
params: req.params.append(token_send_key, token)
|
|
635
|
+
});
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
return req;
|
|
639
|
+
}
|
|
640
|
+
const authSimpleInterceptor = (req, next) => {
|
|
641
|
+
const options = mergeConfig(inject(AlainConfigService));
|
|
642
|
+
if (isAnonymous(req, options))
|
|
643
|
+
return next(req);
|
|
644
|
+
const model = inject(DA_SERVICE_TOKEN).get();
|
|
645
|
+
if (CheckSimple(model))
|
|
646
|
+
return next(newReq(req, model, options));
|
|
647
|
+
return throwErr(req, options);
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
class SimpleTokenModel {
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
var AuthFeatureKind;
|
|
654
|
+
(function (AuthFeatureKind) {
|
|
655
|
+
AuthFeatureKind[AuthFeatureKind["Store"] = 0] = "Store";
|
|
656
|
+
})(AuthFeatureKind || (AuthFeatureKind = {}));
|
|
657
|
+
function makeAuthFeature(kind, providers) {
|
|
658
|
+
return {
|
|
659
|
+
ɵkind: kind,
|
|
660
|
+
ɵproviders: providers
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Configures authentication process service to be available for injection.
|
|
665
|
+
*
|
|
666
|
+
* @see {@link withCookie}
|
|
667
|
+
* @see {@link withLocalStorage}
|
|
668
|
+
* @see {@link withSessionStorage}
|
|
669
|
+
*/
|
|
670
|
+
function provideAuth(store) {
|
|
671
|
+
return makeEnvironmentProviders([(store ?? withLocalStorage()).ɵproviders]);
|
|
672
|
+
}
|
|
673
|
+
/** `cookie` storage */
|
|
674
|
+
function withCookie() {
|
|
675
|
+
return makeAuthFeature(AuthFeatureKind.Store, [
|
|
676
|
+
{ provide: DA_STORE_TOKEN, useClass: CookieStorageStore, deps: [CookieService] }
|
|
677
|
+
]);
|
|
678
|
+
}
|
|
679
|
+
/** `localStorage` storage, **not lost after closing the browser**. */
|
|
680
|
+
function withLocalStorage() {
|
|
681
|
+
return makeAuthFeature(AuthFeatureKind.Store, [{ provide: DA_STORE_TOKEN, useClass: LocalStorageStore }]);
|
|
682
|
+
}
|
|
683
|
+
/** `sessionStorage` storage, **lost after closing the browser**. */
|
|
684
|
+
function withSessionStorage() {
|
|
685
|
+
return makeAuthFeature(AuthFeatureKind.Store, [{ provide: DA_STORE_TOKEN, useClass: SessionStorageStore }]);
|
|
686
|
+
}
|
|
687
|
+
/** Memory storage, **lost after closing the browser tab**. */
|
|
688
|
+
function withMemoryStorage() {
|
|
689
|
+
return makeAuthFeature(AuthFeatureKind.Store, [{ provide: DA_STORE_TOKEN, useClass: MemoryStore }]);
|
|
722
690
|
}
|
|
723
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: DelonAuthModule, decorators: [{
|
|
724
|
-
type: NgModule,
|
|
725
|
-
args: [{}]
|
|
726
|
-
}] });
|
|
727
691
|
|
|
728
692
|
/**
|
|
729
693
|
* Generated bundle index. Do not edit.
|
|
730
694
|
*/
|
|
731
695
|
|
|
732
|
-
export { ALLOW_ANONYMOUS, AUTH_DEFAULT_CONFIG, AuthJWTGuardService, AuthSimpleGuardService,
|
|
696
|
+
export { ALLOW_ANONYMOUS, AUTH_DEFAULT_CONFIG, AuthFeatureKind, AuthJWTGuardService, AuthSimpleGuardService, CookieStorageStore, DA_SERVICE_TOKEN, DA_SERVICE_TOKEN_FACTORY, DA_STORE_TOKEN, DA_STORE_TOKEN_LOCAL_FACTORY, JWTTokenModel, LocalStorageStore, MemoryStore, SessionStorageStore, SimpleTokenModel, SocialService, TokenService, authJWTCanActivate, authJWTCanActivateChild, authJWTCanMatch, authJWTInterceptor, authSimpleCanActivate, authSimpleCanActivateChild, authSimpleCanMatch, authSimpleInterceptor, isAnonymous, mergeConfig, provideAuth, throwErr, urlBase64Decode, withCookie, withLocalStorage, withMemoryStorage, withSessionStorage };
|
|
733
697
|
//# sourceMappingURL=auth.mjs.map
|