Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to use PAT auth for GitLab #2689

Merged
merged 1 commit into from
Feb 1, 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
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