Archived
1
0

refactor: parse options with multiple = in cli

There was a case with the hashed-password which had multiple equal signs in the
value and it wasn't being parsed correctly. This uses a new function and adds a
few tests.
This commit is contained in:
Joe Previte
2021-06-03 16:37:46 -07:00
parent 531b7c0c25
commit 8c2bb61af9
8 changed files with 71 additions and 72 deletions

View File

@ -247,14 +247,8 @@ export function splitOnFirstEquals(str: string): string[] {
// $argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY
// 2 means return two items
// Source: https://stackoverflow.com/a/4607799/3015595
const split = str.split(/=(.+)/, 2)
// It should always return two elements
// because it's used in a place where
// it expected two elements
if (split.length === 1) {
split.push("")
}
// We use the ? to say the the substr after the = is optional
const split = str.split(/=(.+)?/, 2)
return split
}
@ -289,17 +283,9 @@ export const parse = (
let key: keyof Args | undefined
let value: string | undefined
if (arg.startsWith("--")) {
// TODO fix this
const split = arg.replace(/^--/, "").split("=", 2)
const split = splitOnFirstEquals(arg.replace(/^--/, ""))
key = split[0] as keyof Args
value = split[1]
} else {
const short = arg.replace(/^-/, "")
const pair = Object.entries(options).find(([, v]) => v.short === short)
if (pair) {
key = pair[0] as keyof Args
}
}
if (!key || !options[key]) {
throw error(`Unknown option ${arg}`)
@ -563,7 +549,6 @@ export function parseConfigFile(configFile: string, configPath: string): ConfigA
const config = yaml.load(configFile, {
filename: configPath,
})
console.log("what is this config", config)
if (!config || typeof config === "string") {
throw new Error(`invalid config: ${config}`)
}
@ -576,11 +561,9 @@ export function parseConfigFile(configFile: string, configPath: string): ConfigA
}
return `--${optName}=${opt}`
})
console.log("what are the configFileArgv", configFileArgv)
const args = parse(configFileArgv, {
configFile: configPath,
})
console.log(args, "args")
return {
...args,
config: configPath,

View File

@ -63,9 +63,10 @@ export const ensureAuthenticated = async (
*/
export const authenticated = async (req: express.Request): Promise<boolean> => {
switch (req.args.auth) {
case AuthType.None:
case AuthType.None: {
return true
case AuthType.Password:
}
case AuthType.Password: {
// The password is stored in the cookie after being hashed.
const hashedPasswordFromArgs = req.args["hashed-password"]
const passwordMethod = getPasswordMethod(hashedPasswordFromArgs)
@ -77,8 +78,10 @@ export const authenticated = async (req: express.Request): Promise<boolean> => {
}
return await isCookieValid(isCookieValidArgs)
default:
}
default: {
throw new Error(`Unsupported auth type ${req.args.auth}`)
}
}
}

View File

@ -1,15 +1,15 @@
import { logger } from "@coder/logger"
import * as argon2 from "argon2"
import * as cp from "child_process"
import * as crypto from "crypto"
import * as argon2 from "argon2"
import envPaths from "env-paths"
import { promises as fs } from "fs"
import * as net from "net"
import * as os from "os"
import * as path from "path"
import safeCompare from "safe-compare"
import * as util from "util"
import xdgBasedir from "xdg-basedir"
import safeCompare from "safe-compare"
import { logger } from "@coder/logger"
export interface Paths {
data: string