@jini-ai/integrations 0.4.1 → 0.4.3
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/CHANGELOG.md +13 -0
- package/dist/api-connections/access-token.d.ts +9 -0
- package/dist/api-connections/access-token.js +92 -0
- package/dist/api-connections/account-actions.d.ts +40 -0
- package/dist/api-connections/account-actions.js +110 -0
- package/dist/api-connections/attempt-state.d.ts +28 -0
- package/dist/api-connections/attempt-state.js +73 -0
- package/dist/api-connections/authorization.d.ts +45 -0
- package/dist/api-connections/authorization.js +120 -0
- package/dist/api-connections/deps.d.ts +41 -0
- package/dist/api-connections/deps.js +13 -0
- package/dist/api-connections/descriptor.d.ts +6 -0
- package/dist/api-connections/descriptor.js +111 -0
- package/dist/api-connections/errors.d.ts +23 -0
- package/dist/api-connections/errors.js +24 -0
- package/dist/api-connections/finish-authorization.d.ts +24 -0
- package/dist/api-connections/finish-authorization.js +117 -0
- package/dist/api-connections/index.d.ts +18 -0
- package/dist/api-connections/index.js +10 -0
- package/dist/api-connections/install-credential.d.ts +19 -0
- package/dist/api-connections/install-credential.js +12 -0
- package/dist/api-connections/keys.d.ts +27 -0
- package/dist/api-connections/keys.js +22 -0
- package/dist/api-connections/ports.d.ts +73 -0
- package/dist/api-connections/ports.js +2 -0
- package/dist/api-connections/records.d.ts +27 -0
- package/dist/api-connections/records.js +52 -0
- package/dist/api-connections/request-checks.d.ts +18 -0
- package/dist/api-connections/request-checks.js +67 -0
- package/dist/api-connections/service.d.ts +25 -0
- package/dist/api-connections/service.js +26 -0
- package/dist/api-connections/status.d.ts +28 -0
- package/dist/api-connections/status.js +36 -0
- package/dist/api-connections/token-store/errors.d.ts +20 -0
- package/dist/api-connections/token-store/errors.js +38 -0
- package/dist/api-connections/token-store/index.d.ts +6 -0
- package/dist/api-connections/token-store/index.js +6 -0
- package/dist/api-connections/token-store/record.d.ts +69 -0
- package/dist/api-connections/token-store/record.js +97 -0
- package/dist/api-connections/token-store/vault-token-store.d.ts +26 -0
- package/dist/api-connections/token-store/vault-token-store.js +215 -0
- package/dist/api-connections/types.d.ts +101 -0
- package/dist/api-connections/types.js +9 -0
- package/dist/credentialed-http/auth-schemes.js +8 -4
- package/dist/credentialed-http/credentialed-request.js +47 -77
- package/dist/credentialed-http/response-redaction.d.ts +5 -0
- package/dist/credentialed-http/response-redaction.js +62 -0
- package/dist/media-providers/catalog.d.ts +3 -1
- package/dist/media-providers/catalog.js +3 -1
- package/dist/media-providers/dispatch/async-operation-store.d.ts +31 -0
- package/dist/media-providers/dispatch/async-operation-store.js +75 -37
- package/dist/media-providers/dispatch/builtin-vendors.d.ts +1 -1
- package/dist/media-providers/dispatch/context.js +10 -2
- package/dist/media-providers/dispatch/credentials.d.ts +1 -1
- package/dist/media-providers/dispatch/credentials.js +3 -3
- package/dist/media-providers/dispatch/engine.d.ts +6 -3
- package/dist/media-providers/dispatch/engine.js +102 -109
- package/dist/media-providers/dispatch/index.d.ts +4 -2
- package/dist/media-providers/dispatch/index.js +3 -1
- package/dist/media-providers/dispatch/media-provider-registry.d.ts +62 -0
- package/dist/media-providers/dispatch/media-provider-registry.js +141 -0
- package/dist/media-providers/dispatch/node.d.ts +13 -0
- package/dist/media-providers/dispatch/node.js +24 -0
- package/dist/media-providers/dispatch/openai-compatible.d.ts +1 -1
- package/dist/media-providers/dispatch/openai-compatible.js +90 -87
- package/dist/media-providers/dispatch/operation-runtime.js +116 -89
- package/dist/media-providers/dispatch/outbound.d.ts +8 -4
- package/dist/media-providers/dispatch/outbound.js +27 -26
- package/dist/media-providers/dispatch/providers/aihubmix.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/aihubmix.js +61 -56
- package/dist/media-providers/dispatch/providers/custom-image.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/elevenlabs.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/fishaudio.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/grok.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.js +53 -49
- package/dist/media-providers/dispatch/providers/imagerouter.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/imagerouter.js +11 -24
- package/dist/media-providers/dispatch/providers/minimax.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/nanobanana.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/nanobanana.js +22 -30
- package/dist/media-providers/dispatch/providers/openai.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/openai.js +51 -47
- package/dist/media-providers/dispatch/providers/openrouter.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/openrouter.js +52 -37
- package/dist/media-providers/dispatch/providers/senseaudio.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/senseaudio.js +47 -28
- package/dist/media-providers/dispatch/providers/volcengine.d.ts +1 -1
- package/dist/media-providers/dispatch/response-parsers.js +37 -21
- package/dist/media-providers/dispatch/sqlite-async-operation-store.d.ts +35 -1
- package/dist/media-providers/dispatch/sqlite-async-operation-store.js +13 -32
- package/dist/media-providers/dispatch/ssrf-guard.d.ts +3 -2
- package/dist/media-providers/dispatch/ssrf-guard.js +6 -5
- package/dist/media-providers/dispatch/types.d.ts +76 -2
- package/dist/media-providers/dispatch/vendor-adapter.d.ts +2 -43
- package/dist/media-providers/dispatch/vendor-adapter.js +2 -2
- package/dist/media-providers/dispatch/vendor-registry-core.d.ts +24 -0
- package/dist/media-providers/dispatch/vendor-registry-core.js +34 -0
- package/dist/media-providers/dispatch/vendor-registry.d.ts +10 -36
- package/dist/media-providers/dispatch/vendor-registry.js +14 -42
- package/dist/media-providers/policy.js +25 -17
- package/dist/media-providers/providers.d.ts +39 -0
- package/dist/media-providers/providers.js +50 -18
- package/dist/media-providers/sqlite-driver.adapter.d.ts +4 -0
- package/dist/media-providers/sqlite-driver.adapter.js +5 -0
- package/dist/media-providers/sqlite-task-store.d.ts +50 -2
- package/dist/media-providers/sqlite-task-store.js +20 -84
- package/dist/media-providers/staging.js +77 -58
- package/dist/media-providers/task-lifecycle.d.ts +33 -0
- package/dist/media-providers/task-lifecycle.js +124 -0
- package/dist/media-providers/task-store.js +8 -86
- package/dist/media-providers/ui-defaults.d.ts +10 -0
- package/dist/media-providers/ui-defaults.js +167 -0
- package/dist/media-providers/video-request.js +112 -90
- package/dist/webhooks/delivery.d.ts +14 -0
- package/dist/webhooks/delivery.js +41 -10
- package/dist/webhooks/index.d.ts +1 -0
- package/dist/webhooks/index.js +1 -0
- package/dist/webhooks/ports.d.ts +23 -0
- package/dist/webhooks/subscriptions.d.ts +1 -1
- package/dist/webhooks/subscriptions.js +5 -1
- package/dist/webhooks/worker.d.ts +83 -0
- package/dist/webhooks/worker.js +105 -0
- package/package.json +35 -7
- package/dist/composio/bounded-data.d.ts +0 -36
- package/dist/composio/bounded-data.js +0 -181
- package/dist/composio/catalog.d.ts +0 -147
- package/dist/composio/catalog.js +0 -126
- package/dist/composio/composio-config.d.ts +0 -42
- package/dist/composio/composio-config.js +0 -145
- package/dist/composio/composio-descriptions.d.ts +0 -16
- package/dist/composio/composio-descriptions.js +0 -766
- package/dist/composio/composio.d.ts +0 -364
- package/dist/composio/composio.js +0 -1902
- package/dist/composio/errors.d.ts +0 -15
- package/dist/composio/errors.js +0 -16
- package/dist/composio/file-lock.d.ts +0 -13
- package/dist/composio/file-lock.js +0 -92
- package/dist/composio/index.d.ts +0 -18
- package/dist/composio/index.js +0 -7
- package/dist/composio/json-schema.d.ts +0 -93
- package/dist/composio/json-schema.js +0 -498
- package/dist/composio/output-protection.d.ts +0 -32
- package/dist/composio/output-protection.js +0 -105
- package/dist/composio/service.d.ts +0 -181
- package/dist/composio/service.js +0 -758
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Turns a plugin's untrusted connection declaration into a frozen, bounded descriptor.
|
|
3
|
+
*
|
|
4
|
+
* Exists because the declaration arrives from plugin manifests: every id, URL, parameter and
|
|
5
|
+
* field limit is checked once here so the flow code can trust the shape. Endpoint safety (scheme,
|
|
6
|
+
* private hosts, DNS) is still enforced at use by the injected OAuth URL guard.
|
|
7
|
+
*/
|
|
8
|
+
import { isCallbackFieldName, MAX_CALLBACK_FIELD_LENGTH, MAX_CALLBACK_FIELDS } from '@jini-ai/oauth';
|
|
9
|
+
import { ApiConnectionError } from './errors.js';
|
|
10
|
+
const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
11
|
+
const PARAM_NAME_PATTERN = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
12
|
+
const MAX_LABEL_LENGTH = 120;
|
|
13
|
+
const MAX_URL_LENGTH = 2048;
|
|
14
|
+
const MAX_PARAMS = 16;
|
|
15
|
+
const MAX_PARAM_VALUE_LENGTH = 512;
|
|
16
|
+
/** Start values ride in the authorization URL; 8 KiB stays within common server request-line limits. */
|
|
17
|
+
const START_PARAM_RULES = { maxFields: 8, maxLength: 8192, acceptsName: () => true };
|
|
18
|
+
/** The loopback receiver's own limits and name rule (one source of truth in the OAuth core), so a
|
|
19
|
+
* declaration accepted here never fails only on desktop. */
|
|
20
|
+
const CALLBACK_FIELD_RULES = { maxFields: MAX_CALLBACK_FIELDS, maxLength: MAX_CALLBACK_FIELD_LENGTH, acceptsName: isCallbackFieldName };
|
|
21
|
+
/** Names the OAuth core sets itself or reads from the callback; a plugin may not shadow them. */
|
|
22
|
+
const RESERVED_NAMES = new Set([
|
|
23
|
+
'resource', 'response_type', 'client_id', 'redirect_uri', 'scope', 'state', 'code',
|
|
24
|
+
'code_challenge', 'code_challenge_method', 'code_verifier', 'error', 'error_description', 'error_uri', 'iss',
|
|
25
|
+
]);
|
|
26
|
+
function invalid(detail) {
|
|
27
|
+
return new ApiConnectionError({
|
|
28
|
+
code: 'descriptor-invalid',
|
|
29
|
+
message: `The plugin's connection declaration is invalid (${detail}). Fix the plugin manifest and reload it.`,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function isRecord(value) {
|
|
33
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
function readId(source, name) {
|
|
36
|
+
const value = source[name];
|
|
37
|
+
if (typeof value !== 'string' || !ID_PATTERN.test(value))
|
|
38
|
+
throw invalid(`${name} must be a short id`);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
function readLabel(value) {
|
|
42
|
+
if (typeof value !== 'string' || value.trim() === '' || value.length > MAX_LABEL_LENGTH)
|
|
43
|
+
throw invalid('label');
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
function readUrl(value, name) {
|
|
47
|
+
if (typeof value !== 'string' || value.length > MAX_URL_LENGTH || !URL.canParse(value))
|
|
48
|
+
throw invalid(`${name} must be a URL`);
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
function readParamName(value, where, acceptsName = () => true) {
|
|
52
|
+
if (typeof value !== 'string' || !PARAM_NAME_PATTERN.test(value) || RESERVED_NAMES.has(value) || !acceptsName(value)) {
|
|
53
|
+
throw invalid(`${where} uses an invalid or reserved name`);
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
function readParams(value, where) {
|
|
58
|
+
if (value === undefined)
|
|
59
|
+
return Object.freeze({});
|
|
60
|
+
if (!isRecord(value) || Object.keys(value).length > MAX_PARAMS)
|
|
61
|
+
throw invalid(where);
|
|
62
|
+
const params = {};
|
|
63
|
+
for (const [name, raw] of Object.entries(value)) {
|
|
64
|
+
if (typeof raw !== 'string' || raw.length > MAX_PARAM_VALUE_LENGTH)
|
|
65
|
+
throw invalid(`${where} value`);
|
|
66
|
+
params[readParamName(name, where)] = raw;
|
|
67
|
+
}
|
|
68
|
+
return Object.freeze(params);
|
|
69
|
+
}
|
|
70
|
+
function readFields(value, where, rules) {
|
|
71
|
+
if (value === undefined)
|
|
72
|
+
return Object.freeze([]);
|
|
73
|
+
if (!Array.isArray(value) || value.length > rules.maxFields)
|
|
74
|
+
throw invalid(where);
|
|
75
|
+
const fields = value.map((entry) => {
|
|
76
|
+
if (!isRecord(entry))
|
|
77
|
+
throw invalid(where);
|
|
78
|
+
const { maxLength } = entry;
|
|
79
|
+
if (!Number.isSafeInteger(maxLength) || maxLength < 1 || maxLength > rules.maxLength) {
|
|
80
|
+
throw invalid(`${where} maxLength`);
|
|
81
|
+
}
|
|
82
|
+
return Object.freeze({ name: readParamName(entry.name, where, rules.acceptsName), maxLength: maxLength });
|
|
83
|
+
});
|
|
84
|
+
if (new Set(fields.map(field => field.name)).size !== fields.length)
|
|
85
|
+
throw invalid(`${where} repeats a name`);
|
|
86
|
+
return Object.freeze(fields);
|
|
87
|
+
}
|
|
88
|
+
/** Validate and freeze a plugin-declared connection. Throws `descriptor-invalid` naming the bad part. */
|
|
89
|
+
export function parseApiConnectionDescriptor(requiredArgs) {
|
|
90
|
+
const source = requiredArgs.value;
|
|
91
|
+
if (!isRecord(source))
|
|
92
|
+
throw invalid('expected an object');
|
|
93
|
+
const params = source.authorizationParams === undefined ? {} : source.authorizationParams;
|
|
94
|
+
if (!isRecord(params))
|
|
95
|
+
throw invalid('authorizationParams');
|
|
96
|
+
return Object.freeze({
|
|
97
|
+
pluginId: readId(source, 'pluginId'),
|
|
98
|
+
connectionId: readId(source, 'connectionId'),
|
|
99
|
+
label: readLabel(source.label),
|
|
100
|
+
authorizationEndpoint: readUrl(source.authorizationEndpoint, 'authorizationEndpoint'),
|
|
101
|
+
tokenEndpoint: readUrl(source.tokenEndpoint, 'tokenEndpoint'),
|
|
102
|
+
authorizationParams: Object.freeze({
|
|
103
|
+
public: readParams(params.public, 'authorizationParams.public'),
|
|
104
|
+
confidential: readParams(params.confidential, 'authorizationParams.confidential'),
|
|
105
|
+
}),
|
|
106
|
+
forceConsentParams: readParams(source.forceConsentParams, 'forceConsentParams'),
|
|
107
|
+
startParams: readFields(source.startParams, 'startParams', START_PARAM_RULES),
|
|
108
|
+
callbackFields: readFields(source.callbackFields, 'callbackFields', CALLBACK_FIELD_RULES),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=descriptor.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The one error type API-connection operations throw.
|
|
3
|
+
*
|
|
4
|
+
* Exists so hosts branch on a stable `code` and show `message`, which always says what failed and
|
|
5
|
+
* what to do next. Messages never carry tokens, codes, verifiers or provider response bodies;
|
|
6
|
+
* the underlying failure stays on `cause` for server-side diagnosis.
|
|
7
|
+
*/
|
|
8
|
+
import { IntegrationError } from '../argument-error.js';
|
|
9
|
+
export type ApiConnectionErrorCode = 'descriptor-invalid' | 'configuration-invalid' | 'request-invalid' | 'connection-not-found' | 'not-connected' | 'reconnect-required' | 'revocation-pending' | 'invalid-attempt' | 'attempt-superseded' | 'access-denied' | 'authorization-failed' | 'scope-not-granted' | 'account-mismatch' | 'account-changed' | 'account-unchanged' | 'account-identity-invalid' | 'switch-not-staged' | 'switch-in-progress' | 'offline-access-required' | 'storage-conflict';
|
|
10
|
+
export declare class ApiConnectionError extends IntegrationError {
|
|
11
|
+
readonly code: ApiConnectionErrorCode;
|
|
12
|
+
constructor({ code, message }: {
|
|
13
|
+
code: ApiConnectionErrorCode;
|
|
14
|
+
message: string;
|
|
15
|
+
}, { cause }?: {
|
|
16
|
+
cause?: unknown;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/** Shared wording keeps every attempt failure indistinguishable to a caller probing states. */
|
|
20
|
+
export declare function invalidAttempt(_requiredArgs?: Record<string, never>, { cause }?: {
|
|
21
|
+
cause?: unknown;
|
|
22
|
+
}): ApiConnectionError;
|
|
23
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The one error type API-connection operations throw.
|
|
3
|
+
*
|
|
4
|
+
* Exists so hosts branch on a stable `code` and show `message`, which always says what failed and
|
|
5
|
+
* what to do next. Messages never carry tokens, codes, verifiers or provider response bodies;
|
|
6
|
+
* the underlying failure stays on `cause` for server-side diagnosis.
|
|
7
|
+
*/
|
|
8
|
+
import { IntegrationError } from '../argument-error.js';
|
|
9
|
+
export class ApiConnectionError extends IntegrationError {
|
|
10
|
+
code;
|
|
11
|
+
constructor({ code, message }, { cause } = {}) {
|
|
12
|
+
super({ message }, cause === undefined ? {} : { options: { cause } });
|
|
13
|
+
this.name = 'ApiConnectionError';
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Shared wording keeps every attempt failure indistinguishable to a caller probing states. */
|
|
18
|
+
export function invalidAttempt(_requiredArgs = {}, { cause } = {}) {
|
|
19
|
+
return new ApiConnectionError({
|
|
20
|
+
code: 'invalid-attempt',
|
|
21
|
+
message: 'The sign-in request could not be matched; it may have expired or already been used. Start connecting again.',
|
|
22
|
+
}, { cause });
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { OAuthTokenSet } from '@jini-ai/oauth';
|
|
2
|
+
import type { ApiConnectionServiceDeps } from './deps.js';
|
|
3
|
+
import type { ApiConnectionAccount, ApiConnectionAttempt } from './types.js';
|
|
4
|
+
export interface CompletedAuthorization {
|
|
5
|
+
readonly connectionRef: string;
|
|
6
|
+
/** The `attemptId` `start` returned for this sign-in. */
|
|
7
|
+
readonly attemptId: string;
|
|
8
|
+
/** `staged` means a switch-account result awaits `confirmSwitchAccount`. */
|
|
9
|
+
readonly outcome: 'connected' | 'staged';
|
|
10
|
+
readonly account: ApiConnectionAccount;
|
|
11
|
+
readonly grantedScopes: readonly string[];
|
|
12
|
+
/** Plugin-declared callback fields; trust them only after the plugin verifies them via the API. */
|
|
13
|
+
readonly callbackFields: Readonly<Record<string, string>>;
|
|
14
|
+
}
|
|
15
|
+
/** The stored live tokens, or null when none are stored or they are unreadable to this host. */
|
|
16
|
+
export declare function loadReadableTokens(deps: ApiConnectionServiceDeps, requiredArgs: {
|
|
17
|
+
readonly key: string;
|
|
18
|
+
}): Promise<OAuthTokenSet | null>;
|
|
19
|
+
export declare function finishAuthorization(deps: ApiConnectionServiceDeps, requiredArgs: {
|
|
20
|
+
readonly attempt: ApiConnectionAttempt;
|
|
21
|
+
readonly tokens: OAuthTokenSet;
|
|
22
|
+
readonly callbackFields: Readonly<Record<string, string>>;
|
|
23
|
+
}): Promise<CompletedAuthorization>;
|
|
24
|
+
//# sourceMappingURL=finish-authorization.d.ts.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Decides what an exchanged token set becomes: a live credential or a staged switch.
|
|
3
|
+
*
|
|
4
|
+
* Exists to hold the post-exchange rules in one place: every requested scope must be granted,
|
|
5
|
+
* the plugin's identity hook must name the account, the attempt must still match the current
|
|
6
|
+
* credential generation, a reconnect must be the same account, and a switch is only staged.
|
|
7
|
+
*/
|
|
8
|
+
import { nowIso } from '@jini-ai/core/primitives';
|
|
9
|
+
import { ATTEMPT_TTL_MS } from './attempt-state.js';
|
|
10
|
+
import { ApiConnectionError } from './errors.js';
|
|
11
|
+
import { installCredential } from './install-credential.js';
|
|
12
|
+
import { stagedTokenKey } from './keys.js';
|
|
13
|
+
import { blankRecord, writeRecord } from './records.js';
|
|
14
|
+
import { isUnreadableTokenRecord } from './token-store/errors.js';
|
|
15
|
+
const MAX_IDENTITY_LENGTH = 256;
|
|
16
|
+
function grantedScopesOf(tokens, requested) {
|
|
17
|
+
// RFC 6749 §5.1: a token response omits `scope` only when it equals the requested scope.
|
|
18
|
+
return tokens.scopes.length > 0 ? tokens.scopes : requested;
|
|
19
|
+
}
|
|
20
|
+
function assertGranted(requested, granted) {
|
|
21
|
+
if (requested.some(scope => !granted.includes(scope))) {
|
|
22
|
+
throw new ApiConnectionError({ code: 'scope-not-granted',
|
|
23
|
+
message: 'The account did not grant every permission this connection needs. Connect again and allow all requested permissions.' });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function isBoundedText(value, minLength) {
|
|
27
|
+
return typeof value === 'string' && value.length >= minLength && value.length <= MAX_IDENTITY_LENGTH;
|
|
28
|
+
}
|
|
29
|
+
async function readAccount(deps, accessToken) {
|
|
30
|
+
const failed = (cause) => new ApiConnectionError({ code: 'account-identity-invalid',
|
|
31
|
+
message: 'Could not confirm which account signed in. Try connecting again.' }, { cause });
|
|
32
|
+
const account = await deps.hooks.readAccount({ accessToken }).catch((cause) => { throw failed(cause); });
|
|
33
|
+
if (!isBoundedText(account?.accountId, 1) || !isBoundedText(account.displayName, 0))
|
|
34
|
+
throw failed();
|
|
35
|
+
return { accountId: account.accountId, displayName: account.displayName };
|
|
36
|
+
}
|
|
37
|
+
async function currentRecord(deps, attempt) {
|
|
38
|
+
const record = await deps.records.read({ key: attempt.key }) ?? blankRecord(deps, attempt);
|
|
39
|
+
// A reconnect, switch, disconnect or newer completed attempt moved the generation: this one is stale.
|
|
40
|
+
if (record.generation !== attempt.generation) {
|
|
41
|
+
throw new ApiConnectionError({ code: 'attempt-superseded',
|
|
42
|
+
message: 'A newer sign-in or account change replaced this one. Reload the connection status and start again if needed.' });
|
|
43
|
+
}
|
|
44
|
+
// Start refuses while a switch is staged; this catches one staged in the meantime. Either intent
|
|
45
|
+
// would otherwise overwrite (switch) or drop unrevoked (connect) the consented staged credential.
|
|
46
|
+
if (record.staged !== null) {
|
|
47
|
+
throw new ApiConnectionError({ code: 'switch-in-progress',
|
|
48
|
+
message: 'An account switch was staged while this sign-in ran. Confirm or cancel it, then sign in again.' });
|
|
49
|
+
}
|
|
50
|
+
return record;
|
|
51
|
+
}
|
|
52
|
+
/** The stored live tokens, or null when none are stored or they are unreadable to this host. */
|
|
53
|
+
export async function loadReadableTokens(deps, requiredArgs) {
|
|
54
|
+
// A row sealed for another client (the host changed its OAuth client) has no usable refresh token.
|
|
55
|
+
return deps.tokens.load({ key: requiredArgs.key }).catch((error) => {
|
|
56
|
+
if (isUnreadableTokenRecord(error))
|
|
57
|
+
return null;
|
|
58
|
+
throw error;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** Re-consent for the same account may omit a refresh token; the stored one stays valid. */
|
|
62
|
+
async function keepRefreshToken(deps, record, tokens, account) {
|
|
63
|
+
if (tokens.refreshToken !== null || record.account?.accountId !== account.accountId)
|
|
64
|
+
return tokens;
|
|
65
|
+
const existing = await loadReadableTokens(deps, { key: record.key });
|
|
66
|
+
return existing?.refreshToken ? { ...tokens, refreshToken: existing.refreshToken } : tokens;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Without a refresh token the credential would stop working, silently, when its access token
|
|
70
|
+
* expires: an installed app has no other way to renew, and a hosted client gets "reconnect
|
|
71
|
+
* required" within the hour, in a loop when the provider again omits one on the next consent.
|
|
72
|
+
* Refuse before anything is written.
|
|
73
|
+
*/
|
|
74
|
+
function assertRenewable(tokens) {
|
|
75
|
+
if (tokens.refreshToken === null) {
|
|
76
|
+
throw new ApiConnectionError({ code: 'offline-access-required',
|
|
77
|
+
message: 'The account did not grant offline access, so this connection would stop working within the hour. Connect again and allow offline access.' });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function commitConnect(deps, record, tokens, account, grantedScopes) {
|
|
81
|
+
if (record.account !== null && record.account.accountId !== account.accountId) {
|
|
82
|
+
throw new ApiConnectionError({ code: 'account-mismatch',
|
|
83
|
+
message: 'A different account signed in than the one connected. Use "switch account" to change accounts.' });
|
|
84
|
+
}
|
|
85
|
+
const kept = await keepRefreshToken(deps, record, tokens, account);
|
|
86
|
+
assertRenewable(kept);
|
|
87
|
+
await installCredential(deps, { current: record, tokens: kept, account, grantedScopes });
|
|
88
|
+
}
|
|
89
|
+
async function stageSwitch(deps, record, attempt, tokens, account, grantedScopes) {
|
|
90
|
+
// The provider usually holds one grant per client and account, so a staged copy of the live
|
|
91
|
+
// account would share its grant: cancelling the switch would then revoke the live credential.
|
|
92
|
+
if (record.account?.accountId === account.accountId) {
|
|
93
|
+
throw new ApiConnectionError({ code: 'account-unchanged',
|
|
94
|
+
message: 'This is the account already connected. Use reconnect instead of switching accounts.' });
|
|
95
|
+
}
|
|
96
|
+
assertRenewable(tokens);
|
|
97
|
+
const expiresAt = new Date(Date.parse(nowIso({ clock: deps.clock })) + ATTEMPT_TTL_MS).toISOString();
|
|
98
|
+
// Claim the staged slot on the record first: of two concurrent switches only the compare-and-set
|
|
99
|
+
// winner writes tokens, so the slot can never hold one account while the record names another.
|
|
100
|
+
await writeRecord(deps, record, {
|
|
101
|
+
staged: { account, grantedScopes: [...grantedScopes], actorId: attempt.actorId, generation: record.generation, expiresAt },
|
|
102
|
+
});
|
|
103
|
+
await deps.tokens.install({ key: stagedTokenKey({ key: record.key }), tokens, generation: record.generation });
|
|
104
|
+
}
|
|
105
|
+
export async function finishAuthorization(deps, requiredArgs) {
|
|
106
|
+
const { attempt, tokens } = requiredArgs;
|
|
107
|
+
const grantedScopes = grantedScopesOf(tokens, attempt.requestedScopes);
|
|
108
|
+
assertGranted(attempt.requestedScopes, grantedScopes);
|
|
109
|
+
const account = await readAccount(deps, tokens.accessToken);
|
|
110
|
+
const record = await currentRecord(deps, attempt);
|
|
111
|
+
const staged = attempt.intent === 'switch-account';
|
|
112
|
+
await (staged ? stageSwitch(deps, record, attempt, tokens, account, grantedScopes)
|
|
113
|
+
: commitConnect(deps, record, tokens, account, grantedScopes));
|
|
114
|
+
return { connectionRef: attempt.key, attemptId: attempt.attemptId, outcome: staged ? 'staged' : 'connected', account, grantedScopes,
|
|
115
|
+
callbackFields: requiredArgs.callbackFields };
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=finish-authorization.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Public entry `@jini-ai/integrations/api-connections`: plugin-declared OAuth connections
|
|
3
|
+
* (sign-in, status, reconnect, staged account switch, disconnect/revoke, access tokens).
|
|
4
|
+
*/
|
|
5
|
+
export { parseApiConnectionDescriptor } from './descriptor.js';
|
|
6
|
+
export { createApiConnectionService, type ApiConnectionService } from './service.js';
|
|
7
|
+
export { deriveApiConnectionKey, createApiConnectionGenerationReader } from './keys.js';
|
|
8
|
+
export { ApiConnectionError, type ApiConnectionErrorCode } from './errors.js';
|
|
9
|
+
export type { ApiConnectionClientKind, ApiConnectionServiceDeps } from './deps.js';
|
|
10
|
+
export type { ApiConnectionHooks, ApiConnectionRecordStore, ApiConnectionTokenStore } from './ports.js';
|
|
11
|
+
export type { StartApiConnectionResult } from './authorization.js';
|
|
12
|
+
export type { CompletedAuthorization } from './finish-authorization.js';
|
|
13
|
+
export type { ApiConnectionStatusView } from './status.js';
|
|
14
|
+
export type { ApiConnectionDisconnectResult, ApiConnectionRemoteRevocation, ApiConnectionSwitchResult } from './account-actions.js';
|
|
15
|
+
export type { ApiConnectionAccessTokens } from './access-token.js';
|
|
16
|
+
export type { ApiConnectionAccount, ApiConnectionAttempt, ApiConnectionBoundedField, ApiConnectionDescriptor, ApiConnectionIntent, ApiConnectionRecord, ApiConnectionRevocationFailure, ApiConnectionStagedSwitch, ApiConnectionStatus, } from './types.js';
|
|
17
|
+
export { createVaultOAuthTokenStore, parseOAuthTokenVaultMeta, type VaultOAuthTokenStore, type VaultOAuthTokenStoreDeps, type OAuthTokenVaultPort, type ReadConnectionGeneration, type OAuthConnectionOwner, type OAuthConnectionStatus, type OAuthTokenStoreErrorCode, } from './token-store/index.js';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Public entry `@jini-ai/integrations/api-connections`: plugin-declared OAuth connections
|
|
3
|
+
* (sign-in, status, reconnect, staged account switch, disconnect/revoke, access tokens).
|
|
4
|
+
*/
|
|
5
|
+
export { parseApiConnectionDescriptor } from './descriptor.js';
|
|
6
|
+
export { createApiConnectionService } from './service.js';
|
|
7
|
+
export { deriveApiConnectionKey, createApiConnectionGenerationReader } from './keys.js';
|
|
8
|
+
export { ApiConnectionError } from './errors.js';
|
|
9
|
+
export { createVaultOAuthTokenStore, parseOAuthTokenVaultMeta, } from './token-store/index.js';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Replaces a connection's live credential in three fenced steps.
|
|
3
|
+
*
|
|
4
|
+
* Token and record stores cannot share a transaction, so the record first moves to a new
|
|
5
|
+
* generation in the fail-safe `reconnect-required` state (no token can be served and any write
|
|
6
|
+
* fenced on the old generation is refused), then the tokens are written, then the record becomes
|
|
7
|
+
* `connected` with the new account. A crash between steps leaves "reconnect required", never a
|
|
8
|
+
* record that claims one account while the store serves another account's token.
|
|
9
|
+
*/
|
|
10
|
+
import type { OAuthTokenSet } from '@jini-ai/oauth';
|
|
11
|
+
import type { ApiConnectionServiceDeps } from './deps.js';
|
|
12
|
+
import type { ApiConnectionAccount, ApiConnectionRecord } from './types.js';
|
|
13
|
+
export declare function installCredential(deps: Pick<ApiConnectionServiceDeps, 'records' | 'tokens' | 'clock' | 'descriptor'>, requiredArgs: {
|
|
14
|
+
readonly current: ApiConnectionRecord;
|
|
15
|
+
readonly tokens: OAuthTokenSet;
|
|
16
|
+
readonly account: ApiConnectionAccount;
|
|
17
|
+
readonly grantedScopes: readonly string[];
|
|
18
|
+
}): Promise<ApiConnectionRecord>;
|
|
19
|
+
//# sourceMappingURL=install-credential.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { writeRecord } from './records.js';
|
|
2
|
+
export async function installCredential(deps, requiredArgs) {
|
|
3
|
+
const fenced = await writeRecord(deps, requiredArgs.current, {
|
|
4
|
+
status: 'reconnect-required', generation: requiredArgs.current.generation + 1, staged: null,
|
|
5
|
+
});
|
|
6
|
+
// Fenced on the new generation: a disconnect, switch or newer attempt that moves it first wins.
|
|
7
|
+
await deps.tokens.install({ key: fenced.key, tokens: requiredArgs.tokens, generation: fenced.generation });
|
|
8
|
+
return writeRecord(deps, fenced, {
|
|
9
|
+
status: 'connected', account: requiredArgs.account, grantedScopes: [...requiredArgs.grantedScopes], revocationFailure: null,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=install-credential.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Opaque connection keys and the generation reader a vault token store fences on.
|
|
3
|
+
*
|
|
4
|
+
* Exists so one function owns the workspace+plugin+connection → key mapping. The key doubles as
|
|
5
|
+
* the public `connectionRef`; percent-encoding each part keeps the mapping injective, and the
|
|
6
|
+
* staged-switch key lives beside it so the same record fences both.
|
|
7
|
+
*/
|
|
8
|
+
import type { ApiConnectionRecordStore } from './ports.js';
|
|
9
|
+
export declare function deriveApiConnectionKey(requiredArgs: {
|
|
10
|
+
readonly workspaceId: string;
|
|
11
|
+
readonly pluginId: string;
|
|
12
|
+
readonly connectionId: string;
|
|
13
|
+
}): string;
|
|
14
|
+
/** Token-store key for tokens of a switch-account result awaiting confirmation. */
|
|
15
|
+
export declare function stagedTokenKey(requiredArgs: {
|
|
16
|
+
readonly key: string;
|
|
17
|
+
}): string;
|
|
18
|
+
/**
|
|
19
|
+
* Fence reader for a token store: the credential generation of the record owning `key`
|
|
20
|
+
* (the main or staged token key), 0 when no record exists. Generation only ever increases.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createApiConnectionGenerationReader(requiredArgs: {
|
|
23
|
+
readonly records: ApiConnectionRecordStore;
|
|
24
|
+
}): ({ key }: {
|
|
25
|
+
readonly key: string;
|
|
26
|
+
}) => Promise<number>;
|
|
27
|
+
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const KEY_PREFIX = 'api-connection:v1:';
|
|
2
|
+
const STAGED_SUFFIX = '#staged';
|
|
3
|
+
export function deriveApiConnectionKey(requiredArgs) {
|
|
4
|
+
const parts = [requiredArgs.workspaceId, requiredArgs.pluginId, requiredArgs.connectionId];
|
|
5
|
+
return KEY_PREFIX + parts.map(part => encodeURIComponent(part)).join(':');
|
|
6
|
+
}
|
|
7
|
+
/** Token-store key for tokens of a switch-account result awaiting confirmation. */
|
|
8
|
+
export function stagedTokenKey(requiredArgs) {
|
|
9
|
+
return requiredArgs.key + STAGED_SUFFIX;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fence reader for a token store: the credential generation of the record owning `key`
|
|
13
|
+
* (the main or staged token key), 0 when no record exists. Generation only ever increases.
|
|
14
|
+
*/
|
|
15
|
+
export function createApiConnectionGenerationReader(requiredArgs) {
|
|
16
|
+
return async function readGeneration({ key }) {
|
|
17
|
+
const recordKey = key.endsWith(STAGED_SUFFIX) ? key.slice(0, -STAGED_SUFFIX.length) : key;
|
|
18
|
+
const record = await requiredArgs.records.read({ key: recordKey });
|
|
19
|
+
return record?.generation ?? 0;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=keys.js.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Ports the host and the provider plugin implement for API connections.
|
|
3
|
+
*
|
|
4
|
+
* Exists so storage, sealing and provider specifics stay outside the engine: the host owns the
|
|
5
|
+
* vault-backed token store and the metadata store; the plugin owns scope sets, identity and
|
|
6
|
+
* revocation. Tests use typed in-memory fakes of these same interfaces.
|
|
7
|
+
*/
|
|
8
|
+
import type { OAuthTokenSet, OAuthTokenStore } from '@jini-ai/oauth';
|
|
9
|
+
import type { ApiConnectionAccount, ApiConnectionAttempt, ApiConnectionRecord } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Secret storage keyed by the opaque connection key; `delete` clears local tokens on disconnect.
|
|
12
|
+
* `persist` is the refresher's write (the store may require its refresh lease); `install` writes a
|
|
13
|
+
* freshly issued sign-in or switch credential for `generation`, needs no lease, and must refuse
|
|
14
|
+
* once the connection's live generation differs or a newer generation's tokens are stored.
|
|
15
|
+
* `load` must return null for tokens sealed under an older generation (that is how a disconnect
|
|
16
|
+
* disables a credential); `loadForRevocation` must return them anyway, because only the
|
|
17
|
+
* disabled credential can still revoke the grant. The engine passes its result to the plugin's
|
|
18
|
+
* revoke hook and nowhere else: never wire it into a refresher, tool or host read path.
|
|
19
|
+
*/
|
|
20
|
+
export type ApiConnectionTokenStore = OAuthTokenStore & {
|
|
21
|
+
install(requiredArgs: {
|
|
22
|
+
readonly key: string;
|
|
23
|
+
readonly tokens: OAuthTokenSet;
|
|
24
|
+
readonly generation: number;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
loadForRevocation(requiredArgs: {
|
|
27
|
+
readonly key: string;
|
|
28
|
+
}): Promise<OAuthTokenSet | null>;
|
|
29
|
+
delete(requiredArgs: {
|
|
30
|
+
readonly key: string;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
};
|
|
33
|
+
/** Non-secret metadata and attempts. Every method must be atomic per key or state. */
|
|
34
|
+
export interface ApiConnectionRecordStore {
|
|
35
|
+
read(requiredArgs: {
|
|
36
|
+
readonly key: string;
|
|
37
|
+
}): Promise<ApiConnectionRecord | null>;
|
|
38
|
+
/** Write only when the stored revision equals `expectedRevision` (0 = no record yet). */
|
|
39
|
+
compareAndSet(requiredArgs: {
|
|
40
|
+
readonly key: string;
|
|
41
|
+
readonly expectedRevision: number;
|
|
42
|
+
readonly record: ApiConnectionRecord;
|
|
43
|
+
}): Promise<boolean>;
|
|
44
|
+
putAttempt(requiredArgs: {
|
|
45
|
+
readonly attempt: ApiConnectionAttempt;
|
|
46
|
+
}): Promise<void>;
|
|
47
|
+
/** Remove and return the attempt in one step; a replayed state returns null. */
|
|
48
|
+
takeAttempt(requiredArgs: {
|
|
49
|
+
readonly state: string;
|
|
50
|
+
}): Promise<ApiConnectionAttempt | null>;
|
|
51
|
+
}
|
|
52
|
+
/** Provider behaviour the plugin supplies; the engine never calls a provider API itself. */
|
|
53
|
+
export interface ApiConnectionHooks {
|
|
54
|
+
/** Map opaque scope-set ids (e.g. enabled product modules) to the scopes to request. */
|
|
55
|
+
resolveScopes(requiredArgs: {
|
|
56
|
+
readonly scopeSetIds: readonly string[];
|
|
57
|
+
}): Promise<readonly string[]>;
|
|
58
|
+
/** Read the signed-in account with a freshly issued access token. */
|
|
59
|
+
readAccount(requiredArgs: {
|
|
60
|
+
readonly accessToken: string;
|
|
61
|
+
}): Promise<ApiConnectionAccount>;
|
|
62
|
+
/**
|
|
63
|
+
* Revoke the grant remotely. Throw when the provider did not confirm revocation. A retry may
|
|
64
|
+
* send a token the provider already revoked: report that as success, not as a failure. To
|
|
65
|
+
* classify a failure for operators, give the thrown error `code: 'provider-unreachable'` (no
|
|
66
|
+
* answer) or `code: 'provider-refused'` (an answer that was not a confirmation); the engine
|
|
67
|
+
* records only that code (anything else is `unclassified`) and never reads the message.
|
|
68
|
+
*/
|
|
69
|
+
revoke(requiredArgs: {
|
|
70
|
+
readonly token: string;
|
|
71
|
+
}): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=ports.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ApiConnectionServiceDeps } from './deps.js';
|
|
2
|
+
import { ApiConnectionError } from './errors.js';
|
|
3
|
+
import type { ApiConnectionRecord } from './types.js';
|
|
4
|
+
type RecordDeps = Pick<ApiConnectionServiceDeps, 'records' | 'clock' | 'descriptor'>;
|
|
5
|
+
export type RecordPatch = Partial<Pick<ApiConnectionRecord, 'status' | 'generation' | 'account' | 'grantedScopes' | 'staged' | 'revocationFailure'>>;
|
|
6
|
+
export declare function connectionNotFound(): ApiConnectionError;
|
|
7
|
+
/** The ref a caller passes plus the workspace its host authorized it for. */
|
|
8
|
+
export interface OwnedRecordArgs {
|
|
9
|
+
readonly connectionRef: string;
|
|
10
|
+
readonly workspaceId: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A service bound to one plugin connection must never act on another plugin's record, and a ref
|
|
14
|
+
* from another workspace is refused here too, so a host cannot serve one by forgetting to check.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isOwnedRecord(record: ApiConnectionRecord, deps: Pick<RecordDeps, 'descriptor'>, workspaceId: string): boolean;
|
|
17
|
+
/** Load a record owned by this service's plugin connection in `workspaceId`, or throw `connection-not-found`. */
|
|
18
|
+
export declare function loadOwnedRecord(deps: RecordDeps, requiredArgs: OwnedRecordArgs): Promise<ApiConnectionRecord>;
|
|
19
|
+
/** Empty record for a first connect; revision 0 means "absent" to compare-and-set. */
|
|
20
|
+
export declare function blankRecord(deps: RecordDeps, requiredArgs: {
|
|
21
|
+
readonly key: string;
|
|
22
|
+
readonly workspaceId: string;
|
|
23
|
+
}): ApiConnectionRecord;
|
|
24
|
+
/** Apply `patch` iff nobody wrote since `current` was read; returns the stored record. */
|
|
25
|
+
export declare function writeRecord(deps: RecordDeps, current: ApiConnectionRecord, patch: RecordPatch): Promise<ApiConnectionRecord>;
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=records.d.ts.map
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Read and compare-and-set helpers over the record store.
|
|
3
|
+
*
|
|
4
|
+
* Exists so every write bumps `revision` the same way and a lost race surfaces as one
|
|
5
|
+
* `storage-conflict` error instead of a silent overwrite, and so a service can only touch
|
|
6
|
+
* records of its own plugin connection.
|
|
7
|
+
*/
|
|
8
|
+
import { nowIso } from '@jini-ai/core/primitives';
|
|
9
|
+
import { ApiConnectionError } from './errors.js';
|
|
10
|
+
export function connectionNotFound() {
|
|
11
|
+
return new ApiConnectionError({
|
|
12
|
+
code: 'connection-not-found',
|
|
13
|
+
message: 'This connection does not exist for this plugin in this workspace. Connect the account first.',
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A service bound to one plugin connection must never act on another plugin's record, and a ref
|
|
18
|
+
* from another workspace is refused here too, so a host cannot serve one by forgetting to check.
|
|
19
|
+
*/
|
|
20
|
+
export function isOwnedRecord(record, deps, workspaceId) {
|
|
21
|
+
return record.pluginId === deps.descriptor.pluginId && record.connectionId === deps.descriptor.connectionId
|
|
22
|
+
&& record.workspaceId === workspaceId;
|
|
23
|
+
}
|
|
24
|
+
/** Load a record owned by this service's plugin connection in `workspaceId`, or throw `connection-not-found`. */
|
|
25
|
+
export async function loadOwnedRecord(deps, requiredArgs) {
|
|
26
|
+
const record = await deps.records.read({ key: requiredArgs.connectionRef });
|
|
27
|
+
if (record === null || !isOwnedRecord(record, deps, requiredArgs.workspaceId))
|
|
28
|
+
throw connectionNotFound();
|
|
29
|
+
return record;
|
|
30
|
+
}
|
|
31
|
+
/** Empty record for a first connect; revision 0 means "absent" to compare-and-set. */
|
|
32
|
+
export function blankRecord(deps, requiredArgs) {
|
|
33
|
+
return {
|
|
34
|
+
key: requiredArgs.key, workspaceId: requiredArgs.workspaceId,
|
|
35
|
+
pluginId: deps.descriptor.pluginId, connectionId: deps.descriptor.connectionId,
|
|
36
|
+
status: 'disconnected', revision: 0, generation: 0, account: null, grantedScopes: [], staged: null, revocationFailure: null,
|
|
37
|
+
updatedAt: nowIso({ clock: deps.clock }),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Apply `patch` iff nobody wrote since `current` was read; returns the stored record. */
|
|
41
|
+
export async function writeRecord(deps, current, patch) {
|
|
42
|
+
const next = { ...current, ...patch, revision: current.revision + 1, updatedAt: nowIso({ clock: deps.clock }) };
|
|
43
|
+
const written = await deps.records.compareAndSet({ key: current.key, expectedRevision: current.revision, record: next });
|
|
44
|
+
if (!written) {
|
|
45
|
+
throw new ApiConnectionError({
|
|
46
|
+
code: 'storage-conflict',
|
|
47
|
+
message: 'This connection changed while the request was running. Reload its status and try again.',
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return next;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=records.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ApiConnectionError } from './errors.js';
|
|
2
|
+
import type { ApiConnectionBoundedField } from './types.js';
|
|
3
|
+
export declare function requestInvalid(detail: string): ApiConnectionError;
|
|
4
|
+
export declare function assertId(value: unknown, name: string): asserts value is string;
|
|
5
|
+
/** Non-empty, de-duplicated scope list in RFC 6749 token syntax. */
|
|
6
|
+
export declare function checkScopes(value: unknown): readonly string[];
|
|
7
|
+
/** Keep only allowlisted names; refuse a declared value over its limit rather than truncate it. */
|
|
8
|
+
export declare function pickBoundedFields(fields: readonly ApiConnectionBoundedField[], values: Readonly<Record<string, string>>, where: string): Record<string, string>;
|
|
9
|
+
/** Every name a caller passes must be declared; silently ignoring one would hide a plugin bug. */
|
|
10
|
+
export declare function checkStartParams(fields: readonly ApiConnectionBoundedField[], values: Readonly<Record<string, string>>): Record<string, string>;
|
|
11
|
+
export interface CheckedCallback {
|
|
12
|
+
readonly state: string;
|
|
13
|
+
readonly code?: string;
|
|
14
|
+
readonly error?: string;
|
|
15
|
+
}
|
|
16
|
+
/** Bound the raw callback before anything is consumed; OAuth names are validated by the core. */
|
|
17
|
+
export declare function checkCallbackParams(value: Readonly<Record<string, string>>): CheckedCallback;
|
|
18
|
+
//# sourceMappingURL=request-checks.d.ts.map
|