diff --git a/src/SecretStateManager.ts b/src/SecretStateManager.ts deleted file mode 100644 index 2f00bc5..0000000 --- a/src/SecretStateManager.ts +++ /dev/null @@ -1,41 +0,0 @@ -import * as vscode from 'vscode' -import { StateManager } from './StateManager' - -export const enum CLIENT_KEYS { - CLIENT_ID = 'client_id', - CLIENT_SECRET = 'client_secret', -} - -export class SecretStateManager { - private static _instance: SecretStateManager - private secretStorage: vscode.SecretStorage - constructor(private secrets: vscode.SecretStorage) { - this.secretStorage = secrets - } - static init(context: vscode.ExtensionContext): void { - /* - Create instance of new AuthSettings. - */ - SecretStateManager._instance = new SecretStateManager(context.secrets) - } - - static get instance(): SecretStateManager { - /* - Getter of our SecretStateManager existing instance. - */ - return SecretStateManager._instance - } - - async clearSecrets() { - this.secretStorage.store(CLIENT_KEYS.CLIENT_ID, '') - this.secretStorage.store(CLIENT_KEYS.CLIENT_SECRET, '') - } - - async setSecret(key: CLIENT_KEYS, value: string) { - return this.secretStorage.store(key, value) - } - - async getSecret(key: CLIENT_KEYS) { - return this.secretStorage.get(key) - } -} diff --git a/src/api/getProject.ts b/src/api/getProject.ts deleted file mode 100644 index d09b229..0000000 --- a/src/api/getProject.ts +++ /dev/null @@ -1,24 +0,0 @@ -import axios from 'axios' -import { getToken } from '../api/getToken' -import { getCredentials } from '../utils/credentials' - -export const getProject = async (id: string) => { - const { client_id, client_secret } = await getCredentials() - if (!client_id || !client_secret) { - throw new Error('Missing client_id or secret to get auth token') - } - let { access_token } = await getToken(client_id, client_secret) - return await axios({ - method: 'GET', - headers: { - Authorization: `${access_token}`, - }, - url: `https://api.devcycle.com/v1/projects/${id}`, - }) - .then((res) => { - return res.data - }) - .catch((error) => { - return error.response.status - }) -} diff --git a/src/api/getToken.ts b/src/api/getToken.ts deleted file mode 100644 index 7ec3733..0000000 --- a/src/api/getToken.ts +++ /dev/null @@ -1,23 +0,0 @@ -import axios from 'axios' - -export const getToken = async (id: string, secret: string) => { - return await axios({ - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - url: 'https://auth.devcycle.com/oauth/token', - data: { - client_id: id || '', - client_secret: secret || '', - audience: 'https://api.devcycle.com/', - grant_type: 'client_credentials', - }, - }) - .then((res) => { - return res.data - }) - .catch((error) => { - return error.response.status - }) -} diff --git a/src/cli/baseCLIController.ts b/src/cli/baseCLIController.ts index faeca01..eeee08e 100644 --- a/src/cli/baseCLIController.ts +++ b/src/cli/baseCLIController.ts @@ -1,6 +1,5 @@ import * as vscode from 'vscode' import * as cp from 'child_process' -import { getCredentials } from '../utils/credentials' import { StateManager, KEYS } from '../StateManager' import { showBusyMessage, hideBusyMessage } from '../components/statusBarItem' type CommandResponse = { diff --git a/src/cli/cliUtils.ts b/src/cli/cliUtils.ts index 86485d2..a5fe8a2 100644 --- a/src/cli/cliUtils.ts +++ b/src/cli/cliUtils.ts @@ -1,6 +1,4 @@ import { KEYS, StateManager } from '../StateManager' -import { getToken } from '../api/getToken' -import { getCredentials } from '../utils/credentials' import { Organization } from './baseCLIController' import { getAllEnvironments, getEnvironment } from './environmentsCLIController' import { diff --git a/src/extension.ts b/src/extension.ts index 6c61889..e3163d9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,7 +2,6 @@ import * as vscode from 'vscode' import { KEYS, StateManager } from './StateManager' import { init, logout, status as cliStatus, Variable } from './cli' -import { SecretStateManager } from './SecretStateManager' import { autoLoginIfHaveCredentials } from './utils/credentials' import { SidebarProvider } from './components/SidebarProvider' @@ -18,7 +17,6 @@ const SCHEME_FILE = { } export const activate = async (context: vscode.ExtensionContext) => { - SecretStateManager.init(context) StateManager.globalState = context.globalState StateManager.workspaceState = context.workspaceState @@ -75,7 +73,6 @@ export const activate = async (context: vscode.ExtensionContext) => { 'devcycle-feature-flags.logout', async () => { await Promise.all([ - SecretStateManager.instance.clearSecrets(), StateManager.clearState(), vscode.commands.executeCommand( 'setContext', diff --git a/src/utils/credentials.ts b/src/utils/credentials.ts index 4ae63f4..2b56aae 100644 --- a/src/utils/credentials.ts +++ b/src/utils/credentials.ts @@ -1,16 +1,9 @@ import * as fs from 'fs' import * as jsYaml from 'js-yaml' import * as os from 'os' -import { CLIENT_KEYS, SecretStateManager } from '../SecretStateManager' -import { KEYS, StateManager } from '../StateManager' import * as vscode from 'vscode' - -type Auth = { - clientCredentials: { - client_id: string - client_secret: string - } -} +import { status } from '../cli' +import { KEYS, StateManager } from '../StateManager' type User = { project: string @@ -18,36 +11,24 @@ type User = { type Config = { project?: string - client_id?: string - client_secret?: string } export function loadConfig() { const repoConfigPath = './.devcycle/config.yml' - const globalAuthPath = `${os.homedir()}/.config/devcycle/auth.yml` const globalUserPath = `${os.homedir()}/.config/devcycle/user.yml` let config: Config = {} - if (fs.existsSync(globalAuthPath)) { - const globalAuthFile = fs.readFileSync(globalAuthPath, 'utf8') - const auth = jsYaml.load(globalAuthFile) as Auth - if (auth.clientCredentials) { - config.client_id = auth.clientCredentials.client_id - config.client_secret = auth.clientCredentials.client_secret + if (fs.existsSync(repoConfigPath)) { + const repoUserFile = fs.readFileSync(repoConfigPath, 'utf8') + const user = jsYaml.load(repoUserFile) as User + if (user.project) { + config.project = user.project } - - if (fs.existsSync(repoConfigPath)) { - const repoUserFile = fs.readFileSync(repoConfigPath, 'utf8') - const user = jsYaml.load(repoUserFile) as User - if (user.project) { - config.project = user.project - } - } else if (fs.existsSync(globalUserPath)) { - const globalUserFile = fs.readFileSync(globalUserPath, 'utf8') - const user = jsYaml.load(globalUserFile) as User - if (user.project) { - config.project = user.project - } + } else if (fs.existsSync(globalUserPath)) { + const globalUserFile = fs.readFileSync(globalUserPath, 'utf8') + const user = jsYaml.load(globalUserFile) as User + if (user.project) { + config.project = user.project } } @@ -55,21 +36,12 @@ export function loadConfig() { } export async function autoLoginIfHaveCredentials() { - const { - client_id: config_client, - client_secret: config_secret, - project: config_project, - } = loadConfig() - const { - client_id: state_client, - client_secret: state_secret, - projectId: state_project, - } = await getCredentials() - const client_id = config_client || state_client - const client_secret = config_secret || state_secret - const project_id = config_project || state_project + const { project: configProject } = loadConfig() + const projectId = configProject || await StateManager.getState(KEYS.PROJECT_ID) + + const { hasAccessToken, organization } = await status() + const hasAllCredentials = Boolean(hasAccessToken && organization && projectId) - const hasAllCredentials = client_id && client_secret && project_id await vscode.commands.executeCommand( 'setContext', 'devcycle-feature-flags.hasCredentialsAndProject', @@ -78,20 +50,3 @@ export async function autoLoginIfHaveCredentials() { return hasAllCredentials } - -export async function setClientIdAndSecret( - client_id: string, - client_secret: string, -) { - const secrets = SecretStateManager.instance - await secrets.setSecret(CLIENT_KEYS.CLIENT_ID, client_id) - await secrets.setSecret(CLIENT_KEYS.CLIENT_SECRET, client_secret) -} - -export async function getCredentials() { - const secrets = SecretStateManager.instance - const client_id = await secrets.getSecret(CLIENT_KEYS.CLIENT_ID) - const client_secret = await secrets.getSecret(CLIENT_KEYS.CLIENT_SECRET) - const projectId = await StateManager.getState(KEYS.PROJECT_ID) - return { client_id, client_secret, projectId } -}