@schemavaults/auth-server-sdk 0.43.1 → 0.44.2

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 CHANGED
@@ -386,6 +386,50 @@ await revokeToken({ ...client, token: delegated.access_token });
386
386
  resolves for tokens the auth server does not know.
387
387
  - Keep the client secret out of the agent's sandbox: only the delegated token goes in.
388
388
 
389
+ ## Signing users in on devices (confidential clients)
390
+
391
+ A confidential client's backend that drives a device without a browser (a TV
392
+ app, a kiosk, an appliance) signs its user in with the RFC 8628 device
393
+ authorization grant, authenticating with its client secret. The app's
394
+ managers must enable **Device sign-in** on its page first (else
395
+ `OAuthEndpointError` `error: "unauthorized_client"`).
396
+
397
+ ```ts
398
+ import {
399
+ startDeviceAuthorization,
400
+ pollDeviceAuthorization,
401
+ OAuthEndpointError,
402
+ } from "@schemavaults/auth-server-sdk/confidential-client";
403
+
404
+ const authorization = await startDeviceAuthorization({
405
+ ...client, // auth_server_url, client_id, client_secret
406
+ scope: [oidcScopes.openid, oidcScopes.email],
407
+ });
408
+ showOnDevice(authorization.user_code, authorization.verification_uri);
409
+
410
+ try {
411
+ const tokens = await pollDeviceAuthorization({
412
+ ...client,
413
+ device_code: authorization.device_code, // keep it on the backend
414
+ interval: authorization.interval,
415
+ });
416
+ tokens.access_token; tokens.refresh_token; tokens.id_token; // id_token with openid
417
+ } catch (e) {
418
+ if (e instanceof OAuthEndpointError && e.error === "access_denied") { /* the user said no */ }
419
+ if (e instanceof OAuthEndpointError && e.error === "expired_token") { /* start over */ }
420
+ throw e;
421
+ }
422
+ ```
423
+
424
+ - The user enters the code on the auth server's `/device` page, signs in and
425
+ approves or denies the request; the codes expire after 15 minutes.
426
+ - `pollDeviceAuthorization()` waits `interval` seconds between polls and 5 more
427
+ after every `slow_down`. A backend that schedules polls itself calls
428
+ `requestDeviceAccessToken()` once per poll instead: it resolves with
429
+ `{ status: "authorization_pending" | "slow_down" }` or `{ status: "tokens", tokens }`.
430
+ - Public clients (CLIs) use `client.signInWithDeviceAuthorization()` in
431
+ `@schemavaults/auth-client-sdk`.
432
+
389
433
  ## `UserData.sub`
390
434
 
391
435
  `UserData.sub` is the OIDC subject `<auth_server_app_id>|<uid>` (the value the
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 version2 = true ? "0.43.1" : "unknown";
92
+ const version2 = true ? "0.44.2" : "unknown";
93
93
  return `${CODEGEN_MARKER_PREFIX}${version2}`;
94
94
  }
95
95
  function hasCodegenMarker(firstLine) {
@@ -20880,6 +20880,26 @@ var oidcNonceSchema = external_exports.string().min(1).max(MAX_OIDC_NONCE_LENGTH
20880
20880
  message: "OIDC 'nonce' must contain only printable ASCII characters"
20881
20881
  });
20882
20882
 
20883
+ // ../auth-common/dist/oidc/device-authorization/constants.js
20884
+ var DEVICE_AUTHORIZATION_EXPIRES_IN_SECONDS = 15 * 60;
20885
+ var DEVICE_AUTHORIZATION_DEFAULT_INTERVAL_SECONDS = 5;
20886
+
20887
+ // ../auth-common/dist/oidc/device-authorization/user-code.js
20888
+ var USER_CODE_ALPHABET = "BCDFGHJKLMNPQRSTVWXZ";
20889
+ var USER_CODE_LENGTH = 8;
20890
+ var USER_CODE_REGEX = new RegExp(`^[${USER_CODE_ALPHABET}]{${USER_CODE_LENGTH}}$`);
20891
+ var UNBIASED_BYTE_LIMIT = Math.floor(256 / USER_CODE_ALPHABET.length) * USER_CODE_ALPHABET.length;
20892
+
20893
+ // ../auth-common/dist/oidc/device-authorization/device-authorization-response.js
20894
+ var deviceAuthorizationResponseSchema = external_exports.looseObject({
20895
+ device_code: external_exports.string().min(1),
20896
+ user_code: external_exports.string().min(1),
20897
+ verification_uri: external_exports.url(),
20898
+ verification_uri_complete: external_exports.url().optional(),
20899
+ expires_in: external_exports.number().int().positive(),
20900
+ interval: external_exports.number().int().positive().optional().default(DEVICE_AUTHORIZATION_DEFAULT_INTERVAL_SECONDS)
20901
+ });
20902
+
20883
20903
  // ../auth-common/dist/oidc/token-endpoint-extensions.js
20884
20904
  var OIDC_REFRESH_TOKEN_DELIVERY_MODES = [
20885
20905
  /** RFC 6749 §5.1 default: `refresh_token` inlined in the JSON body. */
@@ -23458,7 +23478,7 @@ async function main() {
23458
23478
  return;
23459
23479
  }
23460
23480
  if (args.includes("--version") || args.includes("-v")) {
23461
- console.log(`${PACKAGE_NAME}@${"0.43.1"}`);
23481
+ console.log(`${PACKAGE_NAME}@${"0.44.2"}`);
23462
23482
  return;
23463
23483
  }
23464
23484
  const command = findCommand(args, FLAGS_WITH_VALUES) ?? "codegen";
@@ -1,12 +1,18 @@
1
1
  /**
2
2
  * Calls a confidential client application's backend makes to the auth
3
3
  * server with its client secret: minting delegated tokens for the agents it
4
- * runs for its users (RFC 8693 token exchange) and revoking tokens issued to
5
- * it (RFC 7009). Server-side only: never ship the client secret to a browser
6
- * or into an agent's sandbox.
4
+ * runs for its users (RFC 8693 token exchange), signing users in on devices
5
+ * it drives (RFC 8628 device authorization grant) and revoking tokens issued
6
+ * to it (RFC 7009). Server-side only: never ship the client secret to a
7
+ * browser or into an agent's sandbox.
7
8
  */
8
9
  export { exchangeTokenForDelegatedToken, delegatedTokenResponseSchema, } from "./exchange-token-for-delegated-token";
9
10
  export type { DelegatedTokenResponse, IExchangeTokenForDelegatedTokenOptions, } from "./exchange-token-for-delegated-token";
11
+ export { startDeviceAuthorization } from "./start-device-authorization";
12
+ export type { IStartDeviceAuthorizationOptions } from "./start-device-authorization";
13
+ export { pollDeviceAuthorization, requestDeviceAccessToken, deviceAccessTokenResponseSchema, } from "./poll-device-authorization";
14
+ export type { DeviceAccessTokenPollResult, DeviceAccessTokenResponse, IPollDeviceAuthorizationOptions, IRequestDeviceAccessTokenOptions, } from "./poll-device-authorization";
15
+ export type { DeviceAuthorizationResponse } from "@schemavaults/auth-common";
10
16
  export { revokeToken } from "./revoke-token";
11
17
  export type { IRevokeTokenOptions } from "./revoke-token";
12
18
  export { OAuthEndpointError, clientSecretBasicAuthorization } from "./shared";
@@ -1,11 +1,14 @@
1
1
  /**
2
2
  * Calls a confidential client application's backend makes to the auth
3
3
  * server with its client secret: minting delegated tokens for the agents it
4
- * runs for its users (RFC 8693 token exchange) and revoking tokens issued to
5
- * it (RFC 7009). Server-side only: never ship the client secret to a browser
6
- * or into an agent's sandbox.
4
+ * runs for its users (RFC 8693 token exchange), signing users in on devices
5
+ * it drives (RFC 8628 device authorization grant) and revoking tokens issued
6
+ * to it (RFC 7009). Server-side only: never ship the client secret to a
7
+ * browser or into an agent's sandbox.
7
8
  */
8
9
  export { exchangeTokenForDelegatedToken, delegatedTokenResponseSchema, } from "./exchange-token-for-delegated-token";
10
+ export { startDeviceAuthorization } from "./start-device-authorization";
11
+ export { pollDeviceAuthorization, requestDeviceAccessToken, deviceAccessTokenResponseSchema, } from "./poll-device-authorization";
9
12
  export { revokeToken } from "./revoke-token";
10
13
  export { OAuthEndpointError, clientSecretBasicAuthorization } from "./shared";
11
14
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/confidential-client/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,8BAA8B,EAC9B,4BAA4B,GAC7B,MAAM,sCAAsC,CAAC;AAM9C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,OAAO,EAAE,kBAAkB,EAAE,8BAA8B,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/confidential-client/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,8BAA8B,EAC9B,4BAA4B,GAC7B,MAAM,sCAAsC,CAAC;AAM9C,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAGxE,OAAO,EACL,uBAAuB,EACvB,wBAAwB,EACxB,+BAA+B,GAChC,MAAM,6BAA6B,CAAC;AASrC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,OAAO,EAAE,kBAAkB,EAAE,8BAA8B,EAAE,MAAM,UAAU,CAAC"}
@@ -0,0 +1,66 @@
1
+ import { z } from "zod";
2
+ import { type ConfidentialClientCredentials } from "./shared";
3
+ /** The token response of a redeemed device code (RFC 6749 §5.1). */
4
+ export declare const deviceAccessTokenResponseSchema: z.ZodObject<{
5
+ access_token: z.ZodString;
6
+ token_type: z.ZodLiteral<"Bearer">;
7
+ expires_in: z.ZodNumber;
8
+ refresh_token: z.ZodOptional<z.ZodString>;
9
+ refresh_token_expires_in: z.ZodOptional<z.ZodNumber>;
10
+ scope: z.ZodOptional<z.ZodString>;
11
+ id_token: z.ZodOptional<z.ZodString>;
12
+ }, z.core.$loose>;
13
+ export type DeviceAccessTokenResponse = z.infer<typeof deviceAccessTokenResponseSchema>;
14
+ export interface IRequestDeviceAccessTokenOptions extends ConfidentialClientCredentials {
15
+ /** The `device_code` of `startDeviceAuthorization()`. */
16
+ device_code: string;
17
+ /** RFC 8707 resource indicator: the API server to mint the access token for. */
18
+ resource?: string;
19
+ }
20
+ /** One poll's answer, short of tokens. */
21
+ export type DeviceAccessTokenPollResult = {
22
+ status: "tokens";
23
+ tokens: DeviceAccessTokenResponse;
24
+ }
25
+ /** The user has not decided yet: poll again after the interval. */
26
+ | {
27
+ status: "authorization_pending";
28
+ }
29
+ /** As pending, but polled too fast: wait 5 more seconds from now on. */
30
+ | {
31
+ status: "slow_down";
32
+ };
33
+ /**
34
+ * Polls the token endpoint ONCE with a device code (RFC 8628 §3.4), for
35
+ * backends that schedule the polls themselves. Waiting between polls is the
36
+ * caller's job: at least `interval` seconds, plus 5 more after every
37
+ * `slow_down`.
38
+ *
39
+ * @throws {OAuthEndpointError} when no tokens will come: `access_denied` (the
40
+ * user denied the request), `expired_token` (the code expired), or another
41
+ * RFC 6749 §5.2 code (`invalid_grant`: unknown or already redeemed;
42
+ * `unauthorized_client`: the grant was disabled for the app).
43
+ */
44
+ export declare function requestDeviceAccessToken({ device_code, resource, ...credentials }: IRequestDeviceAccessTokenOptions): Promise<DeviceAccessTokenPollResult>;
45
+ export interface IPollDeviceAuthorizationOptions extends IRequestDeviceAccessTokenOptions {
46
+ /** The `interval` of the device authorization response (default 5 seconds). */
47
+ interval?: number;
48
+ /**
49
+ * Stops polling (e.g. when the user cancels on the device). Without one,
50
+ * polling ends when the auth server answers `expired_token`.
51
+ */
52
+ signal?: AbortSignal;
53
+ }
54
+ /**
55
+ * Polls the token endpoint with a device code until the user approves the
56
+ * request on the auth server's `/device` page (RFC 8628 §3.4-3.5), waiting
57
+ * `interval` seconds between polls and 5 more after every `slow_down`.
58
+ * Resolves with the user's tokens: an access token, a refresh token, and an
59
+ * id_token when the approved scope includes `openid`.
60
+ *
61
+ * @throws {OAuthEndpointError} `access_denied` when the user denies the
62
+ * request, `expired_token` when it expires first, or another RFC 6749 §5.2
63
+ * code. An aborted `signal` rejects with its reason.
64
+ */
65
+ export declare function pollDeviceAuthorization({ interval, signal, ...request }: IPollDeviceAuthorizationOptions): Promise<DeviceAccessTokenResponse>;
66
+ export default pollDeviceAuthorization;
@@ -0,0 +1,101 @@
1
+ import { DEVICE_AUTHORIZATION_DEFAULT_INTERVAL_SECONDS, DEVICE_AUTHORIZATION_SLOW_DOWN_INCREMENT_SECONDS, OAUTH_DEVICE_CODE_GRANT_TYPE, OIDC_TOKEN_RESOURCE_PARAM, getOidcEndpointUrl, } from "@schemavaults/auth-common";
2
+ import { z } from "zod";
3
+ import { OAuthEndpointError, postAsConfidentialClient, } from "./shared";
4
+ /** The token response of a redeemed device code (RFC 6749 §5.1). */
5
+ export const deviceAccessTokenResponseSchema = z.looseObject({
6
+ access_token: z.string().min(1),
7
+ token_type: z.literal("Bearer"),
8
+ expires_in: z.number().int().positive(),
9
+ refresh_token: z.string().min(1).optional(),
10
+ /** @schemavaults/auth-server extension: seconds until the refresh token expires. */
11
+ refresh_token_expires_in: z.number().int().positive().optional(),
12
+ scope: z.string().optional(),
13
+ /** Present when the approved scope includes `openid`. */
14
+ id_token: z.string().min(1).optional(),
15
+ });
16
+ /**
17
+ * Polls the token endpoint ONCE with a device code (RFC 8628 §3.4), for
18
+ * backends that schedule the polls themselves. Waiting between polls is the
19
+ * caller's job: at least `interval` seconds, plus 5 more after every
20
+ * `slow_down`.
21
+ *
22
+ * @throws {OAuthEndpointError} when no tokens will come: `access_denied` (the
23
+ * user denied the request), `expired_token` (the code expired), or another
24
+ * RFC 6749 §5.2 code (`invalid_grant`: unknown or already redeemed;
25
+ * `unauthorized_client`: the grant was disabled for the app).
26
+ */
27
+ export async function requestDeviceAccessToken({ device_code, resource, ...credentials }) {
28
+ if (typeof device_code !== "string" || device_code.length === 0) {
29
+ throw new TypeError("Expected a device_code to poll with!");
30
+ }
31
+ const form = new URLSearchParams({ grant_type: OAUTH_DEVICE_CODE_GRANT_TYPE, device_code });
32
+ if (resource !== undefined) {
33
+ form.set(OIDC_TOKEN_RESOURCE_PARAM, resource);
34
+ }
35
+ let response;
36
+ try {
37
+ response = await postAsConfidentialClient(credentials, "token", getOidcEndpointUrl(credentials.auth_server_url, "token"), form);
38
+ }
39
+ catch (e) {
40
+ if (e instanceof OAuthEndpointError) {
41
+ if (e.error === "authorization_pending")
42
+ return { status: "authorization_pending" };
43
+ if (e.error === "slow_down")
44
+ return { status: "slow_down" };
45
+ }
46
+ throw e;
47
+ }
48
+ const parsed = deviceAccessTokenResponseSchema.safeParse(await response.json());
49
+ if (!parsed.success) {
50
+ throw new Error("Received an unexpected token response from the auth server", {
51
+ cause: parsed.error,
52
+ });
53
+ }
54
+ return { status: "tokens", tokens: parsed.data };
55
+ }
56
+ function wait(seconds, signal) {
57
+ return new Promise((resolve, reject) => {
58
+ if (signal?.aborted) {
59
+ reject(signal.reason);
60
+ return;
61
+ }
62
+ const timer = setTimeout(() => {
63
+ signal?.removeEventListener("abort", onAbort);
64
+ resolve();
65
+ }, seconds * 1000);
66
+ function onAbort() {
67
+ clearTimeout(timer);
68
+ reject(signal?.reason);
69
+ }
70
+ signal?.addEventListener("abort", onAbort, { once: true });
71
+ });
72
+ }
73
+ /**
74
+ * Polls the token endpoint with a device code until the user approves the
75
+ * request on the auth server's `/device` page (RFC 8628 §3.4-3.5), waiting
76
+ * `interval` seconds between polls and 5 more after every `slow_down`.
77
+ * Resolves with the user's tokens: an access token, a refresh token, and an
78
+ * id_token when the approved scope includes `openid`.
79
+ *
80
+ * @throws {OAuthEndpointError} `access_denied` when the user denies the
81
+ * request, `expired_token` when it expires first, or another RFC 6749 §5.2
82
+ * code. An aborted `signal` rejects with its reason.
83
+ */
84
+ export async function pollDeviceAuthorization({ interval = DEVICE_AUTHORIZATION_DEFAULT_INTERVAL_SECONDS, signal, ...request }) {
85
+ if (!Number.isFinite(interval) || interval <= 0) {
86
+ throw new TypeError("Expected 'interval' to be a positive number of seconds!");
87
+ }
88
+ let current_interval = interval;
89
+ for (;;) {
90
+ await wait(current_interval, signal);
91
+ const result = await requestDeviceAccessToken(request);
92
+ if (result.status === "tokens") {
93
+ return result.tokens;
94
+ }
95
+ if (result.status === "slow_down") {
96
+ current_interval += DEVICE_AUTHORIZATION_SLOW_DOWN_INCREMENT_SECONDS;
97
+ }
98
+ }
99
+ }
100
+ export default pollDeviceAuthorization;
101
+ //# sourceMappingURL=poll-device-authorization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"poll-device-authorization.js","sourceRoot":"","sources":["../../src/confidential-client/poll-device-authorization.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6CAA6C,EAC7C,gDAAgD,EAChD,4BAA4B,EAC5B,yBAAyB,EACzB,kBAAkB,GACnB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,kBAAkB,EAElB,wBAAwB,GACzB,MAAM,UAAU,CAAC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AC,CAAC;YACpB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACzB,CAAC;QACD,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAC,EAC5C,QAAQ,GAAG,6CAA6C,EACxD,MAAM,EACN,GAAG,OAAO,EACsB;IAChC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,SAAS,CAAC,yDAAyD,CAAC,CAAC;IACjF,CAAC;IACD,IAAI,gBAAgB,GAAW,QAAQ,CAAC;IACxC,SAAS,CAAC;QACR,MAAM,IAAI,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;QACrC,MAAM,MAAM,GAAG,MAAM,wBAAwB,CAAC,OAAO,CAAC,CAAC;QACvD,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,OAAO,MAAM,CAAC,MAAM,CAAC;QACvB,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;YAClC,gBAAgB,IAAI,gDAAgD,CAAC;QACvE,CAAC;IACH,CAAC;AACH,CAAC;AAED,eAAe,uBAAuB,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { type DeviceAuthorizationResponse, type ScopeInput } from "@schemavaults/auth-common";
2
+ import { type ConfidentialClientCredentials } from "./shared";
3
+ export interface IStartDeviceAuthorizationOptions extends ConfidentialClientCredentials {
4
+ /**
5
+ * The scope to ask for: space-delimited, or a list of scope values and
6
+ * definitions (`[oidcScopes.openid, notesScopes.read]`). With `openid` the
7
+ * device's tokens come with an id_token. Omit to ask for none (a plain
8
+ * OAuth 2.0 grant).
9
+ */
10
+ scope?: ScopeInput;
11
+ /**
12
+ * RFC 8707 resource indicator naming the API server whose API scopes
13
+ * `scope` asks for (an API server id or the URL of one of its domains).
14
+ */
15
+ resource?: string;
16
+ }
17
+ /**
18
+ * Starts an OAuth 2.0 device authorization (RFC 8628 §3.1) as a
19
+ * confidential client, for a device this backend drives (a TV app, a kiosk,
20
+ * an appliance): the auth server answers with a `device_code` for this
21
+ * backend to poll with (`pollDeviceAuthorization()` or
22
+ * `requestDeviceAccessToken()`), and a `user_code` + `verification_uri` to
23
+ * show on the device. The user enters the code on the auth server's
24
+ * `/device` page and approves the request there.
25
+ *
26
+ * The app's managers must have enabled the device authorization grant on the
27
+ * auth server (else `unauthorized_client`). Keep the `device_code` on the
28
+ * backend: whoever holds it receives the user's tokens once they approve.
29
+ *
30
+ * @throws {OAuthEndpointError} when the auth server refuses the request
31
+ * (`unauthorized_client`, `invalid_scope`, `invalid_target`, `invalid_client`).
32
+ */
33
+ export declare function startDeviceAuthorization({ scope, resource, ...credentials }: IStartDeviceAuthorizationOptions): Promise<DeviceAuthorizationResponse>;
34
+ export default startDeviceAuthorization;
@@ -0,0 +1,41 @@
1
+ import { OIDC_TOKEN_RESOURCE_PARAM, deviceAuthorizationResponseSchema, formatScope, getOidcEndpointUrl, } from "@schemavaults/auth-common";
2
+ import { postAsConfidentialClient, } from "./shared";
3
+ /**
4
+ * Starts an OAuth 2.0 device authorization (RFC 8628 §3.1) as a
5
+ * confidential client, for a device this backend drives (a TV app, a kiosk,
6
+ * an appliance): the auth server answers with a `device_code` for this
7
+ * backend to poll with (`pollDeviceAuthorization()` or
8
+ * `requestDeviceAccessToken()`), and a `user_code` + `verification_uri` to
9
+ * show on the device. The user enters the code on the auth server's
10
+ * `/device` page and approves the request there.
11
+ *
12
+ * The app's managers must have enabled the device authorization grant on the
13
+ * auth server (else `unauthorized_client`). Keep the `device_code` on the
14
+ * backend: whoever holds it receives the user's tokens once they approve.
15
+ *
16
+ * @throws {OAuthEndpointError} when the auth server refuses the request
17
+ * (`unauthorized_client`, `invalid_scope`, `invalid_target`, `invalid_client`).
18
+ */
19
+ export async function startDeviceAuthorization({ scope, resource, ...credentials }) {
20
+ if (resource !== undefined && (typeof resource !== "string" || resource.length === 0)) {
21
+ throw new TypeError("Expected 'resource' to be a non-empty string when set!");
22
+ }
23
+ const form = new URLSearchParams();
24
+ if (scope !== undefined) {
25
+ const serialized = formatScope(scope);
26
+ if (serialized.length > 0) {
27
+ form.set("scope", serialized);
28
+ }
29
+ }
30
+ if (resource !== undefined) {
31
+ form.set(OIDC_TOKEN_RESOURCE_PARAM, resource);
32
+ }
33
+ const response = await postAsConfidentialClient(credentials, "device authorization", getOidcEndpointUrl(credentials.auth_server_url, "device_authorization"), form);
34
+ const parsed = deviceAuthorizationResponseSchema.safeParse(await response.json());
35
+ if (!parsed.success) {
36
+ throw new Error("Received an unexpected device authorization response from the auth server", { cause: parsed.error });
37
+ }
38
+ return parsed.data;
39
+ }
40
+ export default startDeviceAuthorization;
41
+ //# sourceMappingURL=start-device-authorization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-device-authorization.js","sourceRoot":"","sources":["../../src/confidential-client/start-device-authorization.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,yBAAyB,EACzB,iCAAiC,EACjC,WAAW,EACX,kBAAkB,GAGnB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,wBAAwB,GACzB,MAAM,UAAU,CAAC;AAkBlB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,EAC7C,KAAK,EACL,QAAQ,EACR,GAAG,WAAW,EACmB;IACjC,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC;QACtF,MAAM,IAAI,SAAS,CAAC,wDAAwD,CAAC,CAAC;IAChF,CAAC;IACD,MAAM,IAAI,GAAG,IAAI,eAAe,EAAE,CAAC;IACnC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,UAAU,GAAW,WAAW,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IACD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,IAAI,CAAC,GAAG,CAAC,yBAAyB,EAAE,QAAQ,CAAC,CAAC;IAChD,CAAC;IAED,MAAM,QAAQ,GAAa,MAAM,wBAAwB,CACvD,WAAW,EACX,sBAAsB,EACtB,kBAAkB,CAAC,WAAW,CAAC,eAAe,EAAE,sBAAsB,CAAC,EACvE,IAAI,CACL,CAAC;IACF,MAAM,MAAM,GAAG,iCAAiC,CAAC,SAAS,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAClF,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CACb,2EAA2E,EAC3E,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CACxB,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC;AACrB,CAAC;AAED,eAAe,wBAAwB,CAAC"}
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.43.1",
4
+ "version": "0.44.2",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
7
7
  "repository": {
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "zod": "4.6.7",
21
- "@schemavaults/jwt": "0.17.4",
22
- "@schemavaults/auth-common": "0.40.1",
21
+ "@schemavaults/jwt": "0.17.5",
22
+ "@schemavaults/auth-common": "0.41.0",
23
23
  "@schemavaults/app-definitions": "0.17.0"
24
24
  },
25
25
  "scripts": {
@@ -39,14 +39,14 @@
39
39
  "bun-types": "1.4.4",
40
40
  "@types/react": "19.3.0",
41
41
  "tsc-alias": "1.9.0",
42
- "next": "16.4.3",
42
+ "next": "16.4.6",
43
43
  "eslint": "9.39.1",
44
44
  "@eslint/js": "9.39.1",
45
45
  "globals": "16.5.0",
46
46
  "@typescript-eslint/eslint-plugin": "8.48.1",
47
47
  "@typescript-eslint/parser": "8.48.1",
48
48
  "esbuild": "0.27.3",
49
- "@schemavaults/openapi-operations": "0.8.2"
49
+ "@schemavaults/openapi-operations": "0.8.3"
50
50
  },
51
51
  "publishConfig": {
52
52
  "access": "public"
@@ -180,7 +180,7 @@
180
180
  }
181
181
  },
182
182
  "peerDependencies": {
183
- "@schemavaults/openapi-operations": "0.8.2"
183
+ "@schemavaults/openapi-operations": "0.8.3"
184
184
  },
185
185
  "peerDependenciesMeta": {
186
186
  "@schemavaults/openapi-operations": {