初始提交:OCI 面板前端(含 GenAI 网关一期)
This commit is contained in:
+151
@@ -0,0 +1,151 @@
|
||||
import { mockOn, mocked, request } from './request'
|
||||
import type {
|
||||
CredentialsInfo,
|
||||
OauthProviderInfo,
|
||||
LoginRequest,
|
||||
LoginResponse,
|
||||
OAuthSettings,
|
||||
TotpSetup,
|
||||
TotpStatus,
|
||||
UpdateCredentialsRequest,
|
||||
UpdateOAuthRequest,
|
||||
UserIdentityInfo,
|
||||
} from '@/types/api'
|
||||
|
||||
export function login(body: LoginRequest): Promise<LoginResponse> {
|
||||
if (mockOn) {
|
||||
if (body.password === 'wrong') return Promise.reject(new Error('用户名或密码错误'))
|
||||
return mocked({ token: 'mock-token', expiresAt: '2099-01-01T00:00:00Z' }, 400)
|
||||
}
|
||||
return request('/auth/login', { method: 'POST', body })
|
||||
}
|
||||
|
||||
/** 服务端登出:当前令牌拉黑至自然过期;调用方无论成败都应清本地会话 */
|
||||
export function logout(): Promise<void> {
|
||||
if (mockOn) return mocked(undefined, 100)
|
||||
return request('/auth/logout', { method: 'POST' })
|
||||
}
|
||||
|
||||
// ---- 登录凭据 ----
|
||||
|
||||
export function getCredentials(): Promise<CredentialsInfo> {
|
||||
if (mockOn) return mocked({ username: 'admin', passwordLoginDisabled: false })
|
||||
return request('/auth/credentials')
|
||||
}
|
||||
|
||||
/** 修改用户名 / 密码;成功后旧 token 随即失效,调用方应登出重登 */
|
||||
export function updateCredentials(body: UpdateCredentialsRequest): Promise<void> {
|
||||
if (mockOn) return mocked(undefined, 400)
|
||||
return request('/auth/credentials', { method: 'PUT', body })
|
||||
}
|
||||
|
||||
/** 保存密码登录禁用开关;开启要求至少绑定一个外部身份(后端 409) */
|
||||
export function updatePasswordLogin(disabled: boolean): Promise<void> {
|
||||
if (mockOn) return mocked(undefined, 300)
|
||||
return request('/auth/password-login', { method: 'PUT', body: { disabled } })
|
||||
}
|
||||
|
||||
// ---- 两步验证(TOTP) ----
|
||||
|
||||
export function getTotpStatus(): Promise<TotpStatus> {
|
||||
if (mockOn) return mocked({ enabled: false })
|
||||
return request('/auth/totp')
|
||||
}
|
||||
|
||||
/** 生成待激活密钥;10 分钟内输入验证码激活,重复调用覆盖旧暂存 */
|
||||
export function setupTotp(): Promise<TotpSetup> {
|
||||
if (mockOn)
|
||||
return mocked({
|
||||
secret: 'JBSWY3DPEHPK3PXP',
|
||||
otpauthUri: 'otpauth://totp/oci-portal:admin?secret=JBSWY3DPEHPK3PXP&issuer=oci-portal',
|
||||
})
|
||||
return request('/auth/totp/setup', { method: 'POST' })
|
||||
}
|
||||
|
||||
export function activateTotp(code: string): Promise<void> {
|
||||
if (mockOn) return mocked(undefined, 300)
|
||||
return request('/auth/totp/activate', { method: 'POST', body: { code } })
|
||||
}
|
||||
|
||||
/** 停用两步验证;密码或当前验证码任一确认 */
|
||||
export function disableTotp(body: { password?: string; code?: string }): Promise<void> {
|
||||
if (mockOn) return mocked(undefined, 300)
|
||||
return request('/auth/totp/disable', { method: 'POST', body })
|
||||
}
|
||||
|
||||
// ---- 外部身份(OAuth) ----
|
||||
|
||||
/** 可登录的 provider 列表(登录页公开接口;已禁用的不返回);
|
||||
* passwordLoginDisabled 为 true 且有可用 provider 时,登录页隐藏密码表单 */
|
||||
export function getOauthProviders(): Promise<{
|
||||
providers: OauthProviderInfo[]
|
||||
passwordLoginDisabled: boolean
|
||||
}> {
|
||||
if (mockOn)
|
||||
return mocked({
|
||||
providers: [
|
||||
{ provider: 'github', displayName: 'GitHub' },
|
||||
{ provider: 'oidc', displayName: 'OIDC SSO' },
|
||||
],
|
||||
passwordLoginDisabled: false,
|
||||
})
|
||||
return request('/auth/oauth/providers')
|
||||
}
|
||||
|
||||
/** 获取授权跳转 URL;bind 模式要求已登录(带 JWT) */
|
||||
export function getOauthAuthorizeUrl(
|
||||
provider: string,
|
||||
mode: 'login' | 'bind',
|
||||
): Promise<{ url: string }> {
|
||||
if (mockOn) return mocked({ url: 'https://example.com/oauth/authorize?mock=1' })
|
||||
return request(`/auth/oauth/${provider}/authorize`, { query: { mode } })
|
||||
}
|
||||
|
||||
export function listIdentities(): Promise<{ items: UserIdentityInfo[] }> {
|
||||
if (mockOn)
|
||||
return mocked({
|
||||
items: [
|
||||
{ id: 1, provider: 'github', display: 'octocat', createdAt: '2026-07-07T10:00:00+08:00' },
|
||||
],
|
||||
})
|
||||
return request('/auth/identities')
|
||||
}
|
||||
|
||||
export function unbindIdentity(id: number): Promise<void> {
|
||||
if (mockOn) return mocked(undefined, 300)
|
||||
return request(`/auth/identities/${id}`, { method: 'DELETE' })
|
||||
}
|
||||
|
||||
// ---- OAuth provider 配置(设置页) ----
|
||||
|
||||
export function getOAuthSettings(): Promise<OAuthSettings> {
|
||||
if (mockOn)
|
||||
return mocked({
|
||||
oidcIssuer: '',
|
||||
oidcClientId: '',
|
||||
oidcSecretSet: false,
|
||||
oidcDisplayName: '',
|
||||
oidcDisabled: false,
|
||||
githubClientId: '',
|
||||
githubSecretSet: false,
|
||||
githubDisplayName: '',
|
||||
githubDisabled: false,
|
||||
})
|
||||
return request('/settings/oauth')
|
||||
}
|
||||
|
||||
export function updateOAuthSettings(body: UpdateOAuthRequest): Promise<OAuthSettings> {
|
||||
if (mockOn)
|
||||
return mocked({
|
||||
oidcIssuer: body.oidcIssuer,
|
||||
oidcClientId: body.oidcClientId,
|
||||
oidcSecretSet: !!body.oidcClientSecret,
|
||||
oidcDisplayName: body.oidcDisplayName,
|
||||
oidcDisabled: body.oidcDisabled,
|
||||
githubClientId: body.githubClientId,
|
||||
githubSecretSet: !!body.githubClientSecret,
|
||||
githubDisplayName: body.githubDisplayName,
|
||||
githubDisabled: body.githubDisabled,
|
||||
})
|
||||
return request('/settings/oauth', { method: 'PUT', body })
|
||||
}
|
||||
Reference in New Issue
Block a user