Skip to content

Commit

Permalink
Add PAT as an Authentication Method (#83)
Browse files Browse the repository at this point in the history
* Remove unused authentication configuration file

* Add support for Azure DevOps Personal Access Token (PAT) authentication
  • Loading branch information
lczerniawski authored Jan 8, 2024
1 parent 23928f2 commit 2f4aacd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 63 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Learn all about different features of the extension in the [wiki](https://github
- _default_: mergebase
- _Description_: The commit to use to get diff against the PR branch's HEAD. Read more about different options in [wiki](https://github.com/ankitbko/vscode-pull-request-azdo/wiki/Diff-Options-HEAD-vs-Merge-Base)

#### azdoPullRequests.patToken
- _type_: string
- _required_: false
- _default_: ""
- _Description_: The PAT Token from Azure DevOps, if provided extension will not prompt for Microsoft login instead it will login using provided PAT

## Known Major Issues

1. Mentions in comments are not resolved to user and no hover support
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"azdoPullRequests.orgUrl": {
"type": "string"
},
"azdoPullRequests.patToken": {
"type": "string",
"default": "",
"description": "Personal Access Token for Azure Devops"
},
"azdoPullRequests.telemetry.enabled": {
"type": "boolean",
"default": true,
Expand Down
45 changes: 0 additions & 45 deletions src/authentication/configuration.ts

This file was deleted.

47 changes: 29 additions & 18 deletions src/azdo/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Logger from '../common/logger';
import { ITelemetry } from '../common/telemetry';
import { SETTINGS_NAMESPACE } from '../constants';

const CREDENTIALS_COMPONENT_ID = 'azdo_component';
const PROJECT_SETTINGS = 'projectName';
const ORGURL_SETTINGS = 'orgUrl';
const PATTOKEN_SETTINGS = 'patToken';
const TRY_AGAIN = vscode.l10n.t('Try again?');
const CANCEL = vscode.l10n.t('Cancel');
const ERROR = vscode.l10n.t('Error signing in to Azure DevOps');
Expand All @@ -18,9 +18,12 @@ export class Azdo {
public connection: azdev.WebApi;
public authenticatedUser: Identity | undefined;

constructor(public orgUrl: string, public projectName: string, token: string) {
this._authHandler = azdev.getBearerHandler(token, true);
// this._authHandler = azdev.getPersonalAccessTokenHandler(token, true);
constructor(public orgUrl: string, public projectName: string, token: string, isPatTokenAuth: boolean = false) {
if (isPatTokenAuth) {
this._authHandler = azdev.getPersonalAccessTokenHandler(token, true);
} else {
this._authHandler = azdev.getBearerHandler(token, true);
}
this.connection = this.getNewWebApiClient(this.orgUrl);
}

Expand Down Expand Up @@ -107,22 +110,30 @@ export class CredentialStore implements vscode.Disposable {
while (retry) {
try
{
const session = await this.getSession(this._sessionOptions);
if (!session) {
Logger.appendLine('Auth> Unable to get session', CredentialStore.ID);
this._telemetry.sendTelemetryEvent('auth.failed');
return undefined;
}
this._sessionId = session.id;
const token = await this.getToken(session);

if (!token) {
Logger.appendLine('Auth> Unable to get token', CredentialStore.ID);
this._telemetry.sendTelemetryEvent('auth.failed');
return undefined;
let isPatTokenAuth = true;
let token = vscode.workspace.getConfiguration(SETTINGS_NAMESPACE).get<string | undefined>(PATTOKEN_SETTINGS);

if(token === undefined || token === null || token === '') {
const session = await this.getSession(this._sessionOptions);
if (!session) {
Logger.appendLine('Auth> Unable to get session', CredentialStore.ID);
this._telemetry.sendTelemetryEvent('auth.failed');
return undefined;
}

this._sessionId = session.id;
token = await this.getToken(session);

if (!token) {
Logger.appendLine('Auth> Unable to get token', CredentialStore.ID);
this._telemetry.sendTelemetryEvent('auth.failed');
return undefined;
}

isPatTokenAuth = false;
}

const azdo = new Azdo(this._orgUrl, projectName, token);
const azdo = new Azdo(this._orgUrl, projectName, token, isPatTokenAuth);
azdo.authenticatedUser = (await azdo.connection.connect()).authenticatedUser;

Logger.debug(`Auth> Successful: Logged userid: ${azdo?.authenticatedUser?.id}`, CredentialStore.ID);
Expand Down

0 comments on commit 2f4aacd

Please sign in to comment.