@uln/auth 0.2.0 → 0.3.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/dist/client.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export type { TokenPayload, VerifyConfig, PublicKeyEntry, CookieConfig } from "./types.ts";
2
2
  export { validateToken, importPublicKey } from "./token.ts";
3
3
  export { makeSessionCookie, makeClearCookie, extractCookie } from "./cookie.ts";
4
- export { getAuthUser, redirectToLogin, getClientIP, checkBearerToken } from "./middleware.ts";
4
+ export { getAuthUser, redirectToLogin, handleCallback, getClientIP, checkBearerToken } from "./middleware.ts";
5
5
  import type { PublicKeyEntry } from "./types.ts";
6
6
  export declare function fetchPublicKeys(baseUrl: string): Promise<PublicKeyEntry[]>;
package/dist/client.js CHANGED
@@ -134,6 +134,24 @@ function redirectToLogin(currentUrl) {
134
134
  const loginUrl = `https://auth.uln.industries/login?redirect=${encodeURIComponent(currentUrl)}`;
135
135
  return Response.redirect(loginUrl, 302);
136
136
  }
137
+ async function handleCallback(request, verifyConfig, cookieConfig) {
138
+ const url = new URL(request.url);
139
+ const token = url.searchParams.get("token");
140
+ if (!token)
141
+ return null;
142
+ const payload = await validateToken(verifyConfig, token);
143
+ if (!payload) {
144
+ return redirectToLogin(url.origin);
145
+ }
146
+ url.searchParams.delete("token");
147
+ return new Response(null, {
148
+ status: 302,
149
+ headers: {
150
+ Location: url.toString(),
151
+ "Set-Cookie": makeSessionCookie(cookieConfig, token)
152
+ }
153
+ });
154
+ }
137
155
  // src/client.ts
138
156
  async function fetchPublicKeys(baseUrl) {
139
157
  const res = await fetch(`${baseUrl}/.well-known/jwk.json`);
@@ -151,6 +169,7 @@ export {
151
169
  makeSessionCookie,
152
170
  makeClearCookie,
153
171
  importPublicKey,
172
+ handleCallback,
154
173
  getClientIP,
155
174
  getAuthUser,
156
175
  fetchPublicKeys,
package/dist/index.js CHANGED
@@ -134,6 +134,24 @@ function redirectToLogin(currentUrl) {
134
134
  const loginUrl = `https://auth.uln.industries/login?redirect=${encodeURIComponent(currentUrl)}`;
135
135
  return Response.redirect(loginUrl, 302);
136
136
  }
137
+ async function handleCallback(request, verifyConfig, cookieConfig) {
138
+ const url = new URL(request.url);
139
+ const token = url.searchParams.get("token");
140
+ if (!token)
141
+ return null;
142
+ const payload = await validateToken(verifyConfig, token);
143
+ if (!payload) {
144
+ return redirectToLogin(url.origin);
145
+ }
146
+ url.searchParams.delete("token");
147
+ return new Response(null, {
148
+ status: 302,
149
+ headers: {
150
+ Location: url.toString(),
151
+ "Set-Cookie": makeSessionCookie(cookieConfig, token)
152
+ }
153
+ });
154
+ }
137
155
 
138
156
  // src/password.ts
139
157
  var SALT_LENGTH = 32;
@@ -3,3 +3,10 @@ export declare function getClientIP(request: Request): string;
3
3
  export declare function checkBearerToken(request: Request, expectedToken: string): boolean;
4
4
  export declare function getAuthUser(request: Request, verifyConfig: VerifyConfig, cookieConfig: CookieConfig): Promise<TokenPayload | null>;
5
5
  export declare function redirectToLogin(currentUrl: string): Response;
6
+ /**
7
+ * Handle the auth callback on consuming sites.
8
+ * If the request URL has a `?token=` param, validates it, sets a session cookie,
9
+ * and redirects to the same URL without the token param.
10
+ * Returns null if there's no token param (not a callback request).
11
+ */
12
+ export declare function handleCallback(request: Request, verifyConfig: VerifyConfig, cookieConfig: CookieConfig): Promise<Response | null>;
@@ -134,8 +134,27 @@ function redirectToLogin(currentUrl) {
134
134
  const loginUrl = `https://auth.uln.industries/login?redirect=${encodeURIComponent(currentUrl)}`;
135
135
  return Response.redirect(loginUrl, 302);
136
136
  }
137
+ async function handleCallback(request, verifyConfig, cookieConfig) {
138
+ const url = new URL(request.url);
139
+ const token = url.searchParams.get("token");
140
+ if (!token)
141
+ return null;
142
+ const payload = await validateToken(verifyConfig, token);
143
+ if (!payload) {
144
+ return redirectToLogin(url.origin);
145
+ }
146
+ url.searchParams.delete("token");
147
+ return new Response(null, {
148
+ status: 302,
149
+ headers: {
150
+ Location: url.toString(),
151
+ "Set-Cookie": makeSessionCookie(cookieConfig, token)
152
+ }
153
+ });
154
+ }
137
155
  export {
138
156
  redirectToLogin,
157
+ handleCallback,
139
158
  getClientIP,
140
159
  getAuthUser,
141
160
  checkBearerToken
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uln/auth",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -35,7 +35,7 @@
35
35
  "build": "bun run scripts/build.ts",
36
36
  "check": "biome check --write .",
37
37
  "test": "bun test",
38
- "deploy": "bunx wrangler deploy",
38
+ "deploy": "bunx wrangler d1 migrations apply uln-auth --remote && bunx wrangler deploy",
39
39
  "publish:check": "bun run scripts/publish.ts check",
40
40
  "publish:release": "bun run scripts/publish.ts release"
41
41
  },
package/src/client.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export type { TokenPayload, VerifyConfig, PublicKeyEntry, CookieConfig } from "./types.ts";
2
2
  export { validateToken, importPublicKey } from "./token.ts";
3
3
  export { makeSessionCookie, makeClearCookie, extractCookie } from "./cookie.ts";
4
- export { getAuthUser, redirectToLogin, getClientIP, checkBearerToken } from "./middleware.ts";
4
+ export { getAuthUser, redirectToLogin, handleCallback, getClientIP, checkBearerToken } from "./middleware.ts";
5
5
 
6
6
  import { importPublicKey } from "./token.ts";
7
7
  import type { PublicKeyEntry } from "./types.ts";
package/src/middleware.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { extractCookie } from "./cookie.ts";
1
+ import { extractCookie, makeSessionCookie } from "./cookie.ts";
2
2
  import { validateToken } from "./token.ts";
3
3
  import type { CookieConfig, TokenPayload, VerifyConfig } from "./types.ts";
4
4
 
@@ -33,3 +33,33 @@ export function redirectToLogin(currentUrl: string): Response {
33
33
  const loginUrl = `https://auth.uln.industries/login?redirect=${encodeURIComponent(currentUrl)}`;
34
34
  return Response.redirect(loginUrl, 302);
35
35
  }
36
+
37
+ /**
38
+ * Handle the auth callback on consuming sites.
39
+ * If the request URL has a `?token=` param, validates it, sets a session cookie,
40
+ * and redirects to the same URL without the token param.
41
+ * Returns null if there's no token param (not a callback request).
42
+ */
43
+ export async function handleCallback(
44
+ request: Request,
45
+ verifyConfig: VerifyConfig,
46
+ cookieConfig: CookieConfig,
47
+ ): Promise<Response | null> {
48
+ const url = new URL(request.url);
49
+ const token = url.searchParams.get("token");
50
+ if (!token) return null;
51
+
52
+ const payload = await validateToken(verifyConfig, token);
53
+ if (!payload) {
54
+ return redirectToLogin(url.origin);
55
+ }
56
+
57
+ url.searchParams.delete("token");
58
+ return new Response(null, {
59
+ status: 302,
60
+ headers: {
61
+ Location: url.toString(),
62
+ "Set-Cookie": makeSessionCookie(cookieConfig, token),
63
+ },
64
+ });
65
+ }