-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05196cb
commit dc0e99b
Showing
16 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# OpenAuth Server | ||
|
||
Deploy an [OpenAuth](https://openauth.js.org/) server on Cloudflare Workers. | ||
|
||
## Develop Locally | ||
|
||
Use this template with [C3](https://developers.cloudflare.com/pages/get-started/c3/) (the `create-cloudflare` CLI): | ||
|
||
``` | ||
npm create cloudflare@latest -- --template=cloudflare/templates/openauth-template | ||
``` | ||
|
||
## Preview Deployment | ||
|
||
A live public deployment of this template is available at [https://openauth-template.templates.workers.dev](https://openauth-template.templates.workers.dev) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- Migration number: 0001 2024-12-27T22:04:18.794Z | ||
CREATE TABLE IF NOT EXISTS user ( | ||
id TEXT PRIMARY KEY NOT NULL DEFAULT (lower(hex(randomblob(16)))), | ||
email TEXT UNIQUE NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "openauth-template", | ||
"description": "Deploy an OpenAuth server on Cloudflare Workers.", | ||
"cloudflare": { | ||
"label": "OpenAuth Server", | ||
"products": [ | ||
"Workers" | ||
], | ||
"icon_urls": [ | ||
"https://imagedelivery.net/wSMYJvS3Xw-n339CbDyDIA/5ca0ca32-e897-4699-d4c1-6b680512f000/public" | ||
], | ||
"preview_image_url": "https://imagedelivery.net/wSMYJvS3Xw-n339CbDyDIA/b2ff10c6-8f7c-419f-8757-e2ccf1c84500/public" | ||
}, | ||
"dependencies": { | ||
"@openauthjs/openauth": "0.2.5", | ||
"valibot": "^1.0.0-beta.9" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "4.20241216.0", | ||
"typescript": "5.6.3", | ||
"wrangler": "3.97.0" | ||
}, | ||
"scripts": { | ||
"check": "tsc && wrangler --experimental-json-config deploy --dry-run", | ||
"deploy": "wrangler --experimental-json-config deploy", | ||
"dev": "wrangler --experimental-json-config dev", | ||
"predeploy": "wrangler --experimental-json-config d1 migrations apply openauth-template-auth-db --remote", | ||
"types": "wrangler --experimental-json-config types" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { authorizer, createSubjects } from "@openauthjs/openauth"; | ||
import { CloudflareStorage } from "@openauthjs/openauth/storage/cloudflare"; | ||
import { PasswordAdapter } from "@openauthjs/openauth/adapter/password"; | ||
import { PasswordUI } from "@openauthjs/openauth/ui/password"; | ||
import { object, string } from "valibot"; | ||
|
||
// TODO: Explain that this should be a shared type | ||
const subjects = createSubjects({ | ||
user: object({ | ||
id: string(), | ||
}), | ||
}); | ||
|
||
export default { | ||
async fetch(request: Request, env: Env, ctx: ExecutionContext) { | ||
// TODO: Explain this stuff at the top (for demo purposes only) | ||
const url = new URL(request.url); | ||
if (url.pathname === "/") { | ||
return Response.redirect( | ||
url.origin + | ||
`/authorize?client_id=your-client-id&redirect_uri=${encodeURIComponent(url.origin + "/callback")}&response_type=code`, | ||
); | ||
} else if (url.pathname === "/callback") { | ||
return Response.json({ | ||
message: "OAuth flow complete!", | ||
params: Object.fromEntries(url.searchParams.entries()), | ||
}); | ||
} | ||
|
||
// The real OpenAuth server code starts here: | ||
return authorizer({ | ||
storage: CloudflareStorage({ | ||
namespace: env.AUTH_STORAGE, | ||
}), | ||
subjects, | ||
providers: { | ||
password: PasswordAdapter( | ||
PasswordUI({ | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
sendCode: async (email, code) => { | ||
// This is where you would email the verification code to the | ||
// user, e.g. using Resend: | ||
// https://resend.com/docs/send-with-cloudflare-workers | ||
console.log(`Sending code ${code} to ${email}`); | ||
}, | ||
copy: { | ||
input_code: "Code (check Worker logs)", | ||
}, | ||
}), | ||
), | ||
}, | ||
theme: { | ||
title: "myAuth", | ||
primary: "#0051c3", | ||
favicon: "https://workers.cloudflare.com//favicon.ico", | ||
logo: { | ||
dark: "https://imagedelivery.net/wSMYJvS3Xw-n339CbDyDIA/db1e5c92-d3a6-4ea9-3e72-155844211f00/public", | ||
light: | ||
"https://imagedelivery.net/wSMYJvS3Xw-n339CbDyDIA/fa5a3023-7da9-466b-98a7-4ce01ee6c700/public", | ||
}, | ||
}, | ||
success: async (ctx, value) => { | ||
return ctx.subject("user", { | ||
id: await getOrCreateUser(env, value.email), | ||
}); | ||
}, | ||
}).fetch(request, env, ctx); | ||
}, | ||
} satisfies ExportedHandler<Env>; | ||
|
||
async function getOrCreateUser(env: Env, email: string): Promise<string> { | ||
const result = await env.AUTH_DB.prepare( | ||
` | ||
INSERT INTO user (email) | ||
VALUES (?) | ||
ON CONFLICT (email) DO UPDATE SET email = email | ||
RETURNING id; | ||
`, | ||
) | ||
.bind(email) | ||
.first<{ id: string }>(); | ||
if (!result) { | ||
throw new Error(`Unable to process user: ${email}`); | ||
} | ||
console.log(`Found or created user ${result.id} with email ${email}`); | ||
return result.id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"lib": ["esnext"], | ||
"module": "esnext", | ||
"moduleResolution": "bundler", | ||
"types": ["@cloudflare/workers-types", "./worker-configuration.d.ts"], | ||
"noEmit": true, | ||
"isolatedModules": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"skipLibCheck": true, | ||
"strict": true | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Generated by Wrangler by running `wrangler --experimental-json-config types` | ||
|
||
interface Env { | ||
AUTH_STORAGE: KVNamespace; | ||
AUTH_DB: D1Database; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compatibility_date": "2024-11-01", | ||
"main": "src/index.ts", | ||
"name": "openauth-template", | ||
"upload_source_maps": true, | ||
"kv_namespaces": [ | ||
{ | ||
"binding": "AUTH_STORAGE", | ||
"id": "afec91ff3f7e4b0b9b9323fc6cf5ff85" | ||
} | ||
], | ||
"d1_databases": [ | ||
{ | ||
"binding": "AUTH_DB", | ||
"database_name": "openauth-template-auth-db", | ||
"database_id": "d4dfb2e9-2fd3-4d04-9c83-57b4336a5958" | ||
} | ||
], | ||
"observability": { | ||
"enabled": true | ||
}, | ||
"compatibility_flags": ["nodejs_compat"] | ||
} |
Oops, something went wrong.