Skip to content

Commit

Permalink
feat: support AuthenticationForceNewSessionOptions (#4115)
Browse files Browse the repository at this point in the history
  • Loading branch information
bk1012 authored Oct 23, 2024
1 parent 2639b6e commit d5d4dae
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 44 deletions.
7 changes: 0 additions & 7 deletions packages/core-common/src/types/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,3 @@ export interface SessionRequest {
export interface SessionRequestInfo {
[scopes: string]: SessionRequest;
}

export interface AuthenticationGetSessionOptions {
createIfNone: boolean;
clearSessionPreference: boolean;
forceNewSession?: boolean | { detail: string };
silent?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Autowired, INJECTOR_TOKEN, Injectable, Injector } from '@opensumi/di';
import { IRPCProtocol } from '@opensumi/ide-connection';
import { Disposable, ILogger, QuickPickService, formatLocalize, localize } from '@opensumi/ide-core-browser';
import {
AuthenticationGetSessionOptions,
AuthenticationSession,
AuthenticationSessionsChangeEvent,
IAuthenticationProvider,
Expand All @@ -13,6 +12,8 @@ import { IDialogService, IMessageService } from '@opensumi/ide-overlay';
import { ExtHostAPIIdentifier, IExtHostAuthentication, IMainThreadAuthentication } from '../../../common/vscode';
import { IActivationEventService } from '../../types';

import type vscode from 'vscode';

@Injectable({ multiple: true })
export class MainThreadAuthenticationProvider extends Disposable implements IAuthenticationProvider {
@Autowired(IAuthenticationService)
Expand Down Expand Up @@ -248,7 +249,7 @@ export class MainThreadAuthentication extends Disposable implements IMainThreadA
scopes: string[],
extensionId: string,
extensionName: string,
options: AuthenticationGetSessionOptions,
options: vscode.AuthenticationGetSessionOptions,
): Promise<AuthenticationSession | undefined> {
const sessions = await this.authenticationService.getSessions(providerId, scopes, true);
const supportsMultipleAccounts = this.authenticationService.supportsMultipleAccounts(providerId);
Expand Down
98 changes: 63 additions & 35 deletions packages/types/vscode/typings/vscode.authentication.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ declare module 'vscode' {
readonly label: string;
}

/**
* Optional options to be used when calling {@link authentication.getSession} with the flag `forceNewSession`.
*/
export interface AuthenticationForceNewSessionOptions {
/**
* An optional message that will be displayed to the user when we ask to re-authenticate. Providing additional context
* as to why you are asking a user to re-authenticate can help increase the odds that they will accept.
*/
detail?: string;
}

/**
* Options to be used when getting an {@link AuthenticationSession} from an {@link AuthenticationProvider}.
Expand All @@ -60,14 +70,17 @@ declare module 'vscode' {
createIfNone?: boolean;

/**
* Whether we should attempt to reauthenticate even if there is already a session available.
*
* If true, a modal dialog will be shown asking the user to sign in again. This is mostly used for scenarios
* where the token needs to be re minted because it has lost some authorization.
*
* Defaults to false.
*/
forceNewSession?: boolean | { detail: string };
* Whether we should attempt to reauthenticate even if there is already a session available.
*
* If true, a modal dialog will be shown asking the user to sign in again. This is mostly used for scenarios
* where the token needs to be re minted because it has lost some authorization.
*
* If there are no existing sessions and forceNewSession is true, it will behave identically to
* {@link AuthenticationGetSessionOptions.createIfNone createIfNone}.
*
* This defaults to false.
*/
forceNewSession?: boolean | AuthenticationForceNewSessionOptions;


/**
Expand Down Expand Up @@ -202,34 +215,49 @@ declare module 'vscode' {
*/
export namespace authentication {
/**
* Get an authentication session matching the desired scopes. Rejects if a provider with providerId is not
* registered, or if the user does not consent to sharing authentication information with
* the extension. If there are multiple sessions with the same scopes, the user will be shown a
* quickpick to select which account they would like to use.
*
* Currently, there are only two authentication providers that are contributed from built in extensions
* to VS Code that implement GitHub and Microsoft authentication: their providerId's are 'github' and 'microsoft'.
* @param providerId The id of the provider to use
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication provider
* @param options The {@link GetSessionOptions} to use
* @returns A thenable that resolves to an authentication session
*/
export function getSession(providerId: string, scopes: string[], options: AuthenticationGetSessionOptions & { createIfNone: true }): Thenable<AuthenticationSession>;
* Get an authentication session matching the desired scopes. Rejects if a provider with providerId is not
* registered, or if the user does not consent to sharing authentication information with
* the extension. If there are multiple sessions with the same scopes, the user will be shown a
* quickpick to select which account they would like to use.
*
* Currently, there are only two authentication providers that are contributed from built in extensions
* to the editor that implement GitHub and Microsoft authentication: their providerId's are 'github' and 'microsoft'.
* @param providerId The id of the provider to use
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication provider
* @param options The {@link AuthenticationGetSessionOptions} to use
* @returns A thenable that resolves to an authentication session
*/
export function getSession(providerId: string, scopes: readonly string[], options: AuthenticationGetSessionOptions & { /** */createIfNone: true }): Thenable<AuthenticationSession>;

/**
* Get an authentication session matching the desired scopes. Rejects if a provider with providerId is not
* registered, or if the user does not consent to sharing authentication information with
* the extension. If there are multiple sessions with the same scopes, the user will be shown a
* quickpick to select which account they would like to use.
*
* Currently, there are only two authentication providers that are contributed from built in extensions
* to VS Code that implement GitHub and Microsoft authentication: their providerId's are 'github' and 'microsoft'.
* @param providerId The id of the provider to use
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication provider
* @param options The {@link GetSessionOptions} to use
* @returns A thenable that resolves to an authentication session if available, or undefined if there are no sessions
*/
export function getSession(providerId: string, scopes: string[], options?: AuthenticationGetSessionOptions): Thenable<AuthenticationSession | undefined>;
/**
* Get an authentication session matching the desired scopes. Rejects if a provider with providerId is not
* registered, or if the user does not consent to sharing authentication information with
* the extension. If there are multiple sessions with the same scopes, the user will be shown a
* quickpick to select which account they would like to use.
*
* Currently, there are only two authentication providers that are contributed from built in extensions
* to the editor that implement GitHub and Microsoft authentication: their providerId's are 'github' and 'microsoft'.
* @param providerId The id of the provider to use
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication provider
* @param options The {@link AuthenticationGetSessionOptions} to use
* @returns A thenable that resolves to an authentication session
*/
export function getSession(providerId: string, scopes: readonly string[], options: AuthenticationGetSessionOptions & { /** literal-type defines return type */forceNewSession: true | AuthenticationForceNewSessionOptions }): Thenable<AuthenticationSession>;

/**
* Get an authentication session matching the desired scopes. Rejects if a provider with providerId is not
* registered, or if the user does not consent to sharing authentication information with
* the extension. If there are multiple sessions with the same scopes, the user will be shown a
* quickpick to select which account they would like to use.
*
* Currently, there are only two authentication providers that are contributed from built in extensions
* to the editor that implement GitHub and Microsoft authentication: their providerId's are 'github' and 'microsoft'.
* @param providerId The id of the provider to use
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication provider
* @param options The {@link AuthenticationGetSessionOptions} to use
* @returns A thenable that resolves to an authentication session if available, or undefined if there are no sessions
*/
export function getSession(providerId: string, scopes: readonly string[], options?: AuthenticationGetSessionOptions): Thenable<AuthenticationSession | undefined>;

/**
* An {@link Event} which fires when the authentication sessions of an authentication provider have
Expand Down

0 comments on commit d5d4dae

Please sign in to comment.