|
| 1 | +import { AccountInfo } from './account-info' |
| 2 | +import { globalCtx } from '@/services/global-ctx' |
| 3 | +import vscode, { authentication, AuthenticationGetSessionOptions, Disposable } from 'vscode' |
| 4 | +import { accountViewDataProvider } from '@/tree-view-providers/account-view-data-provider' |
| 5 | +import { postsDataProvider } from '@/tree-view-providers/posts-data-provider' |
| 6 | +import { postCategoriesDataProvider } from '@/tree-view-providers/post-categories-tree-data-provider' |
| 7 | +import { Oauth } from '@/services/oauth.api' |
| 8 | +import { AuthProvider } from '@/auth/auth-provider' |
| 9 | +import { AuthSession } from '@/auth/auth-session' |
| 10 | +import { BlogExportProvider } from '@/tree-view-providers/blog-export-provider' |
| 11 | +import { AlertService } from '@/services/alert.service' |
| 12 | + |
| 13 | +const isAuthorizedStorageKey = 'isAuthorized' |
| 14 | + |
| 15 | +export const ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED = 'unauthenticated' |
| 16 | +export const ACQUIRE_TOKEN_REJECT_EXPIRED = 'expired' |
| 17 | + |
| 18 | +class AccountManager extends vscode.Disposable { |
| 19 | + private readonly _disposable = Disposable.from( |
| 20 | + AuthProvider.instance.onDidChangeSessions(async ({ added }) => { |
| 21 | + this._session = null |
| 22 | + if (added != null && added.length > 0) await this.ensureSession() |
| 23 | + |
| 24 | + await this.updateAuthStatus() |
| 25 | + |
| 26 | + accountViewDataProvider.fireTreeDataChangedEvent() |
| 27 | + postsDataProvider.fireTreeDataChangedEvent(undefined) |
| 28 | + postCategoriesDataProvider.fireTreeDataChangedEvent() |
| 29 | + |
| 30 | + BlogExportProvider.optionalInstance?.refreshRecords({ force: false, clearCache: true }).catch(console.warn) |
| 31 | + }) |
| 32 | + ) |
| 33 | + |
| 34 | + private _session: AuthSession | null = null |
| 35 | + |
| 36 | + constructor() { |
| 37 | + super(() => { |
| 38 | + this._disposable.dispose() |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + get isAuthorized() { |
| 43 | + return this._session !== null |
| 44 | + } |
| 45 | + |
| 46 | + get currentUser(): AccountInfo { |
| 47 | + return this._session?.account ?? AccountInfo.newAnonymous() |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Acquire the access token. |
| 52 | + * This will reject with a human-readable reason string if not sign-in or the token has expired. |
| 53 | + * @returns The access token of the active session |
| 54 | + */ |
| 55 | + async acquireToken(): Promise<string> { |
| 56 | + const session = await this.ensureSession({ createIfNone: false }) |
| 57 | + |
| 58 | + if (session == null) return Promise.reject(ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED) |
| 59 | + |
| 60 | + if (session.isExpired) return Promise.reject(ACQUIRE_TOKEN_REJECT_EXPIRED) |
| 61 | + |
| 62 | + return session.accessToken |
| 63 | + } |
| 64 | + |
| 65 | + async login() { |
| 66 | + await this.ensureSession({ createIfNone: false, forceNewSession: true }) |
| 67 | + } |
| 68 | + |
| 69 | + async logout() { |
| 70 | + if (!this.isAuthorized) return |
| 71 | + |
| 72 | + const session = await authentication.getSession(AuthProvider.providerId, []) |
| 73 | + |
| 74 | + // WRN: For old version compatibility, **never** remove this line |
| 75 | + await globalCtx.storage.update('user', undefined) |
| 76 | + |
| 77 | + if (session === undefined) return |
| 78 | + |
| 79 | + try { |
| 80 | + await Oauth.revokeToken(session.accessToken) |
| 81 | + await AuthProvider.instance.removeSession(session.id) |
| 82 | + } catch (e: any) { |
| 83 | + void AlertService.err(`登出发生错误: ${e}`) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + async updateAuthStatus() { |
| 88 | + await this.ensureSession({ createIfNone: false }) |
| 89 | + |
| 90 | + await vscode.commands.executeCommand( |
| 91 | + 'setContext', |
| 92 | + `${globalCtx.extName}.${isAuthorizedStorageKey}`, |
| 93 | + this.isAuthorized |
| 94 | + ) |
| 95 | + |
| 96 | + if (this.isAuthorized) { |
| 97 | + await vscode.commands.executeCommand('setContext', `${globalCtx.extName}.user`, { |
| 98 | + name: this.currentUser.name, |
| 99 | + avatar: this.currentUser.avatar, |
| 100 | + }) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private async ensureSession(opt?: AuthenticationGetSessionOptions): Promise<AuthSession | null> { |
| 105 | + const session = await authentication.getSession(AuthProvider.instance.providerId, [], opt).then( |
| 106 | + session => (session ? AuthSession.from(session) : null), |
| 107 | + e => { |
| 108 | + AlertService.err(`创建/获取 Session 失败: ${e}`) |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + if (session != null && session.account.accountId < 0) { |
| 113 | + this._session = null |
| 114 | + await AuthProvider.instance.removeSession(session.id) |
| 115 | + } else { |
| 116 | + this._session = session |
| 117 | + } |
| 118 | + |
| 119 | + return this._session |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export const accountManager = new AccountManager() |
0 commit comments