|
| 1 | +import { CnblogsAccountInformation } from './account-information'; |
| 2 | +import { globalContext } from '../services/global-state'; |
| 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 { OauthApi } from '@/services/oauth.api'; |
| 8 | +import { CnblogsAuthenticationProvider } from '@/authentication/authentication-provider'; |
| 9 | +import { CnblogsAuthenticationSession } from '@/authentication/session'; |
| 10 | + |
| 11 | +const isAuthorizedStorageKey = 'isAuthorized'; |
| 12 | + |
| 13 | +class AccountManager extends vscode.Disposable { |
| 14 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 15 | + static readonly ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED = 'unauthenticated'; |
| 16 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 17 | + static readonly ACQUIRE_TOKEN_REJECT_EXPIRED = 'expired'; |
| 18 | + |
| 19 | + private readonly _authenticationProvider: CnblogsAuthenticationProvider; |
| 20 | + private readonly _disposable: vscode.Disposable; |
| 21 | + |
| 22 | + private _oauthClient?: OauthApi | null; |
| 23 | + private _session?: CnblogsAuthenticationSession | null; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + super(() => { |
| 27 | + this._disposable.dispose(); |
| 28 | + }); |
| 29 | + |
| 30 | + this._disposable = Disposable.from( |
| 31 | + (this._authenticationProvider = CnblogsAuthenticationProvider.instance), |
| 32 | + this._authenticationProvider.onDidChangeSessions(async ({ added }) => { |
| 33 | + this._session = null; |
| 34 | + if (added != null && added.length > 0) await this.ensureSession(); |
| 35 | + |
| 36 | + await this.updateAuthorizationStatus(); |
| 37 | + |
| 38 | + accountViewDataProvider.fireTreeDataChangedEvent(); |
| 39 | + postsDataProvider.fireTreeDataChangedEvent(undefined); |
| 40 | + postCategoriesDataProvider.fireTreeDataChangedEvent(); |
| 41 | + }) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + get isAuthorized() { |
| 46 | + return this._session != null; |
| 47 | + } |
| 48 | + |
| 49 | + get curUser(): CnblogsAccountInformation { |
| 50 | + return this._session?.account ?? CnblogsAccountInformation.parse(); |
| 51 | + } |
| 52 | + |
| 53 | + protected get oauthClient() { |
| 54 | + return (this._oauthClient ??= new OauthApi()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Acquire the access token. |
| 59 | + * This will reject with a human-readable reason string if not sign-in or the token has expired. |
| 60 | + * @returns The access token of the active session |
| 61 | + */ |
| 62 | + async acquireToken(): Promise<string> { |
| 63 | + const session = await this.ensureSession({ createIfNone: false }); |
| 64 | + return session == null |
| 65 | + ? Promise.reject(AccountManager.ACQUIRE_TOKEN_REJECT_UNAUTHENTICATED) |
| 66 | + : session.hasExpired |
| 67 | + ? Promise.reject(AccountManager.ACQUIRE_TOKEN_REJECT_EXPIRED) |
| 68 | + : session.accessToken; |
| 69 | + } |
| 70 | + |
| 71 | + async login() { |
| 72 | + await this.ensureSession({ createIfNone: true, forceNewSession: false }); |
| 73 | + } |
| 74 | + |
| 75 | + async logout() { |
| 76 | + if (!this.isAuthorized) return; |
| 77 | + |
| 78 | + const session = await authentication.getSession(CnblogsAuthenticationProvider.providerId, []); |
| 79 | + if (session) await this._authenticationProvider.removeSession(session.id); |
| 80 | + |
| 81 | + // For old version compatibility, **never** remove this line |
| 82 | + await globalContext.storage.update('user', undefined); |
| 83 | + |
| 84 | + if (session) { |
| 85 | + return this.oauthClient |
| 86 | + .revoke(session.accessToken) |
| 87 | + .catch(console.warn) |
| 88 | + .then(ok => (!ok ? console.warn('Revocation failed') : undefined)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + setup() { |
| 93 | + this.updateAuthorizationStatus().catch(console.warn); |
| 94 | + } |
| 95 | + |
| 96 | + private async updateAuthorizationStatus() { |
| 97 | + await this.ensureSession({ createIfNone: false }); |
| 98 | + await vscode.commands.executeCommand( |
| 99 | + 'setContext', |
| 100 | + `${globalContext.extensionName}.${isAuthorizedStorageKey}`, |
| 101 | + this.isAuthorized |
| 102 | + ); |
| 103 | + if (this.isAuthorized) { |
| 104 | + await vscode.commands.executeCommand('setContext', `${globalContext.extensionName}.user`, { |
| 105 | + name: this.curUser.name, |
| 106 | + avatar: this.curUser.avatar, |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private async ensureSession( |
| 112 | + opt?: AuthenticationGetSessionOptions |
| 113 | + ): Promise<CnblogsAuthenticationSession | undefined | null> { |
| 114 | + const session = await authentication.getSession(this._authenticationProvider.providerId, [], opt).then( |
| 115 | + s => (s ? CnblogsAuthenticationSession.parse(s) : null), |
| 116 | + () => null |
| 117 | + ); |
| 118 | + |
| 119 | + if (session != null && session.account.accountId < 0) { |
| 120 | + this._session = null; |
| 121 | + await this._authenticationProvider.removeSession(session.id); |
| 122 | + } else { |
| 123 | + this._session = session; |
| 124 | + } |
| 125 | + |
| 126 | + return this._session ?? CnblogsAuthenticationSession.parse(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export const accountManager = new AccountManager(); |
| 131 | +export default accountManager; |
0 commit comments