Skip to content

Commit

Permalink
temp user realname
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 committed Jan 23, 2025
1 parent 5535557 commit e798893
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 62 deletions.
212 changes: 152 additions & 60 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion frontend/providers/aiproxy/app/api/user/token/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server'
import { parseJwtToken } from '@/utils/backend/auth'
import { getSealosUserUid, parseJwtToken } from '@/utils/backend/auth'
import { ApiProxyBackendResp, ApiResp } from '@/types/api'
import { validateSealosUserRealNameInfo } from '@/utils/backend/db'

export const dynamic = 'force-dynamic'

Expand Down Expand Up @@ -130,6 +131,20 @@ export async function POST(
)
}

const sealosUserUid = await getSealosUserUid(request.headers)
const isRealName = await validateSealosUserRealNameInfo(sealosUserUid)

if (!isRealName) {
return NextResponse.json(
{
code: 400,
message: 'user not real name',
error: 'user not real name'
},
{ status: 400 }
)
}

const updateTokenBody: UpdateTokenRequestBody = await request.json()

if (typeof updateTokenBody.status !== 'number') {
Expand Down
16 changes: 15 additions & 1 deletion frontend/providers/aiproxy/app/api/user/token/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server'
import { TokenInfo } from '@/types/user/token'

import { parseJwtToken } from '@/utils/backend/auth'
import { getSealosUserUid, parseJwtToken } from '@/utils/backend/auth'
import { ApiProxyBackendResp, ApiResp } from '@/types/api'
import { validateSealosUserRealNameInfo } from '@/utils/backend/db'

export const dynamic = 'force-dynamic'

Expand Down Expand Up @@ -190,6 +191,19 @@ export async function POST(request: NextRequest): Promise<NextResponse<ApiResp<T
)
}

const sealosUserUid = await getSealosUserUid(request.headers)
const isRealName = await validateSealosUserRealNameInfo(sealosUserUid)

if (!isRealName) {
return NextResponse.json(
{
code: 400,
message: 'user not real name',
error: 'user not real name'
},
{ status: 400 }
)
}
// 创建Token
const newToken = await createToken(body.name, group)

Expand Down
2 changes: 2 additions & 0 deletions frontend/providers/aiproxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@sealos/ui": "workspace:*",
"@tanstack/react-query": "^4.35.3",
"@tanstack/react-table": "^8.10.7",
"@types/pg": "^8.11.10",
"accept-language": "^3.0.20",
"axios": "^1.7.7",
"date-fns": "^2.30.0",
Expand All @@ -25,6 +26,7 @@
"immer": "^10.1.1",
"jsonwebtoken": "^9.0.2",
"next": "14.2.5",
"pg": "^8.13.1",
"react": "^18",
"react-day-picker": "^8.8.2",
"react-dom": "^18",
Expand Down
25 changes: 25 additions & 0 deletions frontend/providers/aiproxy/utils/backend/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,28 @@ export async function parseJwtToken(headers: Headers): Promise<string> {
return Promise.reject('Auth: Invalid token')
}
}

export async function getSealosUserUid(headers: Headers): Promise<string> {
try {
const token = headers.get('authorization')
if (!token) {
return Promise.reject('Auth: Token is missing')
}

const decoded = jwt.verify(
token,
global.AppConfig?.auth.appTokenJwtKey || ''
) as AppTokenPayload
const now = Math.floor(Date.now() / 1000)
if (decoded.exp && decoded.exp < now) {
return Promise.reject('Auth: Token expired')
}
if (!decoded.workspaceId) {
return Promise.reject('Auth: Invalid token')
}
return decoded.userUid
} catch (error) {
console.error('Auth: Token parsing error:', error)
return Promise.reject('Auth: Invalid token')
}
}
Loading

0 comments on commit e798893

Please sign in to comment.