mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-24 16:05:44 +08:00
Merge pull request #4290 from wucm667/fix/issue-4287-preserve-antigravity-rt
fix(antigravity): 保留手动输入的 refresh token
This commit is contained in:
@@ -5728,7 +5728,7 @@ const handleAntigravityValidateRT = async (refreshTokenInput: string) => {
|
||||
continue
|
||||
}
|
||||
|
||||
const credentials = antigravityOAuth.buildCredentials(tokenInfo)
|
||||
const credentials = antigravityOAuth.buildCredentials(tokenInfo, refreshTokens[i])
|
||||
applyAntigravityProjectID(credentials, antigravityProjectId.value, 'create')
|
||||
|
||||
// Generate account name with index for batch
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
vi.mock('@/stores/app', () => ({
|
||||
useAppStore: () => ({
|
||||
showError: vi.fn()
|
||||
})
|
||||
}))
|
||||
|
||||
vi.mock('vue-i18n', () => ({
|
||||
useI18n: () => ({
|
||||
t: (key: string) => key
|
||||
})
|
||||
}))
|
||||
|
||||
vi.mock('@/api/admin', () => ({
|
||||
adminAPI: {
|
||||
antigravity: {
|
||||
generateAuthUrl: vi.fn(),
|
||||
exchangeCode: vi.fn(),
|
||||
refreshAntigravityToken: vi.fn()
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
import { useAntigravityOAuth } from '@/composables/useAntigravityOAuth'
|
||||
|
||||
describe('useAntigravityOAuth.buildCredentials', () => {
|
||||
it('falls back to the submitted refresh token when the response omits it', () => {
|
||||
const oauth = useAntigravityOAuth()
|
||||
|
||||
const credentials = oauth.buildCredentials(
|
||||
{
|
||||
access_token: 'access-token',
|
||||
expires_at: 1_900_000_000
|
||||
},
|
||||
'submitted-refresh-token'
|
||||
)
|
||||
|
||||
expect(credentials.refresh_token).toBe('submitted-refresh-token')
|
||||
})
|
||||
|
||||
it('prefers a new refresh token returned by the response', () => {
|
||||
const oauth = useAntigravityOAuth()
|
||||
|
||||
const credentials = oauth.buildCredentials(
|
||||
{
|
||||
access_token: 'access-token',
|
||||
refresh_token: 'rotated-refresh-token',
|
||||
expires_at: 1_900_000_000
|
||||
},
|
||||
'submitted-refresh-token'
|
||||
)
|
||||
|
||||
expect(credentials.refresh_token).toBe('rotated-refresh-token')
|
||||
})
|
||||
})
|
||||
@@ -112,17 +112,23 @@ export function useAntigravityOAuth() {
|
||||
}
|
||||
}
|
||||
|
||||
const buildCredentials = (tokenInfo: AntigravityTokenInfo): Record<string, unknown> => {
|
||||
const buildCredentials = (
|
||||
tokenInfo: AntigravityTokenInfo,
|
||||
fallbackRefreshToken?: string
|
||||
): Record<string, unknown> => {
|
||||
let expiresAt: string | undefined
|
||||
if (typeof tokenInfo.expires_at === 'number' && Number.isFinite(tokenInfo.expires_at)) {
|
||||
expiresAt = Math.floor(tokenInfo.expires_at).toString()
|
||||
} else if (typeof tokenInfo.expires_at === 'string' && tokenInfo.expires_at.trim()) {
|
||||
expiresAt = tokenInfo.expires_at.trim()
|
||||
}
|
||||
const refreshToken = tokenInfo.refresh_token?.trim()
|
||||
? tokenInfo.refresh_token
|
||||
: fallbackRefreshToken
|
||||
|
||||
return {
|
||||
access_token: tokenInfo.access_token,
|
||||
refresh_token: tokenInfo.refresh_token,
|
||||
refresh_token: refreshToken,
|
||||
token_type: tokenInfo.token_type,
|
||||
expires_at: expiresAt,
|
||||
project_id: tokenInfo.project_id,
|
||||
|
||||
Reference in New Issue
Block a user