Skip to content

Commit d66030d

Browse files
authored
fix:(GitHub Auth): Deprecate getAuthUsername (#585)
* remove getAuthUsername and add getGitHubAuthHeader * remove setAuth as it seems to be no longer needed * use token in git auth headers * remove vars no longer needed * remove unneeded parameter for GitHubRemote * clean up imports * going back to usernaem:token@github * clean up comments * restore missing function * replace the word dummyusername with placeholderusername * another to replace
1 parent 1f5cca9 commit d66030d

File tree

5 files changed

+5
-50
lines changed

5 files changed

+5
-50
lines changed

src/targets/awsLambdaLayer.ts

-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import * as path from 'path';
44
import { Octokit } from '@octokit/rest';
55
import simpleGit from 'simple-git';
66
import {
7-
getAuthUsername,
8-
getGitHubApiToken,
97
getGitHubClient,
108
GitHubRemote,
119
} from '../utils/githubApi';
@@ -139,8 +137,6 @@ export class AwsLambdaLayerTarget extends BaseTarget {
139137
this.logger.trace('AWS regions: ', awsRegions);
140138

141139
const remote = this.awsLambdaConfig.registryRemote;
142-
const username = await getAuthUsername(this.github);
143-
remote.setAuth(username, getGitHubApiToken());
144140

145141
await withTempDir(
146142
async directory => {

src/targets/ghPages.ts

-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { GitHubGlobalConfig, TargetConfig } from '../schemas/project_config';
88
import { ConfigurationError, reportError } from '../utils/errors';
99
import { withTempDir } from '../utils/files';
1010
import {
11-
getAuthUsername,
12-
getGitHubApiToken,
1311
getGitHubClient,
1412
GitHubRemote,
1513
} from '../utils/githubApi';
@@ -219,13 +217,9 @@ export class GhPagesTarget extends BaseTarget {
219217
packageFiles[0]
220218
);
221219

222-
const username = await getAuthUsername(this.github);
223-
224220
const remote = new GitHubRemote(
225221
githubOwner,
226222
githubRepo,
227-
username,
228-
getGitHubApiToken()
229223
);
230224

231225
await withTempDir(

src/targets/registry.ts

-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { GitHubGlobalConfig, TargetConfig } from '../schemas/project_config';
66
import { ConfigurationError, reportError } from '../utils/errors';
77
import { withTempDir } from '../utils/files';
88
import {
9-
getAuthUsername,
10-
getGitHubApiToken,
119
getGitHubClient,
1210
GitHubRemote,
1311
} from '../utils/githubApi';
@@ -427,8 +425,6 @@ export class RegistryTarget extends BaseTarget {
427425

428426
private async cloneRegistry(directory: string): Promise<SimpleGit> {
429427
const remote = this.remote;
430-
const username = await getAuthUsername(this.github);
431-
remote.setAuth(username, getGitHubApiToken());
432428

433429
const git = simpleGit(directory);
434430
this.logger.info(

src/targets/upm.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Octokit } from '@octokit/rest';
22
import simpleGit from 'simple-git';
33
import {
4-
getAuthUsername,
54
getGitHubApiToken,
65
getGitHubClient,
76
GitHubRemote,
@@ -107,11 +106,9 @@ export class UpmTarget extends BaseTarget {
107106
packageFile
108107
);
109108

110-
const username = await getAuthUsername(this.github);
111109
const remote = new GitHubRemote(
112110
this.config.releaseRepoOwner,
113111
this.config.releaseRepoName,
114-
username,
115112
getGitHubApiToken()
116113
);
117114
const remoteAddr = remote.getRemoteString();

src/utils/githubApi.ts

+5-33
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export class GitHubRemote {
1212
public readonly owner: string;
1313
/** GitHub repository name */
1414
public readonly repo: string;
15-
/** GitHub username */
16-
protected username?: string;
1715
/** GitHub personal authentication token */
1816
protected apiToken?: string;
1917
/** GitHub hostname */
@@ -26,27 +24,14 @@ export class GitHubRemote {
2624
public constructor(
2725
owner: string,
2826
repo: string,
29-
username?: string,
3027
apiToken?: string
3128
) {
3229
this.owner = owner;
3330
this.repo = repo;
34-
if (username && apiToken) {
35-
this.setAuth(username, apiToken);
36-
}
31+
this.apiToken = apiToken;
3732
this.url = `/${this.owner}/${this.repo}/`;
3833
}
3934

40-
/**
41-
* Sets authentication arguments: username and personal API token
42-
*
43-
* @param username GitHub username
44-
* @param apiToken GitHub API token
45-
*/
46-
public setAuth(username: string, apiToken: string): void {
47-
this.username = username;
48-
this.apiToken = apiToken;
49-
}
5035

5136
/**
5237
* Returns an HTTP-based git remote
@@ -60,12 +45,14 @@ export class GitHubRemote {
6045
/**
6146
* Returns an HTTP-based git remote with embedded HTTP basic auth
6247
*
48+
* Using placeholder username as it does not matter for cloning
49+
*
6350
* It MAY contain sensitive information (e.g. API tokens)
6451
*/
6552
public getRemoteStringWithAuth(): string {
6653
const authData =
67-
this.username && this.apiToken
68-
? `${this.username}:${this.apiToken}@`
54+
this.apiToken
55+
? `placeholderusername:${this.apiToken}@`
6956
: '';
7057
return this.PROTOCOL_PREFIX + authData + this.GITHUB_HOSTNAME + this.url;
7158
}
@@ -123,21 +110,6 @@ export function getGitHubClient(token = ''): Octokit {
123110
return _GitHubClientCache[githubApiToken];
124111
}
125112

126-
/**
127-
* Gets the currently authenticated GitHub user from the client
128-
*
129-
* @param github GitHub client
130-
* @returns GitHub username
131-
*/
132-
export async function getAuthUsername(github: Octokit): Promise<string> {
133-
const userData = await github.users.getAuthenticated({});
134-
const username = (userData.data || {}).login;
135-
if (!username) {
136-
throw new Error('Cannot reliably detect GitHub username, aborting');
137-
}
138-
return username;
139-
}
140-
141113
/**
142114
* Loads a file from the context's repository
143115
*

0 commit comments

Comments
 (0)