mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-24 23:21:18 +00:00
fix(accounts): reauthorize OpenAI with refresh token
This commit is contained in:
2 files changed
+71
-2
No files matched your search
@@ -140,7 +140,7 @@
|
|||||||
:initial-input-method="grokInitialInputMethod"
|
:initial-input-method="grokInitialInputMethod"
|
||||||
@generate-url="handleGenerateUrl"
|
@generate-url="handleGenerateUrl"
|
||||||
@cookie-auth="handleCookieAuth"
|
@cookie-auth="handleCookieAuth"
|
||||||
@validate-refresh-token="handleGrokValidateRefreshToken"
|
@validate-refresh-token="handleValidateRefreshToken"
|
||||||
@import-sso="handleGrokImportSSO"
|
@import-sso="handleGrokImportSSO"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -626,6 +626,74 @@ const applyGrokReauthTokenInfo = async (tokenInfo: {
|
|||||||
handleClose()
|
handleClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Re-auth the existing account with one refresh token. */
|
||||||
|
const handleValidateRefreshToken = async (refreshTokenInput: string) => {
|
||||||
|
if (!props.account) return
|
||||||
|
if (isGrok.value) {
|
||||||
|
await handleGrokValidateRefreshToken(refreshTokenInput)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const refreshToken = refreshTokenInput
|
||||||
|
.split('\n')
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter(Boolean)[0]
|
||||||
|
if (!refreshToken) return
|
||||||
|
|
||||||
|
if (isOpenAILike.value) {
|
||||||
|
openaiOAuth.loading.value = true
|
||||||
|
openaiOAuth.error.value = ''
|
||||||
|
try {
|
||||||
|
const tokenInfo = await openaiOAuth.validateRefreshToken(refreshToken, props.account.proxy_id)
|
||||||
|
if (!tokenInfo) return
|
||||||
|
|
||||||
|
const updatedAccount = await adminAPI.accounts.applyOAuthCredentials(props.account.id, {
|
||||||
|
type: 'oauth',
|
||||||
|
credentials: openaiOAuth.buildCredentials(tokenInfo),
|
||||||
|
extra: openaiOAuth.buildExtraInfo(tokenInfo)
|
||||||
|
})
|
||||||
|
appStore.showSuccess(t('admin.accounts.reAuthorizedSuccess'))
|
||||||
|
emit('reauthorized', updatedAccount)
|
||||||
|
handleClose()
|
||||||
|
} catch (error: any) {
|
||||||
|
openaiOAuth.error.value =
|
||||||
|
error.response?.data?.detail ||
|
||||||
|
error.response?.data?.message ||
|
||||||
|
error.message ||
|
||||||
|
t('admin.accounts.oauth.authFailed')
|
||||||
|
appStore.showError(openaiOAuth.error.value)
|
||||||
|
} finally {
|
||||||
|
openaiOAuth.loading.value = false
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isAntigravity.value) return
|
||||||
|
antigravityOAuth.loading.value = true
|
||||||
|
antigravityOAuth.error.value = ''
|
||||||
|
try {
|
||||||
|
const tokenInfo = await antigravityOAuth.validateRefreshToken(refreshToken, props.account.proxy_id)
|
||||||
|
if (!tokenInfo) return
|
||||||
|
|
||||||
|
const updatedAccount = await adminAPI.accounts.applyOAuthCredentials(props.account.id, {
|
||||||
|
type: 'oauth',
|
||||||
|
credentials: antigravityOAuth.buildCredentials(tokenInfo, refreshToken)
|
||||||
|
})
|
||||||
|
appStore.showSuccess(t('admin.accounts.reAuthorizedSuccess'))
|
||||||
|
emit('reauthorized', updatedAccount)
|
||||||
|
handleClose()
|
||||||
|
} catch (error: any) {
|
||||||
|
antigravityOAuth.error.value =
|
||||||
|
error.response?.data?.detail ||
|
||||||
|
error.response?.data?.message ||
|
||||||
|
error.message ||
|
||||||
|
t('admin.accounts.oauth.authFailed')
|
||||||
|
appStore.showError(antigravityOAuth.error.value)
|
||||||
|
} finally {
|
||||||
|
antigravityOAuth.loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Re-auth with a single SSO cookie → Build OAuth (not batch create). */
|
/** Re-auth with a single SSO cookie → Build OAuth (not batch create). */
|
||||||
const handleGrokImportSSO = async (ssoInput: string) => {
|
const handleGrokImportSSO = async (ssoInput: string) => {
|
||||||
if (!props.account || !isGrok.value) return
|
if (!props.account || !isGrok.value) return
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ describe('ReAuthAccountModal Grok re-auth paths', () => {
|
|||||||
|
|
||||||
it('wires SSO and RT reauth without batch account create', () => {
|
it('wires SSO and RT reauth without batch account create', () => {
|
||||||
expect(source).toContain('@import-sso="handleGrokImportSSO"')
|
expect(source).toContain('@import-sso="handleGrokImportSSO"')
|
||||||
expect(source).toContain('@validate-refresh-token="handleGrokValidateRefreshToken"')
|
expect(source).toContain('@validate-refresh-token="handleValidateRefreshToken"')
|
||||||
|
expect(source).toContain('await handleGrokValidateRefreshToken(refreshTokenInput)')
|
||||||
expect(source).toContain('grokOAuth.validateSSOToken')
|
expect(source).toContain('grokOAuth.validateSSOToken')
|
||||||
expect(source).toContain('grokOAuth.buildCredentials')
|
expect(source).toContain('grokOAuth.buildCredentials')
|
||||||
// Re-auth updates the existing account; must not call createFromSSO batch create
|
// Re-auth updates the existing account; must not call createFromSSO batch create
|
||||||
|
|||||||
Reference in new issue
Block a user