refactor: auth
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
21
helpers/types/HoppRESTAuth.ts
Normal file
21
helpers/types/HoppRESTAuth.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export type HoppRESTAuthNone = {
|
||||
authType: "none"
|
||||
}
|
||||
|
||||
export type HoppRESTAuthBasic = {
|
||||
authType: "basic"
|
||||
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export type HoppRESTAuthBearer = {
|
||||
authType: "bearer"
|
||||
|
||||
token: string
|
||||
}
|
||||
|
||||
export type HoppRESTAuth =
|
||||
| HoppRESTAuthNone
|
||||
| HoppRESTAuthBasic
|
||||
| HoppRESTAuthBearer
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ValidContentTypes } from "../utils/contenttypes"
|
||||
import { HoppRESTAuth } from "./HoppRESTAuth"
|
||||
|
||||
export const RESTReqSchemaVersion = "1"
|
||||
|
||||
@@ -31,6 +32,8 @@ export interface HoppRESTRequest {
|
||||
preRequestScript: string
|
||||
testScript: string
|
||||
|
||||
auth: HoppRESTAuth
|
||||
|
||||
body: HoppRESTReqBody
|
||||
}
|
||||
|
||||
@@ -89,6 +92,8 @@ export function translateToNewRequest(x: any): HoppRESTRequest {
|
||||
|
||||
const body = parseRequestBody(x)
|
||||
|
||||
const auth = parseOldAuth(x)
|
||||
|
||||
const result: HoppRESTRequest = {
|
||||
name,
|
||||
endpoint,
|
||||
@@ -98,9 +103,32 @@ export function translateToNewRequest(x: any): HoppRESTRequest {
|
||||
preRequestScript,
|
||||
testScript,
|
||||
body,
|
||||
auth,
|
||||
v: RESTReqSchemaVersion,
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export function parseOldAuth(x: any): HoppRESTAuth {
|
||||
if (!x.auth || x.auth === "None")
|
||||
return {
|
||||
authType: "none",
|
||||
}
|
||||
|
||||
if (x.auth === "Basic Auth")
|
||||
return {
|
||||
authType: "basic",
|
||||
username: x.httpUser,
|
||||
password: x.httpPassword,
|
||||
}
|
||||
|
||||
if (x.auth === "Bearer Token")
|
||||
return {
|
||||
authType: "bearer",
|
||||
token: x.bearerToken,
|
||||
}
|
||||
|
||||
return { authType: "none" }
|
||||
}
|
||||
|
||||
@@ -27,24 +27,44 @@ export function getEffectiveRESTRequest(
|
||||
request: HoppRESTRequest,
|
||||
environment: Environment
|
||||
): EffectiveHoppRESTRequest {
|
||||
const effectiveFinalHeaders = request.headers
|
||||
.filter(
|
||||
(x) =>
|
||||
x.key !== "" && // Remove empty keys
|
||||
x.active // Only active
|
||||
)
|
||||
.map((x) => ({
|
||||
// Parse out environment template strings
|
||||
active: true,
|
||||
key: parseTemplateString(x.key, environment.variables),
|
||||
value: parseTemplateString(x.value, environment.variables),
|
||||
}))
|
||||
|
||||
// Authentication
|
||||
// TODO: Support a better b64 implementation than btoa ?
|
||||
if (request.auth.authType === "basic") {
|
||||
effectiveFinalHeaders.push({
|
||||
active: true,
|
||||
key: "Authorization",
|
||||
value: `Basic ${btoa(
|
||||
`${request.auth.username}:${request.auth.password}`
|
||||
)}`,
|
||||
})
|
||||
} else if (request.auth.authType === "bearer") {
|
||||
effectiveFinalHeaders.push({
|
||||
active: true,
|
||||
key: "Authorization",
|
||||
value: `Bearer ${request.auth.token}`,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...request,
|
||||
effectiveFinalURL: parseTemplateString(
|
||||
request.endpoint,
|
||||
environment.variables
|
||||
),
|
||||
effectiveFinalHeaders: request.headers
|
||||
.filter(
|
||||
(x) =>
|
||||
x.key !== "" && // Remove empty keys
|
||||
x.active // Only active
|
||||
)
|
||||
.map((x) => ({
|
||||
// Parse out environment template strings
|
||||
active: true,
|
||||
key: parseTemplateString(x.key, environment.variables),
|
||||
value: parseTemplateString(x.value, environment.variables),
|
||||
})),
|
||||
effectiveFinalHeaders,
|
||||
effectiveFinalParams: request.params
|
||||
.filter(
|
||||
(x) =>
|
||||
|
||||
Reference in New Issue
Block a user