forked from denoland/deno_kv_oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
132 lines (121 loc) · 3.64 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { loadSync, type OAuth2ClientConfig, Status } from "./dev_deps.ts";
import {
createAuth0OAuth2Client,
createDiscordOAuth2Client,
createDropboxOAuth2Client,
createFacebookOAuth2Client,
createGitHubOAuth2Client,
createGitLabOAuth2Client,
createGoogleOAuth2Client,
createNotionOAuth2Client,
createOktaOAuth2Client,
createPatreonOAuth2Client,
createSlackOAuth2Client,
createSpotifyOAuth2Client,
createTwitterOAuth2Client,
getSessionAccessToken,
getSessionId,
handleCallback,
signIn,
signOut,
} from "./mod.ts";
loadSync({ export: true });
/**
* Allows for dynamic provider selection useful for testing.
* In production, just use import and use the provider's OAuth 2.0 client creator.
*
* @example
* ```ts
* import { createGitHubOAuth2Client } from "https://deno.land/x/deno_kv_oauth@$VERSION/mod.ts";
*
* const oauth2Client = createGitHubOAuth2Client();
* ```
*/
const provider = Deno.env.get("PROVIDER") ?? "GitHub";
const createOAuth2ClientFn = {
Auth0: createAuth0OAuth2Client,
Discord: createDiscordOAuth2Client,
Dropbox: createDropboxOAuth2Client,
Facebook: createFacebookOAuth2Client,
GitHub: createGitHubOAuth2Client,
GitLab: createGitLabOAuth2Client,
Google: createGoogleOAuth2Client,
Notion: createNotionOAuth2Client,
Okta: createOktaOAuth2Client,
Patreon: createPatreonOAuth2Client,
Slack: createSlackOAuth2Client,
Spotify: createSpotifyOAuth2Client,
Twitter: createTwitterOAuth2Client,
}[provider];
if (createOAuth2ClientFn === undefined) {
throw new Error("Provider not found");
}
const additionalOAuth2ClientConfig: Partial<OAuth2ClientConfig> = {
redirectUri: Deno.env.get("DENO_DEPLOYMENT_ID") === undefined
? "http://localhost:8000/callback"
: undefined,
defaults: {
scope: Deno.env.get("SCOPE"),
},
};
// @ts-ignore Trust me
const oauth2Client = createOAuth2ClientFn(additionalOAuth2ClientConfig);
async function indexHandler(request: Request) {
const sessionId = getSessionId(request);
const hasSessionIdCookie = sessionId !== undefined;
const accessToken = hasSessionIdCookie
? await getSessionAccessToken(oauth2Client, sessionId)
: null;
const accessTokenInnerText = accessToken !== null
? `<span style="filter:blur(3px)">${accessToken}</span> (intentionally blurred for security)`
: accessToken;
const body = `
<p>Provider: ${provider}</p>
<p>Scope: ${oauth2Client.config.defaults?.scope}</p>
<p>Signed in: ${hasSessionIdCookie}</p>
<p>Your access token: ${accessTokenInnerText}</p>
<p>
<a href="/signin">Sign in</a>
</p>
<p>
<a href="/signout">Sign out</a>
</p>
<p>
<a href="https://deno.land/x/deno_kv_oauth/demo.ts?source">Source code</a>
</p>
`;
return new Response(body, {
headers: { "content-type": "text/html; charset=utf-8" },
});
}
export async function handler(request: Request): Promise<Response> {
if (request.method !== "GET") {
return new Response(null, { status: Status.NotFound });
}
switch (new URL(request.url).pathname) {
case "/": {
return await indexHandler(request);
}
case "/signin": {
return await signIn(request, oauth2Client);
}
case "/callback": {
try {
const { response } = await handleCallback(request, oauth2Client);
return response;
} catch {
return new Response(null, { status: Status.InternalServerError });
}
}
case "/signout": {
return await signOut(request);
}
default: {
return new Response(null, { status: Status.NotFound });
}
}
}
if (import.meta.main) {
Deno.serve(handler);
}