actor-gate 0.1.0 → 0.2.1
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.
- package/package.json +1 -1
- package/src/express/index.d.ts +1 -0
- package/src/express/index.js +1 -0
- package/src/express/protected-route.d.ts +10 -0
- package/src/express/protected-route.js +10 -0
- package/src/next/app/protected-route.d.ts +9 -0
- package/src/next/app/protected-route.js +9 -0
- package/src/next/index.d.ts +1 -0
- package/src/next/index.js +1 -0
- package/src/next/pages/protected-route.d.ts +10 -0
- package/src/next/pages/protected-route.js +10 -0
- package/src/shared/protected-route-session.d.ts +34 -0
- package/src/shared/protected-route-session.js +33 -0
package/package.json
CHANGED
package/src/express/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
1
|
export { withExpressProtectedRoute, type ExpressProtectedAuth, type ExpressProtectedRouteContext, type ExpressRequestLike, type ExpressResponseLike, type WithExpressProtectedRouteOptions, } from './protected-route';
|
|
2
|
+
export { buildProtectedRouteSession, buildProtectedRouteSessionWithMeta, type ProtectedRouteSession, type ProtectedRouteSessionWithMeta, } from '../shared/protected-route-session';
|
package/src/express/index.js
CHANGED
|
@@ -40,5 +40,15 @@ export type WithExpressProtectedRouteOptions<TSessionId, TUserId, TActor extends
|
|
|
40
40
|
resourceMetadataUrl?: string;
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Wraps an Express route with access-token authentication and standardized
|
|
45
|
+
* auth/system error responses.
|
|
46
|
+
*
|
|
47
|
+
* Handler output behavior:
|
|
48
|
+
* - Return a non-`undefined` value to send `200` with `res.json(output)`.
|
|
49
|
+
* - Return `undefined` to send `204`.
|
|
50
|
+
* - If your handler already committed the response (for example via
|
|
51
|
+
* `res.status(...).json(...)`), this wrapper does not overwrite it.
|
|
52
|
+
*/
|
|
43
53
|
export declare function withExpressProtectedRoute<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> = Record<string, never>, TClientSessionData extends Record<string, unknown> = Record<string, never>, TExtClaims extends Record<string, unknown> = Record<string, never>, TReq extends ExpressRequestLike = ExpressRequestLike, TRes extends ExpressResponseLike = ExpressResponseLike, TOutput = unknown>(options: WithExpressProtectedRouteOptions<TSessionId, TUserId, TActor, TServerSessionData, TClientSessionData, TExtClaims, TReq, TRes, TOutput>): (req: TReq, res: TRes) => Promise<void>;
|
|
44
54
|
export {};
|
|
@@ -37,6 +37,16 @@ function sendExpressAuthError(res, error, input) {
|
|
|
37
37
|
}
|
|
38
38
|
res.status(built.statusCode).json(built.body);
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Wraps an Express route with access-token authentication and standardized
|
|
42
|
+
* auth/system error responses.
|
|
43
|
+
*
|
|
44
|
+
* Handler output behavior:
|
|
45
|
+
* - Return a non-`undefined` value to send `200` with `res.json(output)`.
|
|
46
|
+
* - Return `undefined` to send `204`.
|
|
47
|
+
* - If your handler already committed the response (for example via
|
|
48
|
+
* `res.status(...).json(...)`), this wrapper does not overwrite it.
|
|
49
|
+
*/
|
|
40
50
|
export function withExpressProtectedRoute(options) {
|
|
41
51
|
const authorizationHeaderName = options.authorizationHeaderName ?? 'authorization';
|
|
42
52
|
const requestIdHeaderName = options.requestIdHeaderName ?? 'x-request-id';
|
|
@@ -24,4 +24,13 @@ export type WithAppProtectedRouteOptions<TSessionId, TUserId, TActor extends Aut
|
|
|
24
24
|
authorizationHeaderName?: string;
|
|
25
25
|
challenge?: WithAppAuthRouteOptions<TOutput>['challenge'];
|
|
26
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Wraps a Next.js App Router handler with access-token authentication and
|
|
29
|
+
* standardized auth/system error handling.
|
|
30
|
+
*
|
|
31
|
+
* Handler output behavior:
|
|
32
|
+
* - Return a `Response` to fully control the HTTP response.
|
|
33
|
+
* - Return a non-`undefined` non-`Response` value to send `200` JSON.
|
|
34
|
+
* - Return `undefined` to send `204`.
|
|
35
|
+
*/
|
|
27
36
|
export declare function withAppProtectedRoute<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> = Record<string, never>, TClientSessionData extends Record<string, unknown> = Record<string, never>, TExtClaims extends Record<string, unknown> = Record<string, never>, TOutput = unknown>(options: WithAppProtectedRouteOptions<TSessionId, TUserId, TActor, TServerSessionData, TClientSessionData, TExtClaims, TOutput>): (req: Request) => Promise<Response>;
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { AuthServiceError } from '../../core/services/auth-error';
|
|
2
2
|
import { assertBearerOnlyActorPolicy, resolveAccessTokenTransportAdapter, } from '../shared/direct-auth-utils';
|
|
3
3
|
import { withAppAuthRoute } from './wrapper';
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a Next.js App Router handler with access-token authentication and
|
|
6
|
+
* standardized auth/system error handling.
|
|
7
|
+
*
|
|
8
|
+
* Handler output behavior:
|
|
9
|
+
* - Return a `Response` to fully control the HTTP response.
|
|
10
|
+
* - Return a non-`undefined` non-`Response` value to send `200` JSON.
|
|
11
|
+
* - Return `undefined` to send `204`.
|
|
12
|
+
*/
|
|
4
13
|
export function withAppProtectedRoute(options) {
|
|
5
14
|
const accessTokenTransportAdapter = resolveAccessTokenTransportAdapter(options.accessTokenTransport);
|
|
6
15
|
return withAppAuthRoute({
|
package/src/next/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export declare const SUPPORTED_NEXT_ROUTERS: readonly ["pages", "app"];
|
|
2
2
|
export type SupportedNextRouter = (typeof SUPPORTED_NEXT_ROUTERS)[number];
|
|
3
|
+
export { buildProtectedRouteSession, buildProtectedRouteSessionWithMeta, type ProtectedRouteSession, type ProtectedRouteSessionWithMeta, } from '../shared/protected-route-session';
|
|
3
4
|
export * from './pages/index';
|
|
4
5
|
export * from './app/index';
|
|
5
6
|
export * from './shared/auth-routes';
|
package/src/next/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const SUPPORTED_NEXT_ROUTERS = ['pages', 'app'];
|
|
2
|
+
export { buildProtectedRouteSession, buildProtectedRouteSessionWithMeta, } from '../shared/protected-route-session';
|
|
2
3
|
export * from './pages/index';
|
|
3
4
|
export * from './app/index';
|
|
4
5
|
export * from './shared/auth-routes';
|
|
@@ -25,4 +25,14 @@ export type WithPagesProtectedRouteOptions<TSessionId, TUserId, TActor extends A
|
|
|
25
25
|
authorizationHeaderName?: string;
|
|
26
26
|
challenge?: WithPagesAuthRouteOptions<TOutput>['challenge'];
|
|
27
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Wraps a Next.js Pages API route with access-token authentication and
|
|
30
|
+
* standardized auth/system error handling.
|
|
31
|
+
*
|
|
32
|
+
* Handler output behavior:
|
|
33
|
+
* - Return a non-`undefined` value to send `200` with `res.json(output)`.
|
|
34
|
+
* - Return `undefined` to send `204`.
|
|
35
|
+
* - If your handler already finished the response (for example by calling
|
|
36
|
+
* `res.status(...).json(...)` directly), the wrapper does not overwrite it.
|
|
37
|
+
*/
|
|
28
38
|
export declare function withPagesProtectedRoute<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> = Record<string, never>, TClientSessionData extends Record<string, unknown> = Record<string, never>, TExtClaims extends Record<string, unknown> = Record<string, never>, TOutput = unknown>(options: WithPagesProtectedRouteOptions<TSessionId, TUserId, TActor, TServerSessionData, TClientSessionData, TExtClaims, TOutput>): NextApiHandler;
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { AuthServiceError } from '../../core/services/auth-error';
|
|
2
2
|
import { assertBearerOnlyActorPolicy, resolveAccessTokenTransportAdapter, } from '../shared/direct-auth-utils';
|
|
3
3
|
import { withPagesAuthRoute } from './wrapper';
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a Next.js Pages API route with access-token authentication and
|
|
6
|
+
* standardized auth/system error handling.
|
|
7
|
+
*
|
|
8
|
+
* Handler output behavior:
|
|
9
|
+
* - Return a non-`undefined` value to send `200` with `res.json(output)`.
|
|
10
|
+
* - Return `undefined` to send `204`.
|
|
11
|
+
* - If your handler already finished the response (for example by calling
|
|
12
|
+
* `res.status(...).json(...)` directly), the wrapper does not overwrite it.
|
|
13
|
+
*/
|
|
4
14
|
export function withPagesProtectedRoute(options) {
|
|
5
15
|
const accessTokenTransportAdapter = resolveAccessTokenTransportAdapter(options.accessTokenTransport);
|
|
6
16
|
return withPagesAuthRoute({
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type AuthServiceErrorCode } from '../core/services/auth-error';
|
|
2
|
+
import type { ValidatedAccessTokenResult } from '../core/services/contracts';
|
|
3
|
+
import type { AuthActor } from '../core/types/auth-contract';
|
|
4
|
+
type SessionMetaValue<TServerSessionData extends {
|
|
5
|
+
meta?: unknown;
|
|
6
|
+
}> = Exclude<TServerSessionData['meta'], undefined | null>;
|
|
7
|
+
export type ProtectedRouteSession<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> = Record<string, never>> = {
|
|
8
|
+
sessionId: TSessionId;
|
|
9
|
+
userId: TUserId;
|
|
10
|
+
actor: TActor;
|
|
11
|
+
issuedAt: number;
|
|
12
|
+
expiresAt: number;
|
|
13
|
+
serverSessionData?: TServerSessionData;
|
|
14
|
+
};
|
|
15
|
+
export type ProtectedRouteSessionWithMeta<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> & {
|
|
16
|
+
meta?: unknown;
|
|
17
|
+
} = {
|
|
18
|
+
meta?: unknown;
|
|
19
|
+
}> = ProtectedRouteSession<TSessionId, TUserId, TActor, TServerSessionData> & {
|
|
20
|
+
serverSessionData: TServerSessionData & {
|
|
21
|
+
meta: SessionMetaValue<TServerSessionData>;
|
|
22
|
+
};
|
|
23
|
+
meta: SessionMetaValue<TServerSessionData>;
|
|
24
|
+
};
|
|
25
|
+
export declare function buildProtectedRouteSession<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> = Record<string, never>, TClientSessionData extends Record<string, unknown> = Record<string, never>, TExtClaims extends Record<string, unknown> = Record<string, never>>(auth: ValidatedAccessTokenResult<TSessionId, TUserId, TActor, TServerSessionData, TClientSessionData, TExtClaims>): ProtectedRouteSession<TSessionId, TUserId, TActor, TServerSessionData>;
|
|
26
|
+
export declare function buildProtectedRouteSessionWithMeta<TSessionId, TUserId, TActor extends AuthActor = AuthActor, TServerSessionData extends Record<string, unknown> & {
|
|
27
|
+
meta?: unknown;
|
|
28
|
+
} = {
|
|
29
|
+
meta?: unknown;
|
|
30
|
+
}, TClientSessionData extends Record<string, unknown> = Record<string, never>, TExtClaims extends Record<string, unknown> = Record<string, never>>(auth: ValidatedAccessTokenResult<TSessionId, TUserId, TActor, TServerSessionData, TClientSessionData, TExtClaims>, options?: {
|
|
31
|
+
errorCode?: AuthServiceErrorCode;
|
|
32
|
+
errorMessage?: string;
|
|
33
|
+
}): ProtectedRouteSessionWithMeta<TSessionId, TUserId, TActor, TServerSessionData>;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AuthServiceError, } from '../core/services/auth-error';
|
|
2
|
+
function buildMissingMetaError(options) {
|
|
3
|
+
return new AuthServiceError({
|
|
4
|
+
code: options?.errorCode ?? 'unauthorized',
|
|
5
|
+
message: options?.errorMessage ?? 'Authenticated session metadata is missing.',
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export function buildProtectedRouteSession(auth) {
|
|
9
|
+
const session = auth.session;
|
|
10
|
+
return {
|
|
11
|
+
sessionId: auth.authContext.sessionId,
|
|
12
|
+
userId: auth.authContext.userId,
|
|
13
|
+
actor: auth.claims.actor,
|
|
14
|
+
issuedAt: session?.issuedAt ?? auth.claims.iat,
|
|
15
|
+
expiresAt: session?.expiresAt ?? auth.claims.exp,
|
|
16
|
+
...(session?.serverSessionData === undefined
|
|
17
|
+
? {}
|
|
18
|
+
: { serverSessionData: session.serverSessionData }),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function buildProtectedRouteSessionWithMeta(auth, options) {
|
|
22
|
+
const session = buildProtectedRouteSession(auth);
|
|
23
|
+
const serverSessionData = session.serverSessionData;
|
|
24
|
+
const meta = serverSessionData?.meta;
|
|
25
|
+
if (meta === undefined || meta === null) {
|
|
26
|
+
throw buildMissingMetaError(options);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
...session,
|
|
30
|
+
serverSessionData: serverSessionData,
|
|
31
|
+
meta: meta,
|
|
32
|
+
};
|
|
33
|
+
}
|