Skip to content

Commit

Permalink
chore: Remove unused client credentials logic (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurawarr authored Aug 2, 2023
1 parent 477341b commit 3c5aebd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 156 deletions.
41 changes: 0 additions & 41 deletions src/SecretStateManager.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/api/getProject.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/api/getToken.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/cli/baseCLIController.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 0 additions & 2 deletions src/cli/cliUtils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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

Expand Down Expand Up @@ -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',
Expand Down
79 changes: 17 additions & 62 deletions src/utils/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,47 @@
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
}

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
}
}

return config
}

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',
Expand All @@ -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 }
}

0 comments on commit 3c5aebd

Please sign in to comment.