From b6eb5180926d909e13c29ca1edcc4e2af8c55529 Mon Sep 17 00:00:00 2001 From: Michael Herzner Date: Mon, 18 Sep 2023 19:10:50 +0200 Subject: [PATCH] feat: add first party Deno KV OAuth plugin (#1774) --- .github/workflows/ci.yml | 4 +- deno.json | 2 +- docs/latest/examples/using-deno-kv-oauth.md | 56 ++++++++++++++++ plugins/kv_oauth.ts | 71 +++++++++++++++++++++ plugins/kv_oauth/plugin_deps.ts | 6 ++ plugins/kv_oauth/plugin_test.ts | 39 +++++++++++ plugins/kv_oauth/plugin_test_deps.ts | 2 + plugins/kv_oauth/plugin_test_utils.ts | 25 ++++++++ 8 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 plugins/kv_oauth.ts create mode 100644 plugins/kv_oauth/plugin_deps.ts create mode 100644 plugins/kv_oauth/plugin_test.ts create mode 100644 plugins/kv_oauth/plugin_test_deps.ts create mode 100644 plugins/kv_oauth/plugin_test_utils.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0476b762..ec7a9ae1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,8 +61,8 @@ jobs: - name: Run tests Windows if: matrix.os == 'windows-latest' - run: deno test -A --trace-ops + run: deno test -A --trace-ops --unstable - name: Run tests macOS + Linux if: matrix.os != 'windows-latest' - run: deno test -A --parallel --trace-ops + run: deno test -A --parallel --trace-ops --unstable diff --git a/deno.json b/deno.json index 9cf4e239..4604fcbb 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "lock": false, "tasks": { - "test": "deno test -A --parallel", + "test": "deno test -A --parallel --unstable", "fixture": "deno run -A --watch=static/,routes/ tests/fixture/dev.ts", "www": "deno task --cwd=www start", "screenshot": "deno run -A www/utils/screenshot.ts", diff --git a/docs/latest/examples/using-deno-kv-oauth.md b/docs/latest/examples/using-deno-kv-oauth.md index 12f66d2d..7841f400 100644 --- a/docs/latest/examples/using-deno-kv-oauth.md +++ b/docs/latest/examples/using-deno-kv-oauth.md @@ -11,6 +11,62 @@ description: Easily integrate OAuth 2.0 to your Fresh project with Deno KV OAuth > change**. See [the documentation](https://deno.land/x/deno_kv_oauth) for > further details. +## Getting Started with the Official Fresh Plugin + +> Note: The minimum required version for plugins in Fresh is 1.3.0 + +If you're not performing anything special in the sign-in, sign-out and callback +handlers, you can add the Fresh plugin to your project. This automatically +handles `GET /oauth/signin`, `GET /oauth/callback` and `GET /oauth/signout` +routes. + +1. Create your OAuth 2.0 application for your given provider. + +2. Create your + [pre-configured](https://deno.land/x/deno_kv_oauth#pre-configured-oauth-20-clients) + or + [custom OAuth 2.0 client instance](https://deno.land/x/deno_kv_oauth#custom-oauth-20-client) + and configure Fresh to use the plugin. + + ```ts + // main.ts + import { start } from "$fresh/server.ts"; + import kvOAuthPlugin from "$fresh/plugins/kv_oauth.ts"; + import { createGitHubOAuth2Client } from "https://deno.land/x/deno_kv_oauth@$VERSION/mod.ts"; + import manifest from "./fresh.gen.ts"; + + await start(manifest, { + plugins: [ + kvOAuthPlugin(createGitHubOAuth2Client()), + ], + }); + ``` + +3. ⚠️ While Deno KV is still **experimental** you need to add the `--unstable` + option to the `start` task in the `deno.json` file. + + ```json + "start": "deno run -A --watch=static/,routes/ --unstable dev.ts", + ``` + +4. Start your project with the necessary environment variables. + + ```sh + GITHUB_CLIENT_ID=xxx GITHUB_CLIENT_SECRET=xxx deno task start + ``` + +If you require more advanced setups, you can create your own plugin. For more +information, see: + +- The [source code](src/fresh_plugin.ts) for `kvOAuthPlugin()` +- The [Plugin documentation](https://fresh.deno.dev/docs/concepts/plugins) for + Fresh +- The + [Fresh + Deno KV OAuth demo](https://github.com/denoland/fresh-deno-kv-oauth-demo) + which uses the Fresh plugin +- [Deno SaaSKit](https://saaskit.deno.dev/)'s custom + [plugin implementation](https://github.com/denoland/saaskit/blob/3accffdc44c2d2eb6dba28126f8d4cb525eba340/plugins/kv_oauth.ts) + ## Getting Started This example uses GitHub as the OAuth 2.0 provider. However there is a suite of diff --git a/plugins/kv_oauth.ts b/plugins/kv_oauth.ts new file mode 100644 index 00000000..ed6275f1 --- /dev/null +++ b/plugins/kv_oauth.ts @@ -0,0 +1,71 @@ +import { handleCallback, signIn, signOut } from "./kv_oauth/plugin_deps.ts"; +import type { OAuth2Client } from "./kv_oauth/plugin_deps.ts"; +import type { Plugin } from "../server.ts"; + +export interface KvOAuthPluginOptions { + /** + * Sign-in page path + * + * @default {"/oauth/signin"} + */ + signInPath?: string; + /** + * Callback page path + * + * @default {"/oauth/callback"} + */ + callbackPath?: string; + /** + * Sign-out page path + * + * @default {"/oauth/signout"} + */ + signOutPath?: string; +} + +/** + * This creates handlers for the following routes: + * - `GET /oauth/signin` for the sign-in page + * - `GET /oauth/callback` for the callback page + * - `GET /oauth/signout` for the sign-out page + * + * ```ts + * // main.ts + * import { start } from "$fresh/server.ts"; + * import kvOAuthPlugin from "$fresh/plugins/kv_oauth.ts"; + * import { createGitHubOAuth2Client } from "https://deno.land/x/deno_kv_oauth@$VERSION/mod.ts"; + * import manifest from "./fresh.gen.ts"; + * + * await start(manifest, { + * plugins: [ + * kvOAuthPlugin(createGitHubOAuth2Client()) + * ] + * }); + * ``` + */ +export default function kvOAuthPlugin( + oauth2Client: OAuth2Client, + options?: KvOAuthPluginOptions, +): Plugin { + return { + name: "kv-oauth", + routes: [ + { + path: options?.signInPath ?? "/oauth/signin", + handler: async (req) => await signIn(req, oauth2Client), + }, + { + path: options?.callbackPath ?? "/oauth/callback", + handler: async (req) => { + // Return object also includes `accessToken` and `sessionId` properties. + const { response } = await handleCallback(req, oauth2Client); + return response; + }, + }, + { + path: options?.signOutPath ?? "/oauth/signout", + handler: signOut, + }, + ], + }; +} diff --git a/plugins/kv_oauth/plugin_deps.ts b/plugins/kv_oauth/plugin_deps.ts new file mode 100644 index 00000000..fafcaab6 --- /dev/null +++ b/plugins/kv_oauth/plugin_deps.ts @@ -0,0 +1,6 @@ +export { + handleCallback, + signIn, + signOut, +} from "https://deno.land/x/deno_kv_oauth@v0.6.1/mod.ts"; +export { OAuth2Client } from "https://deno.land/x/oauth2_client@v1.0.2/mod.ts"; diff --git a/plugins/kv_oauth/plugin_test.ts b/plugins/kv_oauth/plugin_test.ts new file mode 100644 index 00000000..0d6831db --- /dev/null +++ b/plugins/kv_oauth/plugin_test.ts @@ -0,0 +1,39 @@ +// Copyright 2023 the Deno authors. All rights reserved. MIT license. +import kvOAuthPlugin from "$fresh/plugins/kv_oauth.ts"; +import { + assert, + assertArrayIncludes, + assertNotEquals, +} from "$std/testing/asserts.ts"; +import { oauth2Client } from "./plugin_test_utils.ts"; + +Deno.test("kvOAuthPlugin() works correctly", async (test) => { + await test.step("with default values", () => { + const plugin = kvOAuthPlugin(oauth2Client); + assertNotEquals(plugin.routes, undefined); + assert(plugin.routes!.every((route) => route.handler !== undefined)); + assertArrayIncludes(plugin.routes!.map((route) => route.path), [ + "/oauth/signin", + "/oauth/callback", + "/oauth/signout", + ]); + }); + + await test.step("with defined values", () => { + const signInPath = "/signin"; + const callbackPath = "/callback"; + const signOutPath = "/signout"; + const plugin = kvOAuthPlugin(oauth2Client, { + signInPath, + callbackPath, + signOutPath, + }); + assertNotEquals(plugin.routes, undefined); + assert(plugin.routes!.every((route) => route.handler !== undefined)); + assertArrayIncludes(plugin.routes!.map((route) => route.path), [ + signInPath, + callbackPath, + signOutPath, + ]); + }); +}); diff --git a/plugins/kv_oauth/plugin_test_deps.ts b/plugins/kv_oauth/plugin_test_deps.ts new file mode 100644 index 00000000..9b5e7824 --- /dev/null +++ b/plugins/kv_oauth/plugin_test_deps.ts @@ -0,0 +1,2 @@ +export type { Tokens } from "https://deno.land/x/oauth2_client@v1.0.2/mod.ts"; +export type { OAuthSession } from "https://deno.land/x/deno_kv_oauth@v0.6.1/src/core.ts"; diff --git a/plugins/kv_oauth/plugin_test_utils.ts b/plugins/kv_oauth/plugin_test_utils.ts new file mode 100644 index 00000000..035c5158 --- /dev/null +++ b/plugins/kv_oauth/plugin_test_utils.ts @@ -0,0 +1,25 @@ +// Copyright 2023 the Deno authors. All rights reserved. MIT license. +import { OAuth2Client } from "./plugin_deps.ts"; +import type { OAuthSession, Tokens } from "./plugin_test_deps.ts"; + +// Dummy OAuth 2.0 client for testing only. +export const oauth2Client = new OAuth2Client({ + clientId: crypto.randomUUID(), + clientSecret: crypto.randomUUID(), + authorizationEndpointUri: "https://example.com/authorize", + tokenUri: "https://example.com/token", +}); + +export function genOAuthSession(): OAuthSession { + return { + state: crypto.randomUUID(), + codeVerifier: crypto.randomUUID(), + }; +} + +export function genTokens(): Tokens { + return { + accessToken: crypto.randomUUID(), + tokenType: crypto.randomUUID(), + }; +}