-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathsession.ts
94 lines (81 loc) · 1.91 KB
/
session.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
import type { TokenSet } from 'openid-client';
import { Config } from '../auth0-session';
import { NextConfig } from '../config';
/**
* Key-value store for the user's claims.
*
* @category Server
*/
export interface Claims {
[key: string]: any;
}
/**
* The user's session.
*
* @category Server
*/
export default class Session {
/**
* Any of the claims from the `id_token`.
*/
user: Claims;
/**
* The ID token.
*/
idToken?: string | undefined;
/**
* The access token.
*/
accessToken?: string | undefined;
/**
* The access token scopes.
*/
accessTokenScope?: string | undefined;
/**
* The expiration of the access token.
*/
accessTokenExpiresAt?: number;
/**
* The refresh token, which is used to request a new access token.
*
* **IMPORTANT** You need to request the `offline_access` scope on login to get a refresh token
* from Auth0.
*/
refreshToken?: string | undefined;
[key: string]: any;
constructor(user: Claims) {
this.user = user;
}
}
/**
* @ignore
*/
export function fromTokenSet(tokenSet: TokenSet, config: Config | NextConfig): Session {
// Get the claims without any OIDC-specific claim.
const claims = tokenSet.claims();
config.identityClaimFilter.forEach((claim) => {
delete claims[claim];
});
const { id_token, access_token, scope, expires_at, refresh_token, ...remainder } = tokenSet;
const storeIDToken = config.session.storeIDToken;
return Object.assign(
new Session({ ...claims }),
{
accessToken: access_token,
accessTokenScope: scope,
accessTokenExpiresAt: expires_at,
refreshToken: refresh_token,
...(storeIDToken && { idToken: id_token })
},
remainder
);
}
/**
* @ignore
*/
export function fromJson(json: { [key: string]: any } | undefined): Session | null {
if (!json) {
return null;
}
return Object.assign(new Session({ ...json.user }), json);
}