Skip to content
Draft
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
16 changes: 15 additions & 1 deletion src/auth/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export class GoogleAuthProvider implements AuthenticationProvider, Disposable {
}

/**
* Retrieves the Google OAuth2 authentication session.
* Retrieves the Google OAuth2 authentication session. Prompts the user to
* sign in if no matching sessions are found.
*
* @param vs - The VS Code API.
* @returns The authentication session.
Expand All @@ -108,6 +109,19 @@ export class GoogleAuthProvider implements AuthenticationProvider, Disposable {
return session;
}

/**
* Retrieves the Google OAuth2 authentication session.
*
* @param vs - The VS Code API.
* @returns A promise that resolves with the authentication session if it
* exists and undefined otherwise.
*/
static async getSession(
vs: typeof vscode,
): Promise<AuthenticationSession | undefined> {
return vs.authentication.getSession(PROVIDER_ID, REQUIRED_SCOPES);
}

/**
* Disposes the provider and cleans up resources.
*/
Expand Down
21 changes: 16 additions & 5 deletions src/telemetry/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import fetch, { Request } from 'node-fetch';
import fetch, { Headers, Request } from 'node-fetch';
import { Disposable } from 'vscode';
import { CONTENT_TYPE_JSON_HEADER } from '../colab/headers';
import {
AUTHORIZATION_HEADER,
CONTENT_TYPE_JSON_HEADER,
} from '../colab/headers';
import { log } from '../common/logging';
import {
ColabLogEvent,
Expand Down Expand Up @@ -36,6 +39,8 @@ export class ClearcutClient implements Disposable {
// Queue of events to be flushed to Clearcut.
private pendingEvents: LogEvent[] = [];

constructor(private getAccessToken: () => Promise<string | undefined>) {}

dispose() {
if (this.isDisposed) {
return;
Expand Down Expand Up @@ -99,12 +104,18 @@ export class ClearcutClient implements Disposable {
log_source: LOG_SOURCE,
log_event: events,
};

const headers = new Headers();
headers.set(CONTENT_TYPE_JSON_HEADER.key, CONTENT_TYPE_JSON_HEADER.value);
const token = await this.getAccessToken();
if (token) {
headers.set(AUTHORIZATION_HEADER.key, `Bearer ${token}`);
}

const request = new Request(LOGS_ENDPOINT, {
method: 'POST',
body: JSON.stringify(logRequest),
headers: {
[CONTENT_TYPE_JSON_HEADER.key]: CONTENT_TYPE_JSON_HEADER.value,
},
headers,
});
const response = await fetch(request);
// TODO: handle 401 once token is included in request
Expand Down
Loading
Loading