Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Support more than one authentication session (#906)
Browse files Browse the repository at this point in the history
Handle multiple GitHub authentication sessions.
Add back the vscode built-in github plugin to be able to publish https repositories.
  • Loading branch information
vinokurig authored Nov 9, 2020
1 parent 8cc01f3 commit ca076c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions generator/src/templates/theiaPlugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"vscode-builtin-yaml": "https://open-vsx.org/api/vscode/yaml/1.44.2/file/vscode.yaml-1.44.2.vsix",
"vscode-eslint": "https://open-vsx.org/api/dbaeumer/vscode-eslint/2.1.1/file/dbaeumer.vscode-eslint-2.1.1.vsix",
"vscode-git": "https://open-vsx.org/api/vscode/git/1.49.3/file/vscode.git-1.49.3.vsix",
"vscode-github": "https://open-vsx.org/api/vscode/github/1.50.1/file/vscode.github-1.50.1.vsix",
"vscode-references-view": "https://open-vsx.org/api/ms-vscode/references-view/0.0.47/file/ms-vscode.references-view-0.0.47.vsix",
"vscode-builtin-icon-theme-seti": "https://open-vsx.org/api/vscode/vscode-theme-seti/1.44.2/file/vscode.vscode-theme-seti-1.44.2.vsix",
"vscode-builtin-theme-abyss": "https://open-vsx.org/api/vscode/theme-abyss/1.44.2/file/vscode.theme-abyss-1.44.2.vsix",
Expand Down
3 changes: 3 additions & 0 deletions plugins/github-auth-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"activationEvents": [
"*"
],
"dependencies": {
"uuid": "^3.1.0"
},
"devDependencies": {
"@theia/plugin": "next",
"@theia/plugin-packager": "latest",
Expand Down
46 changes: 25 additions & 21 deletions plugins/github-auth-plugin/src/github-auth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,49 @@

import * as theia from '@theia/plugin';
import * as che from '@eclipse-che/plugin';
import { v4 } from 'uuid';

export async function start(context: theia.PluginContext): Promise<void> {
if (theia.plugins.getPlugin('github.vscode-pull-request-github')) {
let session: theia.AuthenticationSession | undefined = context.workspaceState.get('session');
const onDidChangeSessions = new theia.EventEmitter<theia.AuthenticationProviderAuthenticationSessionsChangeEvent>();
theia.authentication.registerAuthenticationProvider({
const sessions: theia.AuthenticationSession[] = context.workspaceState.get('sessions') || [];
const onDidChangeSessions = new theia.EventEmitter<theia.AuthenticationProviderAuthenticationSessionsChangeEvent>();
theia.authentication.registerAuthenticationProvider({
id: 'github',
label: 'GitHub',
supportsMultipleAccounts: false,
onDidChangeSessions: onDidChangeSessions.event,
getSessions: async () => {
if (session) {
return [session];
} else {
return [];
}
},
getSessions: async () => sessions,
login: async (scopes: string[]) => {
const githubUser = await che.github.getUser();
session = {
id: 'github-session',
const session = {
id: v4(),
accessToken: await che.github.getToken(),
account: { label: githubUser.login, id: githubUser.id.toString() },
scopes
};
context.workspaceState.update('session', session);
const sessionIndex = sessions.findIndex(s => s.id === session.id);
if (sessionIndex > -1) {
sessions.splice(sessionIndex, 1, session);
} else {
sessions.push(session);
}
context.workspaceState.update('sessions', sessions);
onDidChangeSessions.fire({ added: [session.id], removed: [], changed: [] });
return session;
},
logout: async (id: string) => {
session = undefined;
context.workspaceState.update('session', session);
onDidChangeSessions.fire({ added: [], removed: [id], changed: [] });
const sessionIndex = sessions.findIndex(session => session.id === id);
if (sessionIndex > -1) {
sessions.splice(sessionIndex, 1);
context.workspaceState.update('sessions', sessions);
onDidChangeSessions.fire({ added: [], removed: [id], changed: [] });
}
}
}
);
if (session) {
onDidChangeSessions.fire({ added: [session.id], removed: [], changed: [] });
// TODO Remove the notification when https://github.com/eclipse-theia/theia/issues/7178 is fixed.
);
if (theia.plugins.getPlugin('github.vscode-pull-request-github')) {
if (sessions.length > 0) {
onDidChangeSessions.fire({ added: sessions.map(s => s.id), removed: [], changed: [] });
// TODO Remove the notification when https://github.com/eclipse-theia/theia/issues/7178 is fixed.
} else {
const signIn = 'Sign in';
const result = await theia.window.showInformationMessage(
Expand Down

0 comments on commit ca076c6

Please sign in to comment.