Skip to content

Commit

Permalink
Change to use PAT auth for GitLab (microsoft#2689)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwateratmsft authored and Dmarch28 committed Mar 4, 2021
1 parent 9c218a8 commit c26626d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
57 changes: 16 additions & 41 deletions src/tree/registries/gitLab/GitLabAccountTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { RequestPromiseOptions } from "request-promise-native";
import { AzExtParentTreeItem, AzExtTreeItem, IActionContext } from "vscode-azureextensionui";
import { AzExtParentTreeItem, AzExtTreeItem, IActionContext, parseError } from "vscode-azureextensionui";
import { PAGE_SIZE } from "../../../constants";
import { ext } from "../../../extensionVariables";
import { nonNullProp } from "../../../utils/nonNull";
Expand All @@ -23,7 +23,6 @@ export class GitLabAccountTreeItem extends AzExtParentTreeItem implements IRegis
public baseUrl: string = 'https://gitlab.com/';
public cachedProvider: ICachedRegistryProvider;

private _token?: string;
private _nextLink?: string;

public constructor(parent: AzExtParentTreeItem, provider: ICachedRegistryProvider) {
Expand All @@ -49,51 +48,32 @@ export class GitLabAccountTreeItem extends AzExtParentTreeItem implements IRegis
public async loadMoreChildrenImpl(clearCache: boolean, _context: IActionContext): Promise<AzExtTreeItem[]> {
if (clearCache) {
this._nextLink = undefined;
}

try {
await this.refreshToken();
} catch (err) {
// If creds are invalid, the above refreshToken will fail
try {
const url: string = this._nextLink || `api/v4/projects?per_page=${PAGE_SIZE}&simple=true&membership=true`;
let response = await registryRequest<IProject[]>(this, 'GET', url);
this._nextLink = getNextLinkFromHeaders(response);
return this.createTreeItemsWithErrorHandling(
response.body,
'invalidGitLabProject',
n => new GitLabProjectTreeItem(this, n.id.toString(), n.path_with_namespace.toLowerCase()),
n => n.path_with_namespace
);
} catch (err) {
const errorType: string = parseError(err).errorType.toLowerCase();
if (errorType === '401' || errorType === 'unauthorized') {
return [new RegistryConnectErrorTreeItem(this, err, this.cachedProvider)];
}
}

const url: string = this._nextLink || `api/v4/projects?per_page=${PAGE_SIZE}&simple=true&membership=true`;
let response = await registryRequest<IProject[]>(this, 'GET', url);
this._nextLink = getNextLinkFromHeaders(response);
return this.createTreeItemsWithErrorHandling(
response.body,
'invalidGitLabProject',
n => new GitLabProjectTreeItem(this, n.id.toString(), n.path_with_namespace.toLowerCase()),
n => n.path_with_namespace
);
}

public hasMoreChildrenImpl(): boolean {
return !!this._nextLink;
}

public async addAuth(options: RequestPromiseOptions): Promise<void> {
if (this._token) {
options.auth = {
bearer: this._token
}
}
}

private async refreshToken(): Promise<void> {
this._token = undefined;
const options = {
form: {
/* eslint-disable-next-line camelcase */
grant_type: "password",
username: this.username,
password: await this.getPassword()
}
};

const response = await registryRequest<IToken>(this, 'POST', 'oauth/token', options);
this._token = response.body.access_token;
options.headers['PRIVATE-TOKEN'] = await this.getPassword();
}
}

Expand All @@ -102,8 +82,3 @@ interface IProject {
/* eslint-disable-next-line camelcase */
path_with_namespace: string;
}

interface IToken {
/* eslint-disable-next-line camelcase */
access_token: string
}
1 change: 1 addition & 0 deletions src/tree/registries/gitLab/gitLabRegistryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const gitLabRegistryProvider: IRegistryProvider = {
wizardTitle: localize('vscode-docker.tree.registries.gitlab.signIn', 'Sign in to GitLab'),
includeUsername: true,
includePassword: true,
passwordPrompt: localize('vscode-docker.tree.registries.gitlab.pat', 'GitLab Personal Access Token'),
},
treeItemFactory: (parent, cachedProvider) => new GitLabAccountTreeItem(parent, cachedProvider),
persistAuth: async (cachedProvider, secret) => await setRegistryPassword(cachedProvider, secret),
Expand Down

0 comments on commit c26626d

Please sign in to comment.