@schemavaults/auth-server-sdk 0.32.8 → 0.33.4
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/README.md +52 -2
- package/dist/NextjsAppDirectoryPlugin/codegen.js +23 -9
- package/dist/NextjsAppDirectoryPlugin/codegen.js.map +1 -1
- package/dist/auth-session-route/index.d.ts +5 -0
- package/dist/auth-session-route/index.js +5 -0
- package/dist/auth-session-route/index.js.map +1 -0
- package/dist/auth-session-route/logout-route.d.ts +14 -0
- package/dist/auth-session-route/logout-route.js +34 -0
- package/dist/auth-session-route/logout-route.js.map +1 -0
- package/dist/auth-session-route/session-cookie.d.ts +26 -0
- package/dist/auth-session-route/session-cookie.js +48 -0
- package/dist/auth-session-route/session-cookie.js.map +1 -0
- package/dist/auth-session-route/session-route-context.d.ts +55 -0
- package/dist/auth-session-route/session-route-context.js +114 -0
- package/dist/auth-session-route/session-route-context.js.map +1 -0
- package/dist/auth-session-route/token-route.d.ts +15 -0
- package/dist/auth-session-route/token-route.js +90 -0
- package/dist/auth-session-route/token-route.js.map +1 -0
- package/dist/auth-session-route/whoami-route.d.ts +14 -0
- package/dist/auth-session-route/whoami-route.js +42 -0
- package/dist/auth-session-route/whoami-route.js.map +1 -0
- package/dist/cli.cjs +26 -12
- package/dist/codegen-templates/auth/auth-provider.tsx +15 -0
- package/dist/codegen-templates/auth/session/logout/route.ts +7 -0
- package/dist/codegen-templates/auth/session/token/route.ts +14 -0
- package/dist/codegen-templates/auth/session/whoami/route.ts +8 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -8,8 +8,9 @@ TypeScript SDK for SchemaVaults Auth Server and Resource Server API backends.
|
|
|
8
8
|
bunx @schemavaults/auth-server-sdk codegen [options]
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Generates the auth pages (`login`, `register`, `logout`, `authorize`, `error`)
|
|
12
|
-
|
|
11
|
+
Generates the auth pages (`login`, `register`, `logout`, `authorize`, `error`),
|
|
12
|
+
the auth session routes (`session/{token,whoami,logout}/route.ts`, see below) and the
|
|
13
|
+
`auth-provider.tsx` into your Next.js App Router project.
|
|
13
14
|
|
|
14
15
|
### Options
|
|
15
16
|
|
|
@@ -29,6 +30,55 @@ and the `auth-provider.tsx` into your Next.js App Router project.
|
|
|
29
30
|
- `--help`, `-h` — Show help.
|
|
30
31
|
- `--version`, `-v` — Show package name and version.
|
|
31
32
|
|
|
33
|
+
## Auth session routes (refresh token in a first-party HTTP-only cookie)
|
|
34
|
+
|
|
35
|
+
`@schemavaults/auth-server-sdk/auth-session-route` serves the browser SDK's
|
|
36
|
+
token, whoami and logout requests from the app's own origin and forwards them
|
|
37
|
+
to the auth server, one route handler each:
|
|
38
|
+
|
|
39
|
+
| Route (codegen path) | Factory | Method |
|
|
40
|
+
| --- | --- | --- |
|
|
41
|
+
| token (`/auth/session/token`) | `createAuthSessionTokenRoute()` | `POST` (authorization code and refresh grants) |
|
|
42
|
+
| whoami (`/auth/session/whoami`) | `createAuthSessionWhoamiRoute()` | `GET` |
|
|
43
|
+
| logout (`/auth/session/logout`) | `createAuthSessionLogoutRoute()` | `POST` |
|
|
44
|
+
|
|
45
|
+
The refresh token is kept in an HTTP-only, `SameSite=Strict`, host-only cookie
|
|
46
|
+
on the app's own host (`__Host-schemavaults_refresh_token_<app>`: the
|
|
47
|
+
`__Host-` prefix makes browsers refuse any such cookie that carries a `Domain`
|
|
48
|
+
or a narrower `Path`, so a sibling subdomain cannot plant one; a request that
|
|
49
|
+
carries the cookie twice is treated as having no session), and is never
|
|
50
|
+
returned to the page, so:
|
|
51
|
+
|
|
52
|
+
- page scripts (an XSS payload included) can never read or exfiltrate it;
|
|
53
|
+
- it is a first-party cookie, which browsers that block third-party cookies
|
|
54
|
+
(Safari, every iOS browser) keep. The auth server's own cookie would be a
|
|
55
|
+
third-party cookie for an app on another site.
|
|
56
|
+
|
|
57
|
+
The codegen writes one route file per action, e.g.
|
|
58
|
+
`auth/session/token/route.ts`:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { createAuthSessionTokenRoute } from "@schemavaults/auth-server-sdk/auth-session-route";
|
|
62
|
+
|
|
63
|
+
export const { POST } = createAuthSessionTokenRoute();
|
|
64
|
+
export const dynamic = "force-dynamic";
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
(`auth/session/whoami/route.ts` exports `{ GET }` from
|
|
68
|
+
`createAuthSessionWhoamiRoute()`, `auth/session/logout/route.ts` exports
|
|
69
|
+
`{ POST }` from `createAuthSessionLogoutRoute()`),
|
|
70
|
+
and the generated `auth-provider.tsx` points the SDK at each of them
|
|
71
|
+
(`session_routes={AUTH_SESSION_ROUTE_PATHS}` on `<AuthProvider>`, one path per
|
|
72
|
+
route; pass `session_routes={null}` to the generated provider to opt out). The
|
|
73
|
+
routes read `SCHEMAVAULTS_CLIENT_APP_ID`, `SCHEMAVAULTS_APP_ENVIRONMENT` and
|
|
74
|
+
`SCHEMAVAULTS_AUTH_SERVER_URL` (or the environment's default auth server); the
|
|
75
|
+
cookie is `Secure` outside development/test. They refuse cross-origin requests
|
|
76
|
+
(`Origin` / `Sec-Fetch-Site` checks), and the token route serves only its own
|
|
77
|
+
app's `authorization_code` and `refresh_token` grants.
|
|
78
|
+
|
|
79
|
+
The auth server rate-limits refresh grants per IP; proxied through the token
|
|
80
|
+
route, they count against the app server's IP.
|
|
81
|
+
|
|
32
82
|
## Accepting resource-URL token audiences
|
|
33
83
|
|
|
34
84
|
Access tokens are normally minted with your API server id as their `aud`.
|
|
@@ -5,27 +5,41 @@ import resolveAppDirectory from "./resolve-app-directory";
|
|
|
5
5
|
import { join } from "path";
|
|
6
6
|
import resolveCodegenTemplatesDirectory from "./resolve-codegen-templates-directory";
|
|
7
7
|
import { extractVersion, getCodegenMarkerComment, hasCodegenMarker, prependCodegenMarker } from "./codegen-marker";
|
|
8
|
-
const
|
|
8
|
+
const filesToCreate = [
|
|
9
9
|
{
|
|
10
|
-
|
|
10
|
+
auth_relative_file: "login/page.tsx",
|
|
11
11
|
codegen_template_path: "auth/login/page.tsx",
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
|
-
|
|
14
|
+
auth_relative_file: "register/page.tsx",
|
|
15
15
|
codegen_template_path: "auth/register/page.tsx",
|
|
16
16
|
},
|
|
17
17
|
{
|
|
18
|
-
|
|
18
|
+
auth_relative_file: "logout/page.tsx",
|
|
19
19
|
codegen_template_path: "auth/logout/page.tsx",
|
|
20
20
|
},
|
|
21
21
|
{
|
|
22
|
-
|
|
22
|
+
auth_relative_file: "authorize/page.tsx",
|
|
23
23
|
codegen_template_path: "auth/authorize/page.tsx",
|
|
24
24
|
},
|
|
25
25
|
{
|
|
26
|
-
|
|
26
|
+
auth_relative_file: "error/page.tsx",
|
|
27
27
|
codegen_template_path: "auth/error/page.tsx",
|
|
28
28
|
},
|
|
29
|
+
// The auth session routes: keep the refresh token in an HTTP-only cookie
|
|
30
|
+
// on the app's own domain (see auth-provider.tsx).
|
|
31
|
+
{
|
|
32
|
+
auth_relative_file: "session/token/route.ts",
|
|
33
|
+
codegen_template_path: "auth/session/token/route.ts",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
auth_relative_file: "session/whoami/route.ts",
|
|
37
|
+
codegen_template_path: "auth/session/whoami/route.ts",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
auth_relative_file: "session/logout/route.ts",
|
|
41
|
+
codegen_template_path: "auth/session/logout/route.ts",
|
|
42
|
+
},
|
|
29
43
|
];
|
|
30
44
|
function isCodegenManagedFile(filePath) {
|
|
31
45
|
const content = readFileSync(filePath, { encoding: "utf-8" });
|
|
@@ -46,9 +60,9 @@ function formatVersionTransition(oldVersion) {
|
|
|
46
60
|
return "";
|
|
47
61
|
}
|
|
48
62
|
function createClientPages(authDirectory, templatesDir) {
|
|
49
|
-
for (const page of
|
|
50
|
-
const destPath = join(authDirectory, page.
|
|
51
|
-
const relPath = `auth/${page.
|
|
63
|
+
for (const page of filesToCreate) {
|
|
64
|
+
const destPath = join(authDirectory, page.auth_relative_file);
|
|
65
|
+
const relPath = `auth/${page.auth_relative_file}`;
|
|
52
66
|
if (existsSync(destPath)) {
|
|
53
67
|
if (isCodegenManagedFile(destPath)) {
|
|
54
68
|
const oldVersion = getExistingVersion(destPath);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codegen.js","sourceRoot":"","sources":["../../src/NextjsAppDirectoryPlugin/codegen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,mBAAmB,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,gCAAgC,MAAM,uCAAuC,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AA2BnH,MAAM,aAAa,GAAiC;IAClD;QACE,kBAAkB,EAAE,
|
|
1
|
+
{"version":3,"file":"codegen.js","sourceRoot":"","sources":["../../src/NextjsAppDirectoryPlugin/codegen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,mBAAmB,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,gCAAgC,MAAM,uCAAuC,CAAC;AACrF,OAAO,E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AC,aAAa,EAAE,YAAY,CAAC,CAAC;IAEtD,OAAO,CAAC,GAAG,CACT,4EAA4E,CAC7E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createAuthSessionTokenRoute } from "./token-route";
|
|
2
|
+
export { createAuthSessionWhoamiRoute } from "./whoami-route";
|
|
3
|
+
export { createAuthSessionLogoutRoute } from "./logout-route";
|
|
4
|
+
export type { AuthSessionRouteHandler, AuthSessionRouteOptions, AuthSessionUpstreamFetch, } from "./session-route-context";
|
|
5
|
+
export { AuthSessionRefreshTokenCookieName } from "./session-cookie";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createAuthSessionTokenRoute } from "./token-route";
|
|
2
|
+
export { createAuthSessionWhoamiRoute } from "./whoami-route";
|
|
3
|
+
export { createAuthSessionLogoutRoute } from "./logout-route";
|
|
4
|
+
export { AuthSessionRefreshTokenCookieName } from "./session-cookie";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth-session-route/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,4BAA4B,EAAE,MAAM,gBAAgB,CAAC;AAM9D,OAAO,EAAE,iCAAiC,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AuthSessionRouteHandler, type AuthSessionRouteOptions } from "./session-route-context";
|
|
2
|
+
/**
|
|
3
|
+
* The logout session route (`POST`): revokes the session at the auth server (best
|
|
4
|
+
* effort) and clears the session cookie. In Next.js,
|
|
5
|
+
* `app/auth/session/logout/route.ts`:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* export const { POST } = createAuthSessionLogoutRoute();
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare function createAuthSessionLogoutRoute(options?: AuthSessionRouteOptions): {
|
|
12
|
+
POST: AuthSessionRouteHandler;
|
|
13
|
+
};
|
|
14
|
+
export default createAuthSessionLogoutRoute;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { serializeClearedSessionCookie } from "./session-cookie";
|
|
2
|
+
import { authServerAppEndpoint, bearerHeaders, createSessionRoute, json, } from "./session-route-context";
|
|
3
|
+
async function handleLogout({ request, config, cookie, refresh_token, callAuthServer, }) {
|
|
4
|
+
if (refresh_token) {
|
|
5
|
+
// Revokes the refresh token and the access tokens minted with it.
|
|
6
|
+
try {
|
|
7
|
+
const upstream = await callAuthServer(authServerAppEndpoint(config, "logout"), {
|
|
8
|
+
method: "POST",
|
|
9
|
+
headers: bearerHeaders(request, refresh_token),
|
|
10
|
+
});
|
|
11
|
+
if (!upstream.ok) {
|
|
12
|
+
console.error(`[auth-session-route] The auth server answered the logout with HTTP ${upstream.status}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
console.error("[auth-session-route] Logout request to the auth server failed:", e);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return json(200, { success: true, error: false, message: "Logged out" }, { "Set-Cookie": serializeClearedSessionCookie(cookie.name, cookie.attrs) });
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The logout session route (`POST`): revokes the session at the auth server (best
|
|
23
|
+
* effort) and clears the session cookie. In Next.js,
|
|
24
|
+
* `app/auth/session/logout/route.ts`:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* export const { POST } = createAuthSessionLogoutRoute();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function createAuthSessionLogoutRoute(options = {}) {
|
|
31
|
+
return { POST: createSessionRoute(options, handleLogout) };
|
|
32
|
+
}
|
|
33
|
+
export default createAuthSessionLogoutRoute;
|
|
34
|
+
//# sourceMappingURL=logout-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logout-route.js","sourceRoot":"","sources":["../../src/auth-session-route/logout-route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAIL,qBAAqB,EACrB,aAAa,EACb,kBAAkB,EAClB,IAAI,GACL,MAAM,yBAAyB,CAAC;AAEjC,KAAK,UAAU,YAAY,CAAC,EAC1B,OAAO,EACP,MAAM,EACN,MAAM,EACN,aAAa,EACb,cAAc,GACM;IACpB,IAAI,aAAa,EAAE,CAAC;QAClB,kEAAkE;QAClE,IAAI,CAAC;YACH,MAAM,QAAQ,GAAa,MAAM,cAAc,CAAC,qBAAqB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAAE;gBACvF,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE,aAAa,CAAC,OAAO,EAAE,aAAa,CAAC;aAC/C,CAAC,CAAC;YACH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,OAAO,CAAC,KAAK,CAAC,sEAAsE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;YACzG,CAAC;QACH,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACpB,OAAO,CAAC,KAAK,CAAC,gEAAgE,EAAE,CAAC,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CACT,GAAG,EACH,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,EACtD,EAAE,YAAY,EAAE,6BAA6B,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAC3E,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,4BAA4B,CAC1C,UAAmC,EAAE;IAErC,OAAO,EAAE,IAAI,EAAE,kBAAkB,CAAC,OAAO,EAAE,YAAY,CAAC,EAAE,CAAC;AAC7D,CAAC;AAED,eAAe,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AppId } from "@schemavaults/app-definitions";
|
|
2
|
+
/**
|
|
3
|
+
* Name of the HTTP-only cookie in which the hosting app's session routes
|
|
4
|
+
* keep the refresh token, on the app's own host.
|
|
5
|
+
*
|
|
6
|
+
* Secure deployments use the `__Host-` prefix: browsers refuse such a cookie
|
|
7
|
+
* unless it is `Secure`, has `Path=/` and has no `Domain` attribute. So no
|
|
8
|
+
* other host can plant one for this host. Under `__Secure-` (or no prefix) a
|
|
9
|
+
* sibling subdomain could set `Domain=<parent>; Path=/auth/session/token`,
|
|
10
|
+
* and the browser would send that more specific cookie first, presenting an
|
|
11
|
+
* attacker's refresh token (session fixation). Plain-HTTP development/test
|
|
12
|
+
* deployments cannot use the prefix (it requires `Secure`).
|
|
13
|
+
*/
|
|
14
|
+
export declare function AuthSessionRefreshTokenCookieName(client_app_id: AppId, secure: boolean): string;
|
|
15
|
+
export interface SessionCookieAttributes {
|
|
16
|
+
secure: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function serializeSessionCookie(name: string, value: string, max_age_seconds: number, attrs: SessionCookieAttributes): string;
|
|
19
|
+
export declare function serializeClearedSessionCookie(name: string, attrs: SessionCookieAttributes): string;
|
|
20
|
+
/**
|
|
21
|
+
* The value of cookie `name` in a `Cookie` request header. Null when absent,
|
|
22
|
+
* and also when the name appears more than once: the header carries no
|
|
23
|
+
* attributes, so the route cannot tell its own cookie from a planted one and
|
|
24
|
+
* must not guess.
|
|
25
|
+
*/
|
|
26
|
+
export declare function readCookie(cookie_header: string | null, name: string): string | null;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Name of the HTTP-only cookie in which the hosting app's session routes
|
|
3
|
+
* keep the refresh token, on the app's own host.
|
|
4
|
+
*
|
|
5
|
+
* Secure deployments use the `__Host-` prefix: browsers refuse such a cookie
|
|
6
|
+
* unless it is `Secure`, has `Path=/` and has no `Domain` attribute. So no
|
|
7
|
+
* other host can plant one for this host. Under `__Secure-` (or no prefix) a
|
|
8
|
+
* sibling subdomain could set `Domain=<parent>; Path=/auth/session/token`,
|
|
9
|
+
* and the browser would send that more specific cookie first, presenting an
|
|
10
|
+
* attacker's refresh token (session fixation). Plain-HTTP development/test
|
|
11
|
+
* deployments cannot use the prefix (it requires `Secure`).
|
|
12
|
+
*/
|
|
13
|
+
export function AuthSessionRefreshTokenCookieName(client_app_id, secure) {
|
|
14
|
+
return `${secure ? "__Host-" : ""}schemavaults_refresh_token_${client_app_id}`;
|
|
15
|
+
}
|
|
16
|
+
// `Path=/` and no `Domain` (host-only): what the `__Host-` prefix requires.
|
|
17
|
+
function attributes({ secure }) {
|
|
18
|
+
return `; Path=/; HttpOnly; SameSite=Strict${secure ? "; Secure" : ""}`;
|
|
19
|
+
}
|
|
20
|
+
export function serializeSessionCookie(name, value, max_age_seconds, attrs) {
|
|
21
|
+
return `${name}=${value}; Max-Age=${Math.max(0, Math.floor(max_age_seconds))}${attributes(attrs)}`;
|
|
22
|
+
}
|
|
23
|
+
export function serializeClearedSessionCookie(name, attrs) {
|
|
24
|
+
return `${name}=; Max-Age=0${attributes(attrs)}`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The value of cookie `name` in a `Cookie` request header. Null when absent,
|
|
28
|
+
* and also when the name appears more than once: the header carries no
|
|
29
|
+
* attributes, so the route cannot tell its own cookie from a planted one and
|
|
30
|
+
* must not guess.
|
|
31
|
+
*/
|
|
32
|
+
export function readCookie(cookie_header, name) {
|
|
33
|
+
if (!cookie_header)
|
|
34
|
+
return null;
|
|
35
|
+
let found = null;
|
|
36
|
+
let count = 0;
|
|
37
|
+
for (const pair of cookie_header.split(/;\s*/)) {
|
|
38
|
+
const separator = pair.indexOf("=");
|
|
39
|
+
if (separator > 0 && pair.slice(0, separator) === name) {
|
|
40
|
+
count += 1;
|
|
41
|
+
found = pair.slice(separator + 1);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (count !== 1 || !found)
|
|
45
|
+
return null;
|
|
46
|
+
return found;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=session-cookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-cookie.js","sourceRoot":"","sources":["../../src/auth-session-route/session-cookie.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iCAAiC,CAC/C,aAAoB,EACpB,MAAe;IAEf,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,8BAA8B,aAAa,EAAE,CAAC;AACjF,CAAC;AAMD,4EAA4E;AAC5E,SAAS,UAAU,CAAC,EAAE,MAAM,EAA2B;IACrD,OAAO,sCAAsC,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AAC1E,CAAC;AAED,MAAM,UAAU,sBAAsB,CACpC,IAAY,EACZ,KAAa,EACb,eAAuB,EACvB,KAA8B;IAE9B,OAAO,GAAG,IAAI,IAAI,KAAK,aAAa,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;AACrG,CAAC;AAED,MAAM,UAAU,6BAA6B,CAC3C,IAAY,EACZ,KAA8B;IAE9B,OAAO,GAAG,IAAI,eAAe,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;AACnD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,aAA4B,EAAE,IAAY;IACnE,IAAI,CAAC,aAAa;QAAE,OAAO,IAAI,CAAC;IAChC,IAAI,KAAK,GAAkB,IAAI,CAAC;IAChC,IAAI,KAAK,GAAW,CAAC,CAAC;IACtB,KAAK,MAAM,IAAI,IAAI,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QAC/C,MAAM,SAAS,GAAW,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC5C,IAAI,SAAS,GAAG,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,KAAK,IAAI,EAAE,CAAC;YACvD,KAAK,IAAI,CAAC,CAAC;YACX,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IACD,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IACvC,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type AppId, type SchemaVaultsAppEnvironment } from "@schemavaults/app-definitions";
|
|
2
|
+
import { type SessionCookieAttributes } from "./session-cookie";
|
|
3
|
+
type FetchInit = NonNullable<Parameters<typeof fetch>[1]>;
|
|
4
|
+
type HeadersInit = ConstructorParameters<typeof Headers>[0];
|
|
5
|
+
/** How the routes call the auth server (a `fetch`). */
|
|
6
|
+
export type AuthSessionUpstreamFetch = (url: string, init: FetchInit) => Promise<Response>;
|
|
7
|
+
export interface AuthSessionRouteOptions {
|
|
8
|
+
/** The client app whose session the routes keep. Default: `SCHEMAVAULTS_CLIENT_APP_ID`. */
|
|
9
|
+
client_app_id?: AppId;
|
|
10
|
+
/**
|
|
11
|
+
* The auth server's URL. Default: `SCHEMAVAULTS_AUTH_SERVER_URL`, else the
|
|
12
|
+
* environment's default auth server (as the browser SDK resolves it).
|
|
13
|
+
*/
|
|
14
|
+
auth_server_url?: string;
|
|
15
|
+
/** Default: `SCHEMAVAULTS_APP_ENVIRONMENT`. Outside development/test the cookie is `Secure`. */
|
|
16
|
+
environment?: SchemaVaultsAppEnvironment;
|
|
17
|
+
/** Upstream fetch (tests). */
|
|
18
|
+
fetch?: AuthSessionUpstreamFetch;
|
|
19
|
+
/** Timeout of each request to the auth server, in milliseconds (default 30s). */
|
|
20
|
+
upstream_timeout_ms?: number;
|
|
21
|
+
}
|
|
22
|
+
/** A route handler: a Web `Request → Response` function. */
|
|
23
|
+
export type AuthSessionRouteHandler = (request: Request) => Promise<Response>;
|
|
24
|
+
export interface SessionRouteConfig {
|
|
25
|
+
client_app_id: AppId;
|
|
26
|
+
auth_server_url: string;
|
|
27
|
+
}
|
|
28
|
+
export interface SessionCookie {
|
|
29
|
+
name: string;
|
|
30
|
+
attrs: SessionCookieAttributes;
|
|
31
|
+
}
|
|
32
|
+
/** What a route's own logic receives once the request passed the shared checks. */
|
|
33
|
+
export interface SessionRouteRequest {
|
|
34
|
+
request: Request;
|
|
35
|
+
config: SessionRouteConfig;
|
|
36
|
+
cookie: SessionCookie;
|
|
37
|
+
/** The refresh token in the session cookie, if any. */
|
|
38
|
+
refresh_token: string | null;
|
|
39
|
+
/** Calls the auth server (no caching, no redirects, with a timeout). */
|
|
40
|
+
callAuthServer: (url: string, init: FetchInit) => Promise<Response>;
|
|
41
|
+
}
|
|
42
|
+
export declare function noStoreHeaders(headers?: HeadersInit): Headers;
|
|
43
|
+
export declare function json(status: number, body: unknown, headers?: HeadersInit): Response;
|
|
44
|
+
/** `<auth server>/api/auth/<endpoint>/<client app id>`. */
|
|
45
|
+
export declare function authServerAppEndpoint(config: SessionRouteConfig, endpoint: "whoami" | "logout"): string;
|
|
46
|
+
/** Headers presenting the session's refresh token to the auth server, with the page's Origin. */
|
|
47
|
+
export declare function bearerHeaders(request: Request, refresh_token: string): Headers;
|
|
48
|
+
/**
|
|
49
|
+
* Wraps one session route's logic with what every route shares: the
|
|
50
|
+
* same-origin check, the configuration (resolved per request: environment
|
|
51
|
+
* variables may be unset while a framework builds the route module), the
|
|
52
|
+
* session cookie and the upstream fetch.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createSessionRoute(options: AuthSessionRouteOptions, handle: (route: SessionRouteRequest) => Promise<Response>): AuthSessionRouteHandler;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Shared plumbing of the hosting app's same-origin auth session routes.
|
|
2
|
+
//
|
|
3
|
+
// Browsers drop the HTTP-only refresh-token cookie the auth server sets when
|
|
4
|
+
// the app is on another site (it is a third-party cookie: Safari and every
|
|
5
|
+
// iOS browser block those), and keeping the refresh token in localStorage
|
|
6
|
+
// hands it to any script on the page. These routes keep it in an HTTP-only
|
|
7
|
+
// cookie on the APP's own domain instead: a first-party cookie, which every
|
|
8
|
+
// browser keeps. The browser SDK sends its token, whoami and logout requests
|
|
9
|
+
// to the app's token, whoami and logout routes (`AUTH_SESSION_ROUTE_PATHS`
|
|
10
|
+
// for the ones codegen writes); each route forwards its request to the auth
|
|
11
|
+
// server server-to-server, presenting the refresh token itself and never
|
|
12
|
+
// returning it to the page.
|
|
13
|
+
import { getAppEnvironment, getAuthServerUrl, } from "@schemavaults/app-definitions";
|
|
14
|
+
import getSchemavaultsClientApplicationId from "../env/get-schemavaults-client-application-id";
|
|
15
|
+
import { AuthSessionRefreshTokenCookieName, readCookie, } from "./session-cookie";
|
|
16
|
+
const NO_STORE = {
|
|
17
|
+
"Cache-Control": "no-store",
|
|
18
|
+
Pragma: "no-cache",
|
|
19
|
+
};
|
|
20
|
+
export function noStoreHeaders(headers = {}) {
|
|
21
|
+
const response_headers = new Headers(headers);
|
|
22
|
+
for (const [name, value] of Object.entries(NO_STORE)) {
|
|
23
|
+
response_headers.set(name, value);
|
|
24
|
+
}
|
|
25
|
+
return response_headers;
|
|
26
|
+
}
|
|
27
|
+
export function json(status, body, headers = {}) {
|
|
28
|
+
const response_headers = noStoreHeaders(headers);
|
|
29
|
+
response_headers.set("Content-Type", "application/json");
|
|
30
|
+
return new Response(JSON.stringify(body), { status, headers: response_headers });
|
|
31
|
+
}
|
|
32
|
+
/** `<auth server>/api/auth/<endpoint>/<client app id>`. */
|
|
33
|
+
export function authServerAppEndpoint(config, endpoint) {
|
|
34
|
+
return `${config.auth_server_url.replace(/\/+$/, "")}/api/auth/${endpoint}/${config.client_app_id}`;
|
|
35
|
+
}
|
|
36
|
+
/** Headers presenting the session's refresh token to the auth server, with the page's Origin. */
|
|
37
|
+
export function bearerHeaders(request, refresh_token) {
|
|
38
|
+
const headers = new Headers({ Authorization: `Bearer ${refresh_token}`, Accept: "application/json" });
|
|
39
|
+
const origin = request.headers.get("origin");
|
|
40
|
+
if (origin)
|
|
41
|
+
headers.set("Origin", origin);
|
|
42
|
+
return headers;
|
|
43
|
+
}
|
|
44
|
+
/** The host the browser addressed (behind a proxy, its forwarded host). */
|
|
45
|
+
function requestHost(request) {
|
|
46
|
+
const forwarded = request.headers
|
|
47
|
+
.get("x-forwarded-host")
|
|
48
|
+
?.split(",")[0]
|
|
49
|
+
?.trim();
|
|
50
|
+
return (forwarded || request.headers.get("host") || new URL(request.url).host).toLowerCase();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Cross-site request forgery guard: the routes act with an ambient cookie,
|
|
54
|
+
* so they only answer the app's own pages. The cookie is `SameSite=Strict`
|
|
55
|
+
* too; this also refuses same-site requests from another origin.
|
|
56
|
+
*/
|
|
57
|
+
function isSameOriginRequest(request) {
|
|
58
|
+
const fetch_site = request.headers.get("sec-fetch-site");
|
|
59
|
+
if (fetch_site !== null && fetch_site !== "same-origin") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
const origin = request.headers.get("origin");
|
|
63
|
+
if (origin === null) {
|
|
64
|
+
// Browsers always send Origin on POST; same-origin GETs may omit it.
|
|
65
|
+
return request.method === "GET";
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
return new URL(origin).host.toLowerCase() === requestHost(request);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Wraps one session route's logic with what every route shares: the
|
|
76
|
+
* same-origin check, the configuration (resolved per request: environment
|
|
77
|
+
* variables may be unset while a framework builds the route module), the
|
|
78
|
+
* session cookie and the upstream fetch.
|
|
79
|
+
*/
|
|
80
|
+
export function createSessionRoute(options, handle) {
|
|
81
|
+
const upstream_fetch = options.fetch ?? ((url, init) => fetch(url, init));
|
|
82
|
+
const timeout_ms = options.upstream_timeout_ms ?? 30_000;
|
|
83
|
+
async function callAuthServer(url, init) {
|
|
84
|
+
return await upstream_fetch(url, {
|
|
85
|
+
...init,
|
|
86
|
+
cache: "no-store",
|
|
87
|
+
redirect: "manual",
|
|
88
|
+
signal: AbortSignal.timeout(timeout_ms),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return async function sessionRoute(request) {
|
|
92
|
+
if (!isSameOriginRequest(request)) {
|
|
93
|
+
return json(403, { success: false, error: "forbidden", message: "Cross-origin requests are refused" });
|
|
94
|
+
}
|
|
95
|
+
const environment = options.environment ?? getAppEnvironment();
|
|
96
|
+
const secure = environment !== "development" && environment !== "test";
|
|
97
|
+
const config = {
|
|
98
|
+
client_app_id: options.client_app_id ?? getSchemavaultsClientApplicationId(),
|
|
99
|
+
auth_server_url: options.auth_server_url ?? getAuthServerUrl(environment),
|
|
100
|
+
};
|
|
101
|
+
const cookie = {
|
|
102
|
+
name: AuthSessionRefreshTokenCookieName(config.client_app_id, secure),
|
|
103
|
+
attrs: { secure },
|
|
104
|
+
};
|
|
105
|
+
return await handle({
|
|
106
|
+
request,
|
|
107
|
+
config,
|
|
108
|
+
cookie,
|
|
109
|
+
refresh_token: readCookie(request.headers.get("cookie"), cookie.name),
|
|
110
|
+
callAuthServer,
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=session-route-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-route-context.js","sourceRoot":"","sources":["../../src/auth-session-route/session-route-context.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3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;SAC1E,CAAC;QACF,MAAM,MAAM,GAAkB;YAC5B,IAAI,EAAE,iCAAiC,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC;YACrE,KAAK,EAAE,EAAE,MAAM,EAAE;SAClB,CAAC;QACF,OAAO,MAAM,MAAM,CAAC;YAClB,OAAO;YACP,MAAM;YACN,MAAM;YACN,aAAa,EAAE,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC;YACrE,cAAc;SACf,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type AuthSessionRouteHandler, type AuthSessionRouteOptions } from "./session-route-context";
|
|
2
|
+
/**
|
|
3
|
+
* The token session route (`POST`): the SDK's authorization code and refresh grants.
|
|
4
|
+
* Forwarded to the auth server's token endpoint; the refresh token goes into
|
|
5
|
+
* the session cookie (a refresh grant presents the cookie's), never into the
|
|
6
|
+
* response body. In Next.js, `app/auth/session/token/route.ts`:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* export const { POST } = createAuthSessionTokenRoute();
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function createAuthSessionTokenRoute(options?: AuthSessionRouteOptions): {
|
|
13
|
+
POST: AuthSessionRouteHandler;
|
|
14
|
+
};
|
|
15
|
+
export default createAuthSessionTokenRoute;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { MaximumBrowserCookieSize, OIDC_TOKEN_REFRESH_TOKEN_DELIVERY_PARAM, getOidcEndpointUrl, } from "@schemavaults/auth-common";
|
|
2
|
+
import getStringByteSize from "../getStringByteSize";
|
|
3
|
+
import { serializeClearedSessionCookie, serializeSessionCookie } from "./session-cookie";
|
|
4
|
+
import { createSessionRoute, json, } from "./session-route-context";
|
|
5
|
+
function oauthError(status, error, error_description) {
|
|
6
|
+
return json(status, { error, error_description });
|
|
7
|
+
}
|
|
8
|
+
async function handleToken({ request, config, cookie, refresh_token, callAuthServer, }) {
|
|
9
|
+
const form = new URLSearchParams(await request.text());
|
|
10
|
+
if (form.get("client_id") !== config.client_app_id) {
|
|
11
|
+
return oauthError(400, "invalid_client", "This session route serves another client.");
|
|
12
|
+
}
|
|
13
|
+
const grant_type = form.get("grant_type");
|
|
14
|
+
// The auth server answers this route inline; the route alone decides
|
|
15
|
+
// where the refresh token goes.
|
|
16
|
+
form.delete(OIDC_TOKEN_REFRESH_TOKEN_DELIVERY_PARAM);
|
|
17
|
+
form.delete("client_secret");
|
|
18
|
+
if (grant_type === "refresh_token") {
|
|
19
|
+
if (!refresh_token) {
|
|
20
|
+
return oauthError(400, "invalid_grant", "No session: sign in again.");
|
|
21
|
+
}
|
|
22
|
+
form.set("refresh_token", refresh_token);
|
|
23
|
+
}
|
|
24
|
+
else if (grant_type === "authorization_code") {
|
|
25
|
+
form.delete("refresh_token");
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return oauthError(400, "unsupported_grant_type", "Only authorization_code and refresh_token grants are served here.");
|
|
29
|
+
}
|
|
30
|
+
const headers = new Headers({
|
|
31
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
32
|
+
Accept: "application/json",
|
|
33
|
+
});
|
|
34
|
+
const origin = request.headers.get("origin");
|
|
35
|
+
if (origin)
|
|
36
|
+
headers.set("Origin", origin);
|
|
37
|
+
let upstream;
|
|
38
|
+
let body;
|
|
39
|
+
try {
|
|
40
|
+
upstream = await callAuthServer(getOidcEndpointUrl(config.auth_server_url, "token"), {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers,
|
|
43
|
+
body: form.toString(),
|
|
44
|
+
});
|
|
45
|
+
const parsed = JSON.parse(await upstream.text());
|
|
46
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
47
|
+
throw new TypeError("token response is not a JSON object");
|
|
48
|
+
}
|
|
49
|
+
body = parsed;
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
console.error("[auth-session-route] Token request to the auth server failed:", e);
|
|
53
|
+
return oauthError(502, "temporarily_unavailable", "The auth server could not be reached.");
|
|
54
|
+
}
|
|
55
|
+
const response_headers = new Headers();
|
|
56
|
+
for (const name of ["WWW-Authenticate", "Retry-After"]) {
|
|
57
|
+
const value = upstream.headers.get(name);
|
|
58
|
+
if (value)
|
|
59
|
+
response_headers.set(name, value);
|
|
60
|
+
}
|
|
61
|
+
if (upstream.ok && typeof body.refresh_token === "string") {
|
|
62
|
+
const issued_refresh_token = body.refresh_token;
|
|
63
|
+
delete body.refresh_token;
|
|
64
|
+
if (getStringByteSize(issued_refresh_token) > MaximumBrowserCookieSize) {
|
|
65
|
+
console.error("[auth-session-route] The refresh token does not fit in a cookie.");
|
|
66
|
+
return oauthError(500, "server_error", "The session could not be stored.");
|
|
67
|
+
}
|
|
68
|
+
const expires_in = typeof body.refresh_token_expires_in === "number" ? body.refresh_token_expires_in : 0;
|
|
69
|
+
response_headers.append("Set-Cookie", serializeSessionCookie(cookie.name, issued_refresh_token, expires_in, cookie.attrs));
|
|
70
|
+
}
|
|
71
|
+
else if (grant_type === "refresh_token" && body.error === "invalid_grant") {
|
|
72
|
+
response_headers.append("Set-Cookie", serializeClearedSessionCookie(cookie.name, cookie.attrs));
|
|
73
|
+
}
|
|
74
|
+
return json(upstream.status, body, response_headers);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The token session route (`POST`): the SDK's authorization code and refresh grants.
|
|
78
|
+
* Forwarded to the auth server's token endpoint; the refresh token goes into
|
|
79
|
+
* the session cookie (a refresh grant presents the cookie's), never into the
|
|
80
|
+
* response body. In Next.js, `app/auth/session/token/route.ts`:
|
|
81
|
+
*
|
|
82
|
+
* ```ts
|
|
83
|
+
* export const { POST } = createAuthSessionTokenRoute();
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function createAuthSessionTokenRoute(options = {}) {
|
|
87
|
+
return { POST: createSessionRoute(options, handleToken) };
|
|
88
|
+
}
|
|
89
|
+
export default createAuthSessionTokenRoute;
|
|
90
|
+
//# sourceMappingURL=token-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-route.js","sourceRoot":"","sources":["../../src/auth-session-route/token-route.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,uCAAuC,EACvC,kBAAkB,GACnB,MAAM,2BAA2B,CAAC;AACnC,OAAO,iBAAiB,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AACzF,OAAO,EAIL,kBAAkB,EAClB,IAAI,GACL,MAAM,yBAAyB,CAAC;AAEjC,SAAS,UAAU,CAAC,MAAc,EAAE,KAAa,EAAE,iBAAyB;IAC1E,OAAO,IAAI,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,iBAAiB,EAAE,CAAC,CAAC;AACpD,CAAC;AAED,KAAK,UAAU,WAAW,CAAC,EACzB,OAAO,EACP,MAAM,EACN,MAAM,EACN,aAAa,EACb,cAAc,GACM;IACpB,MAAM,IAAI,GAAG,IAAI,eAAe,CAAC,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;IACvD,IAAI,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,KAAK,MAAM,CAAC,aAAa,EAAE,CAAC;QACnD,OAAO,UAAU,CAAC,GAAG,EAAE,gBAAgB,EAAE,2CAA2C,CAAC,CAAC;IACxF,CAAC;IACD,MAAM,UAAU,GAAkB,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;IACzD,qEAAqE;IACrE,gCAAgC;IAChC,IAAI,CAAC,MAAM,CAAC,uCAAuC,CAAC,CAAC;IACrD,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IAC7B,IAAI,UAAU,KAAK,eAAe,EAAE,CAAC;QACnC,IAAI,CAAC,aAAa,EAAE,C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E,6BAA6B,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAClG,CAAC;IACD,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,2BAA2B,CACzC,UAAmC,EAAE;IAErC,OAAO,EAAE,IAAI,EAAE,kBAAkB,CAAC,OAAO,EAAE,WAAW,CAAC,EAAE,CAAC;AAC5D,CAAC;AAED,eAAe,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AuthSessionRouteHandler, type AuthSessionRouteOptions } from "./session-route-context";
|
|
2
|
+
/**
|
|
3
|
+
* The whoami session route (`GET`): the signed-in user, from the auth server's whoami
|
|
4
|
+
* with the session's refresh token as a Bearer. In Next.js,
|
|
5
|
+
* `app/auth/session/whoami/route.ts`:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* export const { GET } = createAuthSessionWhoamiRoute();
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare function createAuthSessionWhoamiRoute(options?: AuthSessionRouteOptions): {
|
|
12
|
+
GET: AuthSessionRouteHandler;
|
|
13
|
+
};
|
|
14
|
+
export default createAuthSessionWhoamiRoute;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { serializeClearedSessionCookie } from "./session-cookie";
|
|
2
|
+
import { authServerAppEndpoint, bearerHeaders, createSessionRoute, json, noStoreHeaders, } from "./session-route-context";
|
|
3
|
+
async function handleWhoami({ request, config, cookie, refresh_token, callAuthServer, }) {
|
|
4
|
+
if (!refresh_token) {
|
|
5
|
+
return json(401, { success: false, error: "unauthorized", message: "Authentication required" });
|
|
6
|
+
}
|
|
7
|
+
let upstream;
|
|
8
|
+
let text;
|
|
9
|
+
try {
|
|
10
|
+
upstream = await callAuthServer(authServerAppEndpoint(config, "whoami"), {
|
|
11
|
+
method: "GET",
|
|
12
|
+
headers: bearerHeaders(request, refresh_token),
|
|
13
|
+
});
|
|
14
|
+
text = await upstream.text();
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
console.error("[auth-session-route] whoami request to the auth server failed:", e);
|
|
18
|
+
return json(502, { success: false, error: "bad_gateway", message: "The auth server could not be reached." });
|
|
19
|
+
}
|
|
20
|
+
const response_headers = noStoreHeaders({
|
|
21
|
+
"Content-Type": upstream.headers.get("content-type") ?? "application/json",
|
|
22
|
+
});
|
|
23
|
+
if (upstream.status === 401) {
|
|
24
|
+
// The refresh token no longer verifies (revoked, expired): the session is over.
|
|
25
|
+
response_headers.append("Set-Cookie", serializeClearedSessionCookie(cookie.name, cookie.attrs));
|
|
26
|
+
}
|
|
27
|
+
return new Response(text, { status: upstream.status, headers: response_headers });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The whoami session route (`GET`): the signed-in user, from the auth server's whoami
|
|
31
|
+
* with the session's refresh token as a Bearer. In Next.js,
|
|
32
|
+
* `app/auth/session/whoami/route.ts`:
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* export const { GET } = createAuthSessionWhoamiRoute();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function createAuthSessionWhoamiRoute(options = {}) {
|
|
39
|
+
return { GET: createSessionRoute(options, handleWhoami) };
|
|
40
|
+
}
|
|
41
|
+
export default createAuthSessionWhoamiRoute;
|
|
42
|
+
//# sourceMappingURL=whoami-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whoami-route.js","sourceRoot":"","sources":["../../src/auth-session-route/whoami-route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAIL,qBAAqB,EACrB,aAAa,EACb,kBAAkB,EAClB,IAAI,EACJ,cAAc,GACf,MAAM,yBAAyB,CAAC;AAEjC,KA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OAAO,EAAE,gBAAgB,EAAE,CAAC,CAAC;AACpF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,4BAA4B,CAC1C,UAAmC,EAAE;IAErC,OAAO,EAAE,GAAG,EAAE,kBAAkB,CAAC,OAAO,EAAE,YAAY,CAAC,EAAE,CAAC;AAC5D,CAAC;AAED,eAAe,4BAA4B,CAAC"}
|
package/dist/cli.cjs
CHANGED
|
@@ -89,7 +89,7 @@ var init_resolve_codegen_templates_directory = __esm({
|
|
|
89
89
|
|
|
90
90
|
// src/NextjsAppDirectoryPlugin/codegen-marker.ts
|
|
91
91
|
function getCodegenMarkerComment() {
|
|
92
|
-
const version = true ? "0.
|
|
92
|
+
const version = true ? "0.33.4" : "unknown";
|
|
93
93
|
return `${CODEGEN_MARKER_PREFIX}${version}`;
|
|
94
94
|
}
|
|
95
95
|
function hasCodegenMarker(firstLine) {
|
|
@@ -136,9 +136,9 @@ function formatVersionTransition(oldVersion) {
|
|
|
136
136
|
return "";
|
|
137
137
|
}
|
|
138
138
|
function createClientPages(authDirectory, templatesDir) {
|
|
139
|
-
for (const page of
|
|
140
|
-
const destPath = (0, import_path4.join)(authDirectory, page.
|
|
141
|
-
const relPath = `auth/${page.
|
|
139
|
+
for (const page of filesToCreate) {
|
|
140
|
+
const destPath = (0, import_path4.join)(authDirectory, page.auth_relative_file);
|
|
141
|
+
const relPath = `auth/${page.auth_relative_file}`;
|
|
142
142
|
if ((0, import_fs3.existsSync)(destPath)) {
|
|
143
143
|
if (isCodegenManagedFile(destPath)) {
|
|
144
144
|
const oldVersion = getExistingVersion(destPath);
|
|
@@ -253,7 +253,7 @@ async function codegen(opts) {
|
|
|
253
253
|
`[@schemavaults/auth-server-sdk/NextjsAppDirectoryPlugin] Codegen complete.`
|
|
254
254
|
);
|
|
255
255
|
}
|
|
256
|
-
var import_fs3, import_path3, import_process2, import_path4,
|
|
256
|
+
var import_fs3, import_path3, import_process2, import_path4, filesToCreate;
|
|
257
257
|
var init_codegen = __esm({
|
|
258
258
|
"src/NextjsAppDirectoryPlugin/codegen.ts"() {
|
|
259
259
|
"use strict";
|
|
@@ -264,26 +264,40 @@ var init_codegen = __esm({
|
|
|
264
264
|
import_path4 = require("path");
|
|
265
265
|
init_resolve_codegen_templates_directory();
|
|
266
266
|
init_codegen_marker();
|
|
267
|
-
|
|
267
|
+
filesToCreate = [
|
|
268
268
|
{
|
|
269
|
-
|
|
269
|
+
auth_relative_file: "login/page.tsx",
|
|
270
270
|
codegen_template_path: "auth/login/page.tsx"
|
|
271
271
|
},
|
|
272
272
|
{
|
|
273
|
-
|
|
273
|
+
auth_relative_file: "register/page.tsx",
|
|
274
274
|
codegen_template_path: "auth/register/page.tsx"
|
|
275
275
|
},
|
|
276
276
|
{
|
|
277
|
-
|
|
277
|
+
auth_relative_file: "logout/page.tsx",
|
|
278
278
|
codegen_template_path: "auth/logout/page.tsx"
|
|
279
279
|
},
|
|
280
280
|
{
|
|
281
|
-
|
|
281
|
+
auth_relative_file: "authorize/page.tsx",
|
|
282
282
|
codegen_template_path: "auth/authorize/page.tsx"
|
|
283
283
|
},
|
|
284
284
|
{
|
|
285
|
-
|
|
285
|
+
auth_relative_file: "error/page.tsx",
|
|
286
286
|
codegen_template_path: "auth/error/page.tsx"
|
|
287
|
+
},
|
|
288
|
+
// The auth session routes: keep the refresh token in an HTTP-only cookie
|
|
289
|
+
// on the app's own domain (see auth-provider.tsx).
|
|
290
|
+
{
|
|
291
|
+
auth_relative_file: "session/token/route.ts",
|
|
292
|
+
codegen_template_path: "auth/session/token/route.ts"
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
auth_relative_file: "session/whoami/route.ts",
|
|
296
|
+
codegen_template_path: "auth/session/whoami/route.ts"
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
auth_relative_file: "session/logout/route.ts",
|
|
300
|
+
codegen_template_path: "auth/session/logout/route.ts"
|
|
287
301
|
}
|
|
288
302
|
];
|
|
289
303
|
}
|
|
@@ -331,7 +345,7 @@ async function main() {
|
|
|
331
345
|
return;
|
|
332
346
|
}
|
|
333
347
|
if (args.includes("--version") || args.includes("-v")) {
|
|
334
|
-
console.log(`${PACKAGE_NAME}@${"0.
|
|
348
|
+
console.log(`${PACKAGE_NAME}@${"0.33.4"}`);
|
|
335
349
|
return;
|
|
336
350
|
}
|
|
337
351
|
const command = args.find((arg) => !arg.startsWith("-")) ?? "codegen";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
+
AUTH_SESSION_ROUTE_PATHS,
|
|
5
|
+
type AuthSessionRoutes,
|
|
4
6
|
AuthProvider,
|
|
5
7
|
type SchemaVaultsAppEnvironment,
|
|
6
8
|
type AppId,
|
|
@@ -28,6 +30,14 @@ export interface IAppAuthProviderProps extends PropsWithChildren {
|
|
|
28
30
|
successful_logout_redirect_uri: string;
|
|
29
31
|
successful_authentication_redirect_uri: string;
|
|
30
32
|
authorize_uri: string;
|
|
33
|
+
/**
|
|
34
|
+
* The app's auth session routes, each by its own path (generated at
|
|
35
|
+
* `auth/session/{token,whoami,logout}/route.ts`), which keep the refresh
|
|
36
|
+
* token in an HTTP-only cookie on the app's own domain. Defaults to
|
|
37
|
+
* `AUTH_SESSION_ROUTE_PATHS`; `null` keeps the refresh token in the
|
|
38
|
+
* browser instead (not recommended).
|
|
39
|
+
*/
|
|
40
|
+
session_routes?: AuthSessionRoutes | null;
|
|
31
41
|
autoreacquire_access_tokens?: boolean;
|
|
32
42
|
authMiddlewareRules?: SchemaVaultsAuthProviderProps["authMiddlewareRules"];
|
|
33
43
|
onLogout?: SchemaVaultsAuthProviderProps["onLogout"];
|
|
@@ -58,6 +68,11 @@ export default function AppAuthProvider({
|
|
|
58
68
|
props.successful_authentication_redirect_uri
|
|
59
69
|
}
|
|
60
70
|
authorize_uri={props.authorize_uri}
|
|
71
|
+
session_routes={
|
|
72
|
+
props.session_routes === null
|
|
73
|
+
? undefined
|
|
74
|
+
: (props.session_routes ?? AUTH_SESSION_ROUTE_PATHS)
|
|
75
|
+
}
|
|
61
76
|
router={router}
|
|
62
77
|
path={path}
|
|
63
78
|
default_audiences={props.default_audiences}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createAuthSessionLogoutRoute } from "@schemavaults/auth-server-sdk/auth-session-route";
|
|
2
|
+
|
|
3
|
+
// POST /auth/session/logout — revokes the session at the auth server and
|
|
4
|
+
// clears this app's session cookie (see ../token/route.ts).
|
|
5
|
+
export const { POST } = createAuthSessionLogoutRoute();
|
|
6
|
+
|
|
7
|
+
export const dynamic = "force-dynamic";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createAuthSessionTokenRoute } from "@schemavaults/auth-server-sdk/auth-session-route";
|
|
2
|
+
|
|
3
|
+
// POST /auth/session/token — one of the app's auth session routes (with
|
|
4
|
+
// ../whoami and ../logout). The auth provider sends the SDK's token requests
|
|
5
|
+
// here; this route forwards them to the auth server and keeps the refresh
|
|
6
|
+
// token in an HTTP-only cookie on this app's own domain: page scripts can
|
|
7
|
+
// never read it, and as a first-party cookie it works in browsers that block
|
|
8
|
+
// third-party cookies (Safari, every iOS browser).
|
|
9
|
+
//
|
|
10
|
+
// Reads SCHEMAVAULTS_CLIENT_APP_ID, SCHEMAVAULTS_APP_ENVIRONMENT and
|
|
11
|
+
// (optionally) SCHEMAVAULTS_AUTH_SERVER_URL.
|
|
12
|
+
export const { POST } = createAuthSessionTokenRoute();
|
|
13
|
+
|
|
14
|
+
export const dynamic = "force-dynamic";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createAuthSessionWhoamiRoute } from "@schemavaults/auth-server-sdk/auth-session-route";
|
|
2
|
+
|
|
3
|
+
// GET /auth/session/whoami — the signed-in user, loaded from the auth
|
|
4
|
+
// server with the refresh token this app's session cookie holds (see
|
|
5
|
+
// ../token/route.ts).
|
|
6
|
+
export const { GET } = createAuthSessionWhoamiRoute();
|
|
7
|
+
|
|
8
|
+
export const dynamic = "force-dynamic";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schemavaults/auth-server-sdk",
|
|
3
3
|
"description": "TypeScript SDK for building authenticated endpoints/middlewares for the Auth Server and Resource Servers",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.33.4",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
|
7
7
|
"repository": {
|
|
@@ -157,6 +157,11 @@
|
|
|
157
157
|
"types": "./dist/openapi-operations/index.d.ts",
|
|
158
158
|
"import": "./dist/openapi-operations/index.js",
|
|
159
159
|
"require": "./dist/openapi-operations/index.js"
|
|
160
|
+
},
|
|
161
|
+
"./auth-session-route": {
|
|
162
|
+
"types": "./dist/auth-session-route/index.d.ts",
|
|
163
|
+
"import": "./dist/auth-session-route/index.js",
|
|
164
|
+
"require": "./dist/auth-session-route/index.js"
|
|
160
165
|
}
|
|
161
166
|
},
|
|
162
167
|
"peerDependencies": {
|