diff --git a/webapp/api/oauth/verifyToken.ts b/webapp/api/oauth/verifyToken.ts index d977e9c..7710439 100644 --- a/webapp/api/oauth/verifyToken.ts +++ b/webapp/api/oauth/verifyToken.ts @@ -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() // 仕様はここ参照: https://github.com/saitamau-maximum/auth/issues/43 @@ -19,6 +22,7 @@ interface ValidResponseType { user_id: string expires_at: number scopes: string[] + user_info?: IUserInfo } interface InvalidResponseType { @@ -79,13 +83,20 @@ app.post( return c.json(INVALID_REQUEST_RESPONSE, 404) } - return c.json({ + 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(res) }, ) diff --git a/webapp/constants/scope.ts b/webapp/constants/scope.ts new file mode 100644 index 0000000..3efdfea --- /dev/null +++ b/webapp/constants/scope.ts @@ -0,0 +1,3 @@ +export const READ_SCOPES = { + BASIC_INFO: 'read:basic_info', +} diff --git a/webapp/db/seed.sql b/webapp/db/seed.sql index 119fbfe..0f0c133 100644 --- a/webapp/db/seed.sql +++ b/webapp/db/seed.sql @@ -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、プロフィール画像を読み取ります。") diff --git a/webapp/repository/idp.ts b/webapp/repository/idp.ts index ae9902a..171324e 100644 --- a/webapp/repository/idp.ts +++ b/webapp/repository/idp.ts @@ -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