| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
- import { AuthService } from '@/server/modules/auth/auth.service'
- import { UserService } from '@/server/modules/users/user.service'
- import { ErrorSchema } from '@/server/utils/errorHandler'
- import { AppDataSource } from '@/server/data-source'
- const userService = new UserService(AppDataSource)
- const authService = new AuthService(userService)
- const routeDef = createRoute({
- method: 'get',
- path: '/sso-verify',
- responses: {
- 200: {
- description: 'SSO验证成功',
- headers: {
- 'X-Username': {
- schema: { type: 'string' },
- description: '格式化后的用户名'
- }
- }
- },
- 401: {
- description: '未授权或令牌无效',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- },
- 500: {
- description: '服务器错误',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- }
- }
- })
- const app = new OpenAPIHono().openapi(routeDef, async (c) => {
- try {
- const token = c.req.header('Authorization')?.replace('Bearer ', '')
-
- if (!token) {
- return c.json({ code: 401, message: '未提供授权令牌' }, 401)
- }
- try {
- const userData = await authService.verifyToken(token)
- if (!userData) {
- return c.json({ code: 401, message: '无效令牌' }, 401)
- }
- return c.text('OK', 200)
- } catch (tokenError) {
- return c.json({ code: 401, message: '令牌验证失败' }, 401)
- }
- } catch (error) {
- return c.json({ code: 500, message: 'SSO验证失败' }, 500)
- }
- })
- export default app
|