-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathsession.ts
315 lines (290 loc) · 9.05 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import type {
BackupCodeAttempt,
EmailCodeAttempt,
EmailCodeConfig,
PasskeyAttempt,
PassKeyConfig,
PasswordAttempt,
PhoneCodeAttempt,
PhoneCodeConfig,
PhoneCodeSecondFactorConfig,
TOTPAttempt,
} from './factors';
import type { ActClaim } from './jwtv2';
import type {
ClerkCustomFeatureKey,
ClerkCustomPlanKey,
OrganizationCustomPermissionKey,
OrganizationCustomRoleKey,
OrganizationPermissionKey,
OrganizationSystemPermissionPrefix,
} from './organizationMembership';
import type { ClerkResource } from './resource';
import type {
ReverificationConfig,
SessionVerificationLevel,
SessionVerificationResource,
} from './sessionVerification';
import type { SessionJSONSnapshot } from './snapshots';
import type { TokenResource } from './token';
import type { UserResource } from './user';
/**
* @inline
*/
export type PendingSessionOptions = {
/**
* Determines if pending sessions are considered as signed-out state.
* @default true
*/
treatPendingAsSignedOut?: boolean;
};
type DisallowSystemPermissions<P extends string> = P extends `${OrganizationSystemPermissionPrefix}${string}`
? 'System permissions are not included in session claims and cannot be used on the server-side'
: P;
/** @inline */
export type CheckAuthorizationFn<Params> = (isAuthorizedParams: Params) => boolean;
/** @inline */
export type CheckAuthorizationWithCustomPermissions =
CheckAuthorizationFn<CheckAuthorizationParamsWithCustomPermissions>;
type WithReverification<T> = T & {
reverification?: ReverificationConfig;
};
export type CheckAuthorizationParamsWithCustomPermissions = WithReverification<
| {
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
role?: never;
permission: OrganizationCustomPermissionKey;
feature?: never;
plan?: never;
}
| {
role?: never;
permission?: never;
feature: ClerkCustomFeatureKey;
plan?: never;
}
| {
role?: never;
permission?: never;
feature?: never;
plan: ClerkCustomPlanKey;
}
| { role?: never; permission?: never; feature?: never; plan?: never }
>;
export type CheckAuthorization = CheckAuthorizationFn<CheckAuthorizationParams>;
type CheckAuthorizationParams = WithReverification<
| {
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
role?: never;
permission: OrganizationPermissionKey;
feature?: never;
plan?: never;
}
| {
role?: never;
permission?: never;
feature: ClerkCustomFeatureKey;
plan?: never;
}
| {
role?: never;
permission?: never;
feature?: never;
plan: ClerkCustomPlanKey;
}
| { role?: never; permission?: never; feature?: never; plan?: never }
>;
/**
* Type guard for server-side authorization checks using session claims.
* System permissions are not allowed since they are not included
* in session claims and cannot be verified on the server side.
*/
export type CheckAuthorizationFromSessionClaims = <P extends OrganizationCustomPermissionKey>(
isAuthorizedParams: CheckAuthorizationParamsFromSessionClaims<P>,
) => boolean;
export type CheckAuthorizationParamsFromSessionClaims<P extends OrganizationCustomPermissionKey> = WithReverification<
| {
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
role?: never;
permission: DisallowSystemPermissions<P>;
feature?: never;
plan?: never;
}
| {
role?: never;
permission?: never;
feature: ClerkCustomFeatureKey;
plan?: never;
}
| {
role?: never;
permission?: never;
feature?: never;
plan: ClerkCustomPlanKey;
}
| { role?: never; permission?: never; feature?: never; plan?: never }
>;
/**
* The `Session` object is an abstraction over an HTTP session. It models the period of information exchange between a user and the server.
*
* The `Session` object includes methods for recording session activity and ending the session client-side. For security reasons, sessions can also expire server-side.
*
* As soon as a [`User`](https://clerk.com/docs/references/javascript/user) signs in, Clerk creates a `Session` for the current [`Client`](https://clerk.com/docs/references/javascript/client). Clients can have more than one sessions at any point in time, but only one of those sessions will be **active**.
*
* In certain scenarios, a session might be replaced by another one. This is often the case with [multi-session applications](https://clerk.com/docs/authentication/configuration/session-options#multi-session-applications).
*
* All sessions that are **expired**, **removed**, **replaced**, **ended** or **abandoned** are not considered valid.
*
* > [!NOTE]
* > For more information regarding the different session states, see the [guide on session management](https://clerk.com/docs/authentication/configuration/session-options).
*/
export interface SessionResource extends ClerkResource {
/**
* The unique identifier for the session.
*/
id: string;
/**
* The current state of the session.
*/
status: SessionStatus;
expireAt: Date;
abandonAt: Date;
/**
* An array where each item represents the number of minutes since the last verification of a first or second factor: `[firstFactorAge, secondFactorAge]`.
*/
factorVerificationAge: [firstFactorAge: number, secondFactorAge: number] | null;
lastActiveToken: TokenResource | null;
lastActiveOrganizationId: string | null;
lastActiveAt: Date;
actor: ActClaim | null;
tasks: Array<SessionTask> | null;
currentTask?: SessionTask;
/**
* The user associated with the session.
*/
user: UserResource | null;
publicUserData: PublicUserData;
/**
* Marks the session as ended. The session will no longer be active for this `Client` and its status will become **ended**.
*/
end: () => Promise<SessionResource>;
remove: () => Promise<SessionResource>;
touch: () => Promise<SessionResource>;
getToken: GetToken;
checkAuthorization: CheckAuthorization;
clearCache: () => void;
createdAt: Date;
updatedAt: Date;
startVerification: (params: SessionVerifyCreateParams) => Promise<SessionVerificationResource>;
prepareFirstFactorVerification: (
factor: SessionVerifyPrepareFirstFactorParams,
) => Promise<SessionVerificationResource>;
attemptFirstFactorVerification: (
attemptFactor: SessionVerifyAttemptFirstFactorParams,
) => Promise<SessionVerificationResource>;
prepareSecondFactorVerification: (
params: SessionVerifyPrepareSecondFactorParams,
) => Promise<SessionVerificationResource>;
attemptSecondFactorVerification: (
params: SessionVerifyAttemptSecondFactorParams,
) => Promise<SessionVerificationResource>;
verifyWithPasskey: () => Promise<SessionVerificationResource>;
__internal_toSnapshot: () => SessionJSONSnapshot;
}
/**
* Represents a session resource that has completed all pending tasks
* and authentication factors
*/
export interface ActiveSessionResource extends SessionResource {
status: 'active';
user: UserResource;
}
/**
* Represents a session resource that has completed sign-in but has pending tasks
*/
export interface PendingSessionResource extends SessionResource {
status: 'pending';
user: UserResource;
currentTask: SessionTask;
}
/**
* Represents session resources for users who have completed
* the full sign-in flow
*/
export type SignedInSessionResource = ActiveSessionResource | PendingSessionResource;
export interface SessionWithActivitiesResource extends ClerkResource {
id: string;
status: string;
expireAt: Date;
abandonAt: Date;
lastActiveAt: Date;
latestActivity: SessionActivity;
actor: ActClaim | null;
revoke: () => Promise<SessionWithActivitiesResource>;
}
export interface SessionActivity {
id: string;
browserName?: string;
browserVersion?: string;
deviceType?: string;
ipAddress?: string;
city?: string;
country?: string;
isMobile?: boolean;
}
export type SessionStatus =
| 'abandoned'
| 'active'
| 'ended'
| 'expired'
| 'removed'
| 'replaced'
| 'revoked'
| 'pending';
export interface PublicUserData {
firstName: string | null;
lastName: string | null;
imageUrl: string;
hasImage: boolean;
identifier: string;
userId?: string;
}
export interface SessionTask {
key: 'org';
}
export type GetTokenOptions = {
template?: string;
organizationId?: string;
leewayInSeconds?: number;
skipCache?: boolean;
};
/**
* @inline
*/
export type GetToken = (options?: GetTokenOptions) => Promise<string | null>;
export type SessionVerifyCreateParams = {
level: SessionVerificationLevel;
};
export type SessionVerifyPrepareFirstFactorParams = EmailCodeConfig | PhoneCodeConfig | PassKeyConfig;
export type SessionVerifyAttemptFirstFactorParams =
| EmailCodeAttempt
| PhoneCodeAttempt
| PasswordAttempt
| PasskeyAttempt;
export type SessionVerifyPrepareSecondFactorParams = PhoneCodeSecondFactorConfig;
export type SessionVerifyAttemptSecondFactorParams = PhoneCodeAttempt | TOTPAttempt | BackupCodeAttempt;