@absolutejs/auth 0.75.4 → 0.75.5

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.
@@ -1,4 +1,4 @@
1
- import { Elysia } from 'elysia';
1
+ import { Elysia, StatusMap, type ElysiaStatus } from 'elysia';
2
2
  import type { AuthSessionSource } from '../session/types';
3
3
  import { type AccessTokenPrincipalConfig, type AuthPrincipal } from '../principal';
4
4
  type AuthFailError = {
@@ -8,6 +8,11 @@ type AuthFailError = {
8
8
  readonly code: 'Unauthorized';
9
9
  readonly message: 'User is not authenticated';
10
10
  };
11
+ type AuthFailureResponse = ElysiaStatus<'Bad Request', 'Cookies are missing', (typeof StatusMap)['Bad Request']> | ElysiaStatus<'Unauthorized', 'User is not authenticated', (typeof StatusMap)['Unauthorized']>;
12
+ type ProtectRoute<UserType> = {
13
+ <AuthReturn, AuthFailReturn = never>(handleAuth: (user: UserType) => Promise<AuthReturn>, handleAuthFail?: (error: AuthFailError) => AuthFailReturn): Promise<AuthFailureResponse | AuthReturn | Awaited<AuthFailReturn>>;
14
+ <AuthReturn, AuthFailReturn = never>(handleAuth: (user: UserType) => AuthReturn, handleAuthFail?: (error: AuthFailError) => AuthFailReturn): Promise<AuthFailureResponse | AuthReturn | Awaited<AuthFailReturn>>;
15
+ };
11
16
  export declare const protectRoutePlugin: <UserType>({ accessTokens, authSessionStore }?: {
12
17
  accessTokens?: AccessTokenPrincipalConfig<UserType>;
13
18
  authSessionStore?: AuthSessionSource<UserType>;
@@ -18,8 +23,21 @@ export declare const protectRoutePlugin: <UserType>({ accessTokens, authSessionS
18
23
  unregisteredSession: import("..").UnregisteredSessionRecord;
19
24
  };
20
25
  derive: {
26
+ readonly absoluteAuthStatus: {
27
+ error: {
28
+ readonly code: "Bad Request";
29
+ readonly message: "Cookies are missing";
30
+ };
31
+ user: null;
32
+ impersonator?: undefined;
33
+ } | {
34
+ error: null;
35
+ impersonator: import("..").Impersonator | undefined;
36
+ user: NonNullable<UserType> | null;
37
+ };
21
38
  readonly authPrincipal: AuthPrincipal<UserType> | undefined;
22
- readonly protectRoute: <AuthReturn, AuthFailReturn = never>(handleAuth: (user: UserType) => AuthReturn | Promise<AuthReturn>, handleAuthFail?: (error: AuthFailError) => AuthFailReturn) => import("elysia").ElysiaStatus<"Unauthorized", "User is not authenticated", 401> | import("elysia").ElysiaStatus<"Bad Request", "Cookies are missing", 400> | AuthReturn | Promise<AuthReturn> | NonNullable<AuthFailReturn>;
39
+ } & {
40
+ readonly protectRoute: ProtectRoute<UserType>;
23
41
  };
24
42
  }, {
25
43
  typebox: {};
@@ -36,8 +54,7 @@ export declare const protectRoutePlugin: <UserType>({ accessTokens, authSessionS
36
54
  macroFn: {};
37
55
  parser: {};
38
56
  response: import("elysia/types").ExtractErrorFromHandle<{
39
- readonly authPrincipal: AuthPrincipal<UserType> | undefined;
40
- readonly protectRoute: <AuthReturn, AuthFailReturn = never>(handleAuth: (user: UserType) => AuthReturn | Promise<AuthReturn>, handleAuthFail?: (error: AuthFailError) => AuthFailReturn) => import("elysia").ElysiaStatus<"Unauthorized", "User is not authenticated", 401> | import("elysia").ElysiaStatus<"Bad Request", "Cookies are missing", 400> | AuthReturn | Promise<AuthReturn> | NonNullable<AuthFailReturn>;
57
+ readonly protectRoute: ProtectRoute<UserType>;
41
58
  }>;
42
59
  }, {}, import("elysia/types").DefaultEphemeral, import("elysia/types").DefaultEphemeral>;
43
60
  export {};
@@ -1,9 +1,14 @@
1
- import { Elysia } from 'elysia';
1
+ import { Elysia, StatusMap, type ElysiaStatus } from 'elysia';
2
2
  import type { AuthSessionSource } from '../session/types';
3
3
  type ReauthFailError = {
4
4
  readonly code: 'Unauthorized';
5
5
  readonly message: 'Recent authentication required';
6
6
  };
7
+ type ReauthFailureResponse = ElysiaStatus<'Unauthorized', 'Recent authentication required', (typeof StatusMap)['Unauthorized']>;
8
+ type RequireRecentAuth<UserType> = {
9
+ <AuthReturn, AuthFailReturn = never>(maxAgeMs: number, handleAuth: (user: UserType) => Promise<AuthReturn>, handleAuthFail?: (error: ReauthFailError) => AuthFailReturn): Promise<ReauthFailureResponse | AuthReturn | Awaited<AuthFailReturn>>;
10
+ <AuthReturn, AuthFailReturn = never>(maxAgeMs: number, handleAuth: (user: UserType) => AuthReturn, handleAuthFail?: (error: ReauthFailError) => AuthFailReturn): Promise<ReauthFailureResponse | AuthReturn | Awaited<AuthFailReturn>>;
11
+ };
7
12
  export declare const stepUpPlugin: <UserType>({ authSessionStore }?: {
8
13
  authSessionStore?: AuthSessionSource<UserType>;
9
14
  }) => Elysia<"", "local", {
@@ -13,7 +18,7 @@ export declare const stepUpPlugin: <UserType>({ authSessionStore }?: {
13
18
  unregisteredSession: import("..").UnregisteredSessionRecord;
14
19
  };
15
20
  derive: {
16
- readonly requireRecentAuth: <AuthReturn, AuthFailReturn>(maxAgeMs: number, handleAuth: (user: UserType) => AuthReturn | Promise<AuthReturn>, handleAuthFail?: (error: ReauthFailError) => AuthFailReturn) => Promise<AuthReturn | import("elysia").ElysiaStatus<"Unauthorized", "Recent authentication required", 401> | NonNullable<AuthFailReturn>>;
21
+ readonly requireRecentAuth: RequireRecentAuth<UserType>;
17
22
  };
18
23
  }, {
19
24
  typebox: {};
@@ -30,7 +35,7 @@ export declare const stepUpPlugin: <UserType>({ authSessionStore }?: {
30
35
  macroFn: {};
31
36
  parser: {};
32
37
  response: import("elysia/types").ExtractErrorFromHandle<{
33
- readonly requireRecentAuth: <AuthReturn, AuthFailReturn>(maxAgeMs: number, handleAuth: (user: UserType) => AuthReturn | Promise<AuthReturn>, handleAuthFail?: (error: ReauthFailError) => AuthFailReturn) => Promise<AuthReturn | import("elysia").ElysiaStatus<"Unauthorized", "Recent authentication required", 401> | NonNullable<AuthFailReturn>>;
38
+ readonly requireRecentAuth: RequireRecentAuth<UserType>;
34
39
  }>;
35
40
  }, {}, import("elysia/types").DefaultEphemeral, import("elysia/types").DefaultEphemeral>;
36
41
  export {};
package/dist/server.js CHANGED
@@ -4366,8 +4366,8 @@ var failureResponse = (config, failure, requiredScopes) => new Response(JSON.str
4366
4366
  var agentAuthContextPlugin = (config) => new Elysia2({
4367
4367
  name: "@absolutejs/auth/agent-context",
4368
4368
  seed: pluginDependencySeed(config)
4369
- }).derive(({ request }) => ({
4370
- protectAgent: async (requiredScopes, handleAuth, handleAuthFail) => {
4369
+ }).derive(({ request }) => {
4370
+ const protectAgent = async (requiredScopes, handleAuth, handleAuthFail) => {
4371
4371
  if (config === undefined) {
4372
4372
  const failure = {
4373
4373
  code: "Unauthorized",
@@ -4393,8 +4393,9 @@ var agentAuthContextPlugin = (config) => new Elysia2({
4393
4393
  return await handleAuthFail?.(failure) ?? failureResponse(config, failure, requiredScopes);
4394
4394
  }
4395
4395
  return handleAuth(principal);
4396
- }
4397
- })).as("global");
4396
+ };
4397
+ return { protectAgent };
4398
+ }).as("global");
4398
4399
 
4399
4400
  // src/agents/oauthGuide.ts
4400
4401
  var DEFAULT_GUIDE_ROUTE = "/auth.md";
@@ -5605,17 +5606,17 @@ var protectPermissionPlugin = ({
5605
5606
  }).use(sessionStore()).guard({
5606
5607
  cookie: t3.Cookie({ user_session_id: userSessionIdTypebox }),
5607
5608
  schema: "merge"
5608
- }).derive(({ store: { session }, cookie: { user_session_id }, status }) => ({
5609
- protectPermission: (check, handleAuth, handleAuthFail) => getStatusFromSource({
5609
+ }).derive(({ store: { session }, cookie: { user_session_id }, status }) => {
5610
+ const protectPermission = (check, handleAuth, handleAuthFail) => getStatusFromSource({
5610
5611
  authSessionStore,
5611
5612
  session,
5612
5613
  user_session_id
5613
5614
  }).then(async ({ user, error }) => {
5614
5615
  if (error) {
5615
- return handleAuthFail?.(error) ?? status(error.code, error.message);
5616
+ return await handleAuthFail?.(error) ?? status(error.code, error.message);
5616
5617
  }
5617
5618
  if (!user) {
5618
- return handleAuthFail?.({
5619
+ return await handleAuthFail?.({
5619
5620
  code: "Unauthorized",
5620
5621
  message: "User is not authenticated"
5621
5622
  }) ?? status("Unauthorized", "User is not authenticated");
@@ -5632,14 +5633,15 @@ var protectPermissionPlugin = ({
5632
5633
  organizationId: check.organizationId,
5633
5634
  type: "authorization_denied"
5634
5635
  });
5635
- return handleAuthFail?.({
5636
+ return await handleAuthFail?.({
5636
5637
  code: "Forbidden",
5637
5638
  message: "Insufficient permissions"
5638
5639
  }) ?? status("Forbidden", "Insufficient permissions");
5639
5640
  }
5640
5641
  return handleAuth(user);
5641
- })
5642
- })).as("global");
5642
+ });
5643
+ return { protectPermission };
5644
+ }).as("global");
5643
5645
 
5644
5646
  // src/routes/protectRoute.ts
5645
5647
  import { Elysia as Elysia6, t as t4 } from "elysia";
@@ -5703,8 +5705,7 @@ var protectRoutePlugin = ({
5703
5705
  }).derive(async ({
5704
5706
  store: { session },
5705
5707
  cookie: { user_session_id },
5706
- headers,
5707
- status
5708
+ headers
5708
5709
  }) => {
5709
5710
  const sessionStatus = await getStatusFromSource({
5710
5711
  authSessionStore,
@@ -5723,25 +5724,30 @@ var protectRoutePlugin = ({
5723
5724
  authorization: headers.authorization,
5724
5725
  config: accessTokens
5725
5726
  });
5726
- return {
5727
- authPrincipal,
5728
- protectRoute: (handleAuth, handleAuthFail) => {
5729
- const user = authPrincipal?.user;
5730
- const { error } = sessionStatus;
5731
- if (error) {
5732
- if (user)
5733
- return handleAuth(user);
5734
- return handleAuthFail?.(error) ?? status(error.code, error.message);
5735
- }
5736
- if (!user) {
5737
- return handleAuthFail?.({
5738
- code: "Unauthorized",
5739
- message: "User is not authenticated"
5740
- }) ?? status("Unauthorized", "User is not authenticated");
5741
- }
5742
- return handleAuth(user);
5727
+ return { absoluteAuthStatus: sessionStatus, authPrincipal };
5728
+ }).derive(({ absoluteAuthStatus, authPrincipal, status }) => {
5729
+ const protectRoute = async (handleAuth, handleAuthFail) => {
5730
+ if (!absoluteAuthStatus)
5731
+ return await handleAuthFail?.({
5732
+ code: "Unauthorized",
5733
+ message: "User is not authenticated"
5734
+ }) ?? status("Unauthorized", "User is not authenticated");
5735
+ const user = authPrincipal?.user;
5736
+ const { error } = absoluteAuthStatus;
5737
+ if (error) {
5738
+ if (user)
5739
+ return handleAuth(user);
5740
+ return await handleAuthFail?.(error) ?? status(error.code, error.message);
5741
+ }
5742
+ if (!user) {
5743
+ return await handleAuthFail?.({
5744
+ code: "Unauthorized",
5745
+ message: "User is not authenticated"
5746
+ }) ?? status("Unauthorized", "User is not authenticated");
5743
5747
  }
5748
+ return handleAuth(user);
5744
5749
  };
5750
+ return { protectRoute };
5745
5751
  }).as("global");
5746
5752
 
5747
5753
  // src/routes/stepUp.ts
@@ -5754,23 +5760,24 @@ var stepUpPlugin = ({
5754
5760
  }).use(sessionStore()).guard({
5755
5761
  cookie: t5.Cookie({ user_session_id: userSessionIdTypebox }),
5756
5762
  schema: "merge"
5757
- }).derive(({ store: { session }, cookie: { user_session_id }, status }) => ({
5758
- requireRecentAuth: (maxAgeMs, handleAuth, handleAuthFail) => loadSessionFromSource({
5763
+ }).derive(({ store: { session }, cookie: { user_session_id }, status }) => {
5764
+ const requireRecentAuth = (maxAgeMs, handleAuth, handleAuthFail) => loadSessionFromSource({
5759
5765
  authSessionStore,
5760
5766
  session,
5761
5767
  userSessionId: user_session_id.value
5762
- }).then((userSession) => {
5768
+ }).then(async (userSession) => {
5763
5769
  const authenticatedAt = userSession?.authenticatedAt;
5764
5770
  const isRecent = authenticatedAt !== undefined && Date.now() - authenticatedAt <= maxAgeMs;
5765
5771
  if (!userSession || !isRecent) {
5766
- return handleAuthFail?.({
5772
+ return await handleAuthFail?.({
5767
5773
  code: "Unauthorized",
5768
5774
  message: "Recent authentication required"
5769
5775
  }) ?? status("Unauthorized", "Recent authentication required");
5770
5776
  }
5771
5777
  return handleAuth(userSession.user);
5772
- })
5773
- })).as("global");
5778
+ });
5779
+ return { requireRecentAuth };
5780
+ }).as("global");
5774
5781
 
5775
5782
  // src/oidc/socketTickets.ts
5776
5783
  init_crypto();
@@ -41672,5 +41679,5 @@ export {
41672
41679
  VerificationProviderError
41673
41680
  };
41674
41681
 
41675
- //# debugId=A14F7F9FF66E9B8A64756E2164756E21
41682
+ //# debugId=81F68CD916C89A2764756E2164756E21
41676
41683
  //# sourceMappingURL=server.js.map