@snokam/auth 3.1.0 → 3.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 CHANGED
@@ -3,6 +3,16 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.2.0](https://github.com/snokam/components/compare/@snokam/auth@3.1.1...@snokam/auth@3.2.0) (2026-10-02)
7
+
8
+ ### Features
9
+
10
+ - **auth:** let MCP clients sign in through the Snøsky login ([#2542](https://github.com/snokam/components/issues/2542)) ([5ea6750](https://github.com/snokam/components/commit/5ea67501bde43d999d518e41f98611af2536ff23))
11
+
12
+ ## [3.1.1](https://github.com/snokam/components/compare/@snokam/auth@3.1.0...@snokam/auth@3.1.1) (2026-10-01)
13
+
14
+ **Note:** Version bump only for package @snokam/auth
15
+
6
16
  # [3.1.0](https://github.com/snokam/components/compare/@snokam/auth@3.0.2...@snokam/auth@3.1.0) (2026-09-30)
7
17
 
8
18
  ### Features
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@snokam/auth",
3
3
  "description": "Snøkam auth components",
4
- "version": "3.1.0",
4
+ "version": "3.2.0",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
7
7
  "files": [
@@ -25,11 +25,11 @@
25
25
  "@snokam/employees-client": "^1.3.0",
26
26
  "next-auth": "4.24.15",
27
27
  "path-to-regexp": "^8.4.2",
28
+ "@snokam/platform-client": "3.1.0",
29
+ "@snokam/tenant": "2.1.2",
30
+ "@snokam/react-query": "1.1.0",
28
31
  "@snokam/identity": "1.1.0",
29
- "@snokam/platform-client": "2.0.0",
30
- "@snokam/tenant": "2.1.0",
31
- "@snokam/ui": "2.0.3",
32
- "@snokam/react-query": "1.1.0"
32
+ "@snokam/ui": "2.0.3"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "next": "16.3.3",
@@ -0,0 +1,33 @@
1
+ import { open, seal } from "../ticket/seal";
2
+
3
+ export interface AuthorizationParams {
4
+ clientId: string;
5
+ redirectUri: string;
6
+ codeChallenge: string;
7
+ codeChallengeMethod: string;
8
+ resource: string;
9
+ }
10
+
11
+ export interface ConsentClaims extends AuthorizationParams {
12
+ business: string;
13
+ state: string;
14
+ clientName: string;
15
+ businessName: string;
16
+ email: string;
17
+ }
18
+
19
+ const PURPOSE = "oauth-consent";
20
+ const TTL_SECONDS = 600;
21
+
22
+ export function sealConsent(claims: ConsentClaims, secret: string): string {
23
+ return seal(claims, secret, TTL_SECONDS, PURPOSE);
24
+ }
25
+
26
+ export function readOAuthConsent(
27
+ ticket: string,
28
+ secret: string
29
+ ): ConsentClaims | null {
30
+ return open<ConsentClaims>(ticket, secret, PURPOSE, {
31
+ onReject: (reason) => console.warn(`[oauth] consent rejected: ${reason}`),
32
+ });
33
+ }
@@ -0,0 +1,136 @@
1
+ import { getToken } from "next-auth/jwt";
2
+ import {
3
+ getAuthUrl,
4
+ getSessionCookieSettings,
5
+ requestOrigin,
6
+ } from "../session";
7
+ import { resolveAccessToken } from "../route-handler/token";
8
+ import type { Auth } from "../types";
9
+ import {
10
+ type AuthorizationParams,
11
+ readOAuthConsent,
12
+ sealConsent,
13
+ } from "./consent";
14
+ import { approveAuthorization, previewAuthorization } from "./platform";
15
+
16
+ const ACCESS_DENIED = "access_denied";
17
+
18
+ function invalidRequest(): Response {
19
+ return new Response("Invalid authorization request", { status: 400 });
20
+ }
21
+
22
+ function readParams(query: URLSearchParams): AuthorizationParams | null {
23
+ const params = {
24
+ clientId: query.get("client_id") ?? "",
25
+ redirectUri: query.get("redirect_uri") ?? "",
26
+ codeChallenge: query.get("code_challenge") ?? "",
27
+ codeChallengeMethod: query.get("code_challenge_method") ?? "",
28
+ resource: query.get("resource") ?? "",
29
+ };
30
+ const complete =
31
+ query.get("response_type") === "code" &&
32
+ Object.values(params).every(Boolean);
33
+ return complete && URL.canParse(params.resource) ? params : null;
34
+ }
35
+
36
+ async function signedInEmail(
37
+ request: Request,
38
+ secret: string
39
+ ): Promise<string | null> {
40
+ const { name: cookieName } = getSessionCookieSettings(getAuthUrl());
41
+ const token = await getToken({ req: request as never, secret, cookieName });
42
+ return token?.refreshToken && token.email ? String(token.email) : null;
43
+ }
44
+
45
+ function loginRedirect(request: Request, resource: string, prompt?: string) {
46
+ const url = new URL(request.url);
47
+ const resume = new URLSearchParams(url.searchParams);
48
+ resume.set("returnTo", new URL(resource).origin);
49
+ const login = new URL("/auth/login", requestOrigin(request));
50
+ login.searchParams.set("callbackUrl", `${url.pathname}?${resume}`);
51
+ if (prompt) login.searchParams.set("prompt", prompt);
52
+ return Response.redirect(login.toString(), 302);
53
+ }
54
+
55
+ function clientRedirect(
56
+ redirectUri: string,
57
+ state: string,
58
+ result: Record<string, string>
59
+ ): Response {
60
+ const target = new URL(redirectUri);
61
+ for (const [key, value] of Object.entries(result)) {
62
+ target.searchParams.set(key, value);
63
+ }
64
+ if (state) target.searchParams.set("state", state);
65
+ return new Response(null, {
66
+ status: 303,
67
+ headers: { Location: target.toString() },
68
+ });
69
+ }
70
+
71
+ export function createOAuthAuthorizeHandler(authConfig: Auth.Config) {
72
+ return async function GET(request: Request) {
73
+ const query = new URL(request.url).searchParams;
74
+ const params = readParams(query);
75
+ if (!params) return invalidRequest();
76
+
77
+ const email = await signedInEmail(request, authConfig.nextAuthSecret);
78
+ if (!email) return loginRedirect(request, params.resource);
79
+
80
+ const access = await resolveAccessToken(authConfig, request);
81
+ if ("error" in access) return loginRedirect(request, params.resource);
82
+
83
+ const preview = await previewAuthorization(params, access.accessToken);
84
+ if ("error" in preview) {
85
+ return preview.error === ACCESS_DENIED
86
+ ? loginRedirect(request, params.resource, "login")
87
+ : invalidRequest();
88
+ }
89
+
90
+ const ticket = sealConsent(
91
+ {
92
+ ...params,
93
+ ...preview.value,
94
+ business: new URL(params.resource).hostname,
95
+ state: query.get("state") ?? "",
96
+ email,
97
+ },
98
+ authConfig.nextAuthSecret
99
+ );
100
+ const consent = new URL("/oauth/consent", requestOrigin(request));
101
+ consent.searchParams.set("ticket", ticket);
102
+ return Response.redirect(consent.toString(), 302);
103
+ };
104
+ }
105
+
106
+ export function createOAuthApproveHandler(authConfig: Auth.Config) {
107
+ return async function POST(request: Request) {
108
+ const form = await request.formData();
109
+ const consent = readOAuthConsent(
110
+ String(form.get("ticket") ?? ""),
111
+ authConfig.nextAuthSecret
112
+ );
113
+ if (!consent) return invalidRequest();
114
+
115
+ const email = await signedInEmail(request, authConfig.nextAuthSecret);
116
+ if (email !== consent.email) return invalidRequest();
117
+
118
+ if (form.get("decision") !== "approve") {
119
+ return clientRedirect(consent.redirectUri, consent.state, {
120
+ error: ACCESS_DENIED,
121
+ });
122
+ }
123
+
124
+ const access = await resolveAccessToken(authConfig, request);
125
+ if ("error" in access) return invalidRequest();
126
+
127
+ const approved = await approveAuthorization(consent, access.accessToken);
128
+ return "error" in approved
129
+ ? clientRedirect(consent.redirectUri, consent.state, {
130
+ error: approved.error,
131
+ })
132
+ : clientRedirect(consent.redirectUri, consent.state, {
133
+ code: approved.value.code,
134
+ });
135
+ };
136
+ }
@@ -0,0 +1,53 @@
1
+ import type { AuthorizationParams } from "./consent";
2
+
3
+ export interface AuthorizationPreview {
4
+ clientName: string;
5
+ businessName: string;
6
+ }
7
+
8
+ type Outcome<T> = { value: T } | { error: string };
9
+
10
+ function apiBase(): string {
11
+ return (process.env.NEXT_PUBLIC_PLATFORM_API_BASE_URL ?? "").replace(
12
+ /\/+$/,
13
+ ""
14
+ );
15
+ }
16
+
17
+ async function post<T>(
18
+ path: string,
19
+ params: AuthorizationParams,
20
+ accessToken: string
21
+ ): Promise<Outcome<T>> {
22
+ const response = await fetch(`${apiBase()}${path}`, {
23
+ method: "POST",
24
+ headers: {
25
+ "Content-Type": "application/json",
26
+ Authorization: `Bearer ${accessToken}`,
27
+ },
28
+ body: JSON.stringify(params),
29
+ cache: "no-store",
30
+ });
31
+ const body = await response.json().catch(() => ({}));
32
+ return response.ok
33
+ ? { value: body as T }
34
+ : { error: String(body.error ?? "invalid_request") };
35
+ }
36
+
37
+ export function previewAuthorization(
38
+ params: AuthorizationParams,
39
+ accessToken: string
40
+ ): Promise<Outcome<AuthorizationPreview>> {
41
+ return post(
42
+ "/v1.0/protected/oauth/authorization/preview",
43
+ params,
44
+ accessToken
45
+ );
46
+ }
47
+
48
+ export function approveAuthorization(
49
+ params: AuthorizationParams,
50
+ accessToken: string
51
+ ): Promise<Outcome<{ code: string }>> {
52
+ return post("/v1.0/protected/oauth/authorization", params, accessToken);
53
+ }
@@ -49,4 +49,9 @@ export {
49
49
  createHandoffFinishHandler,
50
50
  createHandoffLogoutHandler,
51
51
  } from "../handoff/handlers";
52
+ export {
53
+ createOAuthAuthorizeHandler,
54
+ createOAuthApproveHandler,
55
+ } from "../oauth/handlers";
56
+ export { readOAuthConsent } from "../oauth/consent";
52
57
  export type { Auth } from "../types";