@canopy-io/nextjs 0.0.0-stage → 0.1.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 +7 -0
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/dist/client.cjs +46 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +35 -0
- package/dist/client.d.ts +35 -0
- package/dist/client.js +26 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +730 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +143 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.js +702 -0
- package/dist/index.js.map +1 -0
- package/package.json +80 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @canopy-io/nextjs
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 558786a: Add `@canopy-io/nextjs`: sign a Next.js app in on the server through Canopy's hosted login as a confidential client, keep the session in an encrypted HttpOnly cookie, refresh it in the proxy, and check permissions in server components, route handlers and server actions with `auth()`. Checks the access token can answer cost no request; checks at a node read the identity's grants through `@canopy-io/node`'s local authorizer.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Canopy Identity Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,125 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @canopy-io/nextjs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sign a Next.js app in on the server through [Canopy](https://canopy-io.com)'s hosted login, and decide what each page shows from the person's permissions, before anything renders.
|
|
4
|
+
|
|
5
|
+
The session lives in an encrypted, HttpOnly cookie that only your server can read. A page arrives already signed in, with no loading state, and a protected route turns a signed-out visitor away before any HTML is sent. Token verification and the permission checks are [`@canopy-io/node`](https://www.npmjs.com/package/@canopy-io/node)'s; this package connects them to Next.js.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @canopy-io/nextjs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Next.js 15.5 or 16, App Router, on the Node.js runtime.
|
|
14
|
+
|
|
15
|
+
## Before the first sign-in
|
|
16
|
+
|
|
17
|
+
1. In the Developer Console (Integrations → OAuth Clients), register an OAuth client with **Client Type** set to **Confidential**. Add `https://app.example.com/auth/callback` as a redirect URI, and the page sign-out lands on (`https://app.example.com/` by default) as a post-logout redirect URL.
|
|
18
|
+
2. Set these environment variables on the server:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
CANOPY_ISSUER=https://auth.canopy-io.com/environments/<environment_id>
|
|
22
|
+
CANOPY_CLIENT_ID=<client_id>
|
|
23
|
+
CANOPY_CLIENT_SECRET=<client_secret>
|
|
24
|
+
CANOPY_APP_URL=https://app.example.com
|
|
25
|
+
# Seals the session cookie: at least 32 characters (openssl rand -base64 32).
|
|
26
|
+
CANOPY_SECRET=<random secret>
|
|
27
|
+
# Only for permission checks at a node: a secret API key from the same Environment.
|
|
28
|
+
CANOPY_API_KEY=<secret API key>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Use
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
// proxy.ts (Next.js 16)
|
|
35
|
+
import { canopyProxy } from "@canopy-io/nextjs";
|
|
36
|
+
|
|
37
|
+
export default canopyProxy({ protect: ["/reports"] });
|
|
38
|
+
|
|
39
|
+
export const config = {
|
|
40
|
+
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
41
|
+
};
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
On Next.js 15.5 the same file is `middleware.ts`, and its `config` adds `runtime: "nodejs"`.
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
// app/reports/page.tsx, a server component
|
|
48
|
+
import { auth } from "@canopy-io/nextjs";
|
|
49
|
+
|
|
50
|
+
export default async function ReportsPage({ team }: { team: { id: string } }) {
|
|
51
|
+
const { user, can } = await auth();
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<>
|
|
55
|
+
<p>Signed in as {user?.email}</p>
|
|
56
|
+
{(await can("deals.update", { nodeId: team.id })) && (
|
|
57
|
+
<button>Edit</button>
|
|
58
|
+
)}
|
|
59
|
+
</>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
// Sign in and out are links to the routes the proxy answers.
|
|
66
|
+
<a href="/auth/sign-in?returnTo=/reports">Sign in</a>
|
|
67
|
+
<a href="/auth/sign-out">Sign out</a>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
- `canopyProxy(options)` answers `/auth/sign-in`, `/auth/callback` and `/auth/sign-out` (`routes` renames them). On every other request it opens the session cookie, refreshes the access token shortly before it expires, and sends a signed-out visitor from a `protect`ed path to sign in, coming back to the page they asked for. A string in `protect` covers that path and everything under it; a function decides for itself.
|
|
71
|
+
- `auth()` in a server component, route handler or server action returns `isSignedIn`, `userId`, `user` (the ID token's claims, for display), `claims` (the verified access token's), `organization` (`{ orgId, orgRoles, orgScope }`), `accessToken` for calling your own API, and `can(permission, { nodeId })`.
|
|
72
|
+
- Sign-out revokes the refresh token, clears the cookie and ends the session at Canopy too, so the next sign-in asks for a password again.
|
|
73
|
+
|
|
74
|
+
## Client components
|
|
75
|
+
|
|
76
|
+
Client components receive the session from the server; they hold no token and send no request.
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
// app/layout.tsx
|
|
80
|
+
import { auth, clientSession } from "@canopy-io/nextjs";
|
|
81
|
+
import { CanopySessionProvider } from "@canopy-io/nextjs/client";
|
|
82
|
+
|
|
83
|
+
export default async function RootLayout({
|
|
84
|
+
children,
|
|
85
|
+
}: {
|
|
86
|
+
children: React.ReactNode;
|
|
87
|
+
}) {
|
|
88
|
+
return (
|
|
89
|
+
<html>
|
|
90
|
+
<body>
|
|
91
|
+
<CanopySessionProvider session={clientSession(await auth())}>
|
|
92
|
+
{children}
|
|
93
|
+
</CanopySessionProvider>
|
|
94
|
+
</body>
|
|
95
|
+
</html>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
"use client";
|
|
102
|
+
import { useCanopySession } from "@canopy-io/nextjs/client";
|
|
103
|
+
|
|
104
|
+
export function Avatar() {
|
|
105
|
+
const { user } = useCanopySession();
|
|
106
|
+
|
|
107
|
+
return <span>{user?.name}</span>;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Pass a permission answer to a client component as a prop: check it with `can()` in the server component that renders it.
|
|
112
|
+
|
|
113
|
+
## What a request costs
|
|
114
|
+
|
|
115
|
+
- **A page with a valid session sends nothing to Canopy.** The proxy opens the cookie and reads the expiry, and `auth()` verifies the access token with signing keys read once and cached.
|
|
116
|
+
- **A refresh happens once per session, about every 15 minutes.** Requests of one page that arrive together share it. Requests on other instances that refresh at the same moment are all answered with the same new refresh token, so none signs the person out.
|
|
117
|
+
- **The commonest checks cost nothing.** A check with no node is answered from the token's `permissions` claim, and a check at the session's organization from `org_permissions`. That needs the `permissions` scope (`scopes` option), which works only on an OAuth client registered with it: a client created with the defaults refuses it with `invalid_scope`.
|
|
118
|
+
- **A check at a node reads the identity's grants once,** with `CANOPY_API_KEY`, and holds them for 60 seconds, revalidating with an `ETag` that Canopy answers with an empty `304` when nothing changed. Repeating a check within that time asks nothing new.
|
|
119
|
+
- **On a serverless host, the cache lives as long as the instance.** A warm instance behaves like a long-running server: about one grants read a minute per active person. A cold start begins empty and reads the grants and the hierarchy in full, so a host that starts a new instance for every page reads them on every page. Keep instances warm (for example Vercel's Fluid compute, or a minimum instance count) when pages check permissions at nodes.
|
|
120
|
+
|
|
121
|
+
These checks decide what a page shows. Your API still verifies the access token (`TokenVerifier` in `@canopy-io/node`, with `audience` set to the `client_id`) and authorizes every request it receives.
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/client.tsx
|
|
22
|
+
var client_exports = {};
|
|
23
|
+
__export(client_exports, {
|
|
24
|
+
CanopySessionProvider: () => CanopySessionProvider,
|
|
25
|
+
useCanopySession: () => useCanopySession
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(client_exports);
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
30
|
+
var SIGNED_OUT = {
|
|
31
|
+
isSignedIn: false,
|
|
32
|
+
userId: null,
|
|
33
|
+
user: null,
|
|
34
|
+
organization: { orgId: null, orgRoles: [], orgScope: null }
|
|
35
|
+
};
|
|
36
|
+
var SessionContext = (0, import_react.createContext)(SIGNED_OUT);
|
|
37
|
+
function CanopySessionProvider({
|
|
38
|
+
session,
|
|
39
|
+
children
|
|
40
|
+
}) {
|
|
41
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SessionContext.Provider, { value: session, children });
|
|
42
|
+
}
|
|
43
|
+
function useCanopySession() {
|
|
44
|
+
return (0, import_react.useContext)(SessionContext);
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.tsx"],"sourcesContent":["import { createContext, useContext, type ReactNode } from \"react\";\n\n/**\n * What a client component may know about the session: who is signed in and\n * the organization, handed down from the server. It holds no token, and\n * nothing here sends a request.\n */\nexport interface CanopyClientSession {\n isSignedIn: boolean;\n userId: string | null;\n user: {\n sub: string;\n name?: string;\n email?: string;\n picture?: string;\n } | null;\n organization: {\n orgId: string | null;\n orgRoles: string[];\n orgScope: \"all\" | null;\n };\n}\n\nconst SIGNED_OUT: CanopyClientSession = {\n isSignedIn: false,\n userId: null,\n user: null,\n organization: { orgId: null, orgRoles: [], orgScope: null },\n};\n\nconst SessionContext = createContext<CanopyClientSession>(SIGNED_OUT);\n\n/**\n * Puts the session `clientSession()` read on the server where client\n * components can reach it. Render it in the root layout:\n * `<CanopySessionProvider session={clientSession(await auth())}>`.\n */\nexport function CanopySessionProvider({\n session,\n children,\n}: {\n session: CanopyClientSession;\n children?: ReactNode;\n}): ReactNode {\n return (\n <SessionContext.Provider value={session}>\n {children}\n </SessionContext.Provider>\n );\n}\n\n/** The session, in a client component. */\nexport function useCanopySession(): CanopyClientSession {\n return useContext(SessionContext);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0D;AA6CtD;AAtBJ,IAAM,aAAkC;AAAA,EACtC,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,cAAc,EAAE,OAAO,MAAM,UAAU,CAAC,GAAG,UAAU,KAAK;AAC5D;AAEA,IAAM,qBAAiB,4BAAmC,UAAU;AAO7D,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AACF,GAGc;AACZ,SACE,4CAAC,eAAe,UAAf,EAAwB,OAAO,SAC7B,UACH;AAEJ;AAGO,SAAS,mBAAwC;AACtD,aAAO,yBAAW,cAAc;AAClC;","names":[]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a client component may know about the session: who is signed in and
|
|
5
|
+
* the organization, handed down from the server. It holds no token, and
|
|
6
|
+
* nothing here sends a request.
|
|
7
|
+
*/
|
|
8
|
+
interface CanopyClientSession {
|
|
9
|
+
isSignedIn: boolean;
|
|
10
|
+
userId: string | null;
|
|
11
|
+
user: {
|
|
12
|
+
sub: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
picture?: string;
|
|
16
|
+
} | null;
|
|
17
|
+
organization: {
|
|
18
|
+
orgId: string | null;
|
|
19
|
+
orgRoles: string[];
|
|
20
|
+
orgScope: "all" | null;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Puts the session `clientSession()` read on the server where client
|
|
25
|
+
* components can reach it. Render it in the root layout:
|
|
26
|
+
* `<CanopySessionProvider session={clientSession(await auth())}>`.
|
|
27
|
+
*/
|
|
28
|
+
declare function CanopySessionProvider({ session, children, }: {
|
|
29
|
+
session: CanopyClientSession;
|
|
30
|
+
children?: ReactNode;
|
|
31
|
+
}): ReactNode;
|
|
32
|
+
/** The session, in a client component. */
|
|
33
|
+
declare function useCanopySession(): CanopyClientSession;
|
|
34
|
+
|
|
35
|
+
export { type CanopyClientSession, CanopySessionProvider, useCanopySession };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a client component may know about the session: who is signed in and
|
|
5
|
+
* the organization, handed down from the server. It holds no token, and
|
|
6
|
+
* nothing here sends a request.
|
|
7
|
+
*/
|
|
8
|
+
interface CanopyClientSession {
|
|
9
|
+
isSignedIn: boolean;
|
|
10
|
+
userId: string | null;
|
|
11
|
+
user: {
|
|
12
|
+
sub: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
picture?: string;
|
|
16
|
+
} | null;
|
|
17
|
+
organization: {
|
|
18
|
+
orgId: string | null;
|
|
19
|
+
orgRoles: string[];
|
|
20
|
+
orgScope: "all" | null;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Puts the session `clientSession()` read on the server where client
|
|
25
|
+
* components can reach it. Render it in the root layout:
|
|
26
|
+
* `<CanopySessionProvider session={clientSession(await auth())}>`.
|
|
27
|
+
*/
|
|
28
|
+
declare function CanopySessionProvider({ session, children, }: {
|
|
29
|
+
session: CanopyClientSession;
|
|
30
|
+
children?: ReactNode;
|
|
31
|
+
}): ReactNode;
|
|
32
|
+
/** The session, in a client component. */
|
|
33
|
+
declare function useCanopySession(): CanopyClientSession;
|
|
34
|
+
|
|
35
|
+
export { type CanopyClientSession, CanopySessionProvider, useCanopySession };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/client.tsx
|
|
4
|
+
import { createContext, useContext } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
var SIGNED_OUT = {
|
|
7
|
+
isSignedIn: false,
|
|
8
|
+
userId: null,
|
|
9
|
+
user: null,
|
|
10
|
+
organization: { orgId: null, orgRoles: [], orgScope: null }
|
|
11
|
+
};
|
|
12
|
+
var SessionContext = createContext(SIGNED_OUT);
|
|
13
|
+
function CanopySessionProvider({
|
|
14
|
+
session,
|
|
15
|
+
children
|
|
16
|
+
}) {
|
|
17
|
+
return /* @__PURE__ */ jsx(SessionContext.Provider, { value: session, children });
|
|
18
|
+
}
|
|
19
|
+
function useCanopySession() {
|
|
20
|
+
return useContext(SessionContext);
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
CanopySessionProvider,
|
|
24
|
+
useCanopySession
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.tsx"],"sourcesContent":["import { createContext, useContext, type ReactNode } from \"react\";\n\n/**\n * What a client component may know about the session: who is signed in and\n * the organization, handed down from the server. It holds no token, and\n * nothing here sends a request.\n */\nexport interface CanopyClientSession {\n isSignedIn: boolean;\n userId: string | null;\n user: {\n sub: string;\n name?: string;\n email?: string;\n picture?: string;\n } | null;\n organization: {\n orgId: string | null;\n orgRoles: string[];\n orgScope: \"all\" | null;\n };\n}\n\nconst SIGNED_OUT: CanopyClientSession = {\n isSignedIn: false,\n userId: null,\n user: null,\n organization: { orgId: null, orgRoles: [], orgScope: null },\n};\n\nconst SessionContext = createContext<CanopyClientSession>(SIGNED_OUT);\n\n/**\n * Puts the session `clientSession()` read on the server where client\n * components can reach it. Render it in the root layout:\n * `<CanopySessionProvider session={clientSession(await auth())}>`.\n */\nexport function CanopySessionProvider({\n session,\n children,\n}: {\n session: CanopyClientSession;\n children?: ReactNode;\n}): ReactNode {\n return (\n <SessionContext.Provider value={session}>\n {children}\n </SessionContext.Provider>\n );\n}\n\n/** The session, in a client component. */\nexport function useCanopySession(): CanopyClientSession {\n return useContext(SessionContext);\n}\n"],"mappings":";;;AAAA,SAAS,eAAe,kBAAkC;AA6CtD;AAtBJ,IAAM,aAAkC;AAAA,EACtC,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,cAAc,EAAE,OAAO,MAAM,UAAU,CAAC,GAAG,UAAU,KAAK;AAC5D;AAEA,IAAM,iBAAiB,cAAmC,UAAU;AAO7D,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AACF,GAGc;AACZ,SACE,oBAAC,eAAe,UAAf,EAAwB,OAAO,SAC7B,UACH;AAEJ;AAGO,SAAS,mBAAwC;AACtD,SAAO,WAAW,cAAc;AAClC;","names":[]}
|