-
Notifications
You must be signed in to change notification settings - Fork 926
/
Copy pathtoken.ts
110 lines (83 loc) · 3.08 KB
/
token.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
import jwtDecode from 'jwt-decode'
import type { JwtPayload } from 'jwt-decode'
import type { TokenableScheme } from '../types'
import type { Storage } from '../core'
import { addTokenPrefix } from '../utils'
import { TokenStatus } from './token-status'
export class Token {
public scheme: TokenableScheme
public $storage: Storage
constructor(scheme: TokenableScheme, storage: Storage) {
this.scheme = scheme
this.$storage = storage
}
get(): string | boolean {
const _key = this.scheme.options.token.prefix + this.scheme.name
return this.$storage.getUniversal(_key) as string | boolean
}
set(tokenValue: string | boolean): string | boolean {
const token = addTokenPrefix(tokenValue, this.scheme.options.token.type)
this._setToken(token)
this._updateExpiration(token)
if (typeof token === 'string') {
this.scheme.requestHandler.setHeader(token)
}
return token
}
sync(): string | boolean {
const token = this._syncToken()
this._syncExpiration()
if (typeof token === 'string') {
this.scheme.requestHandler.setHeader(token)
}
return token
}
reset(): void {
this.scheme.requestHandler.clearHeader()
this._setToken(false)
this._setExpiration(false)
}
status(): TokenStatus {
return new TokenStatus(this.get(), this._getExpiration())
}
private _getExpiration(): number | false {
const _key = this.scheme.options.token.expirationPrefix + this.scheme.name
return this.$storage.getUniversal(_key) as number | false
}
private _setExpiration(expiration: number | false): number | false {
const _key = this.scheme.options.token.expirationPrefix + this.scheme.name
return this.$storage.setUniversal(_key, expiration) as number | false
}
private _syncExpiration(): number | false {
const _key = this.scheme.options.token.expirationPrefix + this.scheme.name
return this.$storage.syncUniversal(_key) as number | false
}
private _updateExpiration(token: string | boolean): number | false | void {
let tokenExpiration
const _tokenIssuedAtMillis = Date.now()
const _tokenTTLMillis = Number(this.scheme.options.token.maxAge) * 1000
const _tokenExpiresAtMillis = _tokenTTLMillis
? _tokenIssuedAtMillis + _tokenTTLMillis
: 0
try {
tokenExpiration =
jwtDecode<JwtPayload>(token + '').exp * 1000 || _tokenExpiresAtMillis
} catch (error) {
// If the token is not jwt, we can't decode and refresh it, use _tokenExpiresAt value
tokenExpiration = _tokenExpiresAtMillis
if (!(error && error.name === 'InvalidTokenError')) {
throw error
}
}
// Set token expiration
return this._setExpiration(tokenExpiration || false)
}
private _setToken(token: string | boolean): string | boolean {
const _key = this.scheme.options.token.prefix + this.scheme.name
return this.$storage.setUniversal(_key, token) as string | boolean
}
private _syncToken(): string | boolean {
const _key = this.scheme.options.token.prefix + this.scheme.name
return this.$storage.syncUniversal(_key) as string | boolean
}
}