fix: use proper values for addTo field when auth type is api-key (#3966)

This commit is contained in:
Akash K
2024-04-16 17:44:28 +05:30
committed by GitHub
parent 787aab650f
commit 0f592d1789
12 changed files with 182 additions and 30 deletions

View File

@@ -136,13 +136,13 @@ export function getEffectiveRESTRequest(
} }
} else if (request.auth.authType === "api-key") { } else if (request.auth.authType === "api-key") {
const { key, value, addTo } = request.auth; const { key, value, addTo } = request.auth;
if (addTo === "Headers") { if (addTo === "HEADERS") {
effectiveFinalHeaders.push({ effectiveFinalHeaders.push({
active: true, active: true,
key: parseTemplateString(key, envVariables), key: parseTemplateString(key, envVariables),
value: parseTemplateString(value, envVariables), value: parseTemplateString(value, envVariables),
}); });
} else if (addTo === "Query params") { } else if (addTo === "QUERY_PARAMS") {
effectiveFinalParams.push({ effectiveFinalParams.push({
active: true, active: true,
key: parseTemplateString(key, envVariables), key: parseTemplateString(key, envVariables),

View File

@@ -162,6 +162,8 @@
"label_client_credentials": "Client Credentials" "label_client_credentials": "Client Credentials"
}, },
"pass_key_by": "Pass by", "pass_key_by": "Pass by",
"pass_by_query_params_label": "Query Parameters",
"pass_by_headers_label": "Headers",
"password": "Password", "password": "Password",
"save_to_inherit": "Please save this request in any collection to inherit the authorization", "save_to_inherit": "Please save this request in any collection to inherit the authorization",
"token": "Token", "token": "Token",

View File

@@ -595,7 +595,7 @@ const getComputedAuthHeaders = (
} else if (request.auth.authType === "api-key") { } else if (request.auth.authType === "api-key") {
const { key, addTo } = request.auth const { key, addTo } = request.auth
if (addTo === "Headers" && key) { if (addTo === "HEADERS" && key) {
headers.push({ headers.push({
active: true, active: true,
key, key,

View File

@@ -28,7 +28,13 @@
> >
<HoppSmartSelectWrapper> <HoppSmartSelectWrapper>
<HoppButtonSecondary <HoppButtonSecondary
:label="auth.addTo || t('state.none')" :label="
auth.addTo
? auth.addTo === 'HEADERS'
? t('authorization.pass_by_headers_label')
: t('authorization.pass_by_query_params_label')
: t('state.none')
"
class="ml-2 rounded-none pr-8" class="ml-2 rounded-none pr-8"
/> />
</HoppSmartSelectWrapper> </HoppSmartSelectWrapper>
@@ -40,23 +46,23 @@
@keyup.escape="hide()" @keyup.escape="hide()"
> >
<HoppSmartItem <HoppSmartItem
:icon="auth.addTo === 'Headers' ? IconCircleDot : IconCircle" :icon="auth.addTo === 'HEADERS' ? IconCircleDot : IconCircle"
:active="auth.addTo === 'Headers'" :active="auth.addTo === 'HEADERS'"
:label="'Headers'" :label="t('authorization.pass_by_headers_label')"
@click=" @click="
() => { () => {
auth.addTo = 'Headers' auth.addTo = 'HEADERS'
hide() hide()
} }
" "
/> />
<HoppSmartItem <HoppSmartItem
:icon="auth.addTo === 'Query params' ? IconCircleDot : IconCircle" :icon="auth.addTo === 'QUERY_PARAMS' ? IconCircleDot : IconCircle"
:active="auth.addTo === 'Query params'" :active="auth.addTo === 'QUERY_PARAMS'"
:label="'Query params'" :label="t('authorization.pass_by_query_params_label')"
@click=" @click="
() => { () => {
auth.addTo = 'Query params' auth.addTo = 'QUERY_PARAMS'
hide() hide()
} }
" "

View File

@@ -281,9 +281,9 @@ export const runGQLOperation = async (options: RunQueryOptions) => {
} }
} else if (auth.authType === "api-key") { } else if (auth.authType === "api-key") {
const { key, value, addTo } = auth const { key, value, addTo } = auth
if (addTo === "Headers") { if (addTo === "HEADERS") {
finalHeaders[key] = value finalHeaders[key] = value
} else if (addTo === "Query params") { } else if (addTo === "QUERY_PARAMS") {
params[key] = value params[key] = value
} }
} }

View File

@@ -260,7 +260,7 @@ const resolveOpenAPIV3SecurityObj = (
return { return {
authType: "api-key", authType: "api-key",
authActive: true, authActive: true,
addTo: "Headers", addTo: "HEADERS",
key: scheme.name, key: scheme.name,
value: "", value: "",
} }
@@ -268,7 +268,7 @@ const resolveOpenAPIV3SecurityObj = (
return { return {
authType: "api-key", authType: "api-key",
authActive: true, authActive: true,
addTo: "Query params", addTo: "QUERY_PARAMS",
key: scheme.in, key: scheme.in,
value: "", value: "",
} }
@@ -430,7 +430,7 @@ const resolveOpenAPIV2SecurityScheme = (
// V2 only supports in: header and in: query // V2 only supports in: header and in: query
return { return {
authType: "api-key", authType: "api-key",
addTo: scheme.in === "header" ? "Headers" : "Query params", addTo: scheme.in === "header" ? "HEADERS" : "QUERY_PARAMS",
authActive: true, authActive: true,
key: scheme.name, key: scheme.name,
value: "", value: "",

View File

@@ -150,8 +150,8 @@ const getHoppReqAuth = (item: Item): HoppRESTAuth => {
), ),
addTo: addTo:
(getVariableValue(auth.apikey, "in") ?? "query") === "query" (getVariableValue(auth.apikey, "in") ?? "query") === "query"
? "Query params" ? "QUERY_PARAMS"
: "Headers", : "HEADERS",
} }
} else if (auth.type === "bearer") { } else if (auth.type === "bearer") {
return { return {

View File

@@ -96,7 +96,7 @@ export const getComputedAuthHeaders = (
}) })
} else if (request.auth.authType === "api-key") { } else if (request.auth.authType === "api-key") {
const { key, addTo } = request.auth const { key, addTo } = request.auth
if (addTo === "Headers" && key) { if (addTo === "HEADERS" && key) {
headers.push({ headers.push({
active: true, active: true,
key: parseTemplateString(key, envVars), key: parseTemplateString(key, envVars),

View File

@@ -3,31 +3,33 @@ import { z } from "zod"
import V1_VERSION from "./v/1" import V1_VERSION from "./v/1"
import V2_VERSION from "./v/2" import V2_VERSION from "./v/2"
import V3_VERSION from "./v/3" import V3_VERSION from "./v/3"
import V4_VERSION from "./v/4"
export { GQLHeader } from "./v/1" export { GQLHeader } from "./v/1"
export { export {
HoppGQLAuthAPIKey,
HoppGQLAuthBasic, HoppGQLAuthBasic,
HoppGQLAuthBearer, HoppGQLAuthBearer,
HoppGQLAuthNone, HoppGQLAuthNone,
HoppGQLAuthInherit, HoppGQLAuthInherit,
} from "./v/2" } from "./v/2"
export { HoppGQLAuth } from "./v/3" export { HoppGQLAuth } from "./v/4"
export { HoppGQLAuthOAuth2 } from "./v/3" export { HoppGQLAuthOAuth2 } from "./v/3"
export { HoppGQLAuthAPIKey } from "./v/4"
export const GQL_REQ_SCHEMA_VERSION = 3 export const GQL_REQ_SCHEMA_VERSION = 4
const versionedObject = z.object({ const versionedObject = z.object({
v: z.number(), v: z.number(),
}) })
export const HoppGQLRequest = createVersionedEntity({ export const HoppGQLRequest = createVersionedEntity({
latestVersion: 3, latestVersion: 4,
versionMap: { versionMap: {
1: V1_VERSION, 1: V1_VERSION,
2: V2_VERSION, 2: V2_VERSION,
3: V3_VERSION, 3: V3_VERSION,
4: V4_VERSION,
}, },
getVersion(x) { getVersion(x) {
const result = versionedObject.safeParse(x) const result = versionedObject.safeParse(x)

View File

@@ -0,0 +1,71 @@
import { z } from "zod"
import { defineVersion } from "verzod"
import { HoppRESTAuthOAuth2 } from "../../rest"
import {
HoppGQLAuthAPIKey as HoppGQLAuthAPIKeyOld,
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthInherit,
HoppGQLAuthNone,
} from "./2"
import { V3_SCHEMA } from "./3"
export { HoppRESTAuthOAuth2 as HoppGQLAuthOAuth2 } from "../../rest"
export const HoppGQLAuthAPIKey = HoppGQLAuthAPIKeyOld.extend({
addTo: z.enum(["HEADERS", "QUERY_PARAMS"]).catch("HEADERS"),
})
export type HoppGqlAuthOAuth2 = z.infer<typeof HoppRESTAuthOAuth2>
export const HoppGQLAuth = z
.discriminatedUnion("authType", [
HoppGQLAuthNone,
HoppGQLAuthInherit,
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthAPIKey,
HoppRESTAuthOAuth2, // both rest and gql have the same auth type for oauth2
])
.and(
z.object({
authActive: z.boolean(),
})
)
export type HoppGQLAuth = z.infer<typeof HoppGQLAuth>
export const V4_SCHEMA = V3_SCHEMA.extend({
v: z.literal(4),
auth: HoppGQLAuth,
})
export default defineVersion({
initial: false,
schema: V4_SCHEMA,
up(old: z.infer<typeof V3_SCHEMA>) {
if (old.auth.authType === "api-key") {
return {
...old,
v: 4 as const,
auth: {
...old.auth,
addTo:
old.auth.addTo === "Query params"
? ("QUERY_PARAMS" as const)
: ("HEADERS" as const),
},
}
}
return {
...old,
v: 4 as const,
auth: {
...old.auth,
},
}
},
})

View File

@@ -5,12 +5,13 @@ import V0_VERSION from "./v/0"
import V1_VERSION from "./v/1" import V1_VERSION from "./v/1"
import V2_VERSION from "./v/2" import V2_VERSION from "./v/2"
import V3_VERSION from "./v/3" import V3_VERSION from "./v/3"
import V4_VERSION from "./v/4"
import { createVersionedEntity, InferredEntity } from "verzod" import { createVersionedEntity, InferredEntity } from "verzod"
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq" import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
import { HoppRESTReqBody, HoppRESTHeaders, HoppRESTParams } from "./v/1" import { HoppRESTReqBody, HoppRESTHeaders, HoppRESTParams } from "./v/1"
import { HoppRESTAuth } from "./v/3" import { HoppRESTAuth } from "./v/4"
import { HoppRESTRequestVariables } from "./v/2" import { HoppRESTRequestVariables } from "./v/2"
import { z } from "zod" import { z } from "zod"
@@ -19,7 +20,6 @@ export * from "./content-types"
export { export {
FormDataKeyValue, FormDataKeyValue,
HoppRESTReqBodyFormData, HoppRESTReqBodyFormData,
HoppRESTAuthAPIKey,
HoppRESTAuthBasic, HoppRESTAuthBasic,
HoppRESTAuthInherit, HoppRESTAuthInherit,
HoppRESTAuthBearer, HoppRESTAuthBearer,
@@ -29,7 +29,6 @@ export {
} from "./v/1" } from "./v/1"
export { export {
HoppRESTAuth,
HoppRESTAuthOAuth2, HoppRESTAuthOAuth2,
AuthCodeGrantTypeParams, AuthCodeGrantTypeParams,
ClientCredentialsGrantTypeParams, ClientCredentialsGrantTypeParams,
@@ -37,6 +36,8 @@ export {
PasswordGrantTypeParams, PasswordGrantTypeParams,
} from "./v/3" } from "./v/3"
export { HoppRESTAuth, HoppRESTAuthAPIKey } from "./v/4"
export { HoppRESTRequestVariables } from "./v/2" export { HoppRESTRequestVariables } from "./v/2"
const versionedObject = z.object({ const versionedObject = z.object({
@@ -45,12 +46,13 @@ const versionedObject = z.object({
}) })
export const HoppRESTRequest = createVersionedEntity({ export const HoppRESTRequest = createVersionedEntity({
latestVersion: 3, latestVersion: 4,
versionMap: { versionMap: {
0: V0_VERSION, 0: V0_VERSION,
1: V1_VERSION, 1: V1_VERSION,
2: V2_VERSION, 2: V2_VERSION,
3: V3_VERSION, 3: V3_VERSION,
4: V4_VERSION,
}, },
getVersion(data) { getVersion(data) {
// For V1 onwards we have the v string storing the number // For V1 onwards we have the v string storing the number
@@ -92,7 +94,7 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
), ),
}) })
export const RESTReqSchemaVersion = "3" export const RESTReqSchemaVersion = "4"
export type HoppRESTParam = HoppRESTRequest["params"][number] export type HoppRESTParam = HoppRESTRequest["params"][number]
export type HoppRESTHeader = HoppRESTRequest["headers"][number] export type HoppRESTHeader = HoppRESTRequest["headers"][number]
@@ -187,7 +189,7 @@ export function makeRESTRequest(
export function getDefaultRESTRequest(): HoppRESTRequest { export function getDefaultRESTRequest(): HoppRESTRequest {
return { return {
v: "3", v: "4",
endpoint: "https://echo.hoppscotch.io", endpoint: "https://echo.hoppscotch.io",
name: "Untitled", name: "Untitled",
params: [], params: [],

View File

@@ -0,0 +1,69 @@
import { z } from "zod"
import { defineVersion } from "verzod"
import { HoppRESTAuthOAuth2, V3_SCHEMA } from "./3"
import {
HoppRESTAuthAPIKey as HoppRESTAuthAPIKeyOld,
HoppRESTAuthBasic,
HoppRESTAuthBearer,
HoppRESTAuthInherit,
HoppRESTAuthNone,
} from "./1"
// in this new version, we update the old 'Headers' and 'Query params' to be more consistent with OAuth addTo values
// also in the previous version addTo was a string, which prevented some bugs from being caught by the type system
// this version uses an enum, so we get the values as literals in the type system
export const HoppRESTAuthAPIKey = HoppRESTAuthAPIKeyOld.extend({
addTo: z.enum(["HEADERS", "QUERY_PARAMS"]).catch("HEADERS"),
})
export type HoppRESTAuthAPIKey = z.infer<typeof HoppRESTAuthAPIKey>
export const HoppRESTAuth = z
.discriminatedUnion("authType", [
HoppRESTAuthNone,
HoppRESTAuthInherit,
HoppRESTAuthBasic,
HoppRESTAuthBearer,
HoppRESTAuthOAuth2,
HoppRESTAuthAPIKey,
])
.and(
z.object({
authActive: z.boolean(),
})
)
export type HoppRESTAuth = z.infer<typeof HoppRESTAuth>
export const V4_SCHEMA = V3_SCHEMA.extend({
v: z.literal("4"),
auth: HoppRESTAuth,
})
export default defineVersion({
schema: V4_SCHEMA,
initial: false,
up(old: z.infer<typeof V3_SCHEMA>) {
if (old.auth.authType === "api-key") {
return {
...old,
v: "4" as const,
auth: {
...old.auth,
addTo:
old.auth.addTo === "Query params"
? ("QUERY_PARAMS" as const)
: ("HEADERS" as const),
},
}
}
return {
...old,
auth: {
...old.auth,
},
v: "4" as const,
}
},
})