@clavia-auth/core 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +45 -1
- package/dist/encoding.d.ts +4 -0
- package/dist/encoding.js +21 -0
- package/dist/errors.d.ts +5 -3
- package/dist/errors.js +2 -1
- package/dist/index.d.ts +12 -3
- package/dist/index.js +9 -3
- package/dist/management/client.js +2 -21
- package/dist/redis/index.d.ts +18 -0
- package/dist/redis/index.js +18 -0
- package/dist/redis/stores.d.ts +51 -0
- package/dist/redis/stores.js +183 -0
- package/dist/session/handle.d.ts +7 -0
- package/dist/session/handle.js +15 -0
- package/dist/session/memory.d.ts +32 -0
- package/dist/session/memory.js +106 -0
- package/dist/session/ports.d.ts +63 -0
- package/dist/session/ports.js +6 -0
- package/dist/session/sealing.d.ts +23 -0
- package/dist/session/sealing.js +130 -0
- package/dist/session/values.d.ts +60 -0
- package/dist/session/values.js +6 -0
- package/dist/sign-in/clavia.d.ts +86 -0
- package/dist/sign-in/clavia.js +190 -0
- package/dist/sign-in/cookies.d.ts +11 -0
- package/dist/sign-in/cookies.js +42 -0
- package/dist/sign-in/options.d.ts +95 -0
- package/dist/sign-in/options.js +79 -0
- package/dist/sign-in/provider.d.ts +57 -0
- package/dist/sign-in/provider.js +279 -0
- package/dist/sign-in/refused.d.ts +42 -0
- package/dist/sign-in/refused.js +49 -0
- package/dist/sign-in/return-to.d.ts +6 -0
- package/dist/sign-in/return-to.js +13 -0
- package/dist/sign-in/session.d.ts +23 -0
- package/dist/sign-in/session.js +136 -0
- package/dist/sign-in/sign-out.d.ts +18 -0
- package/dist/sign-in/sign-out.js +47 -0
- package/dist/testing/contracts.d.ts +43 -0
- package/dist/testing/contracts.js +198 -0
- package/dist/testing/fake-provider.d.ts +102 -0
- package/dist/testing/fake-provider.js +295 -0
- package/dist/testing/index.d.ts +20 -0
- package/dist/testing/index.js +20 -0
- package/dist/urls.d.ts +6 -0
- package/dist/urls.js +29 -0
- package/package.json +19 -1
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// A fake OpenID Connect provider, answering through the `fetch` port, for the tests of the core and of its
|
|
2
|
+
// adapters: discovery, pushed authorization requests, the authorization endpoint (as a browser would reach it,
|
|
3
|
+
// through `authorize()`), the token endpoint, userinfo, revocation and the JWKS, with an ES256 key it holds. It can be told
|
|
4
|
+
// to answer wrongly, so every check the core makes is tested against an attack. Web standards only.
|
|
5
|
+
import { systemClock } from "../clock.js";
|
|
6
|
+
import { toBase64Url, utf8 } from "../encoding.js";
|
|
7
|
+
/**
|
|
8
|
+
* A fake OpenID Connect provider for tests: it holds an ES256 key, issues real signed tokens, and checks what a
|
|
9
|
+
* real one would (client authentication, PKCE S256, the redirect URI, codes used once).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { fakeProvider } from "@clavia-auth/core/testing";
|
|
14
|
+
*
|
|
15
|
+
* const provider = await fakeProvider({ person: { sub: "usr_1", name: "Ada" } });
|
|
16
|
+
* const clavia = createClavia({ issuer: provider.issuer, clientId: provider.clientId,
|
|
17
|
+
* clientSecret: provider.clientSecret, fetch: provider.fetch, … });
|
|
18
|
+
*
|
|
19
|
+
* const started = await clavia.handle(new Request("https://app.example.test/auth/sign-in"));
|
|
20
|
+
* const callback = await provider.authorize(started.headers.get("Location")!);
|
|
21
|
+
* provider.next.idToken = { claims: { nonce: "another" } }; // the next ID token is refused
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export async function fakeProvider(options = {}) {
|
|
25
|
+
const issuer = (options.issuer ?? "https://auth.example.test").replace(/\/+$/, "");
|
|
26
|
+
const clientId = options.clientId ?? "app";
|
|
27
|
+
const clientSecret = options.clientSecret ?? "app-secret";
|
|
28
|
+
const par = options.par ?? true;
|
|
29
|
+
const issParameter = options.issParameter ?? true;
|
|
30
|
+
const clock = options.clock ?? systemClock;
|
|
31
|
+
const person = {
|
|
32
|
+
sub: "usr_fake",
|
|
33
|
+
name: "Ada Lovelace",
|
|
34
|
+
email: "ada@example.test",
|
|
35
|
+
email_verified: true,
|
|
36
|
+
...options.person,
|
|
37
|
+
};
|
|
38
|
+
const sid = options.sid === undefined ? "sid_fake" : options.sid;
|
|
39
|
+
const lifetime = options.accessTokenLifetime ?? 300;
|
|
40
|
+
const signing = await keyPair("fake-key");
|
|
41
|
+
const foreign = await keyPair("fake-key");
|
|
42
|
+
const requests = [];
|
|
43
|
+
const pushed = new Map();
|
|
44
|
+
const codes = new Map();
|
|
45
|
+
const accessTokens = new Map();
|
|
46
|
+
const revoked = [];
|
|
47
|
+
/** Live refresh tokens, with what they were granted for. */
|
|
48
|
+
const refreshTokens = new Map();
|
|
49
|
+
const rotate = options.rotateRefreshTokens ?? true;
|
|
50
|
+
let refreshes = 0;
|
|
51
|
+
const next = {};
|
|
52
|
+
const endpoint = (path) => `${issuer}${path}`;
|
|
53
|
+
const json = (body, status = 200, headers = {}) => new Response(JSON.stringify(body), {
|
|
54
|
+
status,
|
|
55
|
+
headers: { "Content-Type": "application/json", "Cache-Control": "no-store", ...headers },
|
|
56
|
+
});
|
|
57
|
+
const oauthError = (error, status = 400) => json({ error, error_description: `The fake provider: ${error}.` }, status);
|
|
58
|
+
const authenticated = (request) => {
|
|
59
|
+
const basic = request.headers.get("Authorization");
|
|
60
|
+
if (basic?.startsWith("Basic ") === true) {
|
|
61
|
+
const decoded = atob(basic.slice(6));
|
|
62
|
+
const at = decoded.indexOf(":");
|
|
63
|
+
const [id, secret] = [decoded.slice(0, at), decoded.slice(at + 1)].map((part) => decodeURIComponent(part.replace(/\+/g, " ")));
|
|
64
|
+
return id === clientId && secret === clientSecret;
|
|
65
|
+
}
|
|
66
|
+
return request.form.get("client_id") === clientId && request.form.get("client_secret") === clientSecret;
|
|
67
|
+
};
|
|
68
|
+
const issueIdToken = async (parameters) => {
|
|
69
|
+
const now = Math.floor(clock.now() / 1000);
|
|
70
|
+
const tampering = next.idToken ?? {};
|
|
71
|
+
delete next.idToken;
|
|
72
|
+
const claims = {
|
|
73
|
+
iss: issuer,
|
|
74
|
+
sub: person.sub,
|
|
75
|
+
aud: clientId,
|
|
76
|
+
iat: now,
|
|
77
|
+
exp: now + 300,
|
|
78
|
+
auth_time: now,
|
|
79
|
+
nonce: parameters.get("nonce") ?? undefined,
|
|
80
|
+
...(sid === null ? {} : { sid }),
|
|
81
|
+
...tampering.claims,
|
|
82
|
+
};
|
|
83
|
+
const signedWith = tampering.signedWith ?? "issuer";
|
|
84
|
+
const header = {
|
|
85
|
+
alg: signedWith === "none" ? "none" : "ES256",
|
|
86
|
+
typ: "JWT",
|
|
87
|
+
kid: signing.kid,
|
|
88
|
+
...tampering.header,
|
|
89
|
+
};
|
|
90
|
+
return jwt(header, claims, signedWith === "none" ? null : signedWith === "foreign" ? foreign.privateKey : signing.privateKey);
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* A token response for what was granted: a new access token and ID token, and a new refresh token unless
|
|
94
|
+
* `keep` is the one to keep (no rotation: the response then names none).
|
|
95
|
+
*/
|
|
96
|
+
const tokens = async (granted, keep) => {
|
|
97
|
+
const accessToken = `at_${random()}`;
|
|
98
|
+
accessTokens.set(accessToken, person.sub);
|
|
99
|
+
const refreshToken = keep ?? `rt_${random()}`;
|
|
100
|
+
// What a refresh is granted for: the scope, never the sign-in's nonce.
|
|
101
|
+
refreshTokens.set(refreshToken, new URLSearchParams({ scope: granted.get("scope") ?? "openid" }));
|
|
102
|
+
return {
|
|
103
|
+
access_token: accessToken,
|
|
104
|
+
token_type: "Bearer",
|
|
105
|
+
expires_in: lifetime,
|
|
106
|
+
...(keep === null ? { refresh_token: refreshToken } : {}),
|
|
107
|
+
scope: granted.get("scope") ?? "openid",
|
|
108
|
+
id_token: await issueIdToken(granted),
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
const answer = async (request) => {
|
|
112
|
+
const path = request.url.href.startsWith(issuer) ? request.url.href.slice(issuer.length).split("?")[0] : null;
|
|
113
|
+
switch (`${request.method} ${path ?? ""}`) {
|
|
114
|
+
case "GET /.well-known/openid-configuration": {
|
|
115
|
+
const extra = next.discovery ?? {};
|
|
116
|
+
delete next.discovery;
|
|
117
|
+
return json({
|
|
118
|
+
issuer,
|
|
119
|
+
authorization_endpoint: endpoint("/authorize"),
|
|
120
|
+
token_endpoint: endpoint("/oauth2/token"),
|
|
121
|
+
userinfo_endpoint: endpoint("/userinfo"),
|
|
122
|
+
jwks_uri: endpoint("/.well-known/jwks.json"),
|
|
123
|
+
end_session_endpoint: endpoint("/logout"),
|
|
124
|
+
revocation_endpoint: endpoint("/oauth2/revoke"),
|
|
125
|
+
...(par ? { pushed_authorization_request_endpoint: endpoint("/oauth2/par") } : {}),
|
|
126
|
+
authorization_response_iss_parameter_supported: issParameter,
|
|
127
|
+
response_types_supported: ["code"],
|
|
128
|
+
subject_types_supported: ["public"],
|
|
129
|
+
id_token_signing_alg_values_supported: ["ES256"],
|
|
130
|
+
code_challenge_methods_supported: ["S256"],
|
|
131
|
+
token_endpoint_auth_methods_supported: ["client_secret_basic", "client_secret_post"],
|
|
132
|
+
...extra,
|
|
133
|
+
}, 200, { "Cache-Control": "public, max-age=300" });
|
|
134
|
+
}
|
|
135
|
+
case "GET /.well-known/jwks.json":
|
|
136
|
+
return json({ keys: [signing.publicJwk] });
|
|
137
|
+
case "POST /oauth2/par": {
|
|
138
|
+
if (!par) {
|
|
139
|
+
return oauthError("invalid_request", 404);
|
|
140
|
+
}
|
|
141
|
+
if (!authenticated(request)) {
|
|
142
|
+
return oauthError("invalid_client", 401);
|
|
143
|
+
}
|
|
144
|
+
const requestUri = `urn:ietf:params:oauth:request_uri:${random()}`;
|
|
145
|
+
const parameters = new URLSearchParams(request.form);
|
|
146
|
+
parameters.delete("client_secret");
|
|
147
|
+
pushed.set(requestUri, parameters);
|
|
148
|
+
return json({ request_uri: requestUri, expires_in: 60 }, 201);
|
|
149
|
+
}
|
|
150
|
+
case "POST /oauth2/token": {
|
|
151
|
+
if (!authenticated(request)) {
|
|
152
|
+
return oauthError("invalid_client", 401);
|
|
153
|
+
}
|
|
154
|
+
if (next.token !== undefined) {
|
|
155
|
+
const { error, status } = next.token;
|
|
156
|
+
delete next.token;
|
|
157
|
+
return oauthError(error, status ?? 400);
|
|
158
|
+
}
|
|
159
|
+
const grantType = request.form.get("grant_type");
|
|
160
|
+
if (grantType === "refresh_token") {
|
|
161
|
+
const used = request.form.get("refresh_token") ?? "";
|
|
162
|
+
const granted = refreshTokens.get(used);
|
|
163
|
+
if (granted === undefined) {
|
|
164
|
+
return oauthError("invalid_grant");
|
|
165
|
+
}
|
|
166
|
+
if (rotate) {
|
|
167
|
+
refreshTokens.delete(used);
|
|
168
|
+
}
|
|
169
|
+
refreshes += 1;
|
|
170
|
+
return json(await tokens(granted, rotate ? null : used));
|
|
171
|
+
}
|
|
172
|
+
if (grantType !== "authorization_code") {
|
|
173
|
+
return oauthError("unsupported_grant_type");
|
|
174
|
+
}
|
|
175
|
+
const code = request.form.get("code") ?? "";
|
|
176
|
+
const grant = codes.get(code);
|
|
177
|
+
codes.delete(code);
|
|
178
|
+
if (grant === undefined || request.form.get("redirect_uri") !== grant.parameters.get("redirect_uri")) {
|
|
179
|
+
return oauthError("invalid_grant");
|
|
180
|
+
}
|
|
181
|
+
const challenge = await s256(request.form.get("code_verifier") ?? "");
|
|
182
|
+
if (challenge !== grant.parameters.get("code_challenge")) {
|
|
183
|
+
return oauthError("invalid_grant");
|
|
184
|
+
}
|
|
185
|
+
return json(await tokens(grant.parameters, null));
|
|
186
|
+
}
|
|
187
|
+
case "POST /oauth2/revoke": {
|
|
188
|
+
if (!authenticated(request)) {
|
|
189
|
+
return oauthError("invalid_client", 401);
|
|
190
|
+
}
|
|
191
|
+
const failure = next.revocation;
|
|
192
|
+
delete next.revocation;
|
|
193
|
+
if (failure !== undefined) {
|
|
194
|
+
return oauthError("temporarily_unavailable", failure.status);
|
|
195
|
+
}
|
|
196
|
+
// RFC 7009 §2.2: an unknown token is answered 200 as well.
|
|
197
|
+
revoked.push(request.form.get("token") ?? "");
|
|
198
|
+
// A revoked refresh token refreshes nothing more.
|
|
199
|
+
refreshTokens.delete(request.form.get("token") ?? "");
|
|
200
|
+
return new Response(null, { status: 200 });
|
|
201
|
+
}
|
|
202
|
+
case "GET /userinfo": {
|
|
203
|
+
const token = request.headers.get("Authorization")?.replace(/^Bearer /, "") ?? "";
|
|
204
|
+
if (!accessTokens.has(token)) {
|
|
205
|
+
return json({ error: "invalid_token" }, 401, { "WWW-Authenticate": 'Bearer error="invalid_token"' });
|
|
206
|
+
}
|
|
207
|
+
const extra = next.userinfo ?? {};
|
|
208
|
+
delete next.userinfo;
|
|
209
|
+
return json({ ...person, ...extra });
|
|
210
|
+
}
|
|
211
|
+
default:
|
|
212
|
+
return json({ error: "not_found" }, 404);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
return {
|
|
216
|
+
issuer,
|
|
217
|
+
clientId,
|
|
218
|
+
clientSecret,
|
|
219
|
+
requests,
|
|
220
|
+
revoked,
|
|
221
|
+
get refreshes() {
|
|
222
|
+
return refreshes;
|
|
223
|
+
},
|
|
224
|
+
next,
|
|
225
|
+
fetch: async (input, init) => {
|
|
226
|
+
const request = new Request(input, init);
|
|
227
|
+
const form = request.method === "POST" ? new URLSearchParams(await request.text()) : new URLSearchParams();
|
|
228
|
+
const seen = { method: request.method, url: new URL(request.url), headers: request.headers, form };
|
|
229
|
+
requests.push(seen);
|
|
230
|
+
return answer(seen);
|
|
231
|
+
},
|
|
232
|
+
authorize(location, authorizeOptions = {}) {
|
|
233
|
+
return Promise.resolve().then(() => authorizeNow(location, authorizeOptions));
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
function authorizeNow(location, authorizeOptions) {
|
|
237
|
+
const url = new URL(location);
|
|
238
|
+
if (`${url.origin}${url.pathname}` !== endpoint("/authorize")) {
|
|
239
|
+
throw new Error(`The sign-in sent the browser to ${url.origin}${url.pathname}, not to the authorization endpoint.`);
|
|
240
|
+
}
|
|
241
|
+
let parameters = url.searchParams;
|
|
242
|
+
const requestUri = parameters.get("request_uri");
|
|
243
|
+
if (requestUri !== null) {
|
|
244
|
+
const stored = pushed.get(requestUri);
|
|
245
|
+
pushed.delete(requestUri);
|
|
246
|
+
if (stored === undefined || parameters.get("client_id") !== clientId) {
|
|
247
|
+
throw new Error("The authorization request names a request_uri the fake provider did not issue.");
|
|
248
|
+
}
|
|
249
|
+
parameters = stored;
|
|
250
|
+
}
|
|
251
|
+
if (parameters.get("client_id") !== clientId ||
|
|
252
|
+
parameters.get("response_type") !== "code" ||
|
|
253
|
+
parameters.get("code_challenge_method") !== "S256" ||
|
|
254
|
+
(parameters.get("code_challenge") ?? "") === "" ||
|
|
255
|
+
!parameters.has("redirect_uri")) {
|
|
256
|
+
throw new Error("The fake provider refuses the authorization request.");
|
|
257
|
+
}
|
|
258
|
+
const callback = new URL(parameters.get("redirect_uri") ?? "");
|
|
259
|
+
callback.searchParams.set("state", authorizeOptions.state ?? parameters.get("state") ?? "");
|
|
260
|
+
if (issParameter || authorizeOptions.iss !== undefined) {
|
|
261
|
+
callback.searchParams.set("iss", authorizeOptions.iss ?? issuer);
|
|
262
|
+
}
|
|
263
|
+
if (authorizeOptions.error !== undefined) {
|
|
264
|
+
callback.searchParams.set("error", authorizeOptions.error);
|
|
265
|
+
callback.searchParams.set("error_description", "The fake provider says no.");
|
|
266
|
+
return callback;
|
|
267
|
+
}
|
|
268
|
+
const code = `code_${random()}`;
|
|
269
|
+
codes.set(code, { parameters: new URLSearchParams(parameters) });
|
|
270
|
+
callback.searchParams.set("code", code);
|
|
271
|
+
return callback;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async function keyPair(kid) {
|
|
275
|
+
const pair = await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, ["sign", "verify"]);
|
|
276
|
+
const publicJwk = { ...(await crypto.subtle.exportKey("jwk", pair.publicKey)), kid, alg: "ES256", use: "sig" };
|
|
277
|
+
delete publicJwk.key_ops;
|
|
278
|
+
delete publicJwk.ext;
|
|
279
|
+
return { kid, privateKey: pair.privateKey, publicJwk };
|
|
280
|
+
}
|
|
281
|
+
async function jwt(header, claims, key) {
|
|
282
|
+
const encode = (value) => toBase64Url(utf8.encode(JSON.stringify(value)));
|
|
283
|
+
const input = `${encode(header)}.${encode(claims)}`;
|
|
284
|
+
if (key === null) {
|
|
285
|
+
return `${input}.`;
|
|
286
|
+
}
|
|
287
|
+
const signature = await crypto.subtle.sign({ name: "ECDSA", hash: "SHA-256" }, key, utf8.encode(input));
|
|
288
|
+
return `${input}.${toBase64Url(new Uint8Array(signature))}`;
|
|
289
|
+
}
|
|
290
|
+
async function s256(verifier) {
|
|
291
|
+
return toBase64Url(new Uint8Array(await crypto.subtle.digest("SHA-256", utf8.encode(verifier))));
|
|
292
|
+
}
|
|
293
|
+
function random() {
|
|
294
|
+
return toBase64Url(crypto.getRandomValues(new Uint8Array(16)));
|
|
295
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@clavia-auth/core/testing`: what an application or an adapter tests its own code against: the contracts every
|
|
3
|
+
* session store and attempt store must pass, and a fake OpenID Connect provider that issues real signed tokens
|
|
4
|
+
* and can be told to answer wrongly.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { fakeProvider, SessionStoreContract } from "@clavia-auth/core/testing";
|
|
9
|
+
*
|
|
10
|
+
* for (const contract of SessionStoreContract) {
|
|
11
|
+
* it(contract.name, () => contract.run({ store: new MySessionStore(), elapse: (s) => sleep(s * 1000) }));
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* const provider = await fakeProvider();
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
export { AttemptStoreContract, type ContractCase, SessionStoreContract, type StoreSubject } from "./contracts.js";
|
|
20
|
+
export { type FakeProvider, fakeProvider, type FakeProviderOptions, type FakeProviderRequest, type IdTokenTampering, } from "./fake-provider.js";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@clavia-auth/core/testing`: what an application or an adapter tests its own code against: the contracts every
|
|
3
|
+
* session store and attempt store must pass, and a fake OpenID Connect provider that issues real signed tokens
|
|
4
|
+
* and can be told to answer wrongly.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { fakeProvider, SessionStoreContract } from "@clavia-auth/core/testing";
|
|
9
|
+
*
|
|
10
|
+
* for (const contract of SessionStoreContract) {
|
|
11
|
+
* it(contract.name, () => contract.run({ store: new MySessionStore(), elapse: (s) => sleep(s * 1000) }));
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* const provider = await fakeProvider();
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
export { AttemptStoreContract, SessionStoreContract } from "./contracts.js";
|
|
20
|
+
export { fakeProvider, } from "./fake-provider.js";
|
package/dist/urls.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Whether `url` names this machine: plain http is allowed there, for development. */
|
|
2
|
+
export declare function isLoopback(url: URL): boolean;
|
|
3
|
+
/** https, or http to a loopback address in development; never credentials or a fragment in it. */
|
|
4
|
+
export declare function secureUrl(value: string | URL, name: string): URL;
|
|
5
|
+
/** Refuses to run in a browser: what holds a secret runs on a server only. */
|
|
6
|
+
export declare function refuseInBrowser(what: string): void;
|
package/dist/urls.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// What every entry point checks of the URLs it is given, and of where it runs (clavia's docs/design/sdks.md §7).
|
|
2
|
+
const LOOPBACK = ["localhost", "127.0.0.1", "[::1]"];
|
|
3
|
+
/** Whether `url` names this machine: plain http is allowed there, for development. */
|
|
4
|
+
export function isLoopback(url) {
|
|
5
|
+
return LOOPBACK.includes(url.hostname);
|
|
6
|
+
}
|
|
7
|
+
/** https, or http to a loopback address in development; never credentials or a fragment in it. */
|
|
8
|
+
export function secureUrl(value, name) {
|
|
9
|
+
let url;
|
|
10
|
+
try {
|
|
11
|
+
url = new URL(value);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
throw new TypeError(`${name} is not a URL.`);
|
|
15
|
+
}
|
|
16
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && isLoopback(url))) {
|
|
17
|
+
throw new TypeError(`${name} must be https (plain http only to localhost): ${url.origin}.`);
|
|
18
|
+
}
|
|
19
|
+
if (url.username !== "" || url.password !== "" || url.hash !== "") {
|
|
20
|
+
throw new TypeError(`${name} must carry no credentials and no fragment.`);
|
|
21
|
+
}
|
|
22
|
+
return url;
|
|
23
|
+
}
|
|
24
|
+
/** Refuses to run in a browser: what holds a secret runs on a server only. */
|
|
25
|
+
export function refuseInBrowser(what) {
|
|
26
|
+
if (typeof globalThis.document !== "undefined") {
|
|
27
|
+
throw new TypeError(`${what} holds a client secret and runs on a server only, not in a browser.`);
|
|
28
|
+
}
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clavia-auth/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The framework-free TypeScript core of the Clavia SDKs: a typed Management API client, and later sign-in and token checks.",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"author": "Phayne Limited (https://phayne.io)",
|
|
@@ -30,6 +30,14 @@
|
|
|
30
30
|
"types": "./dist/management/index.d.ts",
|
|
31
31
|
"default": "./dist/management/index.js"
|
|
32
32
|
},
|
|
33
|
+
"./redis": {
|
|
34
|
+
"types": "./dist/redis/index.d.ts",
|
|
35
|
+
"default": "./dist/redis/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./testing": {
|
|
38
|
+
"types": "./dist/testing/index.d.ts",
|
|
39
|
+
"default": "./dist/testing/index.js"
|
|
40
|
+
},
|
|
33
41
|
"./package.json": "./package.json"
|
|
34
42
|
},
|
|
35
43
|
"files": [
|
|
@@ -63,11 +71,21 @@
|
|
|
63
71
|
"oauth4webapi": "^3.8.9",
|
|
64
72
|
"openapi-fetch": "^0.17.0"
|
|
65
73
|
},
|
|
74
|
+
"peerDependencies": {
|
|
75
|
+
"ioredis": "^5.0.0 || ^6.0.0"
|
|
76
|
+
},
|
|
77
|
+
"peerDependenciesMeta": {
|
|
78
|
+
"ioredis": {
|
|
79
|
+
"optional": true
|
|
80
|
+
}
|
|
81
|
+
},
|
|
66
82
|
"devDependencies": {
|
|
67
83
|
"@eslint/js": "^10.0.1",
|
|
68
84
|
"@types/node": "^22.20.7",
|
|
69
85
|
"eslint": "^10.16.0",
|
|
86
|
+
"ioredis": "^6.1.0",
|
|
70
87
|
"openapi-typescript": "^7.13.0",
|
|
88
|
+
"playwright": "^1.65.1",
|
|
71
89
|
"prettier": "^3.10.1",
|
|
72
90
|
"typescript": "^5.9.3",
|
|
73
91
|
"typescript-eslint": "^8.74.0",
|