@yaks/connections 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +609 -2
- package/_dist/authorize.d.ts +38 -0
- package/_dist/authorize.d.ts.map +1 -0
- package/_dist/authorize_cli.d.ts +12 -0
- package/_dist/authorize_cli.d.ts.map +1 -0
- package/_dist/brokers.d.ts +66 -0
- package/_dist/brokers.d.ts.map +1 -0
- package/_dist/catalog.d.ts +33 -0
- package/_dist/catalog.d.ts.map +1 -0
- package/_dist/cli.d.ts +3 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/clients.d.ts +18 -0
- package/_dist/clients.d.ts.map +1 -0
- package/_dist/connections.d.ts +296 -0
- package/_dist/connections.d.ts.map +1 -0
- package/_dist/effects.d.ts +15 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/field.d.ts +17 -0
- package/_dist/field.d.ts.map +1 -0
- package/_dist/form.d.ts +49 -0
- package/_dist/form.d.ts.map +1 -0
- package/_dist/given.d.ts +10 -0
- package/_dist/given.d.ts.map +1 -0
- package/_dist/graph.d.ts +15 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/history.d.ts +4 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/identity.d.ts +9 -0
- package/_dist/identity.d.ts.map +1 -0
- package/_dist/integrations.d.ts +196 -0
- package/_dist/integrations.d.ts.map +1 -0
- package/_dist/loopback.d.ts +7 -0
- package/_dist/loopback.d.ts.map +1 -0
- package/_dist/mcp-retirement.d.ts +33 -0
- package/_dist/mcp-retirement.d.ts.map +1 -0
- package/_dist/mcp.d.ts +179 -0
- package/_dist/mcp.d.ts.map +1 -0
- package/_dist/mod.d.ts +70 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/paste.d.ts +13 -0
- package/_dist/paste.d.ts.map +1 -0
- package/_dist/requests.d.ts +30 -0
- package/_dist/requests.d.ts.map +1 -0
- package/_dist/services.d.ts +25 -0
- package/_dist/services.d.ts.map +1 -0
- package/_dist/sign-in.d.ts +9 -0
- package/_dist/sign-in.d.ts.map +1 -0
- package/_dist/signin-mail.d.ts +12 -0
- package/_dist/signin-mail.d.ts.map +1 -0
- package/_dist/signin.d.ts +19 -0
- package/_dist/signin.d.ts.map +1 -0
- package/_dist/sources.d.ts +61 -0
- package/_dist/sources.d.ts.map +1 -0
- package/_dist/tools.d.ts +3 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/views.d.ts +3 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/yaks-app.d.ts +29 -0
- package/_dist/yaks-app.d.ts.map +1 -0
- package/anthropic.json +12 -0
- package/auth_command.js +69 -0
- package/auth_command.js.map +1 -0
- package/auth_command.ts +74 -0
- package/authorize.js +89 -0
- package/authorize.js.map +1 -0
- package/authorize.ts +138 -0
- package/authorize_cli.js +167 -0
- package/authorize_cli.js.map +1 -0
- package/authorize_cli.ts +211 -0
- package/brokers.js +318 -0
- package/brokers.js.map +1 -0
- package/brokers.ts +423 -0
- package/canva.json +32 -0
- package/catalog.js +178 -0
- package/catalog.js.map +1 -0
- package/catalog.ts +216 -0
- package/cli.js +30 -0
- package/cli.js.map +1 -0
- package/cli.ts +26 -0
- package/clients.js +34 -0
- package/clients.js.map +1 -0
- package/clients.ts +65 -0
- package/connections.js +1961 -0
- package/connections.js.map +1 -0
- package/connections.ts +2561 -0
- package/deno.json +1 -0
- package/discord-account.json +22 -0
- package/discord-bot.json +20 -0
- package/discord.json +26 -0
- package/effects.js +168 -0
- package/effects.js.map +1 -0
- package/effects.ts +160 -0
- package/elevenlabs.json +18 -0
- package/facebook.json +18 -0
- package/field.js +19 -0
- package/field.js.map +1 -0
- package/field.ts +31 -0
- package/form.js +112 -0
- package/form.js.map +1 -0
- package/form.ts +178 -0
- package/git-config.js +44 -0
- package/git-config.js.map +1 -0
- package/git-config.ts +26 -0
- package/given.js +83 -0
- package/given.js.map +1 -0
- package/given.ts +88 -0
- package/gmail-mailbox.json +29 -0
- package/gmail.json +31 -0
- package/google-calendar.json +31 -0
- package/google-drive.json +31 -0
- package/google-search-console.json +29 -0
- package/graph.js +36 -0
- package/graph.js.map +1 -0
- package/graph.ts +52 -0
- package/history.js +31 -0
- package/history.js.map +1 -0
- package/history.ts +32 -0
- package/identity.js +15 -0
- package/identity.js.map +1 -0
- package/identity.ts +18 -0
- package/integrations.js +493 -0
- package/integrations.js.map +1 -0
- package/integrations.ts +725 -0
- package/loopback.js +72 -0
- package/loopback.js.map +1 -0
- package/loopback.ts +89 -0
- package/lulu.json +32 -0
- package/mcp-retirement.js +133 -0
- package/mcp-retirement.js.map +1 -0
- package/mcp-retirement.ts +200 -0
- package/mcp-signin.js +14 -0
- package/mcp-signin.js.map +1 -0
- package/mcp-signin.ts +13 -0
- package/mcp.js +1036 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +1283 -0
- package/mod.js +70 -0
- package/mod.js.map +1 -0
- package/mod.ts +216 -0
- package/openai-api.json +12 -0
- package/openai.json +25 -0
- package/openrouter.json +14 -0
- package/package.json +89 -4
- package/paste.js +36 -0
- package/paste.js.map +1 -0
- package/paste.ts +55 -0
- package/requests.js +137 -0
- package/requests.js.map +1 -0
- package/requests.ts +200 -0
- package/services.js +84 -0
- package/services.js.map +1 -0
- package/services.ts +111 -0
- package/sign-in.js +36 -0
- package/sign-in.js.map +1 -0
- package/sign-in.ts +53 -0
- package/signin-mail.js +29 -0
- package/signin-mail.js.map +1 -0
- package/signin-mail.ts +56 -0
- package/signin.js +68 -0
- package/signin.js.map +1 -0
- package/signin.ts +109 -0
- package/slack.json +90 -0
- package/sources.js +178 -0
- package/sources.js.map +1 -0
- package/sources.ts +237 -0
- package/tools.js +39 -0
- package/tools.js.map +1 -0
- package/tools.ts +46 -0
- package/views.js +85 -0
- package/views.js.map +1 -0
- package/views.ts +89 -0
- package/visitor-defaults.json +8 -0
- package/vocab.js +17 -0
- package/vocab.js.map +1 -0
- package/vocab.json +863 -0
- package/vocab.ts +18 -0
- package/voipms.json +32 -0
- package/yaks-app.js +159 -0
- package/yaks-app.js.map +1 -0
- package/yaks-app.ts +177 -0
- package/youtube.json +26 -0
package/requests.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requests.js","sources":["./requests.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,0EAA0E;AAC1E,8EAA8E;AAC9E,4EAA4E;AAC5E,EAAE;AACF,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,0EAA0E;AAC1E,uEAAuE;AAEvE,SAAS,IAAI,2BAAgC;AAE7C,SAAS,SAAS,kCAAuC;AACzD,SAAS,IAAI,2BAAoC;AACjD,SAAS,UAAU,wBAAuB;AAC1C,SAA2B,KAAK,4BAAsC;AAqBtE,IAAI,UAAU;AAEd,IAAI,QAAQ;EACV,MAAM;EACN,YAAY;IACV,QAAQ;MAAE,MAAM;MAAU,aAAa;IAAmC;IAC1E,KAAK;MACH,MAAM;MACN,aAAa;IACf;IACA,SAAS;MACP,MAAM;MACN,sBAAsB;QAAE,MAAM;MAAS;MACvC,aAAa;IACf;IACA,MAAM;MACJ,aACE;IACJ;EACF;EACA,UAAU;IAAC;GAAM;AACnB;AAEA,IAAI,OAAO,CAAC,GAAuB,OAChC,GAAG,CAAC,KAAK,IAAI,CAAC;AAEjB,IAAI,YAAY,CAAC,MAAa,CAAC,WAAW,EAAE,KAAK;AAEjD,0EAA0E,GAC1E,OAAO,IAAI,cAAc,CAAC,IACxB,CAAC,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,OAAO,IAAI,CAAC,EAAE,MAAM,EAAE,UAC9D,CAAC,CAAC,EAAE,IAAI,IAAI,CAAC,CAAC,EAAE,UAAU,KAC1B,EAAE,KAAK,CAAC,MAAM,GAAG,EAAC;AAEpB,o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ests.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// A connection as a source of tools: `connection:<connection>` offers one
|
|
2
|
+
// command, `request`, which sends an HTTPS request through the connection
|
|
3
|
+
// (`call`), its credential added on the way. So it reaches only the hosts its
|
|
4
|
+
// integration names, and its answer is the service's, never the credential.
|
|
5
|
+
//
|
|
6
|
+
// Who may send through which connection is the host's to say (`owners`): the
|
|
7
|
+
// person's own, the space's for its owner, an app's grants. A connection that
|
|
8
|
+
// is an MCP server's credential, or an app service's, is a source of its own
|
|
9
|
+
// kind already. A shared chat bot is read through its platform within the
|
|
10
|
+
// servers it serves. A workspace's own OAuth grant can be called here.
|
|
11
|
+
|
|
12
|
+
import { line } from '@yaks/query'
|
|
13
|
+
import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import { CallError } from '@yaks/tools/errors'
|
|
15
|
+
import { call, type Ctx } from './connections.js'
|
|
16
|
+
import { CONNECTION } from './identity.js'
|
|
17
|
+
import { type Integration, known, type Read } from './integrations.js'
|
|
18
|
+
|
|
19
|
+
/** One command a source offers, as a pool of tools lists it. */
|
|
20
|
+
export type SourceCommand = {
|
|
21
|
+
/** its source's address: `connection:<connection>` */
|
|
22
|
+
at: string
|
|
23
|
+
name: string
|
|
24
|
+
title: string
|
|
25
|
+
description: string
|
|
26
|
+
readOnly: boolean
|
|
27
|
+
/** its arguments, as JSON Schema */
|
|
28
|
+
input: Record<string, unknown>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A request's answer: the service's status, headers and body as text. */
|
|
32
|
+
export type Answer = {
|
|
33
|
+
status: number
|
|
34
|
+
headers: Record<string, string>
|
|
35
|
+
body: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let REQUEST = 'request'
|
|
39
|
+
|
|
40
|
+
let INPUT = {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
method: { type: 'string', description: 'The HTTP method; GET if omitted.' },
|
|
44
|
+
url: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
description: 'The https URL, on one of the hosts the connection names.',
|
|
47
|
+
},
|
|
48
|
+
headers: {
|
|
49
|
+
type: 'object',
|
|
50
|
+
additionalProperties: { type: 'string' },
|
|
51
|
+
description: 'Request headers. The credential is added on the way.',
|
|
52
|
+
},
|
|
53
|
+
body: {
|
|
54
|
+
description:
|
|
55
|
+
'The request body: text as it is, or JSON, sent with content-type application/json.',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
required: ['url'],
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let comp = (b: Bundle | undefined, name: string): Comp =>
|
|
62
|
+
(b?.[name] ?? {}) as Comp
|
|
63
|
+
|
|
64
|
+
let addressOf = (eid: Eid) => `connection:${eid}`
|
|
65
|
+
|
|
66
|
+
/** Whether a service can be called through a connection its owner holds. */
|
|
67
|
+
export let requestable = (i: Integration | undefined): i is Integration =>
|
|
68
|
+
!!i && !i.name.startsWith('mcp:') && !i.service && !i.broker?.scoped &&
|
|
69
|
+
(!i.chat || !!i.chat_grant) &&
|
|
70
|
+
i.hosts.length > 0
|
|
71
|
+
|
|
72
|
+
// A connected source held by one of `owners`, with its integration.
|
|
73
|
+
let offered = async (
|
|
74
|
+
read: Read,
|
|
75
|
+
b: Bundle,
|
|
76
|
+
owners: Eid[],
|
|
77
|
+
): Promise<Integration | undefined> => {
|
|
78
|
+
let c = comp(b, CONNECTION)
|
|
79
|
+
if (
|
|
80
|
+
c.status != 'connected' || !owners.includes(String(c.owner)) ||
|
|
81
|
+
typeof c.integration != 'string' || c.integration.startsWith('mcp:')
|
|
82
|
+
) return undefined
|
|
83
|
+
let i = await known(read, c.integration)
|
|
84
|
+
return requestable(i) ? i : undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let described = (b: Bundle, i: Integration): SourceCommand => {
|
|
88
|
+
let title = i.title || i.name
|
|
89
|
+
let account = comp(b, CONNECTION).account
|
|
90
|
+
return {
|
|
91
|
+
at: addressOf(b.entity.eid),
|
|
92
|
+
name: REQUEST,
|
|
93
|
+
title: `${title}: request`,
|
|
94
|
+
description: `Send an HTTPS request through the ${title} connection${
|
|
95
|
+
account ? ` of ${account}` : ''
|
|
96
|
+
}, its credential added on the way. It reaches ${
|
|
97
|
+
i.hosts.join(', ')
|
|
98
|
+
} and no other host.`,
|
|
99
|
+
readOnly: false,
|
|
100
|
+
input: INPUT,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The `request` command of each connection `owners` hold, or of the ones
|
|
105
|
+
* among `only`. */
|
|
106
|
+
export let requests = async (
|
|
107
|
+
read: Read,
|
|
108
|
+
owners: Eid[],
|
|
109
|
+
only?: Eid[],
|
|
110
|
+
): Promise<SourceCommand[]> => {
|
|
111
|
+
if (!owners.length || only?.length === 0) return []
|
|
112
|
+
let held = await read(
|
|
113
|
+
only
|
|
114
|
+
? line`.entity.eid=${only}&.${CONNECTION}&*`
|
|
115
|
+
: line`.${CONNECTION}.owner=${owners}&.${CONNECTION}.status=${'connected'}&*`,
|
|
116
|
+
)
|
|
117
|
+
let found = await Promise.all(held.map((b) => offered(read, b, owners)))
|
|
118
|
+
return held.flatMap((b, n) => found[n] ? [described(b, found[n]!)] : [])
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let object = (v: unknown): v is Record<string, unknown> =>
|
|
122
|
+
!!v && typeof v == 'object' && !Array.isArray(v)
|
|
123
|
+
|
|
124
|
+
// The request the arguments say, refused where they say none.
|
|
125
|
+
let methodOf = (i: Integration, url: string) => {
|
|
126
|
+
if (!i.api?.reads?.length) return 'GET'
|
|
127
|
+
let base = new URL(i.api.base)
|
|
128
|
+
let at = URL.parse(url)
|
|
129
|
+
let prefix = base.pathname.replace(/\/$/, '')
|
|
130
|
+
if (at?.origin != base.origin || !at.pathname.startsWith(prefix + '/')) {
|
|
131
|
+
return 'GET'
|
|
132
|
+
}
|
|
133
|
+
let path = at.pathname.slice(prefix.length)
|
|
134
|
+
return i.api.reads.find((r) => r.endsWith(' ' + path))?.split(' ')[0] ?? 'GET'
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let asked = (i: Integration, args: Record<string, unknown>): Request => {
|
|
138
|
+
if (typeof args.url != 'string') {
|
|
139
|
+
throw new CallError('arguments', 'request needs a url')
|
|
140
|
+
}
|
|
141
|
+
let method = typeof args.method == 'string'
|
|
142
|
+
? args.method.toUpperCase()
|
|
143
|
+
: methodOf(i, args.url)
|
|
144
|
+
if (args.headers != null && !object(args.headers)) {
|
|
145
|
+
throw new CallError('arguments', 'headers is an object of strings')
|
|
146
|
+
}
|
|
147
|
+
let headers = new Headers(
|
|
148
|
+
Object.entries(args.headers ?? {}).map(([k, v]) => [k, String(v)]),
|
|
149
|
+
)
|
|
150
|
+
let body = args.body == null
|
|
151
|
+
? undefined
|
|
152
|
+
: typeof args.body == 'string'
|
|
153
|
+
? args.body
|
|
154
|
+
: JSON.stringify(args.body)
|
|
155
|
+
if (
|
|
156
|
+
body != null && typeof args.body != 'string' && !headers.has('content-type')
|
|
157
|
+
) {
|
|
158
|
+
headers.set('content-type', 'application/json')
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
return new Request(args.url, { method, headers, body })
|
|
162
|
+
} catch (e) {
|
|
163
|
+
throw new CallError('arguments', `request: ${(e as Error).message}`)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Run a connection's command for a caller that may send through what
|
|
168
|
+
* `owners` hold: the request, sent through `call`. */
|
|
169
|
+
export let request = async (
|
|
170
|
+
c: Ctx,
|
|
171
|
+
a: {
|
|
172
|
+
at: string
|
|
173
|
+
owners: Eid[]
|
|
174
|
+
name: string
|
|
175
|
+
args: Record<string, unknown>
|
|
176
|
+
},
|
|
177
|
+
): Promise<{ text: string; value: Answer }> => {
|
|
178
|
+
let eid = a.at.slice('connection:'.length)
|
|
179
|
+
let [b] = eid
|
|
180
|
+
? await c.graph.read(line`.entity.eid=${eid}&.${CONNECTION}&*`)
|
|
181
|
+
: []
|
|
182
|
+
let i = b && await offered(c.graph.read, b, a.owners)
|
|
183
|
+
if (!i) {
|
|
184
|
+
throw new CallError('access', `${a.at} is not a connection you can use`)
|
|
185
|
+
}
|
|
186
|
+
if (a.name != REQUEST) {
|
|
187
|
+
throw new CallError(
|
|
188
|
+
'missing',
|
|
189
|
+
`${i.title || i.name} has one command, request`,
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
let res = await call(c, eid, asked(i, a.args))
|
|
193
|
+
let headers = Object.fromEntries(
|
|
194
|
+
[...res.headers].filter(([k]) => k != 'set-cookie'),
|
|
195
|
+
)
|
|
196
|
+
return {
|
|
197
|
+
text: '',
|
|
198
|
+
value: { status: res.status, headers, body: await res.text() },
|
|
199
|
+
}
|
|
200
|
+
}
|
package/services.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Service publication references the app's declaration, never a second schema.
|
|
2
|
+
import { line } from "@yaks/query";
|
|
3
|
+
import { token } from "@yaks/graph/sha256";
|
|
4
|
+
import { CallError } from "@yaks/tools/errors";
|
|
5
|
+
import { integrationEid } from "./identity.js";
|
|
6
|
+
import { grantOf } from "./sources.js";
|
|
7
|
+
import { PUBLISHED, publishing, told } from "./integrations.js";
|
|
8
|
+
/** A service keeps its integration name across app and space renames. */ export let serviceName = (app)=>`app-${app}`;
|
|
9
|
+
/** Publish or withdraw an app's offer without deleting its connections. */ export let publishService = async (read, a)=>{
|
|
10
|
+
let name = serviceName(a.app);
|
|
11
|
+
let eid = integrationEid(name);
|
|
12
|
+
let [held] = await read(line`.entity.eid=${eid}&.integration&?${PUBLISHED}`);
|
|
13
|
+
let i = held?.integration;
|
|
14
|
+
if (i && (i.owner != a.owner || i.service?.app != a.app)) {
|
|
15
|
+
throw new CallError('access', 'Only the owner of this app publishes its service');
|
|
16
|
+
}
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
entity: {
|
|
20
|
+
eid
|
|
21
|
+
},
|
|
22
|
+
integration: {
|
|
23
|
+
name,
|
|
24
|
+
owner: a.owner,
|
|
25
|
+
hosts: [],
|
|
26
|
+
service: {
|
|
27
|
+
app: a.app
|
|
28
|
+
},
|
|
29
|
+
...told(a),
|
|
30
|
+
...a.description == null ? {} : {
|
|
31
|
+
description: a.description
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
...a.listed == !!held?.[PUBLISHED] ? {} : {
|
|
35
|
+
[PUBLISHED]: publishing(a.listed)
|
|
36
|
+
},
|
|
37
|
+
...i ? {
|
|
38
|
+
$was: {
|
|
39
|
+
integration: {
|
|
40
|
+
owner: token(i.owner)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} : {}
|
|
44
|
+
}
|
|
45
|
+
];
|
|
46
|
+
};
|
|
47
|
+
/** Check the current person-owned connection and its source-app link: the
|
|
48
|
+
* person's grant of it to the app (./sources.ts), carrying the operation. */ export let serviceAuthority = async (read, a)=>{
|
|
49
|
+
let held = await grantOf(read, {
|
|
50
|
+
app: a.app,
|
|
51
|
+
person: a.person,
|
|
52
|
+
eid: a.connection
|
|
53
|
+
});
|
|
54
|
+
let c = held?.source.connection;
|
|
55
|
+
return !!c && c.status == 'connected' && c.integration == serviceName(a.service) && Array.isArray(c.scopes) && c.scopes.includes(a.operation);
|
|
56
|
+
};
|
|
57
|
+
/** The person's explicit selection of service operations, or its revocation. */ export let grantService = async (read, a)=>{
|
|
58
|
+
let [held] = await read(line`.entity.eid=${a.connection}&.connection`);
|
|
59
|
+
let c = held?.connection;
|
|
60
|
+
if (!c || c.owner != a.person) {
|
|
61
|
+
throw new CallError('access', 'Only the person who owns this connection grants its operations');
|
|
62
|
+
}
|
|
63
|
+
return [
|
|
64
|
+
{
|
|
65
|
+
entity: {
|
|
66
|
+
eid: a.connection
|
|
67
|
+
},
|
|
68
|
+
connection: {
|
|
69
|
+
scopes: [
|
|
70
|
+
...new Set(a.operations)
|
|
71
|
+
],
|
|
72
|
+
status: a.operations.length ? 'connected' : 'needed'
|
|
73
|
+
},
|
|
74
|
+
$was: {
|
|
75
|
+
connection: {
|
|
76
|
+
owner: token(c.owner),
|
|
77
|
+
scopes: token(c.scopes),
|
|
78
|
+
status: token(c.status)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
];
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=services.js.map
|
package/services.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.js","sources":["./services.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,SAAS,IAAI,2BAAgC;AAE7C,SAAS,KAAK,kCAAuC;AACrD,SAAS,SAAS,kCAAuC;AACzD,SAAS,cAAc,wBAAuB;AAC9C,SAAS,OAAO,uBAAsB;AACtC,SAGE,SAAS,EACT,UAAU,EAEV,IAAI,4BACoB;AAE1B,uEAAuE,GACvE,OAAO,IAAI,cAAc,CAAC,MAAgB,CAAC,IAAI,EAAE,KAAK,CAAA;AAEtD,yEAAyE,GACzE,OAAO,IAAI,iBAAiB,OAC1B,MACA;EAOA,IAAI,OAAO,YAAY,EAAE,GAAG;EAC5B,IAAI,MAAM,eAAe;EACzB,IAAI,CAAC,KAAK,GAAG,MAAM,KAAK,IAAI,CAAC,YAAY,EAAE,IAAI,eAAe,EAAE,UAAU,CAAC;EAC3E,IAAI,IAAI,MAAM;EACd,IAAI,KAAK,CAAC,EAAE,KAAK,IAAI,EAAE,KAAK,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,GAAG;IACxD,MAAM,IAAI,UACR,UACA;EAEJ;EACA,OAAO;IAAC;MACN,QAAQ;QAAE;MAAI;MACd,aAAa;QACX;QACA,OAAO,EAAE,KAAK;QACd,OAAO,EAAE;QACT,SAAS;UAAE,KAAK,EAAE,GAAG;QAAC;QACtB,GAAG,KAAK,EAAE;QACV,GAAG,EAAE,WAAW,IAAI,OAAO,CAAC,IAAI;UAAE,aAAa,EAAE,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ACxB;MACF;IACF;GAAE;AACJ,EAAC"}
|
package/services.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Service publication references the app's declaration, never a second schema.
|
|
2
|
+
import { line } from '@yaks/query'
|
|
3
|
+
import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
|
|
4
|
+
import { token } from '@yaks/graph/sha256'
|
|
5
|
+
import { CallError } from '@yaks/tools/errors'
|
|
6
|
+
import { integrationEid } from './identity.js'
|
|
7
|
+
import { grantOf } from './sources.js'
|
|
8
|
+
import {
|
|
9
|
+
type About,
|
|
10
|
+
type Integration,
|
|
11
|
+
PUBLISHED,
|
|
12
|
+
publishing,
|
|
13
|
+
type Read,
|
|
14
|
+
told,
|
|
15
|
+
} from './integrations.js'
|
|
16
|
+
|
|
17
|
+
/** A service keeps its integration name across app and space renames. */
|
|
18
|
+
export let serviceName = (app: string) => `app-${app}`
|
|
19
|
+
|
|
20
|
+
/** Publish or withdraw an app's offer without deleting its connections. */
|
|
21
|
+
export let publishService = async (
|
|
22
|
+
read: Read,
|
|
23
|
+
a: About & {
|
|
24
|
+
app: string
|
|
25
|
+
owner: string
|
|
26
|
+
description?: string
|
|
27
|
+
listed: boolean
|
|
28
|
+
},
|
|
29
|
+
): Promise<Bundle[]> => {
|
|
30
|
+
let name = serviceName(a.app)
|
|
31
|
+
let eid = integrationEid(name)
|
|
32
|
+
let [held] = await read(line`.entity.eid=${eid}&.integration&?${PUBLISHED}`)
|
|
33
|
+
let i = held?.integration as Integration | undefined
|
|
34
|
+
if (i && (i.owner != a.owner || i.service?.app != a.app)) {
|
|
35
|
+
throw new CallError(
|
|
36
|
+
'access',
|
|
37
|
+
'Only the owner of this app publishes its service',
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
return [{
|
|
41
|
+
entity: { eid },
|
|
42
|
+
integration: {
|
|
43
|
+
name,
|
|
44
|
+
owner: a.owner,
|
|
45
|
+
hosts: [],
|
|
46
|
+
service: { app: a.app },
|
|
47
|
+
...told(a),
|
|
48
|
+
...a.description == null ? {} : { description: a.description },
|
|
49
|
+
},
|
|
50
|
+
...a.listed == !!held?.[PUBLISHED]
|
|
51
|
+
? {}
|
|
52
|
+
: { [PUBLISHED]: publishing(a.listed) },
|
|
53
|
+
...i ? { $was: { integration: { owner: token(i.owner) } } } : {},
|
|
54
|
+
}]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The authority retained by a service order or delayed operation. */
|
|
58
|
+
export type ServiceAuthority = {
|
|
59
|
+
person: string
|
|
60
|
+
app: string
|
|
61
|
+
space: string
|
|
62
|
+
service: string
|
|
63
|
+
connection: string
|
|
64
|
+
operation: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Check the current person-owned connection and its source-app link: the
|
|
68
|
+
* person's grant of it to the app (./sources.ts), carrying the operation. */
|
|
69
|
+
export let serviceAuthority = async (
|
|
70
|
+
read: Read,
|
|
71
|
+
a: ServiceAuthority,
|
|
72
|
+
): Promise<boolean> => {
|
|
73
|
+
let held = await grantOf(read, {
|
|
74
|
+
app: a.app,
|
|
75
|
+
person: a.person,
|
|
76
|
+
eid: a.connection,
|
|
77
|
+
})
|
|
78
|
+
let c = held?.source.connection as Comp | undefined
|
|
79
|
+
return !!c && c.status == 'connected' &&
|
|
80
|
+
c.integration == serviceName(a.service) && Array.isArray(c.scopes) &&
|
|
81
|
+
c.scopes.includes(a.operation)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The person's explicit selection of service operations, or its revocation. */
|
|
85
|
+
export let grantService = async (
|
|
86
|
+
read: Read,
|
|
87
|
+
a: { person: string; connection: string; operations: string[] },
|
|
88
|
+
): Promise<Bundle[]> => {
|
|
89
|
+
let [held] = await read(line`.entity.eid=${a.connection}&.connection`)
|
|
90
|
+
let c = held?.connection as Comp | undefined
|
|
91
|
+
if (!c || c.owner != a.person) {
|
|
92
|
+
throw new CallError(
|
|
93
|
+
'access',
|
|
94
|
+
'Only the person who owns this connection grants its operations',
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
return [{
|
|
98
|
+
entity: { eid: a.connection },
|
|
99
|
+
connection: {
|
|
100
|
+
scopes: [...new Set(a.operations)],
|
|
101
|
+
status: a.operations.length ? 'connected' : 'needed',
|
|
102
|
+
},
|
|
103
|
+
$was: {
|
|
104
|
+
connection: {
|
|
105
|
+
owner: token(c.owner),
|
|
106
|
+
scopes: token(c.scopes),
|
|
107
|
+
status: token(c.status),
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
}]
|
|
111
|
+
}
|
package/sign-in.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { line } from "@yaks/query";
|
|
2
|
+
import { secretEids } from "@yaks/secrets";
|
|
3
|
+
import { BROKERS, brokerSecret } from "./brokers.js";
|
|
4
|
+
import { clientSecret } from "./clients.js";
|
|
5
|
+
import { connectable } from "./integrations.js";
|
|
6
|
+
let setupOf = (i)=>i.broker ? Object.hasOwn(BROKERS, i.broker.name) && brokerSecret(i.broker.name) : i.client && clientSecret(i.client);
|
|
7
|
+
/** Which built integrations have sign-in configured, for showing setup links.
|
|
8
|
+
* Read the directory's sealed records together. The sign-in itself reads its
|
|
9
|
+
* credential from the vault when the person follows the link. */ export let configuredConnections = async (read, integrations, brokers = [])=>{
|
|
10
|
+
let setups = integrations.filter((i)=>i.built).map((i)=>{
|
|
11
|
+
let name = setupOf(i);
|
|
12
|
+
return {
|
|
13
|
+
i,
|
|
14
|
+
eids: name ? secretEids(name) : []
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
let brokerSetups = brokers.filter((name)=>Object.hasOwn(BROKERS, name)).map((name)=>({
|
|
18
|
+
name,
|
|
19
|
+
eids: secretEids(brokerSecret(name))
|
|
20
|
+
}));
|
|
21
|
+
let ids = [
|
|
22
|
+
...new Set([
|
|
23
|
+
...setups,
|
|
24
|
+
...brokerSetups
|
|
25
|
+
].flatMap(({ eids })=>eids))
|
|
26
|
+
];
|
|
27
|
+
let rows = ids.length ? await read(line`.entity.eid=${ids}&.secret.value&!provisional&!failed&.fields=entity.eid`) : [];
|
|
28
|
+
let kept = new Set(rows.map((row)=>row.entity.eid));
|
|
29
|
+
let configured = (eids)=>eids.some((e)=>kept.has(e));
|
|
30
|
+
return {
|
|
31
|
+
integrations: new Set(setups.filter(({ i, eids })=>connectable(i, configured(eids))).map(({ i })=>i.name)),
|
|
32
|
+
brokers: new Set(brokerSetups.filter(({ eids })=>configured(eids)).map(({ name })=>name))
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export let configuredSignIns = async (read, integrations)=>(await configuredConnections(read, integrations)).integrations;
|
|
36
|
+
//# sourceMappingURL=sign-in.js.map
|
package/sign-in.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sign-in.js","sources":["./sign-in.ts"],"names":[],"mappings":"AAAA,SAAS,IAAI,2BAAgC;AAC7C,SAAS,UAAU,6BAAkC;AACrD,SAAS,OAAO,EAAE,YAAY,uBAAsB;AACpD,SAAS,YAAY,uBAAsB;AAC3C,SAAS,WAAW,4BAAwD;AAE5E,IAAI,UAAU,CAAC,IACb,EAAE,MAAM,GACJ,OAAO,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,KAAK,aAAa,EAAE,MAAM,CAAC,IAAI,IACnE,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM;AAEvC;;+DAE+D,GAC/D,OAAO,IAAI,wBAAwB,OACjC,MACA,cACA,UAAoB,EAAE;EAEtB,IAAI,SAAS,aAAa,MAAM,CAAC,CAAC,IAAM,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;IACpD,IAAI,OAAO,QAAQ;IACnB,OAAO;MAAE;MAAG,MAAM,OAAO,WAAW,QAAQ,EAAE;IAAC;EACjD;EACA,IAAI,eAAe,QAAQ,MAAM,CAAC,CAAC,OAAS,OAAO,MAAM,CAAC,SAAS,OAChE,GAAG,CAAC,CAAC,OAAS,CAAC;MAAE;MAAM,MAAM,WAAW,aAAa;IAAO,CAAC;EAChE,IAAI,MAAM;OACL,IAAI,IAAI;SAAI;SAAW;KAAa,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,GAAK;GAC/D;EACD,IAAI,OAAO,IAAI,MAAM,GACjB,MAAM,KACN,IAAI,CAAC,YAAY,EAAE,IAAI,sDAAsD,CAAC,IAE9E,EAAE;EACN,IAAI,OAAO,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC,MAAQ,IAAI,MAAM,CAAC,GAAG;EACnD,IAAI,aAAa,CAAC,OAAmB,KAAK,IAAI,CAAC,CAAC,IAAM,KAAK,GAAG,CAAC;EAC/D,OAAO;IACL,cAAc,IAAI,IAChB,OAAO,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,GAAK,YAAY,GAAG,WAAW,QAAQ,GAAG,CAAC,CACnE,EAAE,CAAC,EAAE,GACF,EAAE,IAAI;IAEb,SAAS,IAAI,IACX,aAAa,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,GAAK,WAAW,OAC1C,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,GAAK;EAEzB;AACF,EAAC;AAED,OAAO,IAAI,oBAAoB,OAC7B,MACA,eAEA,CAAC,MAAM,sBAAsB,MAAM,aAAa,EAAE,YAAY,CAAA"}
|
package/sign-in.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { line } from '@yaks/query'
|
|
2
|
+
import { secretEids } from '@yaks/secrets'
|
|
3
|
+
import { BROKERS, brokerSecret } from './brokers.js'
|
|
4
|
+
import { clientSecret } from './clients.js'
|
|
5
|
+
import { connectable, type Integration, type Read } from './integrations.js'
|
|
6
|
+
|
|
7
|
+
let setupOf = (i: Integration) =>
|
|
8
|
+
i.broker
|
|
9
|
+
? Object.hasOwn(BROKERS, i.broker.name) && brokerSecret(i.broker.name)
|
|
10
|
+
: i.client && clientSecret(i.client)
|
|
11
|
+
|
|
12
|
+
/** Which built integrations have sign-in configured, for showing setup links.
|
|
13
|
+
* Read the directory's sealed records together. The sign-in itself reads its
|
|
14
|
+
* credential from the vault when the person follows the link. */
|
|
15
|
+
export let configuredConnections = async (
|
|
16
|
+
read: Read,
|
|
17
|
+
integrations: Integration[],
|
|
18
|
+
brokers: string[] = [],
|
|
19
|
+
): Promise<{ integrations: Set<string>; brokers: Set<string> }> => {
|
|
20
|
+
let setups = integrations.filter((i) => i.built).map((i) => {
|
|
21
|
+
let name = setupOf(i)
|
|
22
|
+
return { i, eids: name ? secretEids(name) : [] }
|
|
23
|
+
})
|
|
24
|
+
let brokerSetups = brokers.filter((name) => Object.hasOwn(BROKERS, name))
|
|
25
|
+
.map((name) => ({ name, eids: secretEids(brokerSecret(name)) }))
|
|
26
|
+
let ids = [
|
|
27
|
+
...new Set([...setups, ...brokerSetups].flatMap(({ eids }) => eids)),
|
|
28
|
+
]
|
|
29
|
+
let rows = ids.length
|
|
30
|
+
? await read(
|
|
31
|
+
line`.entity.eid=${ids}&.secret.value&!provisional&!failed&.fields=entity.eid`,
|
|
32
|
+
)
|
|
33
|
+
: []
|
|
34
|
+
let kept = new Set(rows.map((row) => row.entity.eid))
|
|
35
|
+
let configured = (eids: string[]) => eids.some((e) => kept.has(e))
|
|
36
|
+
return {
|
|
37
|
+
integrations: new Set(
|
|
38
|
+
setups.filter(({ i, eids }) => connectable(i, configured(eids))).map((
|
|
39
|
+
{ i },
|
|
40
|
+
) => i.name),
|
|
41
|
+
),
|
|
42
|
+
brokers: new Set(
|
|
43
|
+
brokerSetups.filter(({ eids }) => configured(eids))
|
|
44
|
+
.map(({ name }) => name),
|
|
45
|
+
),
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export let configuredSignIns = async (
|
|
50
|
+
read: Read,
|
|
51
|
+
integrations: Integration[],
|
|
52
|
+
): Promise<Set<string>> =>
|
|
53
|
+
(await configuredConnections(read, integrations)).integrations
|
package/signin-mail.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The sign-in code is read from this graph's incoming mail, never a token argument.
|
|
2
|
+
import { and, eq, ge, limit, want } from "@yaks/query";
|
|
3
|
+
let prop = (b, name, field)=>b[name]?.[field];
|
|
4
|
+
let SUBJECT = /\b(\d{6})\b is your yaks\.app code/;
|
|
5
|
+
export let codeIn = (letters, address, since)=>{
|
|
6
|
+
let seen = letters.map((b)=>({
|
|
7
|
+
code: SUBJECT.exec(String(prop(b, 'doc', 'title') ?? ''))?.[1],
|
|
8
|
+
to: String(prop(b, 'mail', 'to') ?? ''),
|
|
9
|
+
at: Date.parse(String(prop(b, 'mail', 'at') ?? ''))
|
|
10
|
+
})).filter((m)=>m.code && m.to == address && m.at >= since).sort((a, b)=>b.at - a.at);
|
|
11
|
+
return seen[0]?.code ?? null;
|
|
12
|
+
};
|
|
13
|
+
export let lettersFor = (address, since)=>and(eq('mail.to', address), ge('mail.at', new Date(since).toISOString()), want('doc'), limit(10));
|
|
14
|
+
export let codeFor = async (read, address, since, opts = {})=>{
|
|
15
|
+
let now = opts.now ?? Date.now;
|
|
16
|
+
let sleep = opts.sleep ?? ((ms)=>new Promise((go)=>setTimeout(go, ms)));
|
|
17
|
+
// A delayed mail pull does not invalidate the code's ten-minute lifetime.
|
|
18
|
+
let deadline = since + (opts.wait ?? 10 * 60_000);
|
|
19
|
+
for(;;){
|
|
20
|
+
let code = codeIn(await read(lettersFor(address, since)), address, since);
|
|
21
|
+
let left = deadline - now();
|
|
22
|
+
if (code && left > 0) return code;
|
|
23
|
+
if (left <= 0) {
|
|
24
|
+
throw new Error(`no sign-in code for ${address}; this graph must receive its mail`);
|
|
25
|
+
}
|
|
26
|
+
await sleep(Math.min(opts.poll ?? 3_000, left));
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=signin-mail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signin-mail.js","sources":["./signin-mail.ts"],"names":[],"mappings":"AAAA,oFAAo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C7B,IAAI,QAAQ,GAAG;MACb,MAAM,IAAI,MACR,CAAC,oBAAoB,EAAE,QAAQ,kCAAkC,CAAC;IAEtE;IACA,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,IAAI,IAAI,OAAO;EAC3C;AACF,EAAC"}
|
package/signin-mail.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// The sign-in code is read from this graph's incoming mail, never a token argument.
|
|
2
|
+
import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
|
|
3
|
+
import { type And, and, eq, ge, limit, want } from '@yaks/query'
|
|
4
|
+
import type { Read } from './integrations.ts'
|
|
5
|
+
|
|
6
|
+
let prop = (b: Bundle, name: string, field: string) =>
|
|
7
|
+
(b[name] as Comp)?.[field]
|
|
8
|
+
let SUBJECT = /\b(\d{6})\b is your yaks\.app code/
|
|
9
|
+
export let codeIn = (
|
|
10
|
+
letters: Bundle[],
|
|
11
|
+
address: string,
|
|
12
|
+
since: number,
|
|
13
|
+
): string | null => {
|
|
14
|
+
let seen = letters.map((b) => ({
|
|
15
|
+
code: SUBJECT.exec(String(prop(b, 'doc', 'title') ?? ''))?.[1],
|
|
16
|
+
to: String(prop(b, 'mail', 'to') ?? ''),
|
|
17
|
+
at: Date.parse(String(prop(b, 'mail', 'at') ?? '')),
|
|
18
|
+
})).filter((m) => m.code && m.to == address && m.at >= since)
|
|
19
|
+
.sort((a, b) => b.at - a.at)
|
|
20
|
+
return seen[0]?.code ?? null
|
|
21
|
+
}
|
|
22
|
+
export let lettersFor = (address: string, since: number): And =>
|
|
23
|
+
and(
|
|
24
|
+
eq('mail.to', address),
|
|
25
|
+
ge('mail.at', new Date(since).toISOString()),
|
|
26
|
+
want('doc'),
|
|
27
|
+
limit(10),
|
|
28
|
+
)
|
|
29
|
+
export let codeFor = async (
|
|
30
|
+
read: Read,
|
|
31
|
+
address: string,
|
|
32
|
+
since: number,
|
|
33
|
+
opts: {
|
|
34
|
+
wait?: number
|
|
35
|
+
poll?: number
|
|
36
|
+
now?: () => number
|
|
37
|
+
sleep?: (ms: number) => Promise<void>
|
|
38
|
+
} = {},
|
|
39
|
+
): Promise<string> => {
|
|
40
|
+
let now = opts.now ?? Date.now
|
|
41
|
+
let sleep = opts.sleep ??
|
|
42
|
+
((ms) => new Promise<void>((go) => setTimeout(go, ms)))
|
|
43
|
+
// A delayed mail pull does not invalidate the code's ten-minute lifetime.
|
|
44
|
+
let deadline = since + (opts.wait ?? 10 * 60_000)
|
|
45
|
+
for (;;) {
|
|
46
|
+
let code = codeIn(await read(lettersFor(address, since)), address, since)
|
|
47
|
+
let left = deadline - now()
|
|
48
|
+
if (code && left > 0) return code
|
|
49
|
+
if (left <= 0) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`no sign-in code for ${address}; this graph must receive its mail`,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
await sleep(Math.min(opts.poll ?? 3_000, left))
|
|
55
|
+
}
|
|
56
|
+
}
|
package/signin.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Private sign-in attempts; completed grants belong to connections in the vault. */ import { begin, connect, credential, need, pick, refresh, settle } from "./connections.js";
|
|
2
|
+
import { CONNECTION } from "./identity.js";
|
|
3
|
+
/** The registered browser return address for a local sign-in. */ export const REDIRECT = 'http://localhost:8765/oauth/callback';
|
|
4
|
+
export const signins = (h)=>{
|
|
5
|
+
const ctx = (redirect)=>({
|
|
6
|
+
graph: h.g,
|
|
7
|
+
vault: h.vault,
|
|
8
|
+
fetch: h.fetch,
|
|
9
|
+
redirect
|
|
10
|
+
});
|
|
11
|
+
const pending = new Map();
|
|
12
|
+
const slot = (owner, integration)=>JSON.stringify([
|
|
13
|
+
owner,
|
|
14
|
+
integration
|
|
15
|
+
]);
|
|
16
|
+
const held = async (owner, integration)=>(await pick(h.g.read, owner, integration))?.entity.eid;
|
|
17
|
+
return {
|
|
18
|
+
/** The credential `owner` signed in with, once it has. */ key: async (owner, integration)=>{
|
|
19
|
+
const eid = await held(owner, integration);
|
|
20
|
+
return eid ? await credential(ctx(REDIRECT), eid) : undefined;
|
|
21
|
+
},
|
|
22
|
+
/** Rotate a rejected bearer unless another caller already replaced it. */ refresh: async (owner, integration, stale)=>{
|
|
23
|
+
const eid = await held(owner, integration);
|
|
24
|
+
return eid ? await refresh(ctx(REDIRECT), eid, stale) : undefined;
|
|
25
|
+
},
|
|
26
|
+
begin: async (owner, integration, redirect = REDIRECT)=>{
|
|
27
|
+
const eid = (await h.g.apply(await need(h.g.read, {
|
|
28
|
+
owner,
|
|
29
|
+
integration
|
|
30
|
+
}))).find((b)=>b[CONNECTION]).entity.eid;
|
|
31
|
+
const { url, attempt } = await begin(ctx(redirect), eid);
|
|
32
|
+
pending.set(slot(owner, integration), {
|
|
33
|
+
eid,
|
|
34
|
+
attempt,
|
|
35
|
+
redirect
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
url,
|
|
39
|
+
redirectUrl: redirect
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
complete: async (owner, integration, callback, session)=>{
|
|
43
|
+
const was = pending.get(slot(owner, integration));
|
|
44
|
+
if (!was) {
|
|
45
|
+
throw new Error('Authorization expired or not started; begin again');
|
|
46
|
+
}
|
|
47
|
+
// A brokered sign-in returns to its broker, which holds the grant.
|
|
48
|
+
if (!was.attempt) {
|
|
49
|
+
let made = await settle(ctx(was.redirect), was.eid);
|
|
50
|
+
if (!made.length) throw new Error('Finish signing in at the link first');
|
|
51
|
+
} else {
|
|
52
|
+
await connect(ctx(was.redirect), was.eid, {
|
|
53
|
+
attempt: was.attempt,
|
|
54
|
+
callback: callback.trim(),
|
|
55
|
+
session
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
pending.delete(slot(owner, integration));
|
|
59
|
+
},
|
|
60
|
+
cancel: (owner)=>{
|
|
61
|
+
if (!owner) return pending.clear();
|
|
62
|
+
for (const key of pending.keys()){
|
|
63
|
+
if (JSON.parse(key)[0] == owner) pending.delete(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=signin.js.map
|
package/signin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signin.js","sources":["./signin.ts"],"names":[],"mappings":"AAAA,mFAAmF,GACnF,SACE,KAAK,EACL,OAAO,EACP,UAAU,EAEV,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,MAAM,2BACiB;AACzB,SAAS,UAAU,wBAAuB;AAK1C,+DAA+D,GAC/D,OAAO,MAAM,WAAW,uCAAsC;AAuB9D,OAAO,MAAM,UAAU,CAAC;EAKtB,MAAM,MAAM,CAAC,WAA0B,CAAC;MACtC,OAAO,EAAE,CAAC;MACV,OAAO,EAAE,KAAK;MACd,OAAO,EAAE,KAAK;MACd;IACF,CAAC;EACD,MAAM,UAAU,IAAI;EAIpB,MAAM,OAAO,CAAC,OAAY,cACxB,KAAK,SAAS,CAAC;MAAC;MAAO;KAAY;EACrC,MAAM,OAAO,OAAO,OAAY,cAC9B,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,YAAY,GAAG,OAAO;EACrD,OAAO;IACL,wDAAwD,GACxD,KAAK,OAAO,OAAY;MACtB,MAAM,MAAM,MAAM,KAAK,OAAO;MAC9B,OAAO,MAAM,MAAM,WAAW,IAAI,WAAW,OAAO;IACtD;IACA,wEAAwE,GACxE,SAAS,OAAO,OAAY,aAAqB;MAC/C,MAAM,MAAM,MAAM,KAAK,OAAO;MAC9B,OAAO,MAAM,MAAM,QAAQ,IAAI,WAAW,KAAK,SAAS;IAC1D;IACA,OAAO,OAAO,OAAY,aAAqB,WAAW,QAAQ;MAChE,MAAM,MACJ,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE;QAAE;QAAO;MAAY,GAAG,EAC3D,IAAI,CAAC,CAAC,IAAM,CAA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