Skip to content

Commit

Permalink
feat: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
sakkke committed Apr 8, 2024
1 parent 607e9d1 commit 82fe1bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Client, GatewayIntentBits } from 'discord.js'

function createBot(token) {
function createBot(token: string) {
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] })

client.on('ready', () => {
if (!client.user) {
console.error('client.user is null')
return
}
console.log(`Logged in as ${client.user.tag}!`)
})

Expand Down
45 changes: 34 additions & 11 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { Hono } from 'hono'
import { getCookie, setCookie } from 'hono/cookie'
import { createBot } from './bot'
import { Client, ClientUser } from 'discord.js'

interface Config {
bot: Client<boolean>
client_id: string
client_secret: string
discord_guild_id: string
oauth2_callback: string
oauth2_endpoint: string
}

function createProxy(config) {
function createProxy(config: Config): Hono {
const { bot, client_id, client_secret, discord_guild_id, oauth2_callback, oauth2_endpoint } = config

async function authorizationCode(code) {
interface AuthorizationCodeResponse {
access_token: string
expires_in: number
refresh_token: string
}

async function authorizationCode(code: string): Promise<AuthorizationCodeResponse> {
console.log('authorizing code')
const data = await fetch(`https://discord.com/api/oauth2/token`, {
method: 'POST',
Expand All @@ -20,12 +36,12 @@ function createProxy(config) {
code,
}),
})
const result = await data.json()
const result: AuthorizationCodeResponse = await data.json()
const { access_token, expires_in, refresh_token } = result
return { access_token, expires_in, refresh_token }
}

async function authorizationRefreshToken(refreshToken) {
async function authorizationRefreshToken(refreshToken: string): Promise<AuthorizationCodeResponse> {
console.log('authorizing refresh token')
const data = await fetch(`https://discord.com/api/oauth2/token`, {
method: 'POST',
Expand All @@ -40,22 +56,23 @@ function createProxy(config) {
refresh_token: refreshToken,
}),
})
const result = await data.json()
return ({ access_token, expires_in, refresh_token } = result)
const result: AuthorizationCodeResponse = await data.json()
const { access_token, expires_in, refresh_token } = result
return { access_token, expires_in, refresh_token }
}

async function getUser(accessToken) {
async function getUser(accessToken: string): Promise<ClientUser> {
console.log('getting user')
const data = await fetch(`https://discord.com/api/users/@me`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
const result = await data.json()
const result: ClientUser = await data.json()
return result
}

async function userExists(accessToken, refreshToken) {
async function userExists(accessToken: string, refreshToken: string): Promise<boolean> {
const guild = await bot.guilds.fetch(discord_guild_id)

const { id } = await getUser(accessToken).catch(async e => {
Expand All @@ -65,10 +82,12 @@ function createProxy(config) {
expires_in: refreshed_expires_in,
refresh_token: refreshed_refresh_token,
} = await authorizationRefreshToken(refreshToken)
// @ts-ignore
setCookie(c, 'access_token', refreshed_access_token, {
maxAge: refreshed_expires_in,
httpOnly: true,
})
// @ts-ignore
setCookie(c, 'refresh_token', refreshed_refresh_token, {
maxAge: refreshed_expires_in,
httpOnly: true,
Expand All @@ -86,7 +105,7 @@ function createProxy(config) {
'/login': null,
'/oauth2/callback': null,
}
const isPathAllowed = path => path in allowPaths
const isPathAllowed = (path: string): boolean => path in allowPaths
proxy.use(async (c, next) => {
if (isPathAllowed(c.req.path)) {
console.log(`passed allow path: ${c.req.path}`)
Expand Down Expand Up @@ -117,11 +136,15 @@ function createProxy(config) {

proxy.get('/oauth2/callback', async c => {
console.log('aceessed /oauth2/callback')
const code = c.req.query('code')
if (typeof code === 'undefined') {
throw new Error('code is undefined')
}
const {
access_token,
expires_in,
refresh_token,
} = await authorizationCode(c.req.query('code'))
} = await authorizationCode(code)
setCookie(c, 'access_token', access_token, {
maxAge: expires_in,
httpOnly: true,
Expand Down

0 comments on commit 82fe1bd

Please sign in to comment.