Skip to content

Commit

Permalink
Handle errors at the Device Authentication flow
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
  • Loading branch information
RomanNikitenko committed Oct 25, 2023
1 parent e74d10e commit dd0bd2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion code/extensions/che-api/src/impl/github-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Logger } from '../logger';
import { K8SServiceImpl } from './k8s-service-impl';
import { base64Decode, base64Encode, createLabelsSelector, randomString } from './utils';

export const GIT_CREDENTIALS_PATH = path.resolve('/.git-credentials', 'credentials');
const GIT_CREDENTIALS_PATH = path.resolve('/.git-credentials', 'credentials');
const GIT_CREDENTIAL_LABEL = {
'controller.devfile.io/git-credential': 'true'
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { inject, injectable } from 'inversify';
import * as vscode from 'vscode';
import { ExtensionContext } from './extension-context';
import { GitHubAuthProvider, GithubService } from './github';
import { Logger } from './logger';
import { CHANNEL_NAME, Logger } from './logger';

@injectable()
export class DeviceAuthentication {
Expand All @@ -35,7 +35,7 @@ export class DeviceAuthentication {
this.logger.info('Remove Device Authentication Token command has been registered');
}

async trigger(scopes = 'user:email'): Promise<string> {
async trigger(scopes = 'user:email'): Promise<string | undefined> {
this.logger.info(`Device Authentication is triggered for scopes: ${scopes}`);

const sessionsToRemove = await this.gitHubAuthProvider.getSessions([scopes]);
Expand All @@ -57,10 +57,20 @@ export class DeviceAuthentication {
const token = await vscode.commands.executeCommand<string>('github-authentication.device-code-flow');
this.logger.info(`Device Authentication: token for scopes: ${scopes} has been generated successfully`);

await this.githubService.persistDeviceAuthToken(token);
await this.gitHubAuthProvider.createSession([scopes]);
this.onTokenGenerated(scopes);
return token;
try {
await this.githubService.persistDeviceAuthToken(token);
await this.gitHubAuthProvider.createSession([scopes]);
this.onTokenGenerated(scopes);

return token;
} catch (error) {
const message = 'An error has occurred at the Device Authentication flow';

this.logger.error(`${message}: ${error.message}`);
vscode.window.showErrorMessage(`${message}, details are available in the ${CHANNEL_NAME} output channel.`);

return undefined;
}
}

async removeDeviceAuthToken(): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion code/extensions/che-github-authentication/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import { injectable } from 'inversify';
import * as vscode from 'vscode';

export const CHANNEL_NAME = 'Che GitHub Authentication';

@injectable()
export class Logger {
private outputChannel: vscode.LogOutputChannel;

constructor() {
this.outputChannel = vscode.window.createOutputChannel('Che GitHub Authentication', { log: true });
this.outputChannel = vscode.window.createOutputChannel(CHANNEL_NAME, { log: true });
}

info(message: string): void {
Expand Down

0 comments on commit dd0bd2d

Please sign in to comment.