forked from Erisa/discord-oidc-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Simplify endpoint and Refactor (#4)
- Loading branch information
1 parent
a2a680a
commit f3449e8
Showing
9 changed files
with
247 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
{ | ||
"name": "discord-oidc-worker", | ||
"version": "0.1.0", | ||
"private": true, | ||
"description": "Discord OpenID Connect wrapper in Cloudflare Workers.", | ||
"scripts": { | ||
"start": "wrangler dev", | ||
"deploy": "wrangler deploy --minify src/main.ts", | ||
"lint": "eslint \"src/**/*.{ts,tsx}\"", | ||
"format": "prettier --write \"src/**/*.{ts,tsx}\"", | ||
"check": "prettier --check \"src/**/*.{ts,tsx}\" && tsc --noEmit" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"hono": "^3.11.12", | ||
"jose": "^5.2.0" | ||
}, | ||
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20231218.0", | ||
"@typescript-eslint/eslint-plugin": "^6.13.1", | ||
"@typescript-eslint/parser": "^6.13.1", | ||
"eslint": "^8.54.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-react": "^7.33.2", | ||
"eslint-plugin-simple-import-sort": "^10.0.0", | ||
"prettier": "^3.1.0", | ||
"typescript": "^5.3.2", | ||
"wrangler": "^3.22.1" | ||
} | ||
"name": "discord-oidc-worker", | ||
"version": "0.1.0", | ||
"private": true, | ||
"description": "Discord OpenID Connect wrapper in Cloudflare Workers.", | ||
"scripts": { | ||
"start": "wrangler dev", | ||
"deploy": "wrangler deploy --minify src/main.ts", | ||
"lint": "eslint \"src/**/*.{ts,tsx}\"", | ||
"format": "prettier --write \"src/**/*.{ts,tsx}\"", | ||
"check": "prettier --check \"src/**/*.{ts,tsx}\" && tsc --noEmit" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@mikuroxina/mini-fn": "^5.5.1", | ||
"hono": "^3.11.12", | ||
"jose": "^5.2.0" | ||
}, | ||
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20231218.0", | ||
"@typescript-eslint/eslint-plugin": "^6.13.1", | ||
"@typescript-eslint/parser": "^6.13.1", | ||
"eslint": "^8.54.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-react": "^7.33.2", | ||
"eslint-plugin-simple-import-sort": "^10.0.0", | ||
"prettier": "^3.1.0", | ||
"typescript": "^5.3.2", | ||
"wrangler": "^3.22.1" | ||
} | ||
} |
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,99 @@ | ||
import { Result } from "@mikuroxina/mini-fn"; | ||
|
||
import { | ||
CLOUDFLARE_ACCESS_REDIRECT_URI, | ||
DISCORD_API_ROOT, | ||
DISCORD_CLIENT_ID, | ||
} from "../consts"; | ||
import type { Token, TokenResult, User } from "../service/token"; | ||
|
||
export const generateToken = | ||
(clientSecret: string) => | ||
async (code: string): Promise<TokenResult<Token>> => { | ||
const params = new URLSearchParams({ | ||
client_id: DISCORD_CLIENT_ID, | ||
client_secret: clientSecret, | ||
redirect_uri: CLOUDFLARE_ACCESS_REDIRECT_URI, | ||
code, | ||
grant_type: "authorization_code", | ||
scope: "identify email", | ||
}); | ||
|
||
const tokenResponse = await fetch(`${DISCORD_API_ROOT}/oauth2/token`, { | ||
method: "POST", | ||
body: params, | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
}); | ||
if (!tokenResponse.ok) { | ||
return Result.err("TOKEN_GEN_FAILURE"); | ||
} | ||
const tokenResult = await tokenResponse.json<{ | ||
access_token: string; | ||
token_type: string; | ||
expires_in: number; | ||
refresh_token: string; | ||
scope: string; | ||
}>(); | ||
return Result.ok(tokenResult); | ||
}; | ||
|
||
export const me = async (accessToken: string): Promise<User> => { | ||
const meResponse = await fetch(`${DISCORD_API_ROOT}/users/@me`, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
if (!meResponse.ok) { | ||
meResponse.text().then(console.log); | ||
throw new Error("failed to get user info"); | ||
} | ||
|
||
const meResult = await meResponse.json<{ | ||
id: string; | ||
username: string; | ||
discriminator: string; | ||
global_name?: string; | ||
verified?: boolean; | ||
email?: string; | ||
}>(); | ||
if (!meResult.verified) { | ||
throw new Error("email unverified"); | ||
} | ||
|
||
const guildsResponse = await fetch(`${DISCORD_API_ROOT}/users/@me/guilds`, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
if (!guildsResponse.ok) { | ||
guildsResponse.text().then(console.log); | ||
throw new Error("failed to get guilds info"); | ||
} | ||
const guilds = (await guildsResponse.json<{ id: string }[]>()).map( | ||
({ id }) => id, | ||
); | ||
|
||
return { | ||
...meResult, | ||
joinedGuildIds: guilds, | ||
}; | ||
}; | ||
|
||
export const rolesOf = | ||
(botToken: string) => | ||
async (guildId: string, userId: string): Promise<string[]> => { | ||
const memberResponse = await fetch( | ||
`${DISCORD_API_ROOT}/guilds/${guildId}/members/${userId}`, | ||
{ | ||
headers: { | ||
Authorization: `Bot ${botToken}`, | ||
}, | ||
}, | ||
); | ||
const { roles } = await memberResponse.json<{ | ||
roles: string[]; | ||
}>(); | ||
return roles; | ||
}; |
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,5 @@ | ||
export const DISCORD_CLIENT_ID = "1191642657731121272"; | ||
export const DISCORD_API_ROOT = "https://discord.com/api/v10"; | ||
|
||
export const CLOUDFLARE_ACCESS_REDIRECT_URI = | ||
"https://approvers.cloudflareaccess.com/cdn-cgi/access/callback"; |
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
Oops, something went wrong.