Skip to content

Commit

Permalink
Change GK account authentication to no longer use the VS Code authent…
Browse files Browse the repository at this point in the history
…ication system (#3524)

* Stop registering the authentication provider in VS Code authentication.
(#3524)

* Getting/creating a session honors `createIfNeeded` flag
(#3524)
  • Loading branch information
sergeibbb authored Sep 11, 2024
1 parent 77462fc commit cf6766d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/plus/gk/account/authenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
AuthenticationProviderAuthenticationSessionsChangeEvent,
AuthenticationSession,
} from 'vscode';
import { authentication, Disposable, EventEmitter, window } from 'vscode';
import { Disposable, EventEmitter, window } from 'vscode';
import { uuid } from '@env/crypto';
import type { Container, Environment } from '../../../container';
import { CancellationError } from '../../../errors';
Expand All @@ -27,7 +27,6 @@ interface StoredSession {

export const authenticationProviderId = 'gitlens+';
export const authenticationProviderScopes = ['gitlens'];
const authenticationLabel = 'GitKraken: GitLens';

export interface AuthenticationProviderOptions {
signUp?: boolean;
Expand Down Expand Up @@ -57,9 +56,6 @@ export class AccountAuthenticationProvider implements AuthenticationProvider, Di

this._disposable = Disposable.from(
this._authConnection,
authentication.registerAuthenticationProvider(authenticationProviderId, authenticationLabel, this, {
supportsMultipleAccounts: false,
}),
this.container.storage.onDidChangeSecrets(() => this.checkForUpdates()),
);
}
Expand Down Expand Up @@ -234,6 +230,20 @@ export class AccountAuthenticationProvider implements AuthenticationProvider, Di
}
}

public async getOrCreateSession(
scopes: string[],
createIfNeeded: boolean,
): Promise<AuthenticationSession | undefined> {
const session = (await this.getSessions(scopes))[0];
if (session != null) {
return session;
}
if (!createIfNeeded) {
return undefined;
}
return this.createSession(scopes);
}

private async createSessionForToken(token: string, scopes: string[]): Promise<AuthenticationSession> {
const userInfo = await this._authConnection.getAccountInfo(token);
return {
Expand Down
11 changes: 5 additions & 6 deletions src/plus/gk/account/subscriptionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
StatusBarItem,
} from 'vscode';
import {
authentication,
CancellationTokenSource,
version as codeVersion,
Disposable,
Expand Down Expand Up @@ -50,7 +49,7 @@ import { getSubscriptionFromCheckIn } from '../checkin';
import type { ServerConnection } from '../serverConnection';
import { ensurePlusFeaturesEnabled } from '../utils';
import { AuthenticationContext } from './authenticationConnection';
import { authenticationProviderId, authenticationProviderScopes } from './authenticationProvider';
import { authenticationProviderScopes } from './authenticationProvider';
import type { Organization } from './organization';
import { getApplicablePromo } from './promos';
import type { Subscription } from './subscription';
Expand Down Expand Up @@ -1053,10 +1052,10 @@ export class SubscriptionService implements Disposable {
if (options != null && createIfNeeded) {
this.container.accountAuthentication.setOptionsForScopes(authenticationProviderScopes, options);
}
session = await authentication.getSession(authenticationProviderId, authenticationProviderScopes, {
createIfNone: createIfNeeded,
silent: !createIfNeeded,
});
session = await this.container.accountAuthentication.getOrCreateSession(
authenticationProviderScopes,
createIfNeeded,
);
} catch (ex) {
session = null;
if (options != null && createIfNeeded) {
Expand Down

0 comments on commit cf6766d

Please sign in to comment.