@absolutejs/auth 0.21.1 → 0.22.0

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.
@@ -34,7 +34,7 @@ export declare const authorize: ({ clientProviders, authorizeRoute, onAuthorizeS
34
34
  200: Response;
35
35
  400: "Cookies are missing" | "Provider is required";
36
36
  401: "Client provider not found";
37
- 500: `${string} - ${string}` | `Unknown status: ${string}`;
37
+ 500: "Failed to create authorization URL";
38
38
  422: {
39
39
  type: "validation";
40
40
  on: string;
@@ -63,7 +63,7 @@ export declare const authorize: ({ clientProviders, authorizeRoute, onAuthorizeS
63
63
  200: Response;
64
64
  400: "Cookies are missing" | "Provider is required";
65
65
  401: "Client provider not found";
66
- 500: `${string} - ${string}` | `Unknown status: ${string}`;
66
+ 500: "Failed to create authorization URL";
67
67
  422: {
68
68
  type: "validation";
69
69
  on: string;
@@ -35,7 +35,7 @@ export declare const callback: <UserType>({ clientProviders, callbackRoute, onCa
35
35
  200: any;
36
36
  400: "Cookies are missing" | "Invalid callback request" | "Invalid state mismatch" | "Code verifier not found and is required";
37
37
  401: "Client provider not found";
38
- 500: `${string} - ${string}` | `Failed to validate authorization code: Unknown status: ${string}`;
38
+ 500: "Failed to validate authorization code";
39
39
  422: {
40
40
  type: "validation";
41
41
  on: string;
@@ -7,3 +7,4 @@ export declare const MILLISECONDS_IN_A_DAY: number;
7
7
  export declare const MILLISECONDS_IN_AN_HOUR: number;
8
8
  export declare const COOKIE_MINUTES = 30;
9
9
  export declare const COOKIE_DURATION: number;
10
+ export declare const DEFAULT_MAX_SESSIONS = 10000;
@@ -1,12 +1,13 @@
1
1
  import { Elysia } from 'elysia';
2
2
  import { AbsoluteAuthProps } from './types';
3
- export declare const absoluteAuth: <UserType>({ providersConfiguration, authorizeRoute, callbackRoute, profileRoute, signoutRoute, statusRoute, refreshRoute, revokeRoute, onAuthorizeSuccess, onAuthorizeError, onProfileSuccess, onProfileError, onCallbackSuccess, onCallbackError, onStatus, onRefreshSuccess, onRefreshError, onSignOut, onRevocationSuccess, onRevocationError }: AbsoluteAuthProps<UserType>) => Promise<Elysia<"", {
3
+ export declare const absoluteAuth: <UserType>({ providersConfiguration, authorizeRoute, callbackRoute, profileRoute, signoutRoute, statusRoute, refreshRoute, revokeRoute, cleanupIntervalMs, maxSessions, sessionDurationMs, onAuthorizeSuccess, onAuthorizeError, onProfileSuccess, onProfileError, onCallbackSuccess, onCallbackError, onStatus, onRefreshSuccess, onRefreshError, onSignOut, onRevocationSuccess, onRevocationError, onSessionCleanup }: AbsoluteAuthProps<UserType>) => Promise<Elysia<"", {
4
4
  decorator: {};
5
5
  store: {
6
6
  session: import("./types").SessionRecord<UserType> & import("./types").SessionRecord<unknown>;
7
7
  unregisteredSession: import("./types").UnregisteredSessionRecord;
8
8
  };
9
9
  derive: {
10
+ readonly cleanupSessions: () => Promise<void>;
10
11
  readonly protectRoute: <AuthReturn, AuthFailReturn>(handleAuth: (user: UserType) => AuthReturn, handleAuthFail?: ((error: {
11
12
  readonly code: "Bad Request";
12
13
  readonly message: "Cookies are missing";
@@ -45,7 +46,7 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
45
46
  200: Response;
46
47
  400: "Cookies are missing";
47
48
  401: "No auth provider found" | "No user session id found";
48
- 500: `Error: ${string} - ${string}` | `Unknown Error: ${string}`;
49
+ 500: "Sign out operation failed";
49
50
  422: {
50
51
  type: "validation";
51
52
  on: string;
@@ -70,7 +71,7 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
70
71
  400: "Cookies are missing" | "Invalid provider";
71
72
  401: "Client provider not found" | "No auth provider found" | "No user session found";
72
73
  501: "Provider does not support revocation";
73
- 500: `Failed to revoke token: ${string}` | `Failed to revoke token: Unknown status: ${string}`;
74
+ 500: "Failed to revoke token";
74
75
  422: {
75
76
  type: "validation";
76
77
  on: string;
@@ -129,7 +130,7 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
129
130
  400: "Cookies are missing" | "Invalid provider" | "No refresh token found";
130
131
  401: "Client provider not found" | "No auth provider found" | "No user session found";
131
132
  501: "Provider is not refreshable";
132
- 500: `Failed to refresh token: ${string}` | `Failed to refresh token: Unknown status: ${string}`;
133
+ 500: "Failed to refresh token";
133
134
  422: {
134
135
  type: "validation";
135
136
  on: string;
@@ -156,7 +157,7 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
156
157
  200: Response;
157
158
  400: "Cookies are missing" | "Provider is required";
158
159
  401: "Client provider not found";
159
- 500: `${string} - ${string}` | `Unknown status: ${string}`;
160
+ 500: "Failed to create authorization URL";
160
161
  422: {
161
162
  type: "validation";
162
163
  on: string;
@@ -185,7 +186,7 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
185
186
  200: Response;
186
187
  400: "Cookies are missing" | "Provider is required";
187
188
  401: "Client provider not found";
188
- 500: `${string} - ${string}` | `Unknown status: ${string}`;
189
+ 500: "Failed to create authorization URL";
189
190
  422: {
190
191
  type: "validation";
191
192
  on: string;
@@ -212,7 +213,7 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
212
213
  200: any;
213
214
  400: "Cookies are missing" | "Invalid callback request" | "Invalid state mismatch" | "Code verifier not found and is required";
214
215
  401: "Client provider not found";
215
- 500: `${string} - ${string}` | `Failed to validate authorization code: Unknown status: ${string}`;
216
+ 500: "Failed to validate authorization code";
216
217
  422: {
217
218
  type: "validation";
218
219
  on: string;
@@ -262,9 +263,10 @@ export declare const absoluteAuth: <UserType>({ providersConfiguration, authoriz
262
263
  }>>;
263
264
  export * from './types';
264
265
  export * from './typebox';
265
- export { isValidUser } from './typeGuards';
266
+ export { isUserSessionId, isValidUser } from './typeGuards';
266
267
  export { sessionStore } from './sessionStore';
267
- export { protectRoute } from './protectRoute';
268
+ export { protectRoutePlugin } from './protectRoute';
269
+ export { sessionCleanup } from './sessionCleanup';
268
270
  export * from './utils';
269
271
  export type { OAuth2TokenResponse, OAuth2Client, ProviderOption, PKCEProvider, OIDCProvider, RefreshableProvider, RevocableProvider, ScopeRequiredProvider, ProvidersMap, ProviderConfiguration, CredentialsFor } from 'citra';
270
272
  export { providers, providerOptions, refreshableProviderOptions, revocableProviderOptions, oidcProviderOptions, pkceProviderOptions, scopeRequiredProviderOptions, decodeJWT, extractPropFromIdentity, isValidProviderOption, isRefreshableOAuth2Client, isRefreshableProviderOption, isOIDCProviderOption, isPKCEProviderOption, isRevocableProviderOption, isRevocableOAuth2Client } from 'citra';
@@ -4,7 +4,7 @@ type AuthFailError = Exclude<Awaited<ReturnType<typeof getStatus>>['error'], nul
4
4
  readonly code: 'Unauthorized';
5
5
  readonly message: 'User is not authenticated';
6
6
  };
7
- export declare const protectRoute: <UserType>() => Elysia<"", {
7
+ export declare const protectRoutePlugin: <UserType>() => Elysia<"", {
8
8
  decorator: {};
9
9
  store: {
10
10
  session: import("./types").SessionRecord<UserType>;
@@ -5,8 +5,9 @@ type RefreshProps = {
5
5
  refreshRoute?: RouteString;
6
6
  onRefreshSuccess: OnRefreshSuccess;
7
7
  onRefreshError: OnRefreshError;
8
+ sessionDurationMs?: number;
8
9
  };
9
- export declare const refresh: <UserType>({ clientProviders, refreshRoute, onRefreshSuccess, onRefreshError }: RefreshProps) => Elysia<"", {
10
+ export declare const refresh: <UserType>({ clientProviders, refreshRoute, onRefreshSuccess, onRefreshError, sessionDurationMs }: RefreshProps) => Elysia<"", {
10
11
  decorator: {};
11
12
  store: {
12
13
  session: import("./types").SessionRecord<UserType>;
@@ -35,7 +36,7 @@ export declare const refresh: <UserType>({ clientProviders, refreshRoute, onRefr
35
36
  400: "Cookies are missing" | "Invalid provider" | "No refresh token found";
36
37
  401: "Client provider not found" | "No auth provider found" | "No user session found";
37
38
  501: "Provider is not refreshable";
38
- 500: `Failed to refresh token: ${string}` | `Failed to refresh token: Unknown status: ${string}`;
39
+ 500: "Failed to refresh token";
39
40
  422: {
40
41
  type: "validation";
41
42
  on: string;
@@ -35,7 +35,7 @@ export declare const revoke: <UserType>({ clientProviders, revokeRoute, onRevoca
35
35
  400: "Cookies are missing" | "Invalid provider";
36
36
  401: "Client provider not found" | "No auth provider found" | "No user session found";
37
37
  501: "Provider does not support revocation";
38
- 500: `Failed to revoke token: ${string}` | `Failed to revoke token: Unknown status: ${string}`;
38
+ 500: "Failed to revoke token";
39
39
  422: {
40
40
  type: "validation";
41
41
  on: string;
@@ -0,0 +1,38 @@
1
+ import { Elysia } from 'elysia';
2
+ import { OnSessionCleanup, SessionRecord, UnregisteredSessionRecord } from './types';
3
+ type SessionCleanupProps<UserType> = {
4
+ cleanupIntervalMs?: number;
5
+ maxSessions?: number;
6
+ onSessionCleanup?: OnSessionCleanup<UserType>;
7
+ };
8
+ export declare const sessionCleanup: <UserType>({ cleanupIntervalMs, maxSessions, onSessionCleanup }: SessionCleanupProps<UserType>) => Elysia<"", {
9
+ decorator: {};
10
+ store: {
11
+ session: SessionRecord<UserType>;
12
+ unregisteredSession: UnregisteredSessionRecord;
13
+ };
14
+ derive: {
15
+ readonly cleanupSessions: () => Promise<void>;
16
+ };
17
+ resolve: {};
18
+ }, {
19
+ typebox: {};
20
+ error: {};
21
+ }, {
22
+ schema: import("elysia").MergeSchema<import("elysia").MergeSchema<{}, {}, "">, {}, "">;
23
+ standaloneSchema: {};
24
+ macro: {};
25
+ macroFn: {};
26
+ parser: {};
27
+ }, {}, {
28
+ derive: {};
29
+ resolve: {};
30
+ schema: {};
31
+ standaloneSchema: {};
32
+ }, {
33
+ derive: {};
34
+ resolve: {};
35
+ schema: {};
36
+ standaloneSchema: {};
37
+ }>;
38
+ export {};
@@ -32,7 +32,7 @@ export declare const signout: <UserType>({ signoutRoute, onSignOut }: SignOutPro
32
32
  200: Response;
33
33
  400: "Cookies are missing";
34
34
  401: "No auth provider found" | "No user session id found";
35
- 500: `Error: ${string} - ${string}` | `Unknown Error: ${string}`;
35
+ 500: "Sign out operation failed";
36
36
  422: {
37
37
  type: "validation";
38
38
  on: string;
@@ -1,4 +1,5 @@
1
- import { StatusReturn } from './types';
1
+ import { StatusReturn, UserSessionId } from './types';
2
2
  export declare const isValidUser: <UserType>(user: unknown) => user is UserType;
3
+ export declare const isUserSessionId: (key: string) => key is UserSessionId;
3
4
  export declare const isNonEmptyString: (str: string | null | undefined) => str is string;
4
5
  export declare const isStatusResponse: (value: unknown) => value is StatusReturn;
@@ -12,19 +12,21 @@ export type OAuth2ConfigurationOptions = {
12
12
  });
13
13
  };
14
14
  export type UserSessionId = `${string}-${string}-${string}-${string}-${string}`;
15
- export type SessionRecord<UserType> = Record<UserSessionId, {
15
+ export type SessionData<UserType> = {
16
16
  user: UserType;
17
17
  accessToken: string;
18
18
  refreshToken?: string;
19
19
  expiresAt: number;
20
- }>;
21
- export type UnregisteredSessionRecord = Record<UserSessionId, {
20
+ };
21
+ export type SessionRecord<UserType> = Record<UserSessionId, SessionData<UserType>>;
22
+ export type UnregisteredSessionData = {
22
23
  userIdentity?: Record<string, unknown>;
23
24
  sessionInformation?: Record<string, unknown>;
24
25
  expiresAt: number;
25
26
  accessToken?: string;
26
27
  refreshToken?: string;
27
- }>;
28
+ };
29
+ export type UnregisteredSessionRecord = Record<UserSessionId, UnregisteredSessionData>;
28
30
  export type StatusReturn = ElysiaCustomStatusResponse<any, any, any>;
29
31
  export type OnNewUser<UserType> = (userIdentity: Record<string, unknown>) => UserType | StatusReturn | Response | Promise<UserType | StatusReturn | Response>;
30
32
  export type GetUser<UserType> = (userIdentity: Record<string, unknown>) => UserType | null | undefined | Promise<UserType | null | undefined>;
@@ -88,6 +90,10 @@ export type OnSignOut<UserType> = (({ authProvider, userSessionId, session }: {
88
90
  userSessionId: UserSessionId;
89
91
  session: SessionRecord<UserType>;
90
92
  }) => void | Promise<void>) | undefined;
93
+ export type OnSessionCleanup<UserType> = (({ removedSessions, removedUnregisteredSessions }: {
94
+ removedSessions: Map<UserSessionId, SessionData<UserType>>;
95
+ removedUnregisteredSessions: Map<UserSessionId, UnregisteredSessionData>;
96
+ }) => void | Promise<void>) | undefined;
91
97
  export type RouteString = `/${string}`;
92
98
  export type AuthorizeRoute = `${string}/:provider${'' | `/${string}`}`;
93
99
  export type AbsoluteAuthProps<UserType> = {
@@ -99,6 +105,10 @@ export type AbsoluteAuthProps<UserType> = {
99
105
  revokeRoute?: RouteString;
100
106
  signoutRoute?: RouteString;
101
107
  statusRoute?: RouteString;
108
+ cleanupIntervalMs?: number;
109
+ maxSessions?: number;
110
+ sessionDurationMs?: number;
111
+ unregisteredSessionDurationMs?: number;
102
112
  onAuthorizeSuccess?: OnAuthorizeSuccess;
103
113
  onAuthorizeError?: OnAuthorizeError;
104
114
  onCallbackSuccess?: OnCallbackSuccess<UserType>;
@@ -111,6 +121,7 @@ export type AbsoluteAuthProps<UserType> = {
111
121
  onRevocationError?: OnRevocationError;
112
122
  onProfileSuccess?: OnProfileSuccess;
113
123
  onProfileError?: OnProfileError;
124
+ onSessionCleanup?: OnSessionCleanup<UserType>;
114
125
  };
115
126
  export type ClientProviders = Record<string, {
116
127
  providerInstance: OAuth2Client<ProviderOption>;
@@ -126,4 +137,6 @@ export type InsantiateUserSessionProps<UserType> = {
126
137
  user_session_id: Cookie<UserSessionId | undefined>;
127
138
  onNewUser: OnNewUser<UserType>;
128
139
  getUser: GetUser<UserType>;
140
+ sessionDurationMs?: number;
141
+ unregisteredSessionDurationMs?: number;
129
142
  };
@@ -1,6 +1,6 @@
1
1
  import { Cookie } from 'elysia';
2
- import { AbsoluteAuthProps, InsantiateUserSessionProps, OAuth2ConfigurationOptions, SessionRecord, UserSessionId } from './types';
3
- export declare const instantiateUserSession: <UserType>({ authProvider, session, user_session_id, unregisteredSession, tokenResponse, providerInstance, getUser, onNewUser }: InsantiateUserSessionProps<UserType>) => Promise<import("./types").StatusReturn | Response | undefined>;
2
+ import { AbsoluteAuthProps, InsantiateUserSessionProps, OAuth2ConfigurationOptions, SessionRecord, UnregisteredSessionRecord, UserSessionId } from './types';
3
+ export declare const instantiateUserSession: <UserType>({ authProvider, session, user_session_id, unregisteredSession, tokenResponse, providerInstance, getUser, onNewUser, sessionDurationMs, unregisteredSessionDurationMs }: InsantiateUserSessionProps<UserType>) => Promise<import("./types").StatusReturn | Response | undefined>;
4
4
  export declare const createAuthConfiguration: <UserType>(configuration: AbsoluteAuthProps<UserType>) => AbsoluteAuthProps<UserType>;
5
5
  export declare const createProvidersConfiguration: (providersConfiguration: OAuth2ConfigurationOptions) => OAuth2ConfigurationOptions;
6
6
  export declare const getStatus: <UserType>(session: SessionRecord<UserType>, user_session_id: Cookie<UserSessionId | undefined>) => Promise<{
@@ -22,5 +22,10 @@ type ValidateSessionProps<SessionType extends Record<string, unknown> & {
22
22
  export declare const validateSession: <SessionType extends Record<string, unknown> & {
23
23
  expiresAt: number;
24
24
  }>({ user_session_id, session }: ValidateSessionProps<SessionType>) => SessionType | undefined;
25
- export declare const getUserSessionId: (user_session_id: Cookie<`${string}-${string}-${string}-${string}-${string}` | undefined>) => `${string}-${string}-${string}-${string}-${string}`;
25
+ type GetUserSessionIdProps<UserType> = {
26
+ user_session_id: Cookie<UserSessionId | undefined>;
27
+ session?: SessionRecord<UserType>;
28
+ unregisteredSession?: UnregisteredSessionRecord;
29
+ };
30
+ export declare const getUserSessionId: <UserType>({ user_session_id, session, unregisteredSession }: GetUserSessionIdProps<UserType>) => `${string}-${string}-${string}-${string}-${string}`;
26
31
  export {};
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.21.1",
2
+ "version": "0.22.0",
3
3
  "name": "@absolutejs/auth",
4
4
  "description": "An authorization library for absolutejs",
5
5
  "repository": {