-
Notifications
You must be signed in to change notification settings - Fork 28.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test: Finalized getAccounts
API & AuthenticationProvider
changes
#226540
Comments
Unlike stated in the description above, createSession did not get called when getSessions returned an empty array. I don't necessarily think it should, but am not sure if that's an issue with the description above or my understanding: // 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 sessions = await vscode.authentication.getSession('multi-account-test', [], {});
console.log('sessions', sessions);
});
context.subscriptions.push(disposable);
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 [];
},
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> => {
return;
},
}, { supportsMultipleAccounts: true });
}
// This method is called when your extension is deactivated
export function deactivate() {} |
Not getting a session here, please clarify: // 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 sessions: vscode.AuthenticationSession[] = [
{
id: 'test-id-' + crypto.randomUUID(),
accessToken: 'test-token-' + crypto.randomUUID(),
account: { id: 'test-account-id-1', label: 'Test Account 1' },
scopes: ['scope1'],
}
];
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;
},
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> => {
return;
},
}, { supportsMultipleAccounts: true });
}
// This method is called when your extension is deactivated
export function deactivate() {} |
@chrmarti your call to:
Will only show a badge on the Account menu. When you click on it |
Got it, that helped, thanks. |
Left a few more notes in #226783. How would an auth provider indicate that it knows one or several accounts, but has no session to them yet? This doesn't seem possible, is that an intentional limitation? |
@chrmarti The intent is that you should have a session. I suppose one way this could be done is by having a session with scopes |
@roblourens can you see @chrmarti's latest notes in #226783 and see if maybe you were seeing the same (it was his implementation, not the platform)? |
Refs: #152399
Complexity: 5
Create Issue
This iteration we've finalized the
getAccounts
API that was previously tested here.An "Account" is basically an identity, without access to anything. It just says who you are... while a "Session", on the other hand, has an
accessToken
that can access whatever scopes (or permissions) were requested. This also means that the concept ofscopes
doesn't matter to an "Account"... and scopes only matter to "session".Put plainly, 1 account can have multiple sessions tied to it.
Next context! An
AuthenticationProvider
can be marked as supporting only 1 account or multiple accounts at a time. For this TPI, it's really only applicable when you support multiple accounts so make sure you do that.Alright, enough chat... let's test.
Prereqs
npx @vscode/dts main
to get the latest API fileImplement an Authentication Provider
You'll want to:
authentication
contribution section of the package.jsonYou can absolutely attempt to implement a full auth provider if you'd like... I'm not gonna stop you :) Here's what my Spotify one looks like (but it's not multi-account).
But it's not necessary. Having
getSessions
andcreateSession
return dummy data is totally fine for this TPI. Just make sure you test having it:createSession
to run if you pass in thecreateIfNone
flag)The important part is having
getSessions
andcreateSession
take in the new:and do something with it. This property will be used to filter down the list of sessions we have to only the account passed in or used to create a session specific to that account.
Once you have an Auth Provider, it's time to use it.
Implement something that uses the new
getAccounts
& additions togetSession
APINow time for the consumer! (you can do this in the same extension via a Command or something)
Play around with the
vscode.authentication.getAccounts
API which should return all accounts that you are logged in to.Additionally, play around with passing an account returned by that API into
vscode.authentication.getSession
via the options.The text was updated successfully, but these errors were encountered: