@kokuin/oauth 0.7.1 → 0.7.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/lib/exchange.d.ts CHANGED
@@ -6,12 +6,23 @@ export declare function exchangeCode(params: {
6
6
  code: string;
7
7
  redirectURL: string;
8
8
  codeVerifier: string;
9
+ /**
10
+ * Additional form fields, such as `resource` (RFC 8707). They never override the grant's own
11
+ * fields (`grant_type`, `code`, `redirect_uri`, `client_id`, `code_verifier`, `client_secret`).
12
+ */
13
+ extraParams?: Record<string, string>;
9
14
  } & RequestOptions): Promise<TokenResponse>;
10
15
  export type RefreshTokenParams = {
11
16
  definition: OAuthProviderDefinition;
12
17
  runtime: Runtime;
13
18
  refreshToken: string;
14
19
  broker?: OAuthBrokerTransport;
20
+ /**
21
+ * Additional form fields, such as `resource` (RFC 8707) or `scope`. They never override the
22
+ * grant's own fields (`grant_type`, `refresh_token`, `client_id`, `client_secret`). Not sent in
23
+ * broker mode.
24
+ */
25
+ extraParams?: Record<string, string>;
15
26
  } & RequestOptions;
16
27
  export declare function refreshToken(params: RefreshTokenParams): Promise<TokenResponse>;
17
28
  export declare function validateTokenResponse(value: unknown): TokenResponse;
package/lib/exchange.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import { fetchOAuthJSON } from './http.js';
2
2
  export async function exchangeCode(params) {
3
- const { definition, runtime, code, redirectURL, codeVerifier, signal, timeoutMs, maxBytes } = params;
3
+ const { definition, runtime, code, redirectURL, codeVerifier, extraParams, signal, timeoutMs, maxBytes } = params;
4
4
  if (definition.mode === 'broker') {
5
5
  throw new Error('broker token exchange is not implemented');
6
6
  }
7
7
  assertExchangeableConfig(definition);
8
- const form = new URLSearchParams();
8
+ const form = extraForm(extraParams, CODE_GRANT_FIELDS);
9
9
  form.set('grant_type', 'authorization_code');
10
10
  form.set('code', code);
11
11
  form.set('redirect_uri', redirectURL);
@@ -26,7 +26,7 @@ export async function exchangeCode(params) {
26
26
  return validateTokenResponse(raw);
27
27
  }
28
28
  export async function refreshToken(params) {
29
- const { definition, runtime, refreshToken, signal, timeoutMs, maxBytes, broker } = params;
29
+ const { definition, runtime, refreshToken, extraParams, signal, timeoutMs, maxBytes, broker } = params;
30
30
  if (definition.mode === 'broker') {
31
31
  if (!broker) {
32
32
  throw new Error('broker mode requires a broker transport');
@@ -44,7 +44,7 @@ export async function refreshToken(params) {
44
44
  return validateTokenResponse(raw);
45
45
  }
46
46
  assertExchangeableConfig(definition);
47
- const form = new URLSearchParams();
47
+ const form = extraForm(extraParams, REFRESH_GRANT_FIELDS);
48
48
  form.set('grant_type', 'refresh_token');
49
49
  form.set('refresh_token', refreshToken);
50
50
  form.set('client_id', definition.clientID);
@@ -62,6 +62,27 @@ export async function refreshToken(params) {
62
62
  });
63
63
  return validateTokenResponse(raw);
64
64
  }
65
+ const CODE_GRANT_FIELDS = new Set([
66
+ 'grant_type',
67
+ 'code',
68
+ 'redirect_uri',
69
+ 'client_id',
70
+ 'code_verifier',
71
+ 'client_secret'
72
+ ]);
73
+ const REFRESH_GRANT_FIELDS = new Set([
74
+ 'grant_type',
75
+ 'refresh_token',
76
+ 'client_id',
77
+ 'client_secret'
78
+ ]);
79
+ /** A form holding the caller's extra fields, minus the fields the grant itself owns. */ function extraForm(extraParams, protectedFields) {
80
+ const form = new URLSearchParams();
81
+ for (const [key, value] of Object.entries(extraParams ?? {})){
82
+ if (!protectedFields.has(key)) form.set(key, value);
83
+ }
84
+ return form;
85
+ }
65
86
  function assertExchangeableConfig(definition) {
66
87
  if (definition.mode !== 'confidential' && definition.mode !== 'native') {
67
88
  throw new Error(`unsupported OAuth client mode: ${String(definition.mode)}`);
package/lib/http.d.ts CHANGED
@@ -3,7 +3,15 @@ import { type RequestOptions } from './types.js';
3
3
  export type FetchOAuthJSONParams = {
4
4
  runtime: Pick<Runtime, 'fetch'>;
5
5
  url: string;
6
+ } & ({
7
+ /** Defaults to `POST`. */
8
+ method?: 'POST';
6
9
  body: string;
7
10
  contentType: string;
8
- } & RequestOptions;
11
+ } | {
12
+ /** A metadata read: no body and no content type. */
13
+ method: 'GET';
14
+ body?: undefined;
15
+ contentType?: undefined;
16
+ }) & RequestOptions;
9
17
  export declare function fetchOAuthJSON(params: FetchOAuthJSONParams): Promise<unknown>;
package/lib/http.js CHANGED
@@ -3,7 +3,7 @@ import { OAuthTokenError } from './types.js';
3
3
  const DEFAULT_MAX_BYTES = 1024 * 1024;
4
4
  const DEFAULT_TIMEOUT_MS = 30_000;
5
5
  export async function fetchOAuthJSON(params) {
6
- const { runtime, url, body, contentType, signal, maxBytes = DEFAULT_MAX_BYTES, timeoutMs = DEFAULT_TIMEOUT_MS } = params;
6
+ const { runtime, url, method = 'POST', body, contentType, signal, maxBytes = DEFAULT_MAX_BYTES, timeoutMs = DEFAULT_TIMEOUT_MS } = params;
7
7
  assertSecureURL(url);
8
8
  const timeoutSignal = AbortSignal.timeout(timeoutMs);
9
9
  const combinedSignal = signal ? AbortSignal.any([
@@ -11,8 +11,10 @@ export async function fetchOAuthJSON(params) {
11
11
  timeoutSignal
12
12
  ]) : timeoutSignal;
13
13
  const response = await runtime.fetch(url, {
14
- method: 'POST',
15
- headers: {
14
+ method,
15
+ headers: contentType == null ? {
16
+ accept: 'application/json'
17
+ } : {
16
18
  'content-type': contentType,
17
19
  accept: 'application/json'
18
20
  },
package/lib/secure-url.js CHANGED
@@ -9,5 +9,5 @@ export function assertSecureURL(rawURL) {
9
9
  throw new Error(`OAuth endpoint must use https: ${rawURL}`);
10
10
  }
11
11
  export function isLoopbackHost(hostname) {
12
- return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '[::1]';
12
+ return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '[::1]' || hostname.endsWith('.localhost');
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokuin/oauth",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Generic OAuth 2.0 client primitives for kokuin",
5
5
  "keywords": [
6
6
  "oauth",