feat: add first party Deno KV OAuth plugin (#1774)
This commit is contained in:
@@ -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,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -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";
|
||||
@@ -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,
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -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";
|
||||
@@ -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(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user