nextidp 0.1.2 → 0.1.3
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 +13 -0
- package/README.md +73 -5
- package/dist/cjs/hono.d.ts +65 -0
- package/dist/cjs/hono.js +130 -0
- package/dist/esm/hono.d.ts +65 -0
- package/dist/esm/hono.js +106 -0
- package/package.json +24 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.3
|
|
4
|
+
|
|
5
|
+
### Hono, and edge runtimes
|
|
6
|
+
|
|
7
|
+
- `nextidp/hono`: `routes()`, `middleware()` (the session as `c.get("session")`, refreshed
|
|
8
|
+
before it expires), `requireSession()` and `requireToken()` (the token as
|
|
9
|
+
`c.get("token")`), with the `IdpEnv` type for `new Hono<IdpEnv>()`. A refreshed cookie
|
|
10
|
+
reaches any response the handler makes, its own `Response` included.
|
|
11
|
+
- Runs on edge runtimes (Cloudflare Workers, Vercel Edge, Deno, Bun): the tests bundle the
|
|
12
|
+
core and the Hono adapter for a platform without Node built-ins and run them in Cloudflare's
|
|
13
|
+
`workerd`, with no Node compatibility: sign-in, refresh, sign-out, back-channel logout,
|
|
14
|
+
API tokens and service tokens.
|
|
15
|
+
|
|
3
16
|
## 0.1.2
|
|
4
17
|
|
|
5
18
|
The first release with code in it: sign-in for Next.js, Express and Node, API protection
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# nextidp
|
|
2
2
|
|
|
3
|
-
Sign people in to your Next.js, Express or Node app, and protect your APIs, with
|
|
3
|
+
Sign people in to your Next.js, Express, Hono or Node app, and protect your APIs, with
|
|
4
4
|
[authserver-oidc](https://github.com/sajanv88/authserver-oidc), in one call. It does the
|
|
5
5
|
OpenID Connect work for you:
|
|
6
6
|
|
|
@@ -11,8 +11,8 @@ OpenID Connect work for you:
|
|
|
11
11
|
logout](#sessions-in-a-store-and-back-channel-logout));
|
|
12
12
|
- refreshing tokens before they expire;
|
|
13
13
|
- signing out, of your app or of the auth server too;
|
|
14
|
-
- checking the access tokens your API is called with, and their scopes, in NestJS, Express
|
|
15
|
-
or a Next.js route handler ([below](#protecting-an-api));
|
|
14
|
+
- checking the access tokens your API is called with, and their scopes, in NestJS, Express,
|
|
15
|
+
Hono or a Next.js route handler ([below](#protecting-an-api));
|
|
16
16
|
- tokens for a service to call other APIs as itself ([below](#calling-an-api-as-a-service)).
|
|
17
17
|
|
|
18
18
|
It works with any OpenID Connect provider that supports the code flow with PKCE, but it's
|
|
@@ -22,9 +22,10 @@ made for, and tested against, authserver-oidc.
|
|
|
22
22
|
npm i nextidp
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
ES module and CommonJS builds, with types. Node 24 or later
|
|
25
|
+
ES module and CommonJS builds, with types. Node 24 or later, or an edge runtime
|
|
26
|
+
(Cloudflare Workers, Vercel Edge, Deno, Bun: [below](#edge-runtimes)). Next.js 15 or 16 (App
|
|
26
27
|
Router), Express (tested with 5) or anything else that takes Node middleware, Node's own
|
|
27
|
-
HTTP server, and NestJS 11 or 12 for APIs.
|
|
28
|
+
HTTP server, Hono 4, and NestJS 11 or 12 for APIs.
|
|
28
29
|
|
|
29
30
|
## 1. Register your app on the auth server
|
|
30
31
|
|
|
@@ -154,6 +155,73 @@ createServer(async (req, res) => {
|
|
|
154
155
|
Read the session before you start the response: a refresh sets a new cookie. Read twice in
|
|
155
156
|
one request, it's refreshed once.
|
|
156
157
|
|
|
158
|
+
## Hono
|
|
159
|
+
|
|
160
|
+
On any runtime Hono runs on:
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { Hono } from "hono";
|
|
164
|
+
import { idp, type IdpEnv } from "nextidp/hono";
|
|
165
|
+
|
|
166
|
+
const auth = idp();
|
|
167
|
+
const app = new Hono<IdpEnv>();
|
|
168
|
+
|
|
169
|
+
app.all("/auth/*", auth.routes()); // nextidp's routes
|
|
170
|
+
app.use(auth.middleware()); // the session on every request, refreshed before it expires
|
|
171
|
+
|
|
172
|
+
app.get("/", (c) => {
|
|
173
|
+
const session = c.get("session"); // null if signed out
|
|
174
|
+
return c.text(session ? `Hello, ${session.user.name}` : "Signed out");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Signed out, a GET goes to sign in and comes back; anything else gets 401.
|
|
178
|
+
app.get("/dashboard", auth.requireSession(), (c) => c.text(`Hello, ${c.get("session")?.user.name}`));
|
|
179
|
+
|
|
180
|
+
// An API: 401 or 403 with the RFC 6750 challenge, 503 if the token can't be checked.
|
|
181
|
+
app.get("/api/orders", auth.requireToken({ scopes: ["orders:read"] }), (c) =>
|
|
182
|
+
c.json(ordersOf(c.get("token").subject)),
|
|
183
|
+
);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
A refreshed session's cookie is added to whatever response the handler makes, its own
|
|
187
|
+
`Response` included.
|
|
188
|
+
|
|
189
|
+
## Edge runtimes
|
|
190
|
+
|
|
191
|
+
The core uses only Web APIs (`fetch`, `Request`/`Response`, `crypto.subtle`), so it runs on
|
|
192
|
+
Cloudflare Workers, Vercel Edge, Deno and Bun as well as Node: everything but `nextidp/node`,
|
|
193
|
+
`nextidp/express` and `nextidp/nestjs`, which are for Node. Its tests run it in Cloudflare's
|
|
194
|
+
own runtime, `workerd`, with no Node compatibility turned on.
|
|
195
|
+
|
|
196
|
+
Where there's no `process.env` (Cloudflare Workers), pass the settings from the Worker's
|
|
197
|
+
bindings, read on first use:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { Hono } from "hono";
|
|
201
|
+
import { idp, type IdpEnv } from "nextidp/hono";
|
|
202
|
+
|
|
203
|
+
type Bindings = { IDP_ISSUER: string; IDP_CLIENT_ID: string; IDP_APP_URL: string; IDP_SECRET: string };
|
|
204
|
+
|
|
205
|
+
let bindings: Bindings;
|
|
206
|
+
const auth = idp(() => ({ env: bindings })); // the IDP_* names, from the bindings
|
|
207
|
+
|
|
208
|
+
const app = new Hono<IdpEnv>();
|
|
209
|
+
app.all("/auth/*", auth.routes());
|
|
210
|
+
app.use(auth.middleware());
|
|
211
|
+
|
|
212
|
+
export default {
|
|
213
|
+
fetch(request: Request, env: Bindings) {
|
|
214
|
+
bindings = env;
|
|
215
|
+
return app.fetch(request, env);
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Each isolate has its own memory, so `memoryStore()` there is per isolate. For sessions in a
|
|
221
|
+
store, use one every isolate reaches: a database, or Durable Objects. Cloudflare KV works too,
|
|
222
|
+
but it's eventually consistent: a back-channel logout can take a little while (up to about a
|
|
223
|
+
minute) to reach every location.
|
|
224
|
+
|
|
157
225
|
## The routes
|
|
158
226
|
|
|
159
227
|
All under `basePath` (default `/auth`):
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Context, MiddlewareHandler } from "hono";
|
|
2
|
+
import type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
3
|
+
import type { IdpOptions } from "./config.js";
|
|
4
|
+
import { type Idp } from "./idp.js";
|
|
5
|
+
import type { Session } from "./session.js";
|
|
6
|
+
/**
|
|
7
|
+
* The context variables nextidp sets: `session` (by `middleware()`, null when signed out) and
|
|
8
|
+
* `token` (by `requireToken()`). Give it to your app: `new Hono<IdpEnv>()`.
|
|
9
|
+
*/
|
|
10
|
+
export type IdpEnv = {
|
|
11
|
+
Variables: {
|
|
12
|
+
session: Session | null;
|
|
13
|
+
token: AccessToken;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export type HonoSessionOptions = {
|
|
17
|
+
/** Refresh the access token when it expires within a minute (default true). */
|
|
18
|
+
refresh?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type HonoIdp = Idp & {
|
|
21
|
+
/** nextidp's routes, for `app.all("/auth/*", auth.routes())` (under `basePath`). */
|
|
22
|
+
routes(): (c: Context) => Promise<Response>;
|
|
23
|
+
/**
|
|
24
|
+
* Reads the session for every request (refreshing the access token when it's about to
|
|
25
|
+
* expire, with the new cookie on the response) and puts it on the context:
|
|
26
|
+
* `c.get("session")`, null when signed out. `app.use(auth.middleware())`.
|
|
27
|
+
*/
|
|
28
|
+
middleware(options?: HonoSessionOptions): MiddlewareHandler<IdpEnv>;
|
|
29
|
+
/**
|
|
30
|
+
* Lets a request through only with a session. Signed out, a GET or HEAD goes to sign in and
|
|
31
|
+
* comes back after; anything else gets 401.
|
|
32
|
+
*/
|
|
33
|
+
requireSession(): MiddlewareHandler<IdpEnv>;
|
|
34
|
+
/**
|
|
35
|
+
* Lets a request through only with a valid access token that has every scope in
|
|
36
|
+
* `requirements`, put on the context as `c.get("token")`. A refused one gets 401 or 403 with
|
|
37
|
+
* WWW-Authenticate (RFC 6750); one that can't be checked now (the auth server is down) 503.
|
|
38
|
+
*/
|
|
39
|
+
requireToken(requirements?: TokenRequirements): MiddlewareHandler<IdpEnv>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The client for a Hono app or API:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { Hono } from "hono";
|
|
46
|
+
* import { idp, type IdpEnv } from "nextidp/hono";
|
|
47
|
+
*
|
|
48
|
+
* const auth = idp({ issuer: "https://auth.example.com", clientId: "…", appUrl: "…", secret: "…" });
|
|
49
|
+
* const app = new Hono<IdpEnv>();
|
|
50
|
+
* app.all("/auth/*", auth.routes());
|
|
51
|
+
* app.use(auth.middleware());
|
|
52
|
+
* app.get("/dashboard", auth.requireSession(), (c) => c.text(`Hello, ${c.get("session")?.user.name}`));
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* On a runtime without `process.env` (Cloudflare Workers), pass the settings, or `env`:
|
|
56
|
+
* `idp(() => ({ env: workerEnv }))`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function idp(options?: IdpOptions | (() => IdpOptions)): HonoIdp;
|
|
59
|
+
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
60
|
+
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
61
|
+
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
62
|
+
export type { Idp, Refreshed } from "./idp.js";
|
|
63
|
+
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
|
64
|
+
export type { IdpUser, Session } from "./session.js";
|
|
65
|
+
export { memoryStore, type SessionStore } from "./store.js";
|
package/dist/cjs/hono.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var hono_exports = {};
|
|
20
|
+
__export(hono_exports, {
|
|
21
|
+
IdpError: () => import_errors2.IdpError,
|
|
22
|
+
SIGN_IN_ERROR_CODES: () => import_errors2.SIGN_IN_ERROR_CODES,
|
|
23
|
+
idp: () => idp,
|
|
24
|
+
isIdpError: () => import_errors2.isIdpError,
|
|
25
|
+
isSignInErrorCode: () => import_errors2.isSignInErrorCode,
|
|
26
|
+
isTokenError: () => import_errors2.isTokenError,
|
|
27
|
+
memoryStore: () => import_store.memoryStore
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(hono_exports);
|
|
30
|
+
var import_errors = require("./errors.js");
|
|
31
|
+
var import_idp = require("./idp.js");
|
|
32
|
+
var import_errors2 = require("./errors.js");
|
|
33
|
+
var import_store = require("./store.js");
|
|
34
|
+
function putCookies(c, cookies) {
|
|
35
|
+
if (cookies.length === 0) return;
|
|
36
|
+
try {
|
|
37
|
+
for (const cookie of cookies) c.res.headers.append("set-cookie", cookie);
|
|
38
|
+
} catch {
|
|
39
|
+
const copy = new Response(c.res.body, c.res);
|
|
40
|
+
for (const cookie of cookies) copy.headers.append("set-cookie", cookie);
|
|
41
|
+
c.res = void 0;
|
|
42
|
+
c.res = copy;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function underBasePath(pathname, basePath) {
|
|
46
|
+
return pathname === basePath || pathname.startsWith(`${basePath}/`);
|
|
47
|
+
}
|
|
48
|
+
function idp(options) {
|
|
49
|
+
const core = (0, import_idp.createIdp)(options);
|
|
50
|
+
const sessionOf = async (c, refresh) => {
|
|
51
|
+
if (!refresh) return { session: await core.getSession(c.req.raw), setCookies: [] };
|
|
52
|
+
const { session, setCookies } = await core.refreshIfNeeded(c.req.raw);
|
|
53
|
+
return { session, setCookies };
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
...core,
|
|
57
|
+
routes: () => (c) => core.handler(c.req.raw),
|
|
58
|
+
middleware({ refresh = true } = {}) {
|
|
59
|
+
return async (c, next) => {
|
|
60
|
+
if (underBasePath(new URL(c.req.url).pathname, core.config().basePath)) {
|
|
61
|
+
await next();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const { session, setCookies } = await sessionOf(c, refresh);
|
|
65
|
+
c.set("session", session);
|
|
66
|
+
await next();
|
|
67
|
+
putCookies(c, setCookies);
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
requireSession() {
|
|
71
|
+
return async (c, next) => {
|
|
72
|
+
const known = c.get("session");
|
|
73
|
+
const { session, setCookies } = known === void 0 ? await sessionOf(c, true) : { session: known, setCookies: [] };
|
|
74
|
+
if (session) {
|
|
75
|
+
c.set("session", session);
|
|
76
|
+
await next();
|
|
77
|
+
putCookies(c, setCookies);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const method = c.req.method.toUpperCase();
|
|
81
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
82
|
+
c.res = c.text("Sign in first.", 401, { "cache-control": "no-store" });
|
|
83
|
+
} else {
|
|
84
|
+
const url = new URL(c.req.url);
|
|
85
|
+
const signIn = new URL(
|
|
86
|
+
core.signInPath(`${url.pathname}${url.search}`),
|
|
87
|
+
core.config().appUrl
|
|
88
|
+
);
|
|
89
|
+
c.res = c.redirect(signIn.toString(), 302);
|
|
90
|
+
c.res.headers.set("cache-control", "no-store");
|
|
91
|
+
}
|
|
92
|
+
putCookies(c, setCookies);
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
requireToken(requirements = {}) {
|
|
96
|
+
return async (c, next) => {
|
|
97
|
+
let token;
|
|
98
|
+
try {
|
|
99
|
+
token = await core.verifyBearer(c.req.header("authorization"), requirements);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if ((0, import_errors.isTokenError)(error)) return core.tokenErrorResponse(error);
|
|
102
|
+
console.error(
|
|
103
|
+
`nextidp: a token couldn't be checked: ${(0, import_errors.isIdpError)(error) ? error.code : "unexpected error"}`
|
|
104
|
+
);
|
|
105
|
+
return c.json(
|
|
106
|
+
{
|
|
107
|
+
error: "temporarily_unavailable",
|
|
108
|
+
error_description: "The token can't be checked now."
|
|
109
|
+
},
|
|
110
|
+
503,
|
|
111
|
+
{ "retry-after": "5", "cache-control": "no-store" }
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
c.set("token", token);
|
|
115
|
+
await next();
|
|
116
|
+
return void 0;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
122
|
+
0 && (module.exports = {
|
|
123
|
+
IdpError,
|
|
124
|
+
SIGN_IN_ERROR_CODES,
|
|
125
|
+
idp,
|
|
126
|
+
isIdpError,
|
|
127
|
+
isSignInErrorCode,
|
|
128
|
+
isTokenError,
|
|
129
|
+
memoryStore
|
|
130
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Context, MiddlewareHandler } from "hono";
|
|
2
|
+
import type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
3
|
+
import type { IdpOptions } from "./config.js";
|
|
4
|
+
import { type Idp } from "./idp.js";
|
|
5
|
+
import type { Session } from "./session.js";
|
|
6
|
+
/**
|
|
7
|
+
* The context variables nextidp sets: `session` (by `middleware()`, null when signed out) and
|
|
8
|
+
* `token` (by `requireToken()`). Give it to your app: `new Hono<IdpEnv>()`.
|
|
9
|
+
*/
|
|
10
|
+
export type IdpEnv = {
|
|
11
|
+
Variables: {
|
|
12
|
+
session: Session | null;
|
|
13
|
+
token: AccessToken;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export type HonoSessionOptions = {
|
|
17
|
+
/** Refresh the access token when it expires within a minute (default true). */
|
|
18
|
+
refresh?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type HonoIdp = Idp & {
|
|
21
|
+
/** nextidp's routes, for `app.all("/auth/*", auth.routes())` (under `basePath`). */
|
|
22
|
+
routes(): (c: Context) => Promise<Response>;
|
|
23
|
+
/**
|
|
24
|
+
* Reads the session for every request (refreshing the access token when it's about to
|
|
25
|
+
* expire, with the new cookie on the response) and puts it on the context:
|
|
26
|
+
* `c.get("session")`, null when signed out. `app.use(auth.middleware())`.
|
|
27
|
+
*/
|
|
28
|
+
middleware(options?: HonoSessionOptions): MiddlewareHandler<IdpEnv>;
|
|
29
|
+
/**
|
|
30
|
+
* Lets a request through only with a session. Signed out, a GET or HEAD goes to sign in and
|
|
31
|
+
* comes back after; anything else gets 401.
|
|
32
|
+
*/
|
|
33
|
+
requireSession(): MiddlewareHandler<IdpEnv>;
|
|
34
|
+
/**
|
|
35
|
+
* Lets a request through only with a valid access token that has every scope in
|
|
36
|
+
* `requirements`, put on the context as `c.get("token")`. A refused one gets 401 or 403 with
|
|
37
|
+
* WWW-Authenticate (RFC 6750); one that can't be checked now (the auth server is down) 503.
|
|
38
|
+
*/
|
|
39
|
+
requireToken(requirements?: TokenRequirements): MiddlewareHandler<IdpEnv>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The client for a Hono app or API:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { Hono } from "hono";
|
|
46
|
+
* import { idp, type IdpEnv } from "nextidp/hono";
|
|
47
|
+
*
|
|
48
|
+
* const auth = idp({ issuer: "https://auth.example.com", clientId: "…", appUrl: "…", secret: "…" });
|
|
49
|
+
* const app = new Hono<IdpEnv>();
|
|
50
|
+
* app.all("/auth/*", auth.routes());
|
|
51
|
+
* app.use(auth.middleware());
|
|
52
|
+
* app.get("/dashboard", auth.requireSession(), (c) => c.text(`Hello, ${c.get("session")?.user.name}`));
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* On a runtime without `process.env` (Cloudflare Workers), pass the settings, or `env`:
|
|
56
|
+
* `idp(() => ({ env: workerEnv }))`.
|
|
57
|
+
*/
|
|
58
|
+
export declare function idp(options?: IdpOptions | (() => IdpOptions)): HonoIdp;
|
|
59
|
+
export type { AccessToken, TokenRequirements } from "./access-token.js";
|
|
60
|
+
export type { ApiConfig, IdpConfig, IdpOptions } from "./config.js";
|
|
61
|
+
export { IdpError, type IdpErrorCode, isIdpError, isSignInErrorCode, isTokenError, SIGN_IN_ERROR_CODES, type SignInErrorCode, type TokenErrorCode, } from "./errors.js";
|
|
62
|
+
export type { Idp, Refreshed } from "./idp.js";
|
|
63
|
+
export type { InterceptableClient, ServiceTokenOptions, UseServiceTokenOptions, } from "./service.js";
|
|
64
|
+
export type { IdpUser, Session } from "./session.js";
|
|
65
|
+
export { memoryStore, type SessionStore } from "./store.js";
|
package/dist/esm/hono.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { isIdpError, isTokenError } from "./errors.js";
|
|
2
|
+
import { createIdp } from "./idp.js";
|
|
3
|
+
function putCookies(c, cookies) {
|
|
4
|
+
if (cookies.length === 0) return;
|
|
5
|
+
try {
|
|
6
|
+
for (const cookie of cookies) c.res.headers.append("set-cookie", cookie);
|
|
7
|
+
} catch {
|
|
8
|
+
const copy = new Response(c.res.body, c.res);
|
|
9
|
+
for (const cookie of cookies) copy.headers.append("set-cookie", cookie);
|
|
10
|
+
c.res = void 0;
|
|
11
|
+
c.res = copy;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function underBasePath(pathname, basePath) {
|
|
15
|
+
return pathname === basePath || pathname.startsWith(`${basePath}/`);
|
|
16
|
+
}
|
|
17
|
+
function idp(options) {
|
|
18
|
+
const core = createIdp(options);
|
|
19
|
+
const sessionOf = async (c, refresh) => {
|
|
20
|
+
if (!refresh) return { session: await core.getSession(c.req.raw), setCookies: [] };
|
|
21
|
+
const { session, setCookies } = await core.refreshIfNeeded(c.req.raw);
|
|
22
|
+
return { session, setCookies };
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
...core,
|
|
26
|
+
routes: () => (c) => core.handler(c.req.raw),
|
|
27
|
+
middleware({ refresh = true } = {}) {
|
|
28
|
+
return async (c, next) => {
|
|
29
|
+
if (underBasePath(new URL(c.req.url).pathname, core.config().basePath)) {
|
|
30
|
+
await next();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const { session, setCookies } = await sessionOf(c, refresh);
|
|
34
|
+
c.set("session", session);
|
|
35
|
+
await next();
|
|
36
|
+
putCookies(c, setCookies);
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
requireSession() {
|
|
40
|
+
return async (c, next) => {
|
|
41
|
+
const known = c.get("session");
|
|
42
|
+
const { session, setCookies } = known === void 0 ? await sessionOf(c, true) : { session: known, setCookies: [] };
|
|
43
|
+
if (session) {
|
|
44
|
+
c.set("session", session);
|
|
45
|
+
await next();
|
|
46
|
+
putCookies(c, setCookies);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const method = c.req.method.toUpperCase();
|
|
50
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
51
|
+
c.res = c.text("Sign in first.", 401, { "cache-control": "no-store" });
|
|
52
|
+
} else {
|
|
53
|
+
const url = new URL(c.req.url);
|
|
54
|
+
const signIn = new URL(
|
|
55
|
+
core.signInPath(`${url.pathname}${url.search}`),
|
|
56
|
+
core.config().appUrl
|
|
57
|
+
);
|
|
58
|
+
c.res = c.redirect(signIn.toString(), 302);
|
|
59
|
+
c.res.headers.set("cache-control", "no-store");
|
|
60
|
+
}
|
|
61
|
+
putCookies(c, setCookies);
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
requireToken(requirements = {}) {
|
|
65
|
+
return async (c, next) => {
|
|
66
|
+
let token;
|
|
67
|
+
try {
|
|
68
|
+
token = await core.verifyBearer(c.req.header("authorization"), requirements);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (isTokenError(error)) return core.tokenErrorResponse(error);
|
|
71
|
+
console.error(
|
|
72
|
+
`nextidp: a token couldn't be checked: ${isIdpError(error) ? error.code : "unexpected error"}`
|
|
73
|
+
);
|
|
74
|
+
return c.json(
|
|
75
|
+
{
|
|
76
|
+
error: "temporarily_unavailable",
|
|
77
|
+
error_description: "The token can't be checked now."
|
|
78
|
+
},
|
|
79
|
+
503,
|
|
80
|
+
{ "retry-after": "5", "cache-control": "no-store" }
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
c.set("token", token);
|
|
84
|
+
await next();
|
|
85
|
+
return void 0;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
import {
|
|
91
|
+
IdpError,
|
|
92
|
+
isIdpError as isIdpError2,
|
|
93
|
+
isSignInErrorCode,
|
|
94
|
+
isTokenError as isTokenError2,
|
|
95
|
+
SIGN_IN_ERROR_CODES
|
|
96
|
+
} from "./errors.js";
|
|
97
|
+
import { memoryStore } from "./store.js";
|
|
98
|
+
export {
|
|
99
|
+
IdpError,
|
|
100
|
+
SIGN_IN_ERROR_CODES,
|
|
101
|
+
idp,
|
|
102
|
+
isIdpError2 as isIdpError,
|
|
103
|
+
isSignInErrorCode,
|
|
104
|
+
isTokenError2 as isTokenError,
|
|
105
|
+
memoryStore
|
|
106
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nextidp",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Sign people in to Next.js, Express and Node apps, and protect NestJS, Express and Next.js APIs, with authserver-oidc, in one call.",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Sign people in to Next.js, Express, Hono and Node apps, and protect NestJS, Express, Hono and Next.js APIs, with authserver-oidc, in one call. Runs on Node and edge runtimes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"oidc",
|
|
7
7
|
"openid-connect",
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
"nextjs",
|
|
11
11
|
"nestjs",
|
|
12
12
|
"express",
|
|
13
|
+
"hono",
|
|
14
|
+
"cloudflare-workers",
|
|
15
|
+
"edge",
|
|
13
16
|
"jwt",
|
|
14
17
|
"idp"
|
|
15
18
|
],
|
|
@@ -34,6 +37,9 @@
|
|
|
34
37
|
"node": [
|
|
35
38
|
"./dist/cjs/node.d.ts"
|
|
36
39
|
],
|
|
40
|
+
"hono": [
|
|
41
|
+
"./dist/cjs/hono.d.ts"
|
|
42
|
+
],
|
|
37
43
|
"nestjs": [
|
|
38
44
|
"./dist/cjs/nestjs.d.ts"
|
|
39
45
|
]
|
|
@@ -85,6 +91,16 @@
|
|
|
85
91
|
"default": "./dist/cjs/node.js"
|
|
86
92
|
}
|
|
87
93
|
},
|
|
94
|
+
"./hono": {
|
|
95
|
+
"import": {
|
|
96
|
+
"types": "./dist/esm/hono.d.ts",
|
|
97
|
+
"default": "./dist/esm/hono.js"
|
|
98
|
+
},
|
|
99
|
+
"require": {
|
|
100
|
+
"types": "./dist/cjs/hono.d.ts",
|
|
101
|
+
"default": "./dist/cjs/hono.js"
|
|
102
|
+
}
|
|
103
|
+
},
|
|
88
104
|
"./nestjs": {
|
|
89
105
|
"import": {
|
|
90
106
|
"types": "./dist/esm/nestjs.d.ts",
|
|
@@ -109,6 +125,7 @@
|
|
|
109
125
|
"peerDependencies": {
|
|
110
126
|
"@nestjs/common": "^11.0.0 || ^12.0.0",
|
|
111
127
|
"@nestjs/core": "^11.0.0 || ^12.0.0",
|
|
128
|
+
"hono": "^4.0.0",
|
|
112
129
|
"next": "^15.0.0 || ^16.0.0"
|
|
113
130
|
},
|
|
114
131
|
"peerDependenciesMeta": {
|
|
@@ -118,6 +135,9 @@
|
|
|
118
135
|
"@nestjs/core": {
|
|
119
136
|
"optional": true
|
|
120
137
|
},
|
|
138
|
+
"hono": {
|
|
139
|
+
"optional": true
|
|
140
|
+
},
|
|
121
141
|
"next": {
|
|
122
142
|
"optional": true
|
|
123
143
|
}
|
|
@@ -132,6 +152,8 @@
|
|
|
132
152
|
"@types/node": "26.6.5",
|
|
133
153
|
"esbuild": "0.28.2",
|
|
134
154
|
"express": "5.3.0",
|
|
155
|
+
"hono": "4.13.22",
|
|
156
|
+
"miniflare": "4.20260730.0",
|
|
135
157
|
"next": "16.4.6",
|
|
136
158
|
"publint": "0.3.25",
|
|
137
159
|
"react": "19.3.1",
|