microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.5k stars 28.65k forks source link

Cannot distinguish sessions in picker #226781

Closed chrmarti closed 1 week ago

chrmarti commented 3 weeks ago

Testing #226540

After clicking: Image

I see: Image

Where I can't distinguish the sessions. Code:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as crypto from 'crypto';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "multi-account-test" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with registerCommand
    // The commandId parameter must match the command field in package.json
    const disposable = vscode.commands.registerCommand('multi-account-test.helloWorld', async () => {
        // The code you place here will be executed every time your command is executed
        // Display a message box to the user
        vscode.window.showInformationMessage('Hello World from multi-account-test!');

        const accounts = await vscode.authentication.getAccounts('multi-account-test');
        console.log('accounts', accounts);
        const session = await vscode.authentication.getSession('multi-account-test', ['scope1'], { account: accounts[0] });
        console.log('session', session);
    });

    context.subscriptions.push(disposable);

    const account1 = { id: 'test-account-id-1', label: 'Test Account 1' };
    const account2 = { id: 'test-account-id-2', label: 'Test Account 2' };
    let sessions: vscode.AuthenticationSession[] = [
        {
            id: 'test-id-' + crypto.randomUUID(),
            accessToken: 'test-token-' + crypto.randomUUID(),
            account: account1,
            scopes: ['scope1'],
        },
        {
            id: 'test-id-' + crypto.randomUUID(),
            accessToken: 'test-token-' + crypto.randomUUID(),
            account: account1,
            scopes: ['scope2'],
        },
        {
            id: 'test-id-' + crypto.randomUUID(),
            accessToken: 'test-token-' + crypto.randomUUID(),
            account: account2,
            scopes: ['scope1'],
        },
        {
            id: 'test-id-' + crypto.randomUUID(),
            accessToken: 'test-token-' + crypto.randomUUID(),
            account: account2,
            scopes: ['scope2'],
        },
    ];

    const emitter = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
    vscode.authentication.registerAuthenticationProvider('multi-account-test', 'Multi Account Test', {
        onDidChangeSessions: emitter.event,
        getSessions: async (scopes: readonly string[] | undefined, options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession[]> => {
            return sessions.slice();
        },
        createSession: async (scopes: readonly string[], options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession> => {
            return {
                id: 'test-id-' + crypto.randomUUID(),
                accessToken: 'test-token-' + crypto.randomUUID(),
                account: options.account || { id: 'test-account-id', label: 'Test Account' },
                scopes: scopes,
            };
        },
        removeSession: async (sessionId: string): Promise<void> => {
            const removed = sessions.filter(session => session.id === sessionId);
            sessions = sessions.filter(session => session.id !== sessionId);
            emitter.fire({ added: [], removed, changed: [] });
        },
    }, { supportsMultipleAccounts: true });
}

// This method is called when your extension is deactivated
export function deactivate() {}
chrmarti commented 3 weeks ago

It appears that this gave permission to use all sessions of the picked account. So maybe the accounts just need to be deduplicated in the picker?