@yaks/oauth 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 +130 -2
- package/_dist/client.d.ts +74 -0
- package/_dist/client.d.ts.map +1 -0
- package/_dist/identity.d.ts +38 -0
- package/_dist/identity.d.ts.map +1 -0
- package/_dist/mod.d.ts +22 -0
- package/_dist/mod.d.ts.map +1 -0
- package/client.js +219 -0
- package/client.js.map +1 -0
- package/client.ts +342 -0
- package/deno.json +1 -0
- package/identity.js +69 -0
- package/identity.js.map +1 -0
- package/identity.ts +91 -0
- package/mod.js +16 -0
- package/mod.js.map +1 -0
- package/mod.ts +40 -0
- package/package.json +16 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,131 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/oauth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The OAuth authorization-code client for a provider described as data, and the
|
|
4
|
+
building blocks it shares with protocol-specific adapters: private record
|
|
5
|
+
storage, PKCE, and expiring attempts.
|
|
6
|
+
|
|
7
|
+
## Exports
|
|
8
|
+
|
|
9
|
+
Import paths:
|
|
10
|
+
|
|
11
|
+
- `client(provider, options)` from `@yaks/oauth`: the authorization-code client
|
|
12
|
+
(RFC 6749 §4.1 with PKCE): `begin`, `complete`, `token` and `refresh`.
|
|
13
|
+
- `Provider`, `Tokens`, `Options`, `Client` and `OAuthError` from `@yaks/oauth`:
|
|
14
|
+
its data and its refusal.
|
|
15
|
+
- `AuthorizationStore<Record>` from `@yaks/oauth`: the private-record interface,
|
|
16
|
+
with reads and serialized updates.
|
|
17
|
+
- `attempt(now?)` from `@yaks/oauth`: a unique correlation value and ten-minute
|
|
18
|
+
deadline.
|
|
19
|
+
- `identity(tokens, expect)` and `claims(jwt)` from `@yaks/oauth`: who an OpenID
|
|
20
|
+
Connect provider says signed in, read from the id_token its token endpoint
|
|
21
|
+
answered (see Signing in below).
|
|
22
|
+
- `pkce()` from `@yaks/oauth`: a random verifier and its S256 challenge for
|
|
23
|
+
Proof Key for Code Exchange (PKCE). S256 means the challenge is the URL-safe
|
|
24
|
+
Base64 encoding of the verifier's SHA-256 hash.
|
|
25
|
+
|
|
26
|
+
The store's implementation is [@yaks/secrets](../secrets)'
|
|
27
|
+
`records(graph,
|
|
28
|
+
vault, prefix, check?)`: each record is a secret, so the graph
|
|
29
|
+
holds its name and a handle and the vault holds its contents.
|
|
30
|
+
|
|
31
|
+
## The client
|
|
32
|
+
|
|
33
|
+
A provider is data: where a person authorizes, where codes and refresh tokens
|
|
34
|
+
are exchanged, the scopes to ask for, any extra authorize parameters, and the
|
|
35
|
+
client registered with it.
|
|
36
|
+
|
|
37
|
+
```ts ignore
|
|
38
|
+
import { client, type Provider } from '@yaks/oauth'
|
|
39
|
+
|
|
40
|
+
let google: Provider = {
|
|
41
|
+
authorize: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
42
|
+
token: 'https://oauth2.googleapis.com/token',
|
|
43
|
+
scopes: ['https://www.googleapis.com/auth/calendar.events'],
|
|
44
|
+
params: { access_type: 'offline', prompt: 'consent' },
|
|
45
|
+
client: { id: clientId, secret: clientSecret },
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let calendar = client(google, {
|
|
49
|
+
store, // an AuthorizationStore<Tokens>, such as @yaks/secrets records()
|
|
50
|
+
key: connectionEid, // where this grant is kept in the store
|
|
51
|
+
redirect: 'https://yaks.app/connections/callback',
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
// Request one: send the person to the link, and hold the attempt.
|
|
55
|
+
let { url, attempt } = await calendar.begin()
|
|
56
|
+
|
|
57
|
+
// Request two, at the redirect: exchange the code and keep the grant.
|
|
58
|
+
await calendar.complete(attempt, request.url)
|
|
59
|
+
|
|
60
|
+
// Any time after: a usable access token, refreshed when it is about to expire.
|
|
61
|
+
let token = await calendar.token()
|
|
62
|
+
|
|
63
|
+
// When the API refuses a token, a new one (once, however many callers ask).
|
|
64
|
+
let next = await calendar.refresh(token!)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
- `begin(scopes?)` returns the authorize link and the attempt: its `state`, its
|
|
68
|
+
deadline and its PKCE verifier. The client keeps nothing, because the request
|
|
69
|
+
that begins and the request that completes may not share a process; the caller
|
|
70
|
+
holds the attempt across the redirect (a sealed cookie, a short-lived record).
|
|
71
|
+
Extra `params` never replace the flow's own parameters.
|
|
72
|
+
- `complete(attempt, url)` checks the return's `state`, the deadline, any
|
|
73
|
+
`error`, and that exactly one `code` came back, before any request. It then
|
|
74
|
+
exchanges the code with the verifier and replaces the stored grant whole.
|
|
75
|
+
- `token()` answers from the store while the token has more than a minute left,
|
|
76
|
+
and refreshes it otherwise. It is `undefined` when there is no grant.
|
|
77
|
+
- `refresh(stale)` refreshes after the provider refused `stale`, unless another
|
|
78
|
+
caller already replaced it. A refresh that returns no new refresh token keeps
|
|
79
|
+
the old one (RFC 6749 §6).
|
|
80
|
+
- Refreshing happens inside the store's `update`, so two callers refreshing one
|
|
81
|
+
grant at once never both spend the refresh token.
|
|
82
|
+
- A confidential client authenticates at the token endpoint with HTTP Basic, or
|
|
83
|
+
in the form body with `auth: 'post'`. A client with no secret sends its id in
|
|
84
|
+
the body. Exchanges use form encoding; `refreshEncoding: 'json'` sends only
|
|
85
|
+
refresh grants as JSON for a provider that requires it.
|
|
86
|
+
- A provider with `answers: 'key'` runs OpenRouter's PKCE exchange instead: the
|
|
87
|
+
link carries `callback_url` and the challenge, the exchange posts JSON and
|
|
88
|
+
answers `{key}`, and the key is kept as an access token that never expires. It
|
|
89
|
+
needs no registered client, and since the provider carries no `state`, the
|
|
90
|
+
attempt's state rides the `callback_url`, so its return names its attempt as
|
|
91
|
+
every other flow's does.
|
|
92
|
+
- A refusal throws `OAuthError`, whose `code` is the provider's `error`
|
|
93
|
+
(`invalid_grant` means the grant is gone and the person must connect again),
|
|
94
|
+
`http_<status>` when the provider gave no code, or the client's own: `state`,
|
|
95
|
+
`code`, `expired`.
|
|
96
|
+
|
|
97
|
+
## Signing in
|
|
98
|
+
|
|
99
|
+
Signing a person in with another provider ("Continue with Google") is the same
|
|
100
|
+
flow asking for `openid email profile`, with the grant kept nowhere. The
|
|
101
|
+
`identity(tokens, {issuers, audience})` that follows `complete` reads the
|
|
102
|
+
id_token and refuses one whose `iss` is not among `issuers`, whose `aud` does
|
|
103
|
+
not name the client id `audience`, or that has expired. It answers the subject,
|
|
104
|
+
the email lowercased, whether the provider says it verified the email, and the
|
|
105
|
+
name. The token came straight from the token endpoint over TLS, so its signature
|
|
106
|
+
is not checked (OpenID Connect Core §3.1.3.7, item 6). Do not use `identity` on
|
|
107
|
+
an id_token that arrived any other way.
|
|
108
|
+
|
|
109
|
+
## The store
|
|
110
|
+
|
|
111
|
+
`AuthorizationStore<R>.read(key)` returns a record or `undefined`.
|
|
112
|
+
`update(key, fn)` passes a mutable record to an asynchronous callback and
|
|
113
|
+
returns its result. An implementation serializes updates across every writer and
|
|
114
|
+
saves mutations even if the callback throws. Callers must validate a response
|
|
115
|
+
before mutating the record if failure should leave it unchanged.
|
|
116
|
+
|
|
117
|
+
## Storage and security
|
|
118
|
+
|
|
119
|
+
The store contains sensitive values. Nothing in this package opens a browser,
|
|
120
|
+
accepts an HTTP callback, or keeps anything of its own: the host routes the
|
|
121
|
+
redirect to `complete` and gives the client its store.
|
|
122
|
+
|
|
123
|
+
An MCP server is this flow over data found at the server:
|
|
124
|
+
`@yaks/mcp-client/oauth` discovers its endpoints and registers a client, and
|
|
125
|
+
this client signs in with `resource` (RFC 8707) and checks `issuer` (RFC 9207)
|
|
126
|
+
where the server names one.
|
|
127
|
+
|
|
128
|
+
## Compatibility
|
|
129
|
+
|
|
130
|
+
The module uses `fetch`, Web Crypto and `btoa` (modern Deno, Node, browsers, and
|
|
131
|
+
Workers).
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Attempt, type AuthorizationStore } from "./mod.js";
|
|
2
|
+
/** An OAuth provider as data: where a person authorizes, where codes and
|
|
3
|
+
* refresh tokens are exchanged, and the client registered with it. */ export type Provider = {
|
|
4
|
+
authorize: string;
|
|
5
|
+
token: string;
|
|
6
|
+
scopes?: string[];
|
|
7
|
+
/** Delimiter used by this provider for requested and granted scopes. */ scopeSeparator?: ' ' | ',';
|
|
8
|
+
/** A confidential client whose provider requires PKCE to be omitted. */ pkce?: boolean;
|
|
9
|
+
/** Extra authorize parameters the provider wants, such as Google's
|
|
10
|
+
* `access_type: 'offline'` for a refresh token. They never replace the
|
|
11
|
+
* flow's own (state, challenge, redirect). */ params?: Record<string, string>;
|
|
12
|
+
/** The client registered with it; one that answers a key has none. */ client?: {
|
|
13
|
+
id: string;
|
|
14
|
+
secret?: string;
|
|
15
|
+
};
|
|
16
|
+
/** How a confidential client authenticates at the token endpoint: HTTP
|
|
17
|
+
* Basic, which every server must accept (RFC 6749 §2.3.1), or in the form
|
|
18
|
+
* body. A client with no secret sends its id in the body either way. */ auth?: 'basic' | 'post';
|
|
19
|
+
/** A provider may require JSON for refresh while codes exchange as form. */ refreshEncoding?: 'form' | 'json';
|
|
20
|
+
/** What the exchange answers: tokens, or an API key, as OpenRouter's PKCE
|
|
21
|
+
* does. A key is kept as an access token that never expires. Its flow has
|
|
22
|
+
* no `state` parameter, so the attempt's state rides the `callback_url`,
|
|
23
|
+
* and the return names its attempt as every other flow's does. */ answers?: 'tokens' | 'key';
|
|
24
|
+
/** The resource the grant is for (RFC 8707), as an MCP server asks: sent
|
|
25
|
+
* with the link and with every exchange. */ resource?: string;
|
|
26
|
+
/** The issuer every return must name (RFC 9207), for a server that says it
|
|
27
|
+
* names one: a return from any other server is refused. */ issuer?: string;
|
|
28
|
+
/** The field of the token endpoint's answer that names the account the
|
|
29
|
+
* grant is for, as a dotted path, where the answer says it: Discord's
|
|
30
|
+
* `guild.id`, the server a bot was added to. It is kept as `account`. */ account?: string;
|
|
31
|
+
/** The field of the token endpoint's answer that names that account for
|
|
32
|
+
* people, as a dotted path: Discord's `guild.name`. It is kept as
|
|
33
|
+
* `account_title`. */ account_title?: string;
|
|
34
|
+
};
|
|
35
|
+
/** One grant, as the store keeps it. `expires_at` is epoch milliseconds. */ export type Tokens = {
|
|
36
|
+
access_token?: string;
|
|
37
|
+
token_type?: string;
|
|
38
|
+
/** OpenID identity returned by the provider, retained across refresh. */ id_token?: string;
|
|
39
|
+
refresh_token?: string;
|
|
40
|
+
expires_at?: number;
|
|
41
|
+
scope?: string;
|
|
42
|
+
/** What the provider's `account` field said, retained across refresh. */ account?: string;
|
|
43
|
+
/** What the provider's `account_title` field said, retained across
|
|
44
|
+
* refresh. */ account_title?: string;
|
|
45
|
+
};
|
|
46
|
+
/** The provider's refusal, carrying its error code: `invalid_grant` is a grant
|
|
47
|
+
* revoked or a refresh token already spent, and means connect again. */ export declare class OAuthError extends Error {
|
|
48
|
+
code: string;
|
|
49
|
+
constructor(code: string, message: string);
|
|
50
|
+
}
|
|
51
|
+
export type Options = {
|
|
52
|
+
store: AuthorizationStore<Tokens>;
|
|
53
|
+
/** The store key the grant lives under, such as a connection's eid. */ key: string;
|
|
54
|
+
/** Where the provider sends the person back, as registered with it. */ redirect: string;
|
|
55
|
+
fetch?: typeof fetch;
|
|
56
|
+
now?: () => number;
|
|
57
|
+
};
|
|
58
|
+
export type Client = {
|
|
59
|
+
/** The link to send a person to, and the attempt to hold until they
|
|
60
|
+
* return. */ begin: (scopes?: string[]) => Promise<{
|
|
61
|
+
url: string;
|
|
62
|
+
attempt: Attempt & {
|
|
63
|
+
verifier: string;
|
|
64
|
+
};
|
|
65
|
+
}>;
|
|
66
|
+
/** Exchange the code the return URL carries and keep the grant. */ complete: (pending: Attempt, callback: string | URL) => Promise<void>;
|
|
67
|
+
/** Redeem a provider-started installation for later, explicit attachment. */ redeem: (code: string) => Promise<Tokens>;
|
|
68
|
+
/** A usable access token, refreshed first when it is about to expire;
|
|
69
|
+
* undefined when there is no grant. */ token: () => Promise<string | undefined>;
|
|
70
|
+
/** A new access token after the provider refused `stale`, unless another
|
|
71
|
+
* caller has already replaced it. */ refresh: (stale: string) => Promise<string | undefined>;
|
|
72
|
+
};
|
|
73
|
+
export declare let client: (provider: Provider, o: Options) => Client;
|
|
74
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sources":["../client.ts"],"names":[],"mappings":"AAaA,SAAS,KAAK,OAAO,EAAW,KAAK,kBAAkB,mBAAwB;AAE/E;oEACoE,GACpE,YAAY;EACV,WAAW,MAAM;EACjB,OAAO,MAAM;EACb,SAAS,MAAM;EACf,sEAAsE,GACtE,iBAAiB,MAAM;EACvB,sEAAsE,GACtE,OAAO,OAAO;EACd;;8CAE4C,GAC5C,SAAS,OAAO,MAAM,EAAE,MAAM;EAC9B,oEAAoE,GACpE;IAAW,IAAI,MAAM;IAAE,SAAS,MAAM;;EACtC;;wEAEsE,GACtE,OAAO,UAAU;EACjB,0EAA0E,GAC1E,kBAAkB,SAAS;EAC3B;;;kEAGgE,GAChE,UAAU,WAAW;EACrB;4CAC0C,GAC1C,WAAW,MAAM;EACjB;2DACyD,GACzD,SAAS,MAAM;EACf;;yEAEuE,GACvE,UAAU,MAAM;EAChB;;sBAEoB,GACpB,gBAAgB,MAAM;;AAGxB,0EAA0E,GAC1E,YAAY;EACV,eAAe,MAAM;EACrB,aAAa,MAAM;EACnB,uEAAuE,GACvE,WAAW,MAAM;EACjB,gBAAgB,MAAM;EACtB,aAAa,MAAM;EACnB,QAAQ,MAAM;EACd,uEAAuE,GACvE,UAAU,MAAM;EAChB;cACY,GACZ,gBAAgB,MAAM;;AAGxB;sEACsE,GACtE,OAAO,cAAM,mBAAmB;EAC9B,MAAM,MAAM,CAAA;EACZ,YAAY,MAAM,MAAM,EAAE,SAAS,MAAM;AAK3C;AAEA,YAAY;EACV,OAAO,mBAAmB;EAC1B,qEAAqE,GACrE,KAAK,MAAM;EACX,qEAAqE,GACrE,UAAU,MAAM;EAChB,eAAe;EACf,YAAY,MAAM;;AAGpB,YAAY;EACV;aACW,GACX,QACE,SAAS,MAAM,OACZ;IAAU,KAAK,MAAM;IAAE,SAAS;MAAY,UAAU,MAAM;;;EACjE,iEAAiE,GACjE,WAAW,SAAS,SAAS,UAAU,MAAM,GAAG,QAAQ,QAAQ,IAAI;EACpE,2EAA2E,GAC3E,SAAS,MAAM,MAAM,KAAK,QAAQ;EAClC;uCACqC,GACrC,aAAa,QAAQ,MAAM,GAAG,SAAS;EACvC;qCACmC,GACnC,UAAU,OAAO,MAAM,KAAK,QAAQ,MAAM,GAAG,SAAS;;AAiDxD,OAAO,YAAI,SAAU,UAAU,UAAU,GAAG,YAAU,OAsLrD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Tokens } from "./client.js";
|
|
2
|
+
/** What a provider vouches for: who, and the address it says is theirs. */ export type Identity = {
|
|
3
|
+
issuer: string;
|
|
4
|
+
subject: string;
|
|
5
|
+
email?: string;
|
|
6
|
+
/** Whether the provider says it checked the address belongs to them. */ verified: boolean;
|
|
7
|
+
name?: string;
|
|
8
|
+
};
|
|
9
|
+
/** What an id_token must say to be believed: an issuer among `issuers`, an
|
|
10
|
+
* audience naming `audience` (the client id), and an expiry after `now`. */ export type Expect = {
|
|
11
|
+
issuers: string[];
|
|
12
|
+
audience: string;
|
|
13
|
+
now?: number;
|
|
14
|
+
};
|
|
15
|
+
/** The claims a JWT carries, unchecked; an empty object for one that is not
|
|
16
|
+
* three parts with a JSON object in the middle. */ export declare let claims: (jwt: string) => Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* Who `tokens`' id_token says signed in, refused unless it is from an issuer
|
|
19
|
+
* `expect` names, for its audience, and still standing.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { identity } from './identity.ts'
|
|
23
|
+
* import { assertEquals, assertThrows } from '@std/assert'
|
|
24
|
+
* let jwt = (c: object) => `h.${btoa(JSON.stringify(c))}.s`
|
|
25
|
+
* let said = {
|
|
26
|
+
* iss: 'https://id.test', aud: 'me', sub: '7', exp: 2e9,
|
|
27
|
+
* email: 'Ada@Example.com', email_verified: true, name: 'Ada',
|
|
28
|
+
* }
|
|
29
|
+
* let expect = { issuers: ['https://id.test'], audience: 'me', now: 0 }
|
|
30
|
+
* assertEquals(identity({ id_token: jwt(said) }, expect), {
|
|
31
|
+
* issuer: 'https://id.test', subject: '7', email: 'ada@example.com',
|
|
32
|
+
* verified: true, name: 'Ada',
|
|
33
|
+
* })
|
|
34
|
+
* assertThrows(() => identity({ id_token: jwt({ ...said, aud: 'you' }) }, expect))
|
|
35
|
+
* assertThrows(() => identity({}, expect))
|
|
36
|
+
* ```
|
|
37
|
+
*/ export declare let identity: (tokens: Tokens, expect: Expect) => Identity;
|
|
38
|
+
//# sourceMappingURL=identity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sources":["../identity.ts"],"names":[],"mappings":"AAQA,SAAqB,KAAK,MAAM,sBAAqB;AAErD,yEAAyE,GACzE,YAAY;EACV,QAAQ,MAAM;EACd,SAAS,MAAM;EACf,QAAQ,MAAM;EACd,sEAAsE,GACtE,UAAU,OAAO;EACjB,OAAO,MAAM;;AAGf;0EAC0E,GAC1E,YAAY;EACV,SAAS,MAAM;EACf,UAAU,MAAM;EAChB,MAAM,MAAM;;AAGd;iDACiD,GACjD,OAAO,YAAI,SAAU,KAAK,MAAM,KAAG,OAAO,MAAM,EAAE,OAAO,EAYxD;AAID;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,YAAI,WAAY,QAAQ,QAAQ,QAAQ,WAAS,SAuBvD"}
|
package/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Shared private authorization storage, expiring browser-flow attempts, and
|
|
2
|
+
* the generic authorization-code client for a provider described as data
|
|
3
|
+
* (./client.ts). Protocols that are not that flow — MCP discovery, OpenRouter's
|
|
4
|
+
* key exchange — stay in their own adapters.
|
|
5
|
+
*/ export type AuthorizationStore<R extends object> = {
|
|
6
|
+
read(key: string): Promise<R | undefined>;
|
|
7
|
+
update<T>(key: string, fn: (record: R) => Promise<T>): Promise<T>;
|
|
8
|
+
};
|
|
9
|
+
/** One attempt. Identity checks prevent late exchanges saving after cancellation. */ export type Attempt = {
|
|
10
|
+
state: string;
|
|
11
|
+
until: number;
|
|
12
|
+
verifier?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare const attempt: (now?: number) => Attempt;
|
|
15
|
+
/** A verifier's PKCE S256 challenge: its SHA-256, in base64url. */ export declare const challenge: (verifier: string) => Promise<string>;
|
|
16
|
+
/** PKCE S256; verifier is never sent in the authorization URL. */ export declare const pkce: () => Promise<{
|
|
17
|
+
verifier: string;
|
|
18
|
+
challenge: string;
|
|
19
|
+
}>;
|
|
20
|
+
export { type Client, client, OAuthError, type Options, type Provider, type Tokens } from "./client.js";
|
|
21
|
+
export { claims, type Expect, type Identity, identity } from "./identity.js";
|
|
22
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAEA;;;;CAIC,GACD,YAAY,mBAAmB,UAAU,MAAM;EAC7C,KAAK,KAAK,MAAM,GAAG,QAAQ,IAAI,SAAS;EACxC,OAAO,GAAG,KAAK,MAAM,EAAE,KAAK,QAAQ,MAAM,QAAQ,KAAK,QAAQ;;AAEjE,mFAAmF,GACnF,YAAY;EAAY,OAAO,MAAM;EAAE,OAAO,MAAM;EAAE,WAAW,MAAM;;AACvE,OAAO,cAAM,UAAW,MAAK,MAAM,KAAgB,QAGjD;AACF,iEAAiE,GACjE,OAAO,cAAM,YAAmB,UAAU,MAAM,KAAG,QAAQ,MAAM,EAK9D;AACH,gEAAgE,GAChE,OAAO,cAAM,YAAiB;EAC1B,UAAU,MAAM;EAAE,WAAW,MAAM;GAItC;AACD,SACE,KAAK,MAAM,EACX,MAAM,EACN,UAAU,EACV,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,KAAK,MAAM,sBACO;AACpB,SAAS,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,QAAQ,EAAE,QAAQ,wBAAuB"}
|