From f1740862811c5063a00e6f2dfbbf6dc3f47b3b6a Mon Sep 17 00:00:00 2001 From: Edwin Fajardo <36389489+EdwinFajardoBarrera@users.noreply.github.com> Date: Fri, 17 Dec 2021 10:02:29 -0600 Subject: [PATCH] feat: (authentication) Api key based authentication [#2000] (#2021) Co-authored-by: Liyas Thomas --- .../components/http/Authorization.vue | 91 +++++++++++++++++++ .../helpers/utils/EffectiveURL.ts | 40 +++++--- packages/hoppscotch-app/locales/en.json | 3 +- 3 files changed, 122 insertions(+), 12 deletions(-) diff --git a/packages/hoppscotch-app/components/http/Authorization.vue b/packages/hoppscotch-app/components/http/Authorization.vue index cc312512a..fbc74ebba 100644 --- a/packages/hoppscotch-app/components/http/Authorization.vue +++ b/packages/hoppscotch-app/components/http/Authorization.vue @@ -70,6 +70,18 @@ $refs.authTypeOptions.tippy().hide() " /> +
@@ -213,6 +225,77 @@
+
+
+
+ +
+
+ +
+
+ + + + + + +
+
+
+
+
+ {{ $t("helpers.authorization") }} +
+ +
+
+
@@ -222,6 +305,7 @@ import { HoppRESTAuthBasic, HoppRESTAuthBearer, HoppRESTAuthOAuth2, + HoppRESTAuthApiKey, } from "@hoppscotch/data" import { pluckRef, useStream } from "~/helpers/utils/composables" import { restAuth$, setRESTAuth } from "~/newstore/RESTSession" @@ -239,6 +323,7 @@ export default defineComponent({ if (authType.value === "basic") return "Basic Auth" else if (authType.value === "bearer") return "Bearer" else if (authType.value === "oauth-2") return "OAuth 2.0" + else if (authType.value === "api-key") return "API key" else return "None" }) const authActive = pluckRef(auth, "authActive") @@ -246,6 +331,9 @@ export default defineComponent({ const basicPassword = pluckRef(auth as Ref, "password") const bearerToken = pluckRef(auth as Ref, "token") const oauth2Token = pluckRef(auth as Ref, "token") + const apiKey = pluckRef(auth as Ref, "key") + const apiValue = pluckRef(auth as Ref, "value") + const addTo = pluckRef(auth as Ref, "addTo") const URLExcludes = useSetting("URL_EXCLUDES") const clearContent = () => { auth.value = { @@ -264,6 +352,9 @@ export default defineComponent({ oauth2Token, URLExcludes, clearContent, + apiKey, + apiValue, + addTo, } }, }) diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index 443caf3e9..c0b666869 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -76,6 +76,18 @@ export function getEffectiveRESTRequest( value: parseTemplateString(x.value, envVariables), })) + const effectiveFinalParams = request.params + .filter( + (x) => + x.key !== "" && // Remove empty keys + x.active // Only active + ) + .map((x) => ({ + active: true, + key: parseTemplateString(x.key, envVariables), + value: parseTemplateString(x.value, envVariables), + })) + // Authentication if (request.auth.authActive) { // TODO: Support a better b64 implementation than btoa ? @@ -100,6 +112,22 @@ export function getEffectiveRESTRequest( envVariables )}`, }) + } else if (request.auth.authType === "api-key") { + const { key, value, addTo } = request.auth + + if (addTo === "Header") { + effectiveFinalHeaders.push({ + active: true, + key: parseTemplateString(key, envVariables), + value: parseTemplateString(value, envVariables), + }) + } else if (addTo === "Query params") { + effectiveFinalParams.push({ + active: true, + key: parseTemplateString(key, envVariables), + value: parseTemplateString(value, envVariables), + }) + } } } @@ -115,17 +143,7 @@ export function getEffectiveRESTRequest( ...request, effectiveFinalURL: parseTemplateString(request.endpoint, envVariables), effectiveFinalHeaders, - effectiveFinalParams: request.params - .filter( - (x) => - x.key !== "" && // Remove empty keys - x.active // Only active - ) - .map((x) => ({ - active: true, - key: parseTemplateString(x.key, envVariables), - value: parseTemplateString(x.value, envVariables), - })), + effectiveFinalParams, effectiveFinalBody, } } diff --git a/packages/hoppscotch-app/locales/en.json b/packages/hoppscotch-app/locales/en.json index 979f021ec..7dc109fed 100644 --- a/packages/hoppscotch-app/locales/en.json +++ b/packages/hoppscotch-app/locales/en.json @@ -96,7 +96,8 @@ "password": "Password", "token": "Token", "type": "Authorization Type", - "username": "Username" + "username": "Username", + "pass_key_by": "Pass by" }, "collection": { "created": "Collection created",