@kokuin/oauth 0.7.0 → 0.7.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/lib/broker.d.ts +6 -0
- package/lib/broker.js +49 -0
- package/lib/device.d.ts +21 -0
- package/lib/device.js +140 -0
- package/lib/exchange.d.ts +6 -3
- package/lib/exchange.js +16 -3
- package/lib/index.d.ts +5 -3
- package/lib/index.js +2 -0
- package/lib/pending-auth.d.ts +10 -5
- package/lib/pending-auth.js +63 -14
- package/lib/types.d.ts +21 -0
- package/package.json +1 -1
package/lib/broker.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { OAuthBrokerTransport } from './types.js';
|
|
2
|
+
export type CreateHTTPBrokerTransportParams = {
|
|
3
|
+
baseURL: string;
|
|
4
|
+
fetch: typeof globalThis.fetch;
|
|
5
|
+
};
|
|
6
|
+
export declare function createHTTPBrokerTransport(params: CreateHTTPBrokerTransportParams): OAuthBrokerTransport;
|
package/lib/broker.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { fetchOAuthJSON } from './http.js';
|
|
2
|
+
export function createHTTPBrokerTransport(params) {
|
|
3
|
+
const baseURL = params.baseURL.replace(/\/+$/, '');
|
|
4
|
+
const runtime = {
|
|
5
|
+
fetch: params.fetch
|
|
6
|
+
};
|
|
7
|
+
return {
|
|
8
|
+
async authorize ({ provider, appRedirect, codeChallenge, scopes, authorizationParams, ...options }) {
|
|
9
|
+
const raw = await fetchOAuthJSON({
|
|
10
|
+
runtime,
|
|
11
|
+
url: `${baseURL}/oauth/${encodeURIComponent(provider)}/authorize`,
|
|
12
|
+
body: JSON.stringify({
|
|
13
|
+
appRedirect,
|
|
14
|
+
codeChallenge,
|
|
15
|
+
scopes,
|
|
16
|
+
authorizationParams
|
|
17
|
+
}),
|
|
18
|
+
contentType: 'application/json',
|
|
19
|
+
...options
|
|
20
|
+
});
|
|
21
|
+
return validateBrokerAuthorizeResult(raw);
|
|
22
|
+
},
|
|
23
|
+
token ({ provider, body, ...options }) {
|
|
24
|
+
return fetchOAuthJSON({
|
|
25
|
+
runtime,
|
|
26
|
+
url: `${baseURL}/oauth/${encodeURIComponent(provider)}/token`,
|
|
27
|
+
body: JSON.stringify(body),
|
|
28
|
+
contentType: 'application/json',
|
|
29
|
+
...options
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function validateBrokerAuthorizeResult(value) {
|
|
35
|
+
if (typeof value !== 'object' || value === null) {
|
|
36
|
+
throw new Error('OAuth broker authorization response was not an object');
|
|
37
|
+
}
|
|
38
|
+
const record = value;
|
|
39
|
+
for (const key of [
|
|
40
|
+
'url',
|
|
41
|
+
'state',
|
|
42
|
+
'transactionToken'
|
|
43
|
+
]){
|
|
44
|
+
if (typeof record[key] !== 'string' || record[key].length === 0) {
|
|
45
|
+
throw new Error(`OAuth broker authorization response has an invalid ${key}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
package/lib/device.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Runtime } from '@sozai/runtime';
|
|
2
|
+
import { type OAuthProviderDefinition, type RequestOptions, type TokenResponse } from './types.js';
|
|
3
|
+
export type DeviceAuthorization = {
|
|
4
|
+
deviceCode: string;
|
|
5
|
+
userCode: string;
|
|
6
|
+
verificationURI: string;
|
|
7
|
+
verificationURIComplete?: string;
|
|
8
|
+
expiresAt: number;
|
|
9
|
+
intervalMS: number;
|
|
10
|
+
};
|
|
11
|
+
export declare function startDeviceAuthorization(params: {
|
|
12
|
+
definition: OAuthProviderDefinition;
|
|
13
|
+
runtime: Runtime;
|
|
14
|
+
scopes: Array<string>;
|
|
15
|
+
} & RequestOptions): Promise<DeviceAuthorization>;
|
|
16
|
+
export declare function pollDeviceToken(params: {
|
|
17
|
+
definition: OAuthProviderDefinition;
|
|
18
|
+
runtime: Runtime;
|
|
19
|
+
authorization: DeviceAuthorization;
|
|
20
|
+
sleep?: (ms: number) => Promise<void>;
|
|
21
|
+
} & RequestOptions): Promise<TokenResponse>;
|
package/lib/device.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { validateTokenResponse } from './exchange.js';
|
|
2
|
+
import { fetchOAuthJSON } from './http.js';
|
|
3
|
+
import { OAuthTokenError } from './types.js';
|
|
4
|
+
export async function startDeviceAuthorization(params) {
|
|
5
|
+
const { definition, runtime, scopes, signal, timeoutMs, maxBytes } = params;
|
|
6
|
+
if (!definition.deviceAuthorizationEndpoint) {
|
|
7
|
+
throw new Error('OAuth definition requires a deviceAuthorizationEndpoint');
|
|
8
|
+
}
|
|
9
|
+
const form = new URLSearchParams({
|
|
10
|
+
client_id: definition.clientID,
|
|
11
|
+
scope: scopes.join(' ')
|
|
12
|
+
});
|
|
13
|
+
const raw = await fetchDeviceJSON({
|
|
14
|
+
runtime,
|
|
15
|
+
url: definition.deviceAuthorizationEndpoint,
|
|
16
|
+
body: form.toString(),
|
|
17
|
+
contentType: 'application/x-www-form-urlencoded',
|
|
18
|
+
signal,
|
|
19
|
+
timeoutMs,
|
|
20
|
+
maxBytes
|
|
21
|
+
});
|
|
22
|
+
if (typeof raw !== 'object' || raw === null) {
|
|
23
|
+
throw new Error('OAuth device authorization response was not an object');
|
|
24
|
+
}
|
|
25
|
+
const record = raw;
|
|
26
|
+
const deviceCode = requiredString(record, 'device_code');
|
|
27
|
+
const userCode = requiredString(record, 'user_code');
|
|
28
|
+
const verificationURI = requiredString(record, 'verification_uri');
|
|
29
|
+
const expiresIn = positiveNumber(record.expires_in, 'expires_in');
|
|
30
|
+
const interval = positiveNumber(record.interval === undefined ? 5 : record.interval, 'interval');
|
|
31
|
+
const verificationURIComplete = record.verification_uri_complete === undefined ? undefined : requiredString(record, 'verification_uri_complete');
|
|
32
|
+
return {
|
|
33
|
+
deviceCode,
|
|
34
|
+
userCode,
|
|
35
|
+
verificationURI,
|
|
36
|
+
...verificationURIComplete === undefined ? {} : {
|
|
37
|
+
verificationURIComplete
|
|
38
|
+
},
|
|
39
|
+
expiresAt: Date.now() + expiresIn * 1000,
|
|
40
|
+
intervalMS: interval * 1000
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export async function pollDeviceToken(params) {
|
|
44
|
+
const { definition, runtime, authorization, signal, sleep, timeoutMs, maxBytes } = params;
|
|
45
|
+
const form = new URLSearchParams({
|
|
46
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
47
|
+
device_code: authorization.deviceCode,
|
|
48
|
+
client_id: definition.clientID
|
|
49
|
+
});
|
|
50
|
+
let intervalMS = authorization.intervalMS;
|
|
51
|
+
for(;;){
|
|
52
|
+
signal?.throwIfAborted();
|
|
53
|
+
if (Date.now() >= authorization.expiresAt) {
|
|
54
|
+
throw new OAuthTokenError({
|
|
55
|
+
status: 400,
|
|
56
|
+
code: 'expired_token'
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const raw = await fetchDeviceJSON({
|
|
61
|
+
runtime,
|
|
62
|
+
url: definition.tokenEndpoint,
|
|
63
|
+
body: form.toString(),
|
|
64
|
+
contentType: 'application/x-www-form-urlencoded',
|
|
65
|
+
signal,
|
|
66
|
+
timeoutMs,
|
|
67
|
+
maxBytes
|
|
68
|
+
});
|
|
69
|
+
signal?.throwIfAborted();
|
|
70
|
+
return validateTokenResponse(raw);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (!(error instanceof OAuthTokenError)) throw error;
|
|
73
|
+
if (error.code === 'slow_down') {
|
|
74
|
+
intervalMS += 5000;
|
|
75
|
+
} else if (error.code !== 'authorization_pending') {
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
await wait(Math.min(intervalMS, Math.max(0, authorization.expiresAt - Date.now())), sleep, signal);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function fetchDeviceJSON(params) {
|
|
83
|
+
const raw = await fetchOAuthJSON(params);
|
|
84
|
+
// Some providers return OAuth errors with HTTP 200.
|
|
85
|
+
if (typeof raw === 'object' && raw !== null) {
|
|
86
|
+
const record = raw;
|
|
87
|
+
if (typeof record.error === 'string') {
|
|
88
|
+
throw new OAuthTokenError({
|
|
89
|
+
status: 200,
|
|
90
|
+
code: record.error,
|
|
91
|
+
description: typeof record.error_description === 'string' ? record.error_description : undefined
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return raw;
|
|
96
|
+
}
|
|
97
|
+
function requiredString(record, key) {
|
|
98
|
+
const value = record[key];
|
|
99
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
100
|
+
throw new Error(`OAuth device authorization response has an invalid ${key}`);
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
function positiveNumber(value, key) {
|
|
105
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
|
106
|
+
throw new Error(`OAuth device authorization response has an invalid ${key}`);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
function wait(ms, sleep, signal) {
|
|
111
|
+
signal?.throwIfAborted();
|
|
112
|
+
return new Promise((resolve, reject)=>{
|
|
113
|
+
let timer;
|
|
114
|
+
const cleanup = ()=>{
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
signal?.removeEventListener('abort', abort);
|
|
117
|
+
};
|
|
118
|
+
const abort = ()=>{
|
|
119
|
+
cleanup();
|
|
120
|
+
reject(signal?.reason);
|
|
121
|
+
};
|
|
122
|
+
signal?.addEventListener('abort', abort, {
|
|
123
|
+
once: true
|
|
124
|
+
});
|
|
125
|
+
if (sleep) {
|
|
126
|
+
Promise.resolve().then(()=>sleep(ms)).then(()=>{
|
|
127
|
+
cleanup();
|
|
128
|
+
resolve();
|
|
129
|
+
}, (error)=>{
|
|
130
|
+
cleanup();
|
|
131
|
+
reject(error);
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
timer = setTimeout(()=>{
|
|
135
|
+
cleanup();
|
|
136
|
+
resolve();
|
|
137
|
+
}, ms);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
package/lib/exchange.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Runtime } from '@sozai/runtime';
|
|
2
|
-
import type { OAuthProviderDefinition, RequestOptions, TokenResponse } from './types.js';
|
|
2
|
+
import type { OAuthBrokerTransport, OAuthProviderDefinition, RequestOptions, TokenResponse } from './types.js';
|
|
3
3
|
export declare function exchangeCode(params: {
|
|
4
4
|
definition: OAuthProviderDefinition;
|
|
5
5
|
runtime: Runtime;
|
|
@@ -7,8 +7,11 @@ export declare function exchangeCode(params: {
|
|
|
7
7
|
redirectURL: string;
|
|
8
8
|
codeVerifier: string;
|
|
9
9
|
} & RequestOptions): Promise<TokenResponse>;
|
|
10
|
-
export
|
|
10
|
+
export type RefreshTokenParams = {
|
|
11
11
|
definition: OAuthProviderDefinition;
|
|
12
12
|
runtime: Runtime;
|
|
13
13
|
refreshToken: string;
|
|
14
|
-
|
|
14
|
+
broker?: OAuthBrokerTransport;
|
|
15
|
+
} & RequestOptions;
|
|
16
|
+
export declare function refreshToken(params: RefreshTokenParams): Promise<TokenResponse>;
|
|
17
|
+
export declare function validateTokenResponse(value: unknown): TokenResponse;
|
package/lib/exchange.js
CHANGED
|
@@ -26,9 +26,22 @@ 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 } = params;
|
|
29
|
+
const { definition, runtime, refreshToken, signal, timeoutMs, maxBytes, broker } = params;
|
|
30
30
|
if (definition.mode === 'broker') {
|
|
31
|
-
|
|
31
|
+
if (!broker) {
|
|
32
|
+
throw new Error('broker mode requires a broker transport');
|
|
33
|
+
}
|
|
34
|
+
const raw = await broker.token({
|
|
35
|
+
provider: definition.name,
|
|
36
|
+
body: {
|
|
37
|
+
grant_type: 'refresh_token',
|
|
38
|
+
refresh_token: refreshToken
|
|
39
|
+
},
|
|
40
|
+
signal,
|
|
41
|
+
timeoutMs,
|
|
42
|
+
maxBytes
|
|
43
|
+
});
|
|
44
|
+
return validateTokenResponse(raw);
|
|
32
45
|
}
|
|
33
46
|
assertExchangeableConfig(definition);
|
|
34
47
|
const form = new URLSearchParams();
|
|
@@ -62,7 +75,7 @@ function assertExchangeableConfig(definition) {
|
|
|
62
75
|
}
|
|
63
76
|
}
|
|
64
77
|
}
|
|
65
|
-
function validateTokenResponse(value) {
|
|
78
|
+
export function validateTokenResponse(value) {
|
|
66
79
|
if (typeof value !== 'object' || value === null) {
|
|
67
80
|
throw new Error('OAuth token response was not an object');
|
|
68
81
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { buildAuthorizationURL } from './authorization.js';
|
|
2
|
-
export {
|
|
2
|
+
export { type CreateHTTPBrokerTransportParams, createHTTPBrokerTransport } from './broker.js';
|
|
3
|
+
export { type DeviceAuthorization, pollDeviceToken, startDeviceAuthorization } from './device.js';
|
|
4
|
+
export { exchangeCode, type RefreshTokenParams, refreshToken } from './exchange.js';
|
|
3
5
|
export { type FetchOAuthJSONParams, fetchOAuthJSON } from './http.js';
|
|
4
|
-
export { completeAuthorization, createMemoryPendingAuthStore, DEFAULT_TTL_MS, type PendingAuthRecord, type PendingAuthStore, startAuthorization, } from './pending-auth.js';
|
|
6
|
+
export { type CompleteAuthorizationParams, completeAuthorization, createMemoryPendingAuthStore, DEFAULT_TTL_MS, type PendingAuthRecord, type PendingAuthStore, type StartAuthorizationParams, startAuthorization, } from './pending-auth.js';
|
|
5
7
|
export { deriveCodeChallenge, generateCodeVerifier, generateState } from './pkce.js';
|
|
6
8
|
export type { OAuthTokenErrorParams } from './types.js';
|
|
7
|
-
export { type OAuthClientMode, type OAuthErrorCode, type OAuthProviderDefinition, OAuthTokenError, type RequestOptions, type TokenResponse, } from './types.js';
|
|
9
|
+
export { type BrokerAuthorizeParams, type BrokerAuthorizeResult, type BrokerTokenParams, type OAuthBrokerTransport, type OAuthClientMode, type OAuthErrorCode, type OAuthProviderDefinition, OAuthTokenError, type RequestOptions, type TokenResponse, } from './types.js';
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { buildAuthorizationURL } from './authorization.js';
|
|
2
|
+
export { createHTTPBrokerTransport } from './broker.js';
|
|
3
|
+
export { pollDeviceToken, startDeviceAuthorization } from './device.js';
|
|
2
4
|
export { exchangeCode, refreshToken } from './exchange.js';
|
|
3
5
|
export { fetchOAuthJSON } from './http.js';
|
|
4
6
|
export { completeAuthorization, createMemoryPendingAuthStore, DEFAULT_TTL_MS, startAuthorization } from './pending-auth.js';
|
package/lib/pending-auth.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { Runtime } from '@sozai/runtime';
|
|
2
|
-
import type { OAuthProviderDefinition, RequestOptions, TokenResponse } from './types.js';
|
|
2
|
+
import type { OAuthBrokerTransport, OAuthProviderDefinition, RequestOptions, TokenResponse } from './types.js';
|
|
3
3
|
export type PendingAuthRecord<TExtra> = {
|
|
4
4
|
state: string;
|
|
5
5
|
codeVerifier: string;
|
|
6
|
+
transactionToken?: string;
|
|
6
7
|
provider: string;
|
|
7
8
|
redirectURL: string;
|
|
8
9
|
scopes: Array<string>;
|
|
@@ -18,7 +19,7 @@ export type PendingAuthStore<TExtra> = {
|
|
|
18
19
|
};
|
|
19
20
|
export declare const DEFAULT_TTL_MS: number;
|
|
20
21
|
export declare function createMemoryPendingAuthStore<TExtra>(): PendingAuthStore<TExtra>;
|
|
21
|
-
export
|
|
22
|
+
export type StartAuthorizationParams<TExtra> = {
|
|
22
23
|
runtime: Runtime;
|
|
23
24
|
definition: OAuthProviderDefinition;
|
|
24
25
|
store: PendingAuthStore<TExtra>;
|
|
@@ -27,18 +28,22 @@ export declare function startAuthorization<TExtra>(params: {
|
|
|
27
28
|
extra: TExtra;
|
|
28
29
|
ttlMs?: number;
|
|
29
30
|
authorizationParams?: Record<string, string>;
|
|
30
|
-
|
|
31
|
+
broker?: OAuthBrokerTransport;
|
|
32
|
+
} & RequestOptions;
|
|
33
|
+
export declare function startAuthorization<TExtra>(params: StartAuthorizationParams<TExtra>): Promise<{
|
|
31
34
|
url: string;
|
|
32
35
|
state: string;
|
|
33
36
|
}>;
|
|
34
|
-
export
|
|
37
|
+
export type CompleteAuthorizationParams<TExtra> = {
|
|
35
38
|
runtime: Runtime;
|
|
36
39
|
definition: OAuthProviderDefinition;
|
|
37
40
|
store: PendingAuthStore<TExtra>;
|
|
38
41
|
state: string;
|
|
39
42
|
code: string;
|
|
40
43
|
redirectURL: string;
|
|
41
|
-
|
|
44
|
+
broker?: OAuthBrokerTransport;
|
|
45
|
+
} & RequestOptions;
|
|
46
|
+
export declare function completeAuthorization<TExtra>(params: CompleteAuthorizationParams<TExtra>): Promise<{
|
|
42
47
|
tokens: TokenResponse;
|
|
43
48
|
record: PendingAuthRecord<TExtra>;
|
|
44
49
|
}>;
|
package/lib/pending-auth.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { buildAuthorizationURL } from './authorization.js';
|
|
2
|
-
import { exchangeCode } from './exchange.js';
|
|
2
|
+
import { exchangeCode, validateTokenResponse } from './exchange.js';
|
|
3
3
|
import { deriveCodeChallenge, generateCodeVerifier, generateState } from './pkce.js';
|
|
4
4
|
export const DEFAULT_TTL_MS = 10 * 60 * 1000;
|
|
5
5
|
export function createMemoryPendingAuthStore() {
|
|
@@ -29,7 +29,10 @@ export function createMemoryPendingAuthStore() {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
export async function startAuthorization(params) {
|
|
32
|
-
const { runtime, definition, store, redirectURL, scopes, extra, authorizationParams } = params;
|
|
32
|
+
const { runtime, definition, store, redirectURL, scopes, extra, authorizationParams, broker } = params;
|
|
33
|
+
if (definition.mode === 'broker' && !broker) {
|
|
34
|
+
throw new Error('broker mode requires a broker transport');
|
|
35
|
+
}
|
|
33
36
|
const ttlMs = params.ttlMs ?? DEFAULT_TTL_MS;
|
|
34
37
|
const now = Date.now();
|
|
35
38
|
const expiresAt = now + ttlMs;
|
|
@@ -38,22 +41,43 @@ export async function startAuthorization(params) {
|
|
|
38
41
|
} catch {
|
|
39
42
|
// best-effort cleanup: a sweep failure must not block starting a new flow
|
|
40
43
|
}
|
|
41
|
-
const state = generateState(runtime);
|
|
42
44
|
const codeVerifier = generateCodeVerifier(runtime);
|
|
43
45
|
const codeChallenge = deriveCodeChallenge(codeVerifier);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
let state;
|
|
47
|
+
let url;
|
|
48
|
+
let transactionToken;
|
|
49
|
+
if (definition.mode === 'broker' && broker) {
|
|
50
|
+
const authorization = await broker.authorize({
|
|
51
|
+
provider: definition.name,
|
|
52
|
+
appRedirect: redirectURL,
|
|
53
|
+
codeChallenge,
|
|
54
|
+
scopes,
|
|
55
|
+
authorizationParams,
|
|
56
|
+
signal: params.signal,
|
|
57
|
+
timeoutMs: params.timeoutMs,
|
|
58
|
+
maxBytes: params.maxBytes
|
|
59
|
+
});
|
|
60
|
+
state = authorization.state;
|
|
61
|
+
url = authorization.url;
|
|
62
|
+
transactionToken = authorization.transactionToken;
|
|
63
|
+
} else {
|
|
64
|
+
state = generateState(runtime);
|
|
65
|
+
// Validate the endpoint before persisting a pending flow.
|
|
66
|
+
url = buildAuthorizationURL({
|
|
67
|
+
definition,
|
|
68
|
+
redirectURL,
|
|
69
|
+
scopes,
|
|
70
|
+
state,
|
|
71
|
+
codeChallenge,
|
|
72
|
+
authorizationParams
|
|
73
|
+
});
|
|
74
|
+
}
|
|
54
75
|
await store.create({
|
|
55
76
|
state,
|
|
56
77
|
codeVerifier,
|
|
78
|
+
...transactionToken === undefined ? {} : {
|
|
79
|
+
transactionToken
|
|
80
|
+
},
|
|
57
81
|
provider: definition.name,
|
|
58
82
|
redirectURL,
|
|
59
83
|
scopes,
|
|
@@ -67,7 +91,10 @@ export async function startAuthorization(params) {
|
|
|
67
91
|
};
|
|
68
92
|
}
|
|
69
93
|
export async function completeAuthorization(params) {
|
|
70
|
-
const { runtime, definition, store, state, code, redirectURL, signal, timeoutMs, maxBytes } = params;
|
|
94
|
+
const { runtime, definition, store, state, code, redirectURL, signal, timeoutMs, maxBytes, broker } = params;
|
|
95
|
+
if (definition.mode === 'broker' && !broker) {
|
|
96
|
+
throw new Error('broker mode requires a broker transport');
|
|
97
|
+
}
|
|
71
98
|
const record = await store.consume(state);
|
|
72
99
|
if (record == null) {
|
|
73
100
|
throw new Error('unknown or already-used authorization state');
|
|
@@ -81,6 +108,28 @@ export async function completeAuthorization(params) {
|
|
|
81
108
|
if (record.redirectURL !== redirectURL) {
|
|
82
109
|
throw new Error('redirect URI mismatch');
|
|
83
110
|
}
|
|
111
|
+
if (definition.mode === 'broker' && broker) {
|
|
112
|
+
if (!record.transactionToken) {
|
|
113
|
+
throw new Error('broker authorization requires a transactionToken');
|
|
114
|
+
}
|
|
115
|
+
const raw = await broker.token({
|
|
116
|
+
provider: definition.name,
|
|
117
|
+
body: {
|
|
118
|
+
grant_type: 'authorization_code',
|
|
119
|
+
code,
|
|
120
|
+
code_verifier: record.codeVerifier,
|
|
121
|
+
transactionToken: record.transactionToken,
|
|
122
|
+
state
|
|
123
|
+
},
|
|
124
|
+
signal,
|
|
125
|
+
timeoutMs,
|
|
126
|
+
maxBytes
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
tokens: validateTokenResponse(raw),
|
|
130
|
+
record
|
|
131
|
+
};
|
|
132
|
+
}
|
|
84
133
|
const tokens = await exchangeCode({
|
|
85
134
|
definition,
|
|
86
135
|
runtime,
|
package/lib/types.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export type OAuthProviderDefinition = {
|
|
|
19
19
|
name: string;
|
|
20
20
|
authorizationEndpoint: string;
|
|
21
21
|
tokenEndpoint: string;
|
|
22
|
+
deviceAuthorizationEndpoint?: string;
|
|
22
23
|
baseScopes?: Array<string>;
|
|
23
24
|
} & OAuthClientMode;
|
|
24
25
|
export type TokenResponse = {
|
|
@@ -47,3 +48,23 @@ export type RequestOptions = {
|
|
|
47
48
|
timeoutMs?: number;
|
|
48
49
|
maxBytes?: number;
|
|
49
50
|
};
|
|
51
|
+
export type BrokerAuthorizeParams = {
|
|
52
|
+
provider: string;
|
|
53
|
+
appRedirect: string;
|
|
54
|
+
codeChallenge?: string;
|
|
55
|
+
scopes?: Array<string>;
|
|
56
|
+
authorizationParams?: Record<string, string>;
|
|
57
|
+
} & RequestOptions;
|
|
58
|
+
export type BrokerAuthorizeResult = {
|
|
59
|
+
url: string;
|
|
60
|
+
state: string;
|
|
61
|
+
transactionToken: string;
|
|
62
|
+
};
|
|
63
|
+
export type BrokerTokenParams = {
|
|
64
|
+
provider: string;
|
|
65
|
+
body: Record<string, string>;
|
|
66
|
+
} & RequestOptions;
|
|
67
|
+
export type OAuthBrokerTransport = {
|
|
68
|
+
authorize(params: BrokerAuthorizeParams): Promise<BrokerAuthorizeResult>;
|
|
69
|
+
token(params: BrokerTokenParams): Promise<unknown>;
|
|
70
|
+
};
|