@qvac/core 0.3.1 → 0.3.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/dist-lib/lib/assistant/history.d.ts +10 -1
- package/dist-lib/lib/assistant/history.js +36 -1
- package/dist-lib/lib/assistant/message.d.ts +2 -0
- package/dist-lib/lib/assistant/message.js +11 -0
- package/dist-lib/lib/assistant/skill-encode.js +27 -1
- package/dist-lib/lib/assistant.d.ts +5 -2
- package/dist-lib/lib/assistant.js +41 -8
- package/dist-lib/lib/core/skill-secrets.js +3 -6
- package/dist-lib/lib/core.d.ts +1 -0
- package/dist-lib/lib/core.js +51 -9
- package/dist-lib/lib/credential-pusher.d.ts +2 -3
- package/dist-lib/lib/credential-pusher.js +67 -39
- package/dist-lib/lib/integrations/index.d.ts +1 -2
- package/dist-lib/lib/integrations/index.js +0 -1
- package/dist-lib/lib/integrations/setup-routes.js +0 -15
- package/dist-lib/lib/oauth/flow-core.js +1 -1
- package/dist-lib/lib/oauth/flow.d.ts +4 -2
- package/dist-lib/lib/oauth/flow.js +3 -4
- package/dist-lib/lib/oauth/host.d.ts +3 -1
- package/dist-lib/lib/oauth/host.js +3 -1
- package/dist-lib/lib/oauth/keys.d.ts +21 -20
- package/dist-lib/lib/oauth/keys.js +59 -85
- package/dist-lib/lib/oauth/loopback.d.ts +2 -2
- package/dist-lib/lib/oauth/loopback.js +32 -19
- package/dist-lib/lib/oauth/loopback.stub.d.ts +2 -2
- package/dist-lib/lib/oauth/loopback.stub.js +2 -2
- package/dist-lib/lib/oauth/providers.d.ts +3 -5
- package/dist-lib/lib/oauth/providers.js +20 -67
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/oauth/registry.d.ts +10 -0
- package/dist-lib/lib/oauth/registry.js +25 -0
- package/dist-lib/lib/oauth/resolve.js +1 -1
- package/dist-lib/lib/oauth/token-exchange-core.js +2 -1
- package/dist-lib/lib/oauth/types.d.ts +1 -1
- package/dist-lib/lib/oauth.client.d.ts +3 -1
- package/dist-lib/lib/oauth.client.js +3 -1
- package/dist-lib/lib/oauth.d.ts +4 -7
- package/dist-lib/lib/oauth.js +4 -7
- package/dist-lib/lib/skill-types.d.ts +12 -0
- package/dist-lib/schema/engine.js +21 -1
- package/dist-lib/spec/hrpc/messages.js +114 -5
- package/dist-lib/spec/hyperschema/index.js +114 -5
- package/dist-lib/spec/hyperschema/schema.json +84 -1
- package/dist-lib/spec/hyperschema/types.d.ts +18 -1
- package/package.json +4 -4
- package/tui/app.mjs +98 -155
- package/tui/lib/save-file.mjs +1 -1
- package/dist-lib/lib/integrations/oauth-providers.d.ts +0 -14
- package/dist-lib/lib/integrations/oauth-providers.js +0 -116
- package/dist-lib/lib/oauth/asana.d.ts +0 -35
- package/dist-lib/lib/oauth/asana.js +0 -71
- package/dist-lib/lib/oauth/google.d.ts +0 -37
- package/dist-lib/lib/oauth/google.js +0 -77
- package/dist-lib/lib/oauth/notion.d.ts +0 -34
- package/dist-lib/lib/oauth/notion.js +0 -82
- package/dist-lib/lib/oauth/spotify.d.ts +0 -32
- package/dist-lib/lib/oauth/spotify.js +0 -74
- package/dist-lib/lib/oauth/verify.d.ts +0 -8
- package/dist-lib/lib/oauth/verify.js +0 -58
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { OBSIDIAN_VAULT_PATH_CREDENTIAL_KEY } from '../obsidian-key.js';
|
|
2
2
|
import { MCP_TOKEN_PROVIDERS } from './mcp-providers.js';
|
|
3
|
-
import { resolveOAuthProviderByCredentialKey } from './oauth-providers.js';
|
|
4
3
|
const UNSUPPORTED_STATE = 'unsupported';
|
|
5
4
|
export const SETUP_ROUTE_KIND = {
|
|
6
5
|
oauth: 'oauth',
|
|
@@ -67,20 +66,6 @@ function credentialKeysFor(entry) {
|
|
|
67
66
|
return entry.metadata.requires?.credentials ?? [];
|
|
68
67
|
}
|
|
69
68
|
function credentialRoute(credentialKey) {
|
|
70
|
-
const oauth = resolveOAuthProviderByCredentialKey(credentialKey);
|
|
71
|
-
if (oauth) {
|
|
72
|
-
return {
|
|
73
|
-
kind: SETUP_ROUTE_KIND.oauth,
|
|
74
|
-
label: `Connect ${oauth.label}`,
|
|
75
|
-
provider: oauth.provider,
|
|
76
|
-
credentialKey,
|
|
77
|
-
description: oauth.description,
|
|
78
|
-
helpUrl: oauth.helpUrl,
|
|
79
|
-
fields: oauth.fields,
|
|
80
|
-
steps: oauth.guide,
|
|
81
|
-
configSteps: oauth.configGuide
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
69
|
const token = MCP_TOKEN_PROVIDERS.find((config) => config.credentialKey === credentialKey);
|
|
85
70
|
if (token) {
|
|
86
71
|
return {
|
|
@@ -18,7 +18,7 @@ function buildAuthParams(config, clientId, redirectUri, state, pkce) {
|
|
|
18
18
|
export async function runOAuthFlowWithRuntime(config, deps, runtime) {
|
|
19
19
|
const openUrl = deps.openUrl ??
|
|
20
20
|
(() => Promise.reject(new Error('OAuth browser launcher is unavailable; inject openUrl or createRedirect')));
|
|
21
|
-
const redirect = await deps.createRedirect({ port: config.port }, { openUrl });
|
|
21
|
+
const redirect = await deps.createRedirect({ port: config.port, credentialKey: config.credentialKey }, { openUrl });
|
|
22
22
|
try {
|
|
23
23
|
const clientId = config.dynamicRegistration && !config.clientId
|
|
24
24
|
? await resolveDynamicClientId(config, redirect.redirectUri, deps, runtime.fetch)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import type { OAuthFlowDeps,
|
|
2
|
-
export declare function cancelOAuthFlow(
|
|
1
|
+
import type { OAuthFlowDeps, OAuthProviderConfig, OAuthTokens } from './types.ts';
|
|
2
|
+
export declare function cancelOAuthFlow(target: {
|
|
3
|
+
credentialKey: string;
|
|
4
|
+
}): Promise<boolean>;
|
|
3
5
|
export declare function runOAuthFlow(config: OAuthProviderConfig, deps?: OAuthFlowDeps): Promise<OAuthTokens>;
|
|
@@ -2,12 +2,11 @@ import { abortOAuthLoopback, startOAuthLoopback } from '#oauth-loopback';
|
|
|
2
2
|
import { randomBase64Url, sha256Base64Url } from '@qvac/core/oauth/runtime/crypto';
|
|
3
3
|
import defaultFetch from '@qvac/core/oauth/runtime/fetch';
|
|
4
4
|
import { runOAuthFlowWithRuntime } from './flow-core.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return abortOAuthLoopback(OAUTH_PROVIDER_PORTS[provider]);
|
|
5
|
+
export function cancelOAuthFlow(target) {
|
|
6
|
+
return abortOAuthLoopback(target.credentialKey);
|
|
8
7
|
}
|
|
9
8
|
const loopbackRedirect = async (target, deps) => {
|
|
10
|
-
const loopback = await startOAuthLoopback(target.port);
|
|
9
|
+
const loopback = await startOAuthLoopback(target.port, target.credentialKey);
|
|
11
10
|
return {
|
|
12
11
|
redirectUri: loopback.redirectUri,
|
|
13
12
|
async authorize(authUrl, timeoutMs, expectedState) {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { buildProviderConfig
|
|
1
|
+
export { buildProviderConfig } from './providers.ts';
|
|
2
|
+
export { specFromRoute, type OAuthKeySpec, type OAuthRouteLike } from './keys.ts';
|
|
3
|
+
export { oauthSpecFor, oauthSpecs } from './registry.ts';
|
|
2
4
|
export { isTokenExpired, refreshOAuthTokens } from './token-exchange.ts';
|
|
3
5
|
export { cancelOAuthFlow, runOAuthFlow } from './flow.ts';
|
|
4
6
|
export type { OAuthCredentials, OAuthFlowDeps, OAuthFlowResult, OAuthProvider, OAuthProviderConfig, OAuthTokens } from './types.ts';
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export { buildProviderConfig
|
|
1
|
+
export { buildProviderConfig } from './providers.js';
|
|
2
|
+
export { specFromRoute } from './keys.js';
|
|
3
|
+
export { oauthSpecFor, oauthSpecs } from './registry.js';
|
|
2
4
|
export { isTokenExpired, refreshOAuthTokens } from './token-exchange.js';
|
|
3
5
|
export { cancelOAuthFlow, runOAuthFlow } from './flow.js';
|
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SkillOAuthConfig } from '@qvac/harness';
|
|
2
2
|
export declare function scopedKey(agentId: string | undefined, key: string): string;
|
|
3
|
-
export
|
|
4
|
-
export declare const GOOGLE_CALENDAR_CREDENTIAL_KEY = "google_calendar_access_token";
|
|
5
|
-
export declare const GOOGLE_DOCS_CREDENTIAL_KEY = "google_docs_access_token";
|
|
6
|
-
export declare const GOOGLE_DRIVE_CREDENTIAL_KEY = "google_drive_access_token";
|
|
7
|
-
export declare const GOOGLE_SHEETS_CREDENTIAL_KEY = "google_sheets_access_token";
|
|
8
|
-
export declare const SPOTIFY_CREDENTIAL_KEY = "spotify_access_token";
|
|
9
|
-
export declare const ASANA_MCP_CREDENTIAL_KEY = "asana_mcp_access_token";
|
|
10
|
-
export declare const NOTION_MCP_CREDENTIAL_KEY = "notion_mcp_access_token";
|
|
11
|
-
export declare const LEGACY_GOOGLE_CREDENTIAL_KEY = "google_workspace_access_token";
|
|
12
|
-
export type OAuthKeySpec = {
|
|
3
|
+
export type OAuthKeySpec = SkillOAuthConfig & {
|
|
13
4
|
readonly credentialKey: string;
|
|
14
|
-
readonly
|
|
15
|
-
readonly provider: OAuthProvider;
|
|
16
|
-
readonly scopes: ReadonlyArray<string>;
|
|
5
|
+
readonly provider: string;
|
|
17
6
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
7
|
+
type OAuthConfigLike = {
|
|
8
|
+
readonly stateKey?: string | null | undefined;
|
|
9
|
+
readonly authUrl?: string | null | undefined;
|
|
10
|
+
readonly tokenUrl?: string | null | undefined;
|
|
11
|
+
readonly scopes?: ReadonlyArray<string> | null | undefined;
|
|
12
|
+
readonly tokenAuth?: string | null | undefined;
|
|
13
|
+
readonly extraAuthParams?: unknown;
|
|
14
|
+
readonly registrationEndpoint?: string | null | undefined;
|
|
15
|
+
readonly port?: number | null | undefined;
|
|
16
|
+
readonly sharedIosSession?: boolean | null | undefined;
|
|
17
|
+
};
|
|
18
|
+
export type OAuthRouteLike = {
|
|
19
|
+
readonly credentialKey?: string | null | undefined;
|
|
20
|
+
readonly provider?: string | null | undefined;
|
|
21
|
+
readonly oauth?: OAuthConfigLike | null | undefined;
|
|
22
|
+
};
|
|
23
|
+
export declare function specFromRoute(route: OAuthRouteLike): OAuthKeySpec | null;
|
|
24
24
|
export declare const OAUTH_CALLBACK_PATH = "/callback";
|
|
25
25
|
export declare const OAUTH_WAIT_FOR_CODE_TIMEOUT_MS = 120000;
|
|
26
26
|
export declare function oauthRedirectUri(port: number): string;
|
|
27
|
+
export {};
|
|
@@ -1,95 +1,69 @@
|
|
|
1
1
|
export function scopedKey(agentId, key) {
|
|
2
2
|
return agentId ? `skills.${agentId}.${key}` : `skills.${key}`;
|
|
3
3
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
credentialKey: GMAIL_CREDENTIAL_KEY,
|
|
17
|
-
stateKey: 'gmail_oauth',
|
|
18
|
-
provider: 'google',
|
|
19
|
-
scopes: ['https://www.googleapis.com/auth/gmail.modify']
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
credentialKey: GOOGLE_CALENDAR_CREDENTIAL_KEY,
|
|
23
|
-
stateKey: 'google_calendar_oauth',
|
|
24
|
-
provider: 'google',
|
|
25
|
-
scopes: ['https://www.googleapis.com/auth/calendar']
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
credentialKey: GOOGLE_DOCS_CREDENTIAL_KEY,
|
|
29
|
-
stateKey: 'google_docs_oauth',
|
|
30
|
-
provider: 'google',
|
|
31
|
-
scopes: ['https://www.googleapis.com/auth/documents', DRIVE_SCOPE]
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
credentialKey: GOOGLE_DRIVE_CREDENTIAL_KEY,
|
|
35
|
-
stateKey: 'google_drive_oauth',
|
|
36
|
-
provider: 'google',
|
|
37
|
-
scopes: [DRIVE_SCOPE]
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
credentialKey: GOOGLE_SHEETS_CREDENTIAL_KEY,
|
|
41
|
-
stateKey: 'google_sheets_oauth',
|
|
42
|
-
provider: 'google',
|
|
43
|
-
scopes: ['https://www.googleapis.com/auth/spreadsheets', DRIVE_SCOPE]
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
credentialKey: SPOTIFY_CREDENTIAL_KEY,
|
|
47
|
-
stateKey: 'spotify_oauth',
|
|
48
|
-
provider: 'spotify',
|
|
49
|
-
scopes: [
|
|
50
|
-
'user-read-private',
|
|
51
|
-
'user-read-playback-state',
|
|
52
|
-
'user-modify-playback-state',
|
|
53
|
-
'user-read-currently-playing',
|
|
54
|
-
'playlist-read-private',
|
|
55
|
-
'playlist-read-collaborative',
|
|
56
|
-
'user-library-read',
|
|
57
|
-
'user-top-read',
|
|
58
|
-
'user-read-recently-played'
|
|
59
|
-
]
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
credentialKey: ASANA_MCP_CREDENTIAL_KEY,
|
|
63
|
-
stateKey: 'asana_oauth',
|
|
64
|
-
provider: 'asana',
|
|
65
|
-
scopes: []
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
credentialKey: NOTION_MCP_CREDENTIAL_KEY,
|
|
69
|
-
stateKey: 'notion_oauth',
|
|
70
|
-
provider: 'notion',
|
|
71
|
-
scopes: []
|
|
4
|
+
const TOKEN_AUTH = ['pkce-only', 'secret-in-body', 'secret-in-basic'];
|
|
5
|
+
function tokenAuthOf(value) {
|
|
6
|
+
return TOKEN_AUTH.find((candidate) => candidate === value) ?? null;
|
|
7
|
+
}
|
|
8
|
+
function httpsUrl(value) {
|
|
9
|
+
if (!value)
|
|
10
|
+
return null;
|
|
11
|
+
try {
|
|
12
|
+
return new URL(value).protocol === 'https:' ? value : null;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
72
16
|
}
|
|
73
|
-
];
|
|
74
|
-
export function oauthKeySpec(credentialKey) {
|
|
75
|
-
return OAUTH_KEY_SPECS.find((spec) => spec.credentialKey === credentialKey) ?? null;
|
|
76
17
|
}
|
|
77
|
-
|
|
78
|
-
|
|
18
|
+
function portOf(value) {
|
|
19
|
+
if (value === undefined || value === null)
|
|
20
|
+
return null;
|
|
21
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535
|
|
22
|
+
? value
|
|
23
|
+
: null;
|
|
24
|
+
}
|
|
25
|
+
export function specFromRoute(route) {
|
|
26
|
+
const oauth = route.oauth;
|
|
27
|
+
if (!oauth)
|
|
28
|
+
return null;
|
|
29
|
+
const tokenAuth = tokenAuthOf(oauth.tokenAuth);
|
|
30
|
+
const authUrl = httpsUrl(oauth.authUrl);
|
|
31
|
+
const tokenUrl = httpsUrl(oauth.tokenUrl);
|
|
32
|
+
if (!route.credentialKey || !oauth.stateKey || !authUrl || !tokenUrl || !tokenAuth)
|
|
33
|
+
return null;
|
|
34
|
+
if (oauth.stateKey === route.credentialKey)
|
|
35
|
+
return null;
|
|
36
|
+
if (oauth.port !== undefined && oauth.port !== null && portOf(oauth.port) === null)
|
|
37
|
+
return null;
|
|
38
|
+
if (oauth.registrationEndpoint && !httpsUrl(oauth.registrationEndpoint))
|
|
39
|
+
return null;
|
|
40
|
+
if (oauth.registrationEndpoint && tokenAuth !== 'pkce-only')
|
|
41
|
+
return null;
|
|
42
|
+
const extraParams = stringRecord(oauth.extraAuthParams);
|
|
43
|
+
const port = portOf(oauth.port);
|
|
44
|
+
return {
|
|
45
|
+
credentialKey: route.credentialKey,
|
|
46
|
+
provider: route.provider ?? route.credentialKey,
|
|
47
|
+
stateKey: oauth.stateKey,
|
|
48
|
+
authUrl,
|
|
49
|
+
tokenUrl,
|
|
50
|
+
tokenAuth,
|
|
51
|
+
scopes: stringList(oauth.scopes),
|
|
52
|
+
...(extraParams ? { extraAuthParams: extraParams } : {}),
|
|
53
|
+
...(oauth.registrationEndpoint ? { registrationEndpoint: oauth.registrationEndpoint } : {}),
|
|
54
|
+
...(port !== null ? { port } : {}),
|
|
55
|
+
...(oauth.sharedIosSession === true ? { sharedIosSession: true } : {})
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function stringRecord(value) {
|
|
59
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
60
|
+
return null;
|
|
61
|
+
const entries = Object.entries(value).filter(([, item]) => typeof item === 'string');
|
|
62
|
+
return entries.length > 0 ? Object.fromEntries(entries) : null;
|
|
79
63
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (!spec)
|
|
83
|
-
throw new Error(`Unknown OAuth provider: ${String(provider)}`);
|
|
84
|
-
return spec.credentialKey;
|
|
64
|
+
function stringList(value) {
|
|
65
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === 'string') : [];
|
|
85
66
|
}
|
|
86
|
-
export const GOOGLE_CREDENTIAL_KEYS = OAUTH_KEY_SPECS.filter((spec) => spec.provider === 'google').map((spec) => spec.credentialKey);
|
|
87
|
-
export const OAUTH_PROVIDER_PORTS = {
|
|
88
|
-
google: 18978,
|
|
89
|
-
spotify: 18974,
|
|
90
|
-
asana: 18981,
|
|
91
|
-
notion: 18982
|
|
92
|
-
};
|
|
93
67
|
export const OAUTH_CALLBACK_PATH = '/callback';
|
|
94
68
|
export const OAUTH_WAIT_FOR_CODE_TIMEOUT_MS = 120_000;
|
|
95
69
|
export function oauthRedirectUri(port) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { OAuthLoopbackServer } from './redirect-types.ts';
|
|
2
|
-
export declare function abortOAuthLoopback(
|
|
3
|
-
export declare function startOAuthLoopback(port: number): Promise<OAuthLoopbackServer>;
|
|
2
|
+
export declare function abortOAuthLoopback(credentialKey: string): Promise<boolean>;
|
|
3
|
+
export declare function startOAuthLoopback(port: number, credentialKey: string): Promise<OAuthLoopbackServer>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createServer } from 'http';
|
|
2
2
|
import { OAUTH_CALLBACK_PATH, OAUTH_WAIT_FOR_CODE_TIMEOUT_MS, oauthRedirectUri } from './keys.js';
|
|
3
|
-
const activeLoopbacks = new
|
|
3
|
+
const activeLoopbacks = new Set();
|
|
4
4
|
function closeServer(server) {
|
|
5
5
|
return new Promise((resolve) => {
|
|
6
6
|
if (!server.listening) {
|
|
@@ -11,17 +11,23 @@ function closeServer(server) {
|
|
|
11
11
|
server.close(() => resolve());
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return false;
|
|
18
|
-
await handle.abort(new Error('OAuth flow cancelled'));
|
|
19
|
-
return true;
|
|
14
|
+
async function abortMatching(matches, reason) {
|
|
15
|
+
await Promise.all(matches.map((handle) => handle.abort(reason)));
|
|
16
|
+
return matches.length > 0;
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
function abortFor(credentialKey, reason) {
|
|
19
|
+
return abortMatching([...activeLoopbacks].filter((handle) => handle.credentialKey === credentialKey), reason);
|
|
20
|
+
}
|
|
21
|
+
export function abortOAuthLoopback(credentialKey) {
|
|
22
|
+
return abortFor(credentialKey, new Error('OAuth flow cancelled'));
|
|
23
|
+
}
|
|
24
|
+
export async function startOAuthLoopback(port, credentialKey) {
|
|
25
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
26
|
+
throw new Error(`OAuth loopback port must be an integer in [0, 65535], got ${port}`);
|
|
27
|
+
}
|
|
28
|
+
await abortFor(credentialKey, new Error('OAuth loopback superseded by a new connect attempt'));
|
|
29
|
+
if (port !== 0) {
|
|
30
|
+
await abortMatching([...activeLoopbacks].filter((handle) => handle.port === port), new Error('OAuth loopback superseded by a new connect attempt on the same port'));
|
|
25
31
|
}
|
|
26
32
|
let resolveCode = null;
|
|
27
33
|
let rejectCode = null;
|
|
@@ -46,7 +52,7 @@ export async function startOAuthLoopback(port) {
|
|
|
46
52
|
reject?.(new Error('OAuth callback missing code or state'));
|
|
47
53
|
};
|
|
48
54
|
const server = createServer((request, response) => {
|
|
49
|
-
const url = new URL(request.url ?? '/',
|
|
55
|
+
const url = new URL(request.url ?? '/', 'http://127.0.0.1');
|
|
50
56
|
if (url.pathname !== OAUTH_CALLBACK_PATH) {
|
|
51
57
|
response.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
52
58
|
response.end('Not found');
|
|
@@ -74,27 +80,34 @@ export async function startOAuthLoopback(port) {
|
|
|
74
80
|
return;
|
|
75
81
|
listenSettled = true;
|
|
76
82
|
server.removeListener('listening', onListening);
|
|
77
|
-
reject(error
|
|
83
|
+
reject(error.code === 'EADDRINUSE'
|
|
84
|
+
? new Error(`OAuth loopback port ${port} is already in use by another process`)
|
|
85
|
+
: error);
|
|
78
86
|
};
|
|
79
87
|
const onListening = () => {
|
|
80
88
|
if (listenSettled)
|
|
81
89
|
return;
|
|
82
90
|
listenSettled = true;
|
|
83
91
|
server.removeListener('error', onError);
|
|
92
|
+
const address = server.address();
|
|
93
|
+
if (typeof address !== 'object' || !address) {
|
|
94
|
+
server.close();
|
|
95
|
+
reject(new Error('OAuth loopback bound to no address'));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
84
98
|
const handle = {
|
|
99
|
+
credentialKey,
|
|
100
|
+
port: address.port,
|
|
85
101
|
abort: (reason) => {
|
|
86
102
|
abortedReason = reason;
|
|
87
103
|
rejectWaiter(reason);
|
|
88
104
|
return closeServer(server);
|
|
89
105
|
}
|
|
90
106
|
};
|
|
91
|
-
activeLoopbacks.
|
|
92
|
-
server.once('close', () =>
|
|
93
|
-
if (activeLoopbacks.get(port) === handle)
|
|
94
|
-
activeLoopbacks.delete(port);
|
|
95
|
-
});
|
|
107
|
+
activeLoopbacks.add(handle);
|
|
108
|
+
server.once('close', () => activeLoopbacks.delete(handle));
|
|
96
109
|
resolve({
|
|
97
|
-
redirectUri: oauthRedirectUri(port),
|
|
110
|
+
redirectUri: oauthRedirectUri(address.port),
|
|
98
111
|
waitForCode(timeoutMs = OAUTH_WAIT_FOR_CODE_TIMEOUT_MS, expected) {
|
|
99
112
|
if (abortedReason)
|
|
100
113
|
throw abortedReason;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { OAuthLoopbackServer } from './redirect-types.ts';
|
|
2
|
-
export declare function abortOAuthLoopback(
|
|
3
|
-
export declare function startOAuthLoopback(_port: number): Promise<OAuthLoopbackServer>;
|
|
2
|
+
export declare function abortOAuthLoopback(_credentialKey: string): Promise<boolean>;
|
|
3
|
+
export declare function startOAuthLoopback(_port: number, _credentialKey: string): Promise<OAuthLoopbackServer>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export function abortOAuthLoopback(
|
|
1
|
+
export function abortOAuthLoopback(_credentialKey) {
|
|
2
2
|
return Promise.resolve(false);
|
|
3
3
|
}
|
|
4
|
-
export function startOAuthLoopback(_port) {
|
|
4
|
+
export function startOAuthLoopback(_port, _credentialKey) {
|
|
5
5
|
return Promise.reject(new Error('OAuth loopback is unavailable in this runtime; inject createRedirect instead'));
|
|
6
6
|
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function providerSupportsSharedIosSession(provider: OAuthProvider): boolean;
|
|
5
|
-
export declare function buildProviderConfig(credentialKey: string, credentials: OAuthCredentials): OAuthProviderConfig;
|
|
1
|
+
import type { OAuthKeySpec } from './keys.ts';
|
|
2
|
+
import type { OAuthCredentials, OAuthProviderConfig } from './types.ts';
|
|
3
|
+
export declare function buildProviderConfig(spec: OAuthKeySpec, credentials: OAuthCredentials): OAuthProviderConfig;
|
|
@@ -1,70 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
tokenAuth: 'secret-in-body',
|
|
22
|
-
authUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
23
|
-
tokenUrl: 'https://oauth2.googleapis.com/token',
|
|
24
|
-
scopes: spec.scopes,
|
|
25
|
-
credentialKey: spec.credentialKey,
|
|
26
|
-
port: OAUTH_PROVIDER_PORTS[provider],
|
|
27
|
-
extraAuthParams: { access_type: 'offline', prompt: 'consent' }
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
if (provider === 'spotify') {
|
|
31
|
-
return {
|
|
32
|
-
clientId: credentials.clientId,
|
|
33
|
-
tokenAuth: 'pkce-only',
|
|
34
|
-
authUrl: 'https://accounts.spotify.com/authorize',
|
|
35
|
-
tokenUrl: 'https://accounts.spotify.com/api/token',
|
|
36
|
-
scopes: spec.scopes,
|
|
37
|
-
credentialKey: spec.credentialKey,
|
|
38
|
-
port: OAUTH_PROVIDER_PORTS[provider]
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
if (provider === 'asana') {
|
|
42
|
-
return {
|
|
43
|
-
clientId: credentials.clientId,
|
|
44
|
-
clientSecret: requireSecret(credentials.clientSecret, provider),
|
|
45
|
-
tokenAuth: 'secret-in-body',
|
|
46
|
-
authUrl: 'https://app.asana.com/-/oauth_authorize',
|
|
47
|
-
tokenUrl: 'https://app.asana.com/-/oauth_token',
|
|
48
|
-
scopes: spec.scopes,
|
|
49
|
-
credentialKey: spec.credentialKey,
|
|
50
|
-
port: OAUTH_PROVIDER_PORTS[provider],
|
|
51
|
-
extraAuthParams: { resource: ASANA_MCP_RESOURCE }
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
if (provider === 'notion') {
|
|
55
|
-
return {
|
|
56
|
-
clientId: credentials.clientId,
|
|
57
|
-
tokenAuth: 'pkce-only',
|
|
58
|
-
authUrl: 'https://mcp.notion.com/authorize',
|
|
59
|
-
tokenUrl: 'https://mcp.notion.com/token',
|
|
60
|
-
registrationEndpoint: 'https://mcp.notion.com/register',
|
|
61
|
-
dynamicRegistration: true,
|
|
62
|
-
scopes: spec.scopes,
|
|
63
|
-
credentialKey: spec.credentialKey,
|
|
64
|
-
port: OAUTH_PROVIDER_PORTS[provider]
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
throw new Error(`Unknown OAuth provider: ${String(provider)}`);
|
|
1
|
+
export function buildProviderConfig(spec, credentials) {
|
|
2
|
+
const base = {
|
|
3
|
+
clientId: credentials.clientId,
|
|
4
|
+
authUrl: spec.authUrl,
|
|
5
|
+
tokenUrl: spec.tokenUrl,
|
|
6
|
+
scopes: spec.scopes,
|
|
7
|
+
credentialKey: spec.credentialKey,
|
|
8
|
+
port: spec.port ?? 0,
|
|
9
|
+
...(spec.extraAuthParams ? { extraAuthParams: spec.extraAuthParams } : {}),
|
|
10
|
+
...(spec.registrationEndpoint
|
|
11
|
+
? { registrationEndpoint: spec.registrationEndpoint, dynamicRegistration: true }
|
|
12
|
+
: {})
|
|
13
|
+
};
|
|
14
|
+
if (spec.tokenAuth === 'pkce-only')
|
|
15
|
+
return { ...base, tokenAuth: 'pkce-only' };
|
|
16
|
+
return {
|
|
17
|
+
...base,
|
|
18
|
+
tokenAuth: spec.tokenAuth,
|
|
19
|
+
clientSecret: requireSecret(credentials.clientSecret, spec.provider)
|
|
20
|
+
};
|
|
68
21
|
}
|
|
69
22
|
function requireSecret(secret, provider) {
|
|
70
23
|
if (!secret)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type OAuthKeySpec, type OAuthRouteLike } from './keys.ts';
|
|
2
|
+
type SkillLike = {
|
|
3
|
+
readonly origin?: string;
|
|
4
|
+
readonly setup?: {
|
|
5
|
+
readonly routes?: ReadonlyArray<OAuthRouteLike> | null | undefined;
|
|
6
|
+
} | null;
|
|
7
|
+
};
|
|
8
|
+
export declare function oauthSpecs(skills: ReadonlyArray<SkillLike>): ReadonlyArray<OAuthKeySpec>;
|
|
9
|
+
export declare function oauthSpecFor(skills: ReadonlyArray<SkillLike>, credentialKey: string): OAuthKeySpec | null;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { specFromRoute } from './keys.js';
|
|
2
|
+
export function oauthSpecs(skills) {
|
|
3
|
+
const byKey = new Map();
|
|
4
|
+
for (const skill of skills) {
|
|
5
|
+
if (skill.origin === 'overlay')
|
|
6
|
+
continue;
|
|
7
|
+
for (const route of skill.setup?.routes ?? []) {
|
|
8
|
+
const spec = specFromRoute(route);
|
|
9
|
+
if (spec && !byKey.has(spec.credentialKey))
|
|
10
|
+
byKey.set(spec.credentialKey, spec);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const claimed = new Set();
|
|
14
|
+
const specs = [];
|
|
15
|
+
for (const spec of byKey.values()) {
|
|
16
|
+
if (byKey.has(spec.stateKey) || claimed.has(spec.stateKey))
|
|
17
|
+
continue;
|
|
18
|
+
claimed.add(spec.stateKey);
|
|
19
|
+
specs.push(spec);
|
|
20
|
+
}
|
|
21
|
+
return specs;
|
|
22
|
+
}
|
|
23
|
+
export function oauthSpecFor(skills, credentialKey) {
|
|
24
|
+
return oauthSpecs(skills).find((spec) => spec.credentialKey === credentialKey) ?? null;
|
|
25
|
+
}
|
|
@@ -41,7 +41,7 @@ export async function resolveOAuthCredential(opts) {
|
|
|
41
41
|
key: scopedKey(agentId, stateKey),
|
|
42
42
|
value: JSON.stringify({
|
|
43
43
|
...stored,
|
|
44
|
-
refreshToken: refreshed.refreshToken,
|
|
44
|
+
refreshToken: refreshed.refreshToken ?? refreshToken,
|
|
45
45
|
expiresAt: refreshed.expiresAt
|
|
46
46
|
})
|
|
47
47
|
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AbortController } from 'bare-abort-controller';
|
|
1
2
|
import * as z from 'zod';
|
|
2
3
|
const TOKEN_REQUEST_TIMEOUT_MS = 30_000;
|
|
3
4
|
const TokenResponseSchema = z.object({
|
|
@@ -73,7 +74,7 @@ export async function refreshOAuthTokensWithFetch(config, refreshToken, fetchImp
|
|
|
73
74
|
Accept: 'application/json'
|
|
74
75
|
};
|
|
75
76
|
applyClientSecret(config, body, headers);
|
|
76
|
-
const controller = new
|
|
77
|
+
const controller = new AbortController();
|
|
77
78
|
const timeout = globalThis.setTimeout(() => controller.abort(new Error('OAuth token refresh timed out')), TOKEN_REQUEST_TIMEOUT_MS);
|
|
78
79
|
try {
|
|
79
80
|
const response = await fetchImpl(config.tokenUrl, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { OAuthRedirectFactory } from './redirect-types.ts';
|
|
2
|
-
export type OAuthProvider =
|
|
2
|
+
export type OAuthProvider = string;
|
|
3
3
|
export type OAuthTokens = {
|
|
4
4
|
readonly accessToken: string;
|
|
5
5
|
readonly refreshToken?: string;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { createOAuthClient, isTokenExpired, type OAuthClient } from './oauth/client.ts';
|
|
2
|
-
export { buildProviderConfig
|
|
2
|
+
export { buildProviderConfig } from './oauth/providers.ts';
|
|
3
|
+
export { specFromRoute, type OAuthKeySpec, type OAuthRouteLike } from './oauth/keys.ts';
|
|
4
|
+
export { oauthSpecFor, oauthSpecs } from './oauth/registry.ts';
|
|
3
5
|
export type { OAuthCallbackResult, OAuthRedirect, OAuthRedirectFactory } from './oauth/redirect-types.ts';
|
|
4
6
|
export type { OAuthRuntimeFlowDeps } from './oauth/flow-core.ts';
|
|
5
7
|
export type { OAuthCredentials, OAuthFetch, OAuthFlowDeps, OAuthFlowResult, OAuthProvider, OAuthProviderConfig, OAuthRuntime, OAuthTokens } from './oauth/types.ts';
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { createOAuthClient, isTokenExpired } from './oauth/client.js';
|
|
2
|
-
export { buildProviderConfig
|
|
2
|
+
export { buildProviderConfig } from './oauth/providers.js';
|
|
3
|
+
export { specFromRoute } from './oauth/keys.js';
|
|
4
|
+
export { oauthSpecFor, oauthSpecs } from './oauth/registry.js';
|
package/dist-lib/lib/oauth.d.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
export { createPkce, scopedKey, type ConfigEngine, type Fetch, type FetchResponse, type OAuthTokens } from './oauth/common.ts';
|
|
2
2
|
export { waitForOAuthCallback } from './oauth/browser-connect.ts';
|
|
3
|
-
export { buildGoogleAuthUrl, connectGoogle, exchangeGoogleCode, GOOGLE_OAUTH_PORT, refreshGoogleToken, resolveGoogleCredential, type GoogleTokens } from './oauth/google.ts';
|
|
4
|
-
export { buildNotionAuthUrl, connectNotion, exchangeNotionCode, NOTION_MCP_CREDENTIAL_KEY, NOTION_OAUTH_PORT, NOTION_OAUTH_STATE_KEY, refreshNotionToken, registerNotionClient, resolveNotionCredential, type NotionTokens } from './oauth/notion.ts';
|
|
5
|
-
export { buildAsanaAuthUrl, connectAsana, exchangeAsanaCode, ASANA_MCP_CREDENTIAL_KEY, ASANA_OAUTH_PORT, ASANA_OAUTH_STATE_KEY, refreshAsanaToken, resolveAsanaCredential, type AsanaTokens } from './oauth/asana.ts';
|
|
6
|
-
export { buildSpotifyAuthUrl, connectSpotify, exchangeSpotifyCode, refreshSpotifyToken, resolveSpotifyCredential, SPOTIFY_CREDENTIAL_KEY, SPOTIFY_OAUTH_PORT, SPOTIFY_OAUTH_STATE_KEY, type SpotifyTokens } from './oauth/spotify.ts';
|
|
7
3
|
export { cancelOAuthFlow, runOAuthFlow } from './oauth/flow.ts';
|
|
8
|
-
export {
|
|
4
|
+
export { OAUTH_CALLBACK_PATH, OAUTH_WAIT_FOR_CODE_TIMEOUT_MS, oauthRedirectUri, specFromRoute, type OAuthKeySpec, type OAuthRouteLike } from './oauth/keys.ts';
|
|
5
|
+
export { oauthSpecFor, oauthSpecs } from './oauth/registry.ts';
|
|
9
6
|
export { generatePkceChallenge, type PkceChallenge } from './oauth/pkce.ts';
|
|
10
|
-
export { buildProviderConfig
|
|
7
|
+
export { buildProviderConfig } from './oauth/providers.ts';
|
|
11
8
|
export type { OAuthCallbackResult, OAuthRedirect, OAuthRedirectFactory } from './oauth/redirect-types.ts';
|
|
12
9
|
export { registerOAuthClient, resolveDynamicClientId } from './oauth/register.ts';
|
|
10
|
+
export { resolveOAuthCredential } from './oauth/resolve.ts';
|
|
13
11
|
export { isTokenExpired, refreshOAuthTokens } from './oauth/token-exchange.ts';
|
|
14
|
-
export { verifyOAuthTokens, type VerifyResult } from './oauth/verify.ts';
|
|
15
12
|
export type { OAuthCredentials, OAuthFlowDeps, OAuthFlowResult, OAuthProvider, OAuthProviderConfig } from './oauth/types.ts';
|