Skip to content

Commit

Permalink
feat: verify-token 時に read:basic_info scope あれば基本的な情報を渡す
Browse files Browse the repository at this point in the history
  • Loading branch information
a01sa01to committed Dec 6, 2024
1 parent c117e18 commit 6dac1b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
13 changes: 11 additions & 2 deletions webapp/api/oauth/verifyToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { HonoEnv } from 'load-context'
import { IUserInfo } from 'repository/idp'
import { z } from 'zod'

const app = new Hono<HonoEnv>()
Expand All @@ -19,6 +20,7 @@ interface ValidResponseType {
user_id: string
expires_at: number
scopes: string[]
user_info?: IUserInfo
}

interface InvalidResponseType {
Expand Down Expand Up @@ -79,13 +81,20 @@ app.post(
return c.json<InvalidResponseType>(INVALID_REQUEST_RESPONSE, 404)
}

return c.json<ValidResponseType>({
const res: ValidResponseType = {
valid: true,
client: tokenInfo.client,
user_id: tokenInfo.user_id,
expires_at: tokenInfo.access_token_expires_at.getTime(),
scopes: tokenInfo.scopes.map(s => s.scope.name),
})
}

if (res.scopes.includes('read:basic_info')) {
const user = await c.var.idpClient.findUserById(res.user_id)
if (user) res.user_info = user
}

return c.json<ValidResponseType>(res)
},
)

Expand Down
2 changes: 2 additions & 0 deletions webapp/db/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
-- https://orm.drizzle.team/docs/kit-seed-data

INSERT OR IGNORE INTO `oauth_provider` (`id`, `name`) VALUES (1, "GitHub")

INSERT OR IGNORE INTO `scope` (`id`, `name`, `description`) VALUES (1, "read:basic_info", "あなたのユーザー名やユーザー ID、プロフィール画像を読み取ります。")
5 changes: 3 additions & 2 deletions webapp/repository/idp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable sort-exports/sort-exports */
// saitamau-maximum/id の db/schema.ts 参照
interface IUserInfo {
export interface IUserInfo {
id: string
display_name: string
profile_image_url: string | null
}
interface IOauthConnection {
export interface IOauthConnection {
user_id: string
provider_id: number
provider_user_id: string
Expand Down

0 comments on commit 6dac1b9

Please sign in to comment.