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

Add docs notification if GitHub oAuth is not setup #969

Merged
merged 4 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ export class CheGithubMainImpl implements CheGithubMain {

private async updateToken(): Promise<void> {
const oAuthProvider = 'github';
try {
const authenticateAndUpdateToken: () => Promise<void> = async () => {
await this.oAuthUtils.authenticate(oAuthProvider, ['repo', 'user', 'write:public_key']);
this.token = await this.oAuthUtils.getToken(oAuthProvider);
// Validate the GitHub token.
await this.getUser();
} catch (e) {
if (e.message.indexOf('Request failed with status code 401') !== -1) {
await this.oAuthUtils.authenticate(oAuthProvider, ['write:public_key']);
this.token = await this.oAuthUtils.getToken(oAuthProvider);
};
if (await this.oAuthUtils.isAuthenticated(oAuthProvider)) {
try {
// Validate the GitHub token.
await this.getUser();
} catch (e) {
if (/Request failed with status code 401/g.test(e.message)) {
await authenticateAndUpdateToken();
}
}
} else {
await authenticateAndUpdateToken();
}
}
}
60 changes: 37 additions & 23 deletions extensions/eclipse-che-theia-remote-api/src/browser/oauth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { inject, injectable } from 'inversify';

import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { OAuthService } from '../common/oauth-service';
import { UserService } from '../common/user-service';

@injectable()
export class OauthUtils {
Expand All @@ -27,7 +28,8 @@ export class OauthUtils {

constructor(
@inject(EnvVariablesServer) private readonly envVariableServer: EnvVariablesServer,
@inject(OAuthService) private readonly oAuthService: OAuthService
@inject(OAuthService) private readonly oAuthService: OAuthService,
@inject(UserService) private readonly userService: UserService
) {
const onDidReceiveTokenEmitter = new Emitter<void>();
this.onDidReceiveToken = onDidReceiveTokenEmitter.event;
Expand Down Expand Up @@ -57,29 +59,41 @@ export class OauthUtils {
}

async getUserToken(): Promise<string | undefined> {
if (this.userToken) {
return this.userToken;
} else if (this.machineToken && this.machineToken.length > 0) {
const timer = setTimeout(() => {
this.messageService.warn(
'Authentication is taking too long, the oauth pop-up may be blocked by your browser, ' +
'if so, allow popup windows for the current url and restart the workspace'
const updateToken: () => Promise<string | undefined> = async () => {
if (this.machineToken && this.machineToken.length > 0) {
const timer = setTimeout(() => {
this.messageService.warn(
'Authentication is taking too long, the OAuth pop-up may be blocked by your browser, ' +
'if so, allow pop-up windows for the current url and restart the workspace'
);
}, 10000);
const popup = window.open(
`${this.apiUrl.substring(0, this.apiUrl.indexOf('/api'))}/_app/oauth.html`,
'popup',
'toolbar=no, status=no, menubar=no, scrollbars=no, width=10, height=10, visible=none'
);
}, 10000);
const popup = window.open(
`${this.apiUrl.substring(0, this.apiUrl.indexOf('/api'))}/_app/oauth.html`,
'popup',
'toolbar=no, status=no, menubar=no, scrollbars=no, width=10, height=10, visible=none'
);
if (popup) {
this.oAuthPopup = popup;
}
return new Promise(async resolve => {
this.onDidReceiveToken(() => {
clearTimeout(timer);
resolve(this.userToken);
if (popup) {
this.oAuthPopup = popup;
}
return new Promise(async resolve => {
this.onDidReceiveToken(() => {
clearTimeout(timer);
resolve(this.userToken);
});
});
});
}
};
if (this.userToken) {
try {
await this.userService.getCurrentUser(this.userToken);
} catch (e) {
if (/Request getCurrentUser failed with message: "401"/g.test(e.message)) {
return updateToken();
}
}
return this.userToken;
} else {
return updateToken();
}
}

Expand All @@ -105,7 +119,7 @@ export class OauthUtils {
await this.oAuthService.getOAuthToken(provider, await this.getUserToken());
return true;
} catch (e) {
return e.message.indexOf('Request failed with status code 401') > 0;
return new RegExp(`User \\[.*] is not associated with identity provider \\[${provider}]\\.`).test(e.message);
}
}

Expand Down
30 changes: 14 additions & 16 deletions plugins/github-auth-plugin/src/github-auth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export async function start(context: theia.PluginContext): Promise<void> {
onDidChangeSessions: onDidChangeSessions.event,
getSessions: async () => sessions,
login: async (scopes: string[]) => {
if (!(await che.oAuth.isRegistered('github'))) {
if (
await theia.window.showWarningMessage(
'Che could not authenticate to your Github account. The setup for Github OAuth provider is not complete.',
'Setup instructions'
)
) {
theia.commands.executeCommand(
'theia.open',
'https://www.eclipse.org/che/docs/che-7/administration-guide/configuring-authorization/#configuring-github-oauth_che'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the correct link for the downstream as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the eclipse docs page is relevant for downstream as well.

);
}
return;
}
const githubUser = await che.github.getUser();
const session = {
id: v4(),
Expand All @@ -49,22 +63,6 @@ export async function start(context: theia.PluginContext): Promise<void> {
}
},
});
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(
'In order to use the Pull Requests functionality, you must sign in to GitHub',
signIn
);

if (result === signIn) {
theia.authentication.getSession('github', ['read:user', 'user:email', 'repo'], { createIfNone: true });
}
}
}
}

export function stop(): void {}