@unboundcx/sdk 4.13.112-security.0 → 4.13.112-security.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/base.js +10 -1
- package/index.js +3 -0
- package/lib/refreshInterceptor.js +28 -7
- package/package.json +1 -1
- package/services/oauth.js +159 -0
package/base.js
CHANGED
|
@@ -568,14 +568,23 @@ export class BaseSDK {
|
|
|
568
568
|
return handleUnauthorized(this, {
|
|
569
569
|
status: response.status,
|
|
570
570
|
endpoint,
|
|
571
|
+
method,
|
|
571
572
|
alreadyRetried: retryCtx?.originalParams?.__skipAutoRefresh === true,
|
|
572
573
|
originalError: httpError,
|
|
573
574
|
retry: () =>
|
|
575
|
+
// forceFetch: true (not retryCtx.forceFetch) -- the retry must
|
|
576
|
+
// always go over plain HTTP, never the original transport. A
|
|
577
|
+
// socket transport call that 401'd still holds the pre-rotation
|
|
578
|
+
// JWT (the rotation reaches the socket asynchronously via NATS),
|
|
579
|
+
// so retrying over the same socket would 401 again and trigger
|
|
580
|
+
// onUnauthorized even though the refresh above just succeeded.
|
|
581
|
+
// See _getAvailableTransport (base.js:~185): forceFetch skips
|
|
582
|
+
// transports entirely and goes straight to HTTP.
|
|
574
583
|
this.#request(
|
|
575
584
|
endpoint,
|
|
576
585
|
method,
|
|
577
586
|
{ ...(retryCtx.originalParams || {}), __skipAutoRefresh: true },
|
|
578
|
-
|
|
587
|
+
true,
|
|
579
588
|
),
|
|
580
589
|
});
|
|
581
590
|
}
|
package/index.js
CHANGED
|
@@ -48,6 +48,7 @@ import { ChatService } from './services/chat.js';
|
|
|
48
48
|
import { DeveloperApisService } from './services/developerApis.js';
|
|
49
49
|
import { TextService } from './services/text.js';
|
|
50
50
|
import { ReportingService } from './services/reporting.js';
|
|
51
|
+
import { OAuthService } from './services/oauth.js';
|
|
51
52
|
import {
|
|
52
53
|
DataImportService,
|
|
53
54
|
DataExportService,
|
|
@@ -146,6 +147,7 @@ class UnboundSDK extends BaseSDK {
|
|
|
146
147
|
this.recents = new RecentsService(this);
|
|
147
148
|
this.search = new SearchService(this);
|
|
148
149
|
this.directory = new DirectoryService(this);
|
|
150
|
+
this.oauth = new OAuthService(this);
|
|
149
151
|
this.chat = new ChatService(this);
|
|
150
152
|
this.developerApis = new DeveloperApisService(this);
|
|
151
153
|
this.dataImport = new DataImportService(this);
|
|
@@ -352,6 +354,7 @@ export { ReportingService } from './services/reporting.js';
|
|
|
352
354
|
export { RecentsService } from './services/recents.js';
|
|
353
355
|
export { SearchService } from './services/search.js';
|
|
354
356
|
export { DirectoryService } from './services/directory.js';
|
|
357
|
+
export { OAuthService } from './services/oauth.js';
|
|
355
358
|
export { ChatService } from './services/chat.js';
|
|
356
359
|
export { DeveloperApisService } from './services/developerApis.js';
|
|
357
360
|
export {
|
|
@@ -17,15 +17,26 @@
|
|
|
17
17
|
// still single-flights its own concurrent 401s against itself.
|
|
18
18
|
const inFlightRefreshBySdk = new WeakMap();
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
// Only the routes that WOULD cause a refresh loop (or make no sense to
|
|
21
|
+
// refresh) are excluded: refreshing itself (POST /login/refresh), the
|
|
22
|
+
// initial password login (POST /login), and logout (DELETE /login). Every
|
|
23
|
+
// other /login/* call -- notably GET /login/validate, which a page reload
|
|
24
|
+
// hits with a stale access cookie -- is refreshable: a valid refresh cookie
|
|
25
|
+
// must be able to recover it instead of bouncing the user to the password
|
|
26
|
+
// form.
|
|
27
|
+
function isNonRefreshableLoginRoute(method, endpoint) {
|
|
28
|
+
if (typeof endpoint !== 'string') return false;
|
|
29
|
+
const m = typeof method === 'string' ? method.toUpperCase() : '';
|
|
30
|
+
if (endpoint === '/login/refresh' && m === 'POST') return true;
|
|
31
|
+
if (endpoint === '/login' && (m === 'POST' || m === 'DELETE')) return true;
|
|
32
|
+
return false;
|
|
22
33
|
}
|
|
23
34
|
|
|
24
|
-
export function shouldAttemptRefresh(sdk, status, endpoint) {
|
|
35
|
+
export function shouldAttemptRefresh(sdk, status, endpoint, method) {
|
|
25
36
|
return (
|
|
26
37
|
status === 401 &&
|
|
27
38
|
sdk?._autoRefresh === true &&
|
|
28
|
-
!
|
|
39
|
+
!isNonRefreshableLoginRoute(method, endpoint) &&
|
|
29
40
|
typeof sdk?.login?.refresh === 'function'
|
|
30
41
|
);
|
|
31
42
|
}
|
|
@@ -47,7 +58,16 @@ async function runRefresh(sdk) {
|
|
|
47
58
|
// token back in the body; cookie clients rely on the Set-Cookie
|
|
48
59
|
// pair the refresh response already sent -- there is no token
|
|
49
60
|
// string to apply to the SDK instance for them.
|
|
50
|
-
if (result?.token)
|
|
61
|
+
if (result?.token) {
|
|
62
|
+
sdk.setToken(result.token);
|
|
63
|
+
} else if (typeof sdk?.setToken === 'function') {
|
|
64
|
+
// Cookie-mode refresh: no token string comes back (the Set-Cookie
|
|
65
|
+
// pair already rotated the browser's cookies), but a stale
|
|
66
|
+
// in-memory access token left on the SDK would keep going out as
|
|
67
|
+
// an Authorization header, which checkApiAuth prefers over the
|
|
68
|
+
// fresh cookie. Clear it so the next request relies on the cookie.
|
|
69
|
+
sdk.setToken(null);
|
|
70
|
+
}
|
|
51
71
|
if (result?.refreshToken) sdk._refreshToken = result.refreshToken;
|
|
52
72
|
return result;
|
|
53
73
|
})
|
|
@@ -76,6 +96,7 @@ function notifyUnauthorized(sdk, err) {
|
|
|
76
96
|
* @param {object} ctx
|
|
77
97
|
* @param {number} ctx.status
|
|
78
98
|
* @param {string} ctx.endpoint
|
|
99
|
+
* @param {string} [ctx.method]
|
|
79
100
|
* @param {() => Promise<any>} ctx.retry
|
|
80
101
|
* @param {Error} ctx.originalError -- rethrown whenever refresh isn't
|
|
81
102
|
* attempted, fails, or the retry itself still comes back 401 (never loop
|
|
@@ -84,9 +105,9 @@ function notifyUnauthorized(sdk, err) {
|
|
|
84
105
|
*/
|
|
85
106
|
export async function handleUnauthorized(
|
|
86
107
|
sdk,
|
|
87
|
-
{ status, endpoint, retry, originalError, alreadyRetried = false },
|
|
108
|
+
{ status, endpoint, method, retry, originalError, alreadyRetried = false },
|
|
88
109
|
) {
|
|
89
|
-
if (alreadyRetried || !shouldAttemptRefresh(sdk, status, endpoint)) {
|
|
110
|
+
if (alreadyRetried || !shouldAttemptRefresh(sdk, status, endpoint, method)) {
|
|
90
111
|
throw originalError;
|
|
91
112
|
}
|
|
92
113
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unboundcx/sdk",
|
|
3
|
-
"version": "4.13.112-security.
|
|
3
|
+
"version": "4.13.112-security.2",
|
|
4
4
|
"description": "Official JavaScript SDK for the Unbound API - A comprehensive toolkit for integrating with Unbound's communication, AI, and data management services",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { internalRequest } from '../base.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OAuth provider -- staff registry of OAuth clients (security-upgrades H1
|
|
5
|
+
* frozen contract). Every method hits `/oauth/clients*` under
|
|
6
|
+
* checkApiAuth; the server enforces `admin:oauthclient:manage`. Public
|
|
7
|
+
* authorize/token/introspect/revoke endpoints (H2/H3/H4) are not client
|
|
8
|
+
* methods -- they are third-party-facing, not called through this SDK.
|
|
9
|
+
*
|
|
10
|
+
* Exposed as `sdk.oauth.clients.*` (nested, since `oauth` is expected to
|
|
11
|
+
* grow other sub-surfaces alongside `clients` as H2-H4 land).
|
|
12
|
+
*/
|
|
13
|
+
export class OAuthService {
|
|
14
|
+
constructor(sdk) {
|
|
15
|
+
this.sdk = sdk;
|
|
16
|
+
this.clients = new OAuthClientsService(sdk);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class OAuthClientsService {
|
|
21
|
+
constructor(sdk) {
|
|
22
|
+
this.sdk = sdk;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* List this account's OAuth clients.
|
|
27
|
+
* @returns {Promise<Object>} `{ clients: [...] }`
|
|
28
|
+
* @example
|
|
29
|
+
* const { clients } = await sdk.oauth.clients.list();
|
|
30
|
+
*/
|
|
31
|
+
async list() {
|
|
32
|
+
return internalRequest(this.sdk, '/oauth/clients', 'GET');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Register a new OAuth client.
|
|
37
|
+
* @param {Object} body
|
|
38
|
+
* @param {string} body.name
|
|
39
|
+
* @param {string} body.clientType - 'confidential' | 'public'
|
|
40
|
+
* @param {string[]} body.redirectUris
|
|
41
|
+
* @param {string[]} body.grantTypes - subset of 'authorization_code' | 'refresh_token' | 'client_credentials'
|
|
42
|
+
* @param {string[]} body.allowedScopes
|
|
43
|
+
* @param {string} [body.linkedUserId] - required when grantTypes includes 'client_credentials' (D11)
|
|
44
|
+
* @param {string} [body.logoUrl]
|
|
45
|
+
* @returns {Promise<Object>} `{ client, secret? }` -- secret is plaintext, shown once, confidential clients only
|
|
46
|
+
* @example
|
|
47
|
+
* const { client, secret } = await sdk.oauth.clients.create({
|
|
48
|
+
* name: 'Zapier',
|
|
49
|
+
* clientType: 'confidential',
|
|
50
|
+
* redirectUris: ['https://zapier.com/oauth/callback'],
|
|
51
|
+
* grantTypes: ['authorization_code', 'refresh_token'],
|
|
52
|
+
* allowedScopes: ['cdp:object:*:read'],
|
|
53
|
+
* });
|
|
54
|
+
*/
|
|
55
|
+
async create(body) {
|
|
56
|
+
this.sdk.validateParams(body || {}, {
|
|
57
|
+
name: { type: 'string', required: true },
|
|
58
|
+
clientType: { type: 'string', required: true },
|
|
59
|
+
redirectUris: { type: 'array', required: true },
|
|
60
|
+
grantTypes: { type: 'array', required: true },
|
|
61
|
+
allowedScopes: { type: 'array', required: true },
|
|
62
|
+
linkedUserId: { type: 'string', required: false },
|
|
63
|
+
logoUrl: { type: 'string', required: false },
|
|
64
|
+
});
|
|
65
|
+
return internalRequest(this.sdk, '/oauth/clients', 'POST', { body });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get one OAuth client.
|
|
70
|
+
* @param {string} id
|
|
71
|
+
* @returns {Promise<Object>} `{ client }`
|
|
72
|
+
*/
|
|
73
|
+
async get(id) {
|
|
74
|
+
this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
|
|
75
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}`, 'GET');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Update an OAuth client. Any subset of the create fields, plus `status`
|
|
80
|
+
* ('active' | 'disabled').
|
|
81
|
+
* @param {string} id
|
|
82
|
+
* @param {Object} body
|
|
83
|
+
* @returns {Promise<Object>} `{ client }`
|
|
84
|
+
*/
|
|
85
|
+
async update(id, body) {
|
|
86
|
+
this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
|
|
87
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}`, 'PUT', { body });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Soft-delete an OAuth client.
|
|
92
|
+
* @param {string} id
|
|
93
|
+
* @returns {Promise<Object>} `{ message }`
|
|
94
|
+
*/
|
|
95
|
+
async remove(id) {
|
|
96
|
+
this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
|
|
97
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}`, 'DELETE');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* List a client's secrets (id/createdAt/expiresAt/revokedAt/lastUsedAt
|
|
102
|
+
* only -- never the hash or plaintext). Use the returned `id` values
|
|
103
|
+
* with revokeSecret() to decommission an old/compromised secret.
|
|
104
|
+
* @param {string} id
|
|
105
|
+
* @returns {Promise<Object>} `{ secrets: [...] }`
|
|
106
|
+
*/
|
|
107
|
+
async listSecrets(id) {
|
|
108
|
+
this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
|
|
109
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}/secrets`, 'GET');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Mint a new client secret. The old secret keeps working until revoked.
|
|
114
|
+
* @param {string} id
|
|
115
|
+
* @returns {Promise<Object>} `{ secret }` -- plaintext, shown once
|
|
116
|
+
*/
|
|
117
|
+
async rotateSecret(id) {
|
|
118
|
+
this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
|
|
119
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}/secrets`, 'POST');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Revoke one of a client's secrets.
|
|
124
|
+
* @param {string} id
|
|
125
|
+
* @param {string} secretId
|
|
126
|
+
* @returns {Promise<Object>} `{ message }`
|
|
127
|
+
*/
|
|
128
|
+
async revokeSecret(id, secretId) {
|
|
129
|
+
this.sdk.validateParams(
|
|
130
|
+
{ id, secretId },
|
|
131
|
+
{ id: { type: 'string', required: true }, secretId: { type: 'string', required: true } },
|
|
132
|
+
);
|
|
133
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}/secrets/${secretId}`, 'DELETE');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* List a client's user consent grants.
|
|
138
|
+
* @param {string} id
|
|
139
|
+
* @returns {Promise<Object>} `{ grants: [...] }`
|
|
140
|
+
*/
|
|
141
|
+
async listGrants(id) {
|
|
142
|
+
this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
|
|
143
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}/grants`, 'GET');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Revoke a client's consent grant for one user.
|
|
148
|
+
* @param {string} id
|
|
149
|
+
* @param {string} grantId
|
|
150
|
+
* @returns {Promise<Object>} `{ message }`
|
|
151
|
+
*/
|
|
152
|
+
async revokeGrant(id, grantId) {
|
|
153
|
+
this.sdk.validateParams(
|
|
154
|
+
{ id, grantId },
|
|
155
|
+
{ id: { type: 'string', required: true }, grantId: { type: 'string', required: true } },
|
|
156
|
+
);
|
|
157
|
+
return internalRequest(this.sdk, `/oauth/clients/${id}/grants/${grantId}`, 'DELETE');
|
|
158
|
+
}
|
|
159
|
+
}
|