Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: verify-token 時に read:basic_info scope あれば基本的な情報を渡す #47

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions webapp/api/oauth/verifyToken.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { HonoEnv } from 'load-context'
import { z } from 'zod'

import { READ_SCOPES } from '../../constants/scope'
import { HonoEnv } from '../../load-context'
import { IUserInfo } from '../../repository/idp'

const app = new Hono<HonoEnv>()

// 仕様はここ参照: https://github.com/saitamau-maximum/auth/issues/43
Expand All @@ -19,6 +22,7 @@ interface ValidResponseType {
user_id: string
expires_at: number
scopes: string[]
user_info?: IUserInfo
}

interface InvalidResponseType {
Expand Down Expand Up @@ -79,13 +83,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_SCOPES.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
3 changes: 3 additions & 0 deletions webapp/constants/scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const READ_SCOPES = {
BASIC_INFO: 'read:basic_info',
}
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
Loading