Remove token for running a remote query
This commit is contained in:
@@ -7,16 +7,11 @@ const GITHUB_AUTH_PROVIDER_ID = 'github';
|
||||
// https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps
|
||||
const SCOPES = ['repo'];
|
||||
|
||||
interface OctokitAndToken {
|
||||
octokit: Octokit.Octokit;
|
||||
token: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles authentication to GitHub, using the VS Code [authentication API](https://code.visualstudio.com/api/references/vscode-api#authentication).
|
||||
*/
|
||||
export class Credentials {
|
||||
private octokitAndToken: OctokitAndToken | undefined;
|
||||
private octokit: Octokit.Octokit | undefined;
|
||||
|
||||
// Explicitly make the constructor private, so that we can't accidentally call the constructor from outside the class
|
||||
// without also initializing the class.
|
||||
@@ -26,20 +21,17 @@ export class Credentials {
|
||||
static async initialize(context: vscode.ExtensionContext): Promise<Credentials> {
|
||||
const c = new Credentials();
|
||||
c.registerListeners(context);
|
||||
c.octokitAndToken = await c.createOctokit(false);
|
||||
c.octokit = await c.createOctokit(false);
|
||||
return c;
|
||||
}
|
||||
|
||||
private async createOctokit(createIfNone: boolean): Promise<OctokitAndToken | undefined> {
|
||||
private async createOctokit(createIfNone: boolean): Promise<Octokit.Octokit | undefined> {
|
||||
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone });
|
||||
|
||||
if (session) {
|
||||
return {
|
||||
octokit: new Octokit.Octokit({
|
||||
auth: session.accessToken
|
||||
}),
|
||||
token: session.accessToken
|
||||
};
|
||||
return new Octokit.Octokit({
|
||||
auth: session.accessToken
|
||||
});
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
@@ -49,33 +41,22 @@ export class Credentials {
|
||||
// Sessions are changed when a user logs in or logs out.
|
||||
context.subscriptions.push(vscode.authentication.onDidChangeSessions(async e => {
|
||||
if (e.provider.id === GITHUB_AUTH_PROVIDER_ID) {
|
||||
this.octokitAndToken = await this.createOctokit(false);
|
||||
this.octokit = await this.createOctokit(false);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
async getOctokit(): Promise<Octokit.Octokit> {
|
||||
if (this.octokitAndToken) {
|
||||
return this.octokitAndToken.octokit;
|
||||
if (this.octokit) {
|
||||
return this.octokit;
|
||||
}
|
||||
|
||||
this.octokitAndToken = await this.createOctokit(true);
|
||||
this.octokit = await this.createOctokit(true);
|
||||
// octokit shouldn't be undefined, since we've set "createIfNone: true".
|
||||
// The following block is mainly here to prevent a compiler error.
|
||||
if (!this.octokitAndToken) {
|
||||
if (!this.octokit) {
|
||||
throw new Error('Did not initialize Octokit.');
|
||||
}
|
||||
return this.octokitAndToken.octokit;
|
||||
}
|
||||
|
||||
async getToken(): Promise<string> {
|
||||
if (this.octokitAndToken) {
|
||||
return this.octokitAndToken.token;
|
||||
}
|
||||
this.octokitAndToken = await this.createOctokit(true);
|
||||
if (!this.octokitAndToken) {
|
||||
throw new Error('Did not initialize Octokit.');
|
||||
}
|
||||
return this.octokitAndToken.token;
|
||||
return this.octokit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,6 @@ export async function runRemoteQuery(cliServer: cli.CodeQLCliServer, credentials
|
||||
|
||||
async function runRemoteQueriesApiRequest(credentials: Credentials, ref: string, language: string, repositories: string[], query: string) {
|
||||
const octokit = await credentials.getOctokit();
|
||||
const token = await credentials.getToken();
|
||||
|
||||
try {
|
||||
await octokit.request(
|
||||
@@ -125,7 +124,6 @@ async function runRemoteQueriesApiRequest(credentials: Credentials, ref: string,
|
||||
language: language,
|
||||
repositories: repositories,
|
||||
query: query,
|
||||
token: token,
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user