Skip to content

Commit

Permalink
store selected id in secret store
Browse files Browse the repository at this point in the history
inject auth service to api client
  • Loading branch information
meniRoy committed Sep 26, 2023
1 parent b1adc4d commit 4c8a496
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
30 changes: 14 additions & 16 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,27 @@ import axios, { AxiosInstance } from "axios";
import { ENV0_API_URL } from "./common";
import { EnvironmentModel } from "./get-environments";
import { DeploymentStepLogsResponse, DeploymentStepResponse } from "./types";
import { AuthService } from "./auth";

class ApiClient {
private credentials?: { username: string; password: string };
private currentOrganizationId?: string;
private readonly instance: AxiosInstance;
private authService?: AuthService;
constructor() {
this.instance = axios.create({ baseURL: `https://${ENV0_API_URL}` });
this.instance.interceptors.request.use((config) => {
if (this.credentials) {
config.auth = this.credentials;
const credentials = this.authService?.getCredentials();
if (credentials) {
config.auth = {
username: credentials.username,
password: credentials.password,
};
}
return config;
});
}

public init(
credentials: { username: string; password: string },
organizationId: string
) {
this.credentials = credentials;
this.currentOrganizationId = organizationId;
}

public clearCredentials() {
this.currentOrganizationId = undefined;
this.credentials = undefined;
public init(authService: AuthService) {
this.authService = authService;
}

public async abortDeployment(deploymentId: string) {
Expand Down Expand Up @@ -72,7 +67,10 @@ class ApiClient {
const response = await this.instance.get<EnvironmentModel[]>(
`/environments`,
{
params: { organizationId: this.currentOrganizationId, isActive: true },
params: {
organizationId: this.authService?.getCredentials().selectedOrgId,
isActive: true,
},
}
);

Expand Down
59 changes: 36 additions & 23 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import {
showUnexpectedErrorMessage,
} from "./notification-messages";

const env0KeyIdKey = "env0.keyId";
const env0SecretKey = "env0.secret";
const env0KeyIdStoreKey = "env0.keyId";
const env0SecretStoreKey = "env0.secret";
const selectedOrgIdStoreKey = "env0.selectedOrgId";

export class AuthService {
constructor(private readonly context: vscode.ExtensionContext) {}

private selectedOrgId: string | undefined;

public getSelectedOrg() {
return this.selectedOrgId;
}
private credentials?: {
username: string;
password: string;
selectedOrgId: string;
};

public registerLoginCommand(onLogin: () => void) {
const disposable = vscode.commands.registerCommand(
Expand Down Expand Up @@ -56,8 +57,7 @@ export class AuthService {

const selectedOrgId = await this.pickOrganization(keyId!, secret!);
if (selectedOrgId) {
this.selectedOrgId = selectedOrgId;
await this.storeAuthData(keyId!, secret!);
await this.storeAuthData(keyId!, secret!, selectedOrgId);
await onLogin();
}
}
Expand All @@ -77,22 +77,28 @@ export class AuthService {
}

public async isLoggedIn() {
const { secret, keyId } = await this.getAuthData();
return !!(secret && keyId);
const { secret, keyId, selectedOrgId } = await this.getAuthData();
if (!(secret && keyId && selectedOrgId)) {
return false;
}
this.credentials = { username: keyId, password: secret, selectedOrgId };
return true;
}

public async getApiKeyCredentials() {
const { secret, keyId } = await this.getAuthData();
if (!secret || !keyId) {
throw new Error("Could not read env0 api key values");
public getCredentials() {
if (!this.credentials) {
// this should happen only if the user is logged out
throw new Error("Could not read credentials");
}
return { username: keyId, password: secret };

return this.credentials;
}

private async getAuthData() {
return {
keyId: await this.context.secrets.get(env0KeyIdKey),
secret: await this.context.secrets.get(env0SecretKey),
keyId: await this.context.secrets.get(env0KeyIdStoreKey),
secret: await this.context.secrets.get(env0SecretStoreKey),
selectedOrgId: await this.context.secrets.get(selectedOrgIdStoreKey),
};
}

Expand Down Expand Up @@ -146,13 +152,20 @@ export class AuthService {
);
}

private async storeAuthData(keyId: string, secret: string) {
await this.context.secrets.store(env0KeyIdKey, keyId);
await this.context.secrets.store(env0SecretKey, secret);
private async storeAuthData(
keyId: string,
secret: string,
selectedOrgId: string
) {
this.credentials = { username: keyId, password: secret, selectedOrgId };
await this.context.secrets.store(env0KeyIdStoreKey, keyId);
await this.context.secrets.store(env0SecretStoreKey, secret);
await this.context.secrets.store(selectedOrgIdStoreKey, selectedOrgId);
}

private async clearAuthData() {
await this.context.secrets.delete(env0KeyIdKey);
await this.context.secrets.delete(env0SecretKey);
await this.context.secrets.delete(env0KeyIdStoreKey);
await this.context.secrets.delete(env0SecretStoreKey);
await this.context.secrets.delete(selectedOrgIdStoreKey);
}
}
6 changes: 1 addition & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ const init = async (
environmentsTree: vscode.TreeView<Environment>,
authService: AuthService
) => {
apiClient.init(
await authService.getApiKeyCredentials(),
authService.getSelectedOrg()!
);
apiClient.init(authService);
extensionState.setLoggedIn(true);
await loadEnvironments(environmentsDataProvider);
};
Expand All @@ -84,7 +81,6 @@ const onLogOut = async () => {
}
stopEnvironmentPolling();
environmentsDataProvider.clear();
apiClient.clearCredentials();
extensionState.setLoggedIn(false);
};

Expand Down

0 comments on commit 4c8a496

Please sign in to comment.