Skip to content
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 GitHub authentication provider #91309

Closed
3 tasks done
RMacfarlane opened this issue Feb 24, 2020 · 2 comments
Closed
3 tasks done

Test GitHub authentication provider #91309

RMacfarlane opened this issue Feb 24, 2020 · 2 comments

Comments

@RMacfarlane
Copy link
Contributor

RMacfarlane commented Feb 24, 2020

Refs: #90384

Complexity: 3

A built in GitHub authentication provider is now in core. This is not yet being used by anything else in core, but is available to other extensions. To test, please use the new Authentication Provider API in an extension:

// #region auth provider: https://github.com/microsoft/vscode/issues/88309
export interface AuthenticationSession {
id: string;
accessToken(): Promise<string>;
accountName: string;
scopes: string[]
}
/**
* An [event](#Event) which fires when an [AuthenticationProvider](#AuthenticationProvider) is added or removed.
*/
export interface AuthenticationProvidersChangeEvent {
/**
* The ids of the [authenticationProvider](#AuthenticationProvider)s that have been added.
*/
readonly added: string[];
/**
* The ids of the [authenticationProvider](#AuthenticationProvider)s that have been removed..
*/
readonly removed: string[];
}
export interface AuthenticationProvider {
/**
* Used as an identifier for extensions trying to work with a particular
* provider: 'Microsoft', 'GitHub', etc. id must be unique, registering
* another provider with the same id will fail.
*/
readonly id: string;
readonly displayName: string;
/**
* A [enent](#Event) which fires when the array of sessions has changed, or data
* within a session has changed.
*/
readonly onDidChangeSessions: Event<void>;
/**
* Returns an array of current sessions.
*/
getSessions(): Promise<ReadonlyArray<AuthenticationSession>>;
/**
* Prompts a user to login.
*/
login(scopes: string[]): Promise<AuthenticationSession>;
logout(sessionId: string): Promise<void>;
}
export namespace authentication {
export function registerAuthenticationProvider(provider: AuthenticationProvider): Disposable;
/**
* Fires with the provider id that was registered or unregistered.
*/
export const onDidChangeAuthenticationProviders: Event<AuthenticationProvidersChangeEvent>;
export const providers: ReadonlyArray<AuthenticationProvider>;
}

  • Are you able to find the provider in vscode.authentication.providers
  • When calling login, is a consent dialog shown
  • When fetching the value of accessToken on a session, is a consent dialog shown
  • Are you able to complete GitHub auth and get an access token

Here is some example code you can use to get started:

async function getCachedSesssionOrLogin(provider: vscode.AuthenticationProvider) {
	const sessions = await provider.getSessions();
	if (sessions.length) {
		const token = await sessions[0].accessToken();
		console.log(token);
	} else {
		const token = await provider.login(['user:email']);
		console.log(token);
	}	
}

const provider = vscode.authentication.providers.find( provider => provider.id === 'GitHub');
if (provider) {
	getCachedSesssionOrLogin(provider);
}

vscode.authentication.onDidChangeAuthenticationProviders(async changed => {
	if (changed.added.includes('GitHub')) {
		const provider = vscode.authentication.providers.find(provider => provider.id === 'GitHub');
		if (provider) {
			getCachedSesssionOrLogin(provider);
		}
	}
});

Documentation on the available scopes that can be used with login is available here: https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/

@kieferrm
Copy link
Member

I have two items that I will file:

  • the open URL dialog after login
  • that I have to OK the access to the token for the extension that logged in

@kieferrm kieferrm removed their assignment Feb 27, 2020
@KamasamaK
Copy link

@kieferrm Have you filed those issues yet? I can't find them.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants