@softistx/janus-hono 0.0.0-stage → 0.11.34
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 +21 -0
- package/README.md +547 -2
- package/dist/bind.d.ts +104 -0
- package/dist/bind.d.ts.map +1 -0
- package/dist/device.d.ts +15 -0
- package/dist/device.d.ts.map +1 -0
- package/dist/errors.d.ts +60 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fresh.d.ts +41 -0
- package/dist/fresh.d.ts.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +496 -0
- package/dist/index.js.map +22 -0
- package/dist/no-store.d.ts +8 -0
- package/dist/no-store.d.ts.map +1 -0
- package/dist/passkeys/body.d.ts +12 -0
- package/dist/passkeys/body.d.ts.map +1 -0
- package/dist/passkeys/index.d.ts +8 -0
- package/dist/passkeys/index.d.ts.map +1 -0
- package/dist/passkeys/routes.d.ts +32 -0
- package/dist/passkeys/routes.d.ts.map +1 -0
- package/dist/passkeys/types.d.ts +105 -0
- package/dist/passkeys/types.d.ts.map +1 -0
- package/dist/permission.d.ts +80 -0
- package/dist/permission.d.ts.map +1 -0
- package/dist/provide.d.ts +19 -0
- package/dist/provide.d.ts.map +1 -0
- package/dist/session.d.ts +156 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/social/index.d.ts +9 -0
- package/dist/social/index.d.ts.map +1 -0
- package/dist/social/redirect.d.ts +9 -0
- package/dist/social/redirect.d.ts.map +1 -0
- package/dist/social/refusals.d.ts +14 -0
- package/dist/social/refusals.d.ts.map +1 -0
- package/dist/social/routes.d.ts +32 -0
- package/dist/social/routes.d.ts.map +1 -0
- package/dist/social/types.d.ts +90 -0
- package/dist/social/types.d.ts.map +1 -0
- package/docs/README.md +15 -0
- package/docs/guide/permissions.md +225 -0
- package/docs/guide/routes.md +1164 -0
- package/docs/roadmap.md +76 -0
- package/docs/troubleshooting.md +871 -0
- package/package.json +62 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
// src/passkeys/routes.ts
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
|
|
4
|
+
// src/device.ts
|
|
5
|
+
import {
|
|
6
|
+
DEVICE_COOKIE,
|
|
7
|
+
deviceCookie,
|
|
8
|
+
deviceFromCookieHeader
|
|
9
|
+
} from "@softistx/janus";
|
|
10
|
+
function deviceOf(c, options = {}) {
|
|
11
|
+
return deviceFromCookieHeader(c.req.header("cookie"), options);
|
|
12
|
+
}
|
|
13
|
+
function sendDevice(c, deviceToken, options = {}) {
|
|
14
|
+
c.header("Set-Cookie", deviceCookie(deviceToken, options), { append: true });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/no-store.ts
|
|
18
|
+
function noStore(c) {
|
|
19
|
+
c.header("Cache-Control", "no-store");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/session.ts
|
|
23
|
+
import { getCookie } from "hono/cookie";
|
|
24
|
+
function session(auth, options = {}) {
|
|
25
|
+
return async (c, next) => {
|
|
26
|
+
const type = options.type === undefined ? {} : { type: options.type };
|
|
27
|
+
const current = options.apiKey === true ? await auth.authenticate(c.req.raw, { ...type, apiKey: true }) : await auth.authenticate(c.req.raw, options.type === undefined ? undefined : { type: options.type });
|
|
28
|
+
if (current === null && options.required === true) {
|
|
29
|
+
return c.body(null, 401);
|
|
30
|
+
}
|
|
31
|
+
c.set("user", current?.user ?? null);
|
|
32
|
+
c.set("session", current?.session ?? null);
|
|
33
|
+
if (options.apiKey === true) {
|
|
34
|
+
c.set("principal", current);
|
|
35
|
+
c.set("apiKey", current?.via === "apiKey" ? current.apiKey : null);
|
|
36
|
+
}
|
|
37
|
+
await next();
|
|
38
|
+
const name = `${auth.cookie.name}=`;
|
|
39
|
+
if (current?.via !== "apiKey" && current?.renewed === true && getCookie(c, auth.cookie.name) === current.token && !c.res.headers.getSetCookie().some((value) => value.startsWith(name))) {
|
|
40
|
+
sendSession(c, auth, current);
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function sendSession(c, auth, signedIn, options = {}) {
|
|
46
|
+
noStore(c);
|
|
47
|
+
c.header("Set-Cookie", auth.cookie.serialize(signedIn.token, signedIn.session), {
|
|
48
|
+
append: true
|
|
49
|
+
});
|
|
50
|
+
if (typeof signedIn.deviceToken === "string") {
|
|
51
|
+
sendDevice(c, signedIn.deviceToken, options.device);
|
|
52
|
+
}
|
|
53
|
+
return signedIn.user;
|
|
54
|
+
}
|
|
55
|
+
async function signOut(c, auth) {
|
|
56
|
+
const revoked = await auth.signOut(c.req.raw);
|
|
57
|
+
c.header("Set-Cookie", auth.cookie.clear(), { append: true });
|
|
58
|
+
return revoked;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/passkeys/body.ts
|
|
62
|
+
async function bodyOf(c) {
|
|
63
|
+
const body = await c.req.json().catch(() => null);
|
|
64
|
+
return typeof body === "object" && body !== null && !Array.isArray(body) ? body : {};
|
|
65
|
+
}
|
|
66
|
+
function textOf(body, key) {
|
|
67
|
+
const value = body[key];
|
|
68
|
+
return typeof value === "string" ? value : "";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/passkeys/routes.ts
|
|
72
|
+
function passkeyRoutes(auth, options = {}) {
|
|
73
|
+
const typeOption = options.type === undefined ? {} : { type: options.type };
|
|
74
|
+
const api = apiOf(auth, options.type);
|
|
75
|
+
const wiring = {
|
|
76
|
+
auth,
|
|
77
|
+
api,
|
|
78
|
+
options,
|
|
79
|
+
current: (c) => auth.authenticate(c.req.raw, options.type === undefined ? undefined : typeOption)
|
|
80
|
+
};
|
|
81
|
+
const signedIn = options.session ?? session(auth, { required: true, ...typeOption });
|
|
82
|
+
const app = new Hono().get("/", signedIn, listHandler(wiring)).post("/registration-options", signedIn, optionsHandler(wiring)).post("/", signedIn, registerHandler(wiring)).post("/sign-in", signInHandler(wiring)).post("/:credentialId/rename", signedIn, renameHandler(wiring)).delete("/:credentialId", signedIn, removeHandler(wiring));
|
|
83
|
+
const { stepUp } = api;
|
|
84
|
+
if (stepUp !== undefined) {
|
|
85
|
+
app.post("/step-up", signedIn, stepUpHandler(wiring, stepUp)).post("/step-up/confirm", signedIn, stepUpConfirmHandler(stepUp));
|
|
86
|
+
}
|
|
87
|
+
return app;
|
|
88
|
+
}
|
|
89
|
+
function apiOf(auth, type) {
|
|
90
|
+
const top = auth;
|
|
91
|
+
const holder = type === undefined || top.passkeys !== undefined ? top : auth[type];
|
|
92
|
+
if (holder?.passkeys === undefined || holder.secondFactor?.confirmPasskey === undefined) {
|
|
93
|
+
throw new TypeError(type === undefined ? "passkeyRoutes(): auth has no passkeys — give janus() `passkeys` and a `secondFactor`; with several user types, name the one with `type`" : `passkeyRoutes(): auth.${type} has no passkeys — give janus() \`passkeys\` and a \`secondFactor\`, and name a user type that exists`);
|
|
94
|
+
}
|
|
95
|
+
return holder;
|
|
96
|
+
}
|
|
97
|
+
function listHandler(wiring) {
|
|
98
|
+
return async (c) => {
|
|
99
|
+
const current = await wiring.current(c);
|
|
100
|
+
if (current === null)
|
|
101
|
+
return c.body(null, 401);
|
|
102
|
+
return c.json({ passkeys: await wiring.api.passkeys.list(current.user) });
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function optionsHandler(wiring) {
|
|
106
|
+
return async (c) => {
|
|
107
|
+
const current = await wiring.current(c);
|
|
108
|
+
if (current === null)
|
|
109
|
+
return c.body(null, 401);
|
|
110
|
+
noStore(c);
|
|
111
|
+
return c.json(await wiring.api.passkeys.registrationOptions(current.user));
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function registerHandler(wiring) {
|
|
115
|
+
return async (c) => {
|
|
116
|
+
const current = await wiring.current(c);
|
|
117
|
+
if (current === null)
|
|
118
|
+
return c.body(null, 401);
|
|
119
|
+
const body = await bodyOf(c);
|
|
120
|
+
const passkey = await wiring.api.passkeys.register(current, {
|
|
121
|
+
challenge: textOf(body, "challenge"),
|
|
122
|
+
response: body["response"],
|
|
123
|
+
name: textOf(body, "name")
|
|
124
|
+
});
|
|
125
|
+
return c.json(passkey, 201);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function renameHandler(wiring) {
|
|
129
|
+
return async (c) => {
|
|
130
|
+
const current = await wiring.current(c);
|
|
131
|
+
if (current === null)
|
|
132
|
+
return c.body(null, 401);
|
|
133
|
+
const body = await bodyOf(c);
|
|
134
|
+
const renamed = await wiring.api.passkeys.rename(current.user, c.req.param("credentialId") ?? "", textOf(body, "name"));
|
|
135
|
+
return c.body(null, renamed ? 204 : 404);
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function removeHandler(wiring) {
|
|
139
|
+
return async (c) => {
|
|
140
|
+
const current = await wiring.current(c);
|
|
141
|
+
if (current === null)
|
|
142
|
+
return c.body(null, 401);
|
|
143
|
+
const removed = await wiring.api.passkeys.remove(current, c.req.param("credentialId") ?? "");
|
|
144
|
+
return c.body(null, removed ? 204 : 404);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function signInHandler({ auth, api, options }) {
|
|
148
|
+
return async (c) => {
|
|
149
|
+
const body = await bodyOf(c);
|
|
150
|
+
const signedIn = await api.secondFactor.confirmPasskey(textOf(body, "challenge"), body["assertion"], options.device === undefined ? undefined : { device: deviceOf(c, options.device) });
|
|
151
|
+
const user = sendSession(c, auth, signedIn, options.device === undefined ? {} : { device: options.device });
|
|
152
|
+
return options.onSignedIn === undefined ? c.json({ id: user.id }) : options.onSignedIn(c, signedIn);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function stepUpHandler(wiring, stepUp) {
|
|
156
|
+
return async (c) => {
|
|
157
|
+
const current = await wiring.current(c);
|
|
158
|
+
if (current === null)
|
|
159
|
+
return c.body(null, 401);
|
|
160
|
+
const issued = await stepUp.request(current.user, { via: "passkey" });
|
|
161
|
+
noStore(c);
|
|
162
|
+
return c.json({
|
|
163
|
+
challenge: issued.challenge,
|
|
164
|
+
options: issued.options,
|
|
165
|
+
expiresAt: issued.expiresAt
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function stepUpConfirmHandler(stepUp) {
|
|
170
|
+
return async (c) => {
|
|
171
|
+
const body = await bodyOf(c);
|
|
172
|
+
await stepUp.confirmPasskey(c.req.raw, textOf(body, "challenge"), body["assertion"]);
|
|
173
|
+
return c.body(null, 204);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
// src/permission.ts
|
|
177
|
+
function permission(access, permission, type, load, options = {}) {
|
|
178
|
+
const can = access.can;
|
|
179
|
+
return async (c, next) => {
|
|
180
|
+
if (c.get("object") !== undefined) {
|
|
181
|
+
throw new TypeError("permission(): c.var.object is already set — one permission() per route; check a parent object through an arrow in the model");
|
|
182
|
+
}
|
|
183
|
+
const subject = options.subject === undefined ? userOf(c) : await options.subject(c);
|
|
184
|
+
if (subject === null || subject === undefined)
|
|
185
|
+
return c.body(null, 401);
|
|
186
|
+
const object = await load(c);
|
|
187
|
+
if (object === null)
|
|
188
|
+
return c.body(null, 404);
|
|
189
|
+
const allowed = await can(subject, permission, view(object, type), options.ctx === undefined ? undefined : { ctx: await options.ctx(c, object) });
|
|
190
|
+
if (!allowed)
|
|
191
|
+
return c.body(null, 403);
|
|
192
|
+
c.set("object", object);
|
|
193
|
+
await next();
|
|
194
|
+
return;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function byParam(name, find) {
|
|
198
|
+
return (c) => {
|
|
199
|
+
const id = c.req.param(name);
|
|
200
|
+
return id === undefined ? null : find(id);
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function view(object, type) {
|
|
204
|
+
return new Proxy(object, {
|
|
205
|
+
get: (target, key) => key === "type" ? type : Reflect.get(target, key),
|
|
206
|
+
has: (target, key) => key === "type" || Reflect.has(target, key)
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function userOf(c) {
|
|
210
|
+
const user = c.get("user");
|
|
211
|
+
if (user === undefined) {
|
|
212
|
+
throw new TypeError("permission(): c.var.user is not set — put session(auth) before it, or pass { subject }");
|
|
213
|
+
}
|
|
214
|
+
return user;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/provide.ts
|
|
218
|
+
function provide(instances) {
|
|
219
|
+
return async (c, next) => {
|
|
220
|
+
if (instances.auth !== undefined)
|
|
221
|
+
c.set("auth", instances.auth);
|
|
222
|
+
if (instances.access !== undefined)
|
|
223
|
+
c.set("access", instances.access);
|
|
224
|
+
await next();
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/social/routes.ts
|
|
229
|
+
import {
|
|
230
|
+
isReturnTo as isReturnTo2
|
|
231
|
+
} from "@softistx/janus";
|
|
232
|
+
import { Hono as Hono2 } from "hono";
|
|
233
|
+
import { getCookie as getCookie2 } from "hono/cookie";
|
|
234
|
+
|
|
235
|
+
// src/fresh.ts
|
|
236
|
+
import {
|
|
237
|
+
parseDuration,
|
|
238
|
+
StepUpRequiredError,
|
|
239
|
+
systemClock
|
|
240
|
+
} from "@softistx/janus";
|
|
241
|
+
function fresh(maxAge, options = {}) {
|
|
242
|
+
const maxAgeMs = parseDuration(maxAge, "fresh: maxAge");
|
|
243
|
+
return async (c, next) => {
|
|
244
|
+
const current = c.var.session;
|
|
245
|
+
if (current === undefined) {
|
|
246
|
+
throw new TypeError("fresh(): c.var.session is not set — put session(auth) before it");
|
|
247
|
+
}
|
|
248
|
+
const principal = c.var.principal;
|
|
249
|
+
if (principal?.via === "apiKey") {
|
|
250
|
+
throw new StepUpRequiredError("fresh(): an API key is never fresh — the caller needs a signed-in session", { userId: principal.apiKey.userId, reason: "apiKey" });
|
|
251
|
+
}
|
|
252
|
+
if (current === null)
|
|
253
|
+
return c.body(null, 401);
|
|
254
|
+
const now = (options.clock ?? systemClock).now().getTime();
|
|
255
|
+
if (now - current.authenticatedAt.getTime() >= maxAgeMs) {
|
|
256
|
+
throw new StepUpRequiredError("fresh(): the session proved who it is longer ago than maxAge — confirm with a step-up", { userId: current.userId });
|
|
257
|
+
}
|
|
258
|
+
await next();
|
|
259
|
+
return;
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/social/redirect.ts
|
|
264
|
+
import { isReturnTo } from "@softistx/janus";
|
|
265
|
+
function localPathOr(value, fallback) {
|
|
266
|
+
return isReturnTo(value) ? value : fallback;
|
|
267
|
+
}
|
|
268
|
+
function withSocialError(path, code) {
|
|
269
|
+
const hash = path.indexOf("#");
|
|
270
|
+
const head = hash === -1 ? path : path.slice(0, hash);
|
|
271
|
+
const tail = hash === -1 ? "" : path.slice(hash);
|
|
272
|
+
const joined = head.includes("?") ? "&" : "?";
|
|
273
|
+
return `${head}${joined}socialError=${encodeURIComponent(code)}${tail}`;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/social/refusals.ts
|
|
277
|
+
import { JanusError } from "@softistx/janus";
|
|
278
|
+
var REFUSALS = new Set([
|
|
279
|
+
"FLOW_EXPIRED",
|
|
280
|
+
"FLOW_MISMATCH",
|
|
281
|
+
"FLOW_PURPOSE_MISMATCH",
|
|
282
|
+
"RETURN_TO_INVALID",
|
|
283
|
+
"PROVIDER_ERROR",
|
|
284
|
+
"SOCIAL_PROVIDER_UNKNOWN",
|
|
285
|
+
"SOCIAL_STEP_UP_UNMET",
|
|
286
|
+
"SIGN_UP_DISABLED",
|
|
287
|
+
"EMAIL_NOT_VERIFIED_FOR_LINK",
|
|
288
|
+
"SOCIAL_ACCOUNT_TAKEN",
|
|
289
|
+
"LAST_SIGN_IN_METHOD",
|
|
290
|
+
"USER_INACTIVE",
|
|
291
|
+
"EMAIL_UNVERIFIED",
|
|
292
|
+
"STEP_UP_REQUIRED"
|
|
293
|
+
]);
|
|
294
|
+
function isRefusal(error) {
|
|
295
|
+
return error instanceof JanusError && REFUSALS.has(error.code);
|
|
296
|
+
}
|
|
297
|
+
function refusedResponse(c, error, flowCookieClear, options, defaultReturnTo) {
|
|
298
|
+
if (error.flowSpent !== false) {
|
|
299
|
+
c.header("Set-Cookie", flowCookieClear, { append: true });
|
|
300
|
+
}
|
|
301
|
+
const location = withSocialError(localPathOr(error.returnTo, defaultReturnTo), error.code);
|
|
302
|
+
return options.onRefusal === undefined ? c.redirect(location, 302) : options.onRefusal(c, error, location);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// src/social/routes.ts
|
|
306
|
+
var FALLBACK = "/";
|
|
307
|
+
function socialRoutes(auth, options) {
|
|
308
|
+
const defaultReturnTo = options.defaultReturnTo ?? FALLBACK;
|
|
309
|
+
if (!isReturnTo2(defaultReturnTo)) {
|
|
310
|
+
throw new TypeError('socialRoutes: defaultReturnTo must be a path of this site, starting with a single "/"');
|
|
311
|
+
}
|
|
312
|
+
const typeOption = options.type === undefined ? {} : { type: options.type };
|
|
313
|
+
const wiring = {
|
|
314
|
+
auth,
|
|
315
|
+
options,
|
|
316
|
+
defaultReturnTo,
|
|
317
|
+
typeOption,
|
|
318
|
+
authenticate: (c) => auth.authenticate(c.req.raw, options.type === undefined ? undefined : typeOption)
|
|
319
|
+
};
|
|
320
|
+
const signedIn = options.session ?? session(auth, { required: true, ...typeOption });
|
|
321
|
+
const recent = fresh(options.freshWithin ?? "10m", {
|
|
322
|
+
...options.clock === undefined ? {} : { clock: options.clock }
|
|
323
|
+
});
|
|
324
|
+
return new Hono2().get("/:provider", startHandler(wiring)).get("/:provider/callback", callbackHandler(wiring)).post("/:provider/link", signedIn, recent, linkHandler(wiring)).delete("/links/:provider/:accountId", signedIn, recent, unlinkHandler(wiring));
|
|
325
|
+
}
|
|
326
|
+
function startHandler({ auth, typeOption }) {
|
|
327
|
+
return async (c) => {
|
|
328
|
+
const returnTo = c.req.query("returnTo");
|
|
329
|
+
const started = await auth.social.start({
|
|
330
|
+
provider: c.req.param("provider") ?? "",
|
|
331
|
+
...typeOption,
|
|
332
|
+
...returnTo === undefined ? {} : { returnTo }
|
|
333
|
+
});
|
|
334
|
+
return redirectTo(c, started, 302);
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function callbackHandler(wiring) {
|
|
338
|
+
const { auth, options, defaultReturnTo } = wiring;
|
|
339
|
+
const { social } = auth;
|
|
340
|
+
return async (c) => {
|
|
341
|
+
const flow = getCookie2(c, social.cookie.name) ?? "";
|
|
342
|
+
const current = await wiring.authenticate(c);
|
|
343
|
+
let done;
|
|
344
|
+
try {
|
|
345
|
+
done = await social.complete({ callback: new URL(c.req.url).searchParams, flow, current }, options.device === undefined ? undefined : { device: deviceOf(c, options.device) });
|
|
346
|
+
} catch (error) {
|
|
347
|
+
if (!isRefusal(error))
|
|
348
|
+
throw error;
|
|
349
|
+
return refusedResponse(c, error, social.cookie.clear(), options, defaultReturnTo);
|
|
350
|
+
}
|
|
351
|
+
c.header("Set-Cookie", social.cookie.clear(), { append: true });
|
|
352
|
+
noStore(c);
|
|
353
|
+
if (done.status === "secondFactor") {
|
|
354
|
+
return options.onSecondFactor(c, done);
|
|
355
|
+
}
|
|
356
|
+
if (done.status === "signedIn") {
|
|
357
|
+
sendSession(c, auth, done, options.device === undefined ? {} : { device: options.device });
|
|
358
|
+
}
|
|
359
|
+
return c.redirect(localPathOr(done.returnTo, defaultReturnTo), 302);
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
function linkHandler(wiring) {
|
|
363
|
+
return async (c) => {
|
|
364
|
+
const current = await wiring.authenticate(c);
|
|
365
|
+
if (current === null)
|
|
366
|
+
return c.body(null, 401);
|
|
367
|
+
const returnTo = c.req.query("returnTo");
|
|
368
|
+
const started = await wiring.auth.social.link(current, {
|
|
369
|
+
provider: c.req.param("provider") ?? "",
|
|
370
|
+
...returnTo === undefined ? {} : { returnTo }
|
|
371
|
+
});
|
|
372
|
+
return redirectTo(c, started, 303);
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function unlinkHandler(wiring) {
|
|
376
|
+
return async (c) => {
|
|
377
|
+
const current = await wiring.authenticate(c);
|
|
378
|
+
if (current === null)
|
|
379
|
+
return c.body(null, 401);
|
|
380
|
+
const unlinked = await wiring.auth.social.unlink(current.user, {
|
|
381
|
+
provider: c.req.param("provider") ?? "",
|
|
382
|
+
accountId: c.req.param("accountId") ?? ""
|
|
383
|
+
});
|
|
384
|
+
return c.body(null, unlinked ? 204 : 404);
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
function redirectTo(c, started, status) {
|
|
388
|
+
noStore(c);
|
|
389
|
+
c.header("Set-Cookie", started.setCookie, { append: true });
|
|
390
|
+
return c.redirect(started.url, status);
|
|
391
|
+
}
|
|
392
|
+
// src/bind.ts
|
|
393
|
+
function bindJanus(instances) {
|
|
394
|
+
const { auth, access } = instances;
|
|
395
|
+
const bound = {
|
|
396
|
+
provide: () => provide(instances)
|
|
397
|
+
};
|
|
398
|
+
if (auth !== undefined) {
|
|
399
|
+
bound["session"] = (options = {}) => session(auth, options);
|
|
400
|
+
bound["sendSession"] = (c, signedIn, options) => sendSession(c, auth, signedIn, options);
|
|
401
|
+
bound["signOut"] = (c) => signOut(c, auth);
|
|
402
|
+
if ("social" in auth) {
|
|
403
|
+
const social = auth;
|
|
404
|
+
bound["socialRoutes"] = (options) => socialRoutes(social, options);
|
|
405
|
+
}
|
|
406
|
+
bound["passkeyRoutes"] = (options) => passkeyRoutes(auth, options);
|
|
407
|
+
}
|
|
408
|
+
if (access !== undefined) {
|
|
409
|
+
const guard = permission;
|
|
410
|
+
bound["permission"] = (...rest) => guard(access, ...rest);
|
|
411
|
+
}
|
|
412
|
+
return bound;
|
|
413
|
+
}
|
|
414
|
+
// src/errors.ts
|
|
415
|
+
import {
|
|
416
|
+
JanusError as JanusError2,
|
|
417
|
+
statusOf as janusStatusOf
|
|
418
|
+
} from "@softistx/janus";
|
|
419
|
+
function statusOf(code) {
|
|
420
|
+
return janusStatusOf(code);
|
|
421
|
+
}
|
|
422
|
+
function bodyOf2(error) {
|
|
423
|
+
switch (error.code) {
|
|
424
|
+
case "USER_INVALID":
|
|
425
|
+
return { code: error.code, issues: error.issues ?? [] };
|
|
426
|
+
case "PASSWORD_TOO_SHORT":
|
|
427
|
+
return error.minLength === undefined ? { code: error.code } : { code: error.code, minLength: error.minLength };
|
|
428
|
+
case "CODE_INVALID":
|
|
429
|
+
return {
|
|
430
|
+
code: error.code,
|
|
431
|
+
...error.attemptsLeft === undefined ? {} : { attemptsLeft: error.attemptsLeft },
|
|
432
|
+
...error.retryAfter === undefined ? {} : { retryAfter: error.retryAfter }
|
|
433
|
+
};
|
|
434
|
+
case "CREDENTIALS_INVALID":
|
|
435
|
+
case "MAIL_THROTTLED":
|
|
436
|
+
return error.retryAfter === undefined ? { code: error.code } : { code: error.code, retryAfter: error.retryAfter };
|
|
437
|
+
case "API_KEY_LIMIT":
|
|
438
|
+
case "INVITATION_LIMIT":
|
|
439
|
+
case "PASSKEY_LIMIT":
|
|
440
|
+
return error.maxActive === undefined ? { code: error.code } : { code: error.code, maxActive: error.maxActive };
|
|
441
|
+
case "API_KEY_INVALID":
|
|
442
|
+
case "INVITATION_INVALID":
|
|
443
|
+
case "PASSKEY_INVALID":
|
|
444
|
+
return error.field === undefined ? { code: error.code } : { code: error.code, field: error.field };
|
|
445
|
+
default:
|
|
446
|
+
return { code: error.code };
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function honoDefault(error, c) {
|
|
450
|
+
if ("getResponse" in error) {
|
|
451
|
+
const response = error.getResponse();
|
|
452
|
+
return c.newResponse(response.body, response);
|
|
453
|
+
}
|
|
454
|
+
console.error(error);
|
|
455
|
+
return c.text("Internal Server Error", 500);
|
|
456
|
+
}
|
|
457
|
+
function janusErrors(options = {}) {
|
|
458
|
+
const { report, fallback = honoDefault } = options;
|
|
459
|
+
return (error, c) => {
|
|
460
|
+
if (!(error instanceof JanusError2))
|
|
461
|
+
return fallback(error, c);
|
|
462
|
+
const status = statusOf(error.code);
|
|
463
|
+
if (status >= 500 && report !== undefined)
|
|
464
|
+
reportSafely(report, error, c);
|
|
465
|
+
const headers = error.retryAfter === undefined ? undefined : { "retry-after": String(error.retryAfter) };
|
|
466
|
+
return c.json(bodyOf2(error), status, headers);
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
function reportSafely(report, error, c) {
|
|
470
|
+
const warn = (failure) => process.emitWarning(`janusErrors: report failed on ${error.code}: ${failure instanceof Error ? failure.name : typeof failure}`);
|
|
471
|
+
try {
|
|
472
|
+
Promise.resolve(report(error, c)).then(undefined, warn);
|
|
473
|
+
} catch (failure) {
|
|
474
|
+
warn(failure);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
export {
|
|
478
|
+
DEVICE_COOKIE,
|
|
479
|
+
bindJanus,
|
|
480
|
+
bodyOf2 as bodyOf,
|
|
481
|
+
byParam,
|
|
482
|
+
deviceOf,
|
|
483
|
+
fresh,
|
|
484
|
+
janusErrors,
|
|
485
|
+
passkeyRoutes,
|
|
486
|
+
permission,
|
|
487
|
+
provide,
|
|
488
|
+
sendSession,
|
|
489
|
+
session,
|
|
490
|
+
signOut,
|
|
491
|
+
socialRoutes,
|
|
492
|
+
statusOf
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
//# debugId=DADF220056EDEB4964756E2164756E21
|
|
496
|
+
//# sourceMappingURL=index.js.map
|