1 | import { Inject, Injectable, InjectionToken } from '@angular/core';
|
2 | import { nbAuthCreateToken } from './token';
|
3 | import { NB_AUTH_TOKENS } from '../../auth.options';
|
4 | export const NB_AUTH_FALLBACK_TOKEN = new InjectionToken('Nebular Auth Options');
|
5 |
|
6 |
|
7 |
|
8 | export class NbAuthTokenParceler {
|
9 | constructor(fallbackClass, tokenClasses) {
|
10 | this.fallbackClass = fallbackClass;
|
11 | this.tokenClasses = tokenClasses;
|
12 | }
|
13 | wrap(token) {
|
14 | return JSON.stringify({
|
15 | name: token.getName(),
|
16 | ownerStrategyName: token.getOwnerStrategyName(),
|
17 | createdAt: token.getCreatedAt().getTime(),
|
18 | value: token.toString(),
|
19 | });
|
20 | }
|
21 | unwrap(value) {
|
22 | let tokenClass = this.fallbackClass;
|
23 | let tokenValue = '';
|
24 | let tokenOwnerStrategyName = '';
|
25 | let tokenCreatedAt = null;
|
26 | const tokenPack = this.parseTokenPack(value);
|
27 | if (tokenPack) {
|
28 | tokenClass = this.getClassByName(tokenPack.name) || this.fallbackClass;
|
29 | tokenValue = tokenPack.value;
|
30 | tokenOwnerStrategyName = tokenPack.ownerStrategyName;
|
31 | tokenCreatedAt = new Date(Number(tokenPack.createdAt));
|
32 | }
|
33 | return nbAuthCreateToken(tokenClass, tokenValue, tokenOwnerStrategyName, tokenCreatedAt);
|
34 | }
|
35 |
|
36 | getClassByName(name) {
|
37 | return this.tokenClasses.find((tokenClass) => tokenClass.NAME === name);
|
38 | }
|
39 | parseTokenPack(value) {
|
40 | try {
|
41 | return JSON.parse(value);
|
42 | }
|
43 | catch (e) { }
|
44 | return null;
|
45 | }
|
46 | }
|
47 | NbAuthTokenParceler.decorators = [
|
48 | { type: Injectable }
|
49 | ];
|
50 | NbAuthTokenParceler.ctorParameters = () => [
|
51 | { type: undefined, decorators: [{ type: Inject, args: [NB_AUTH_FALLBACK_TOKEN,] }] },
|
52 | { type: Array, decorators: [{ type: Inject, args: [NB_AUTH_TOKENS,] }] }
|
53 | ];
|
54 |
|
\ | No newline at end of file |