Compare commits

..

3 Commits

Author SHA1 Message Date
Balu Babu
ab495177da chore: changed file import size limit to 10mb 2024-03-19 17:09:35 +05:30
Balu Babu
a3e6bae032 chore: changed target of hopp-old-backend service to prod 2024-03-19 17:09:35 +05:30
Balu Babu
1b19b8aed5 refactor: modified title search to be a query param 2024-03-19 17:09:35 +05:30
50 changed files with 398 additions and 3716 deletions

View File

@@ -17,7 +17,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io",
"name": "RequestD",
"params": [],
@@ -53,7 +53,7 @@
],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io",
"name": "RequestC",
"params": [],
@@ -90,7 +90,7 @@
],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io",
"name": "RequestB",
"params": [],
@@ -119,7 +119,7 @@
],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io",
"name": "RequestA",
"params": [],
@@ -162,7 +162,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io",
"name": "RequestB",
"params": [],
@@ -191,7 +191,7 @@
],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io",
"name": "RequestA",
"params": [],

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "<<URL>>",
"name": "test1",
"params": [],

View File

@@ -5,7 +5,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io/<<HEADERS_TYPE1>>",
"name": "",
"params": [],
@@ -13,7 +13,10 @@
"method": "GET",
"auth": {
"authType": "none",
"authActive": true
"authActive": true,
"addTo": "Headers",
"key": "",
"value": ""
},
"preRequestScript": "pw.env.set(\"HEADERS_TYPE1\", \"devblin_local1\");",
"testScript": "// Check status code is 200\npwd.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(200);\n});\n\n// Check JSON response property\npw.test(\"Check JSON response property\", ()=> {\n pw.expect(pw.response.body.method).toBe(\"GET\");\n pw.expect(pw.response.body.headers).toBeType(\"string\");\n});",
@@ -21,10 +24,10 @@
"contentType": "application/json",
"body": "{\n\"test\": \"<<HEADERS_TYPE1>>\"\n}"
},
"requestVariables": []
"requestVariables": [],
},
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.dio/<<HEADERS_TYPE2>>",
"name": "success",
"params": [],
@@ -32,7 +35,10 @@
"method": "GET",
"auth": {
"authType": "none",
"authActive": true
"authActive": true,
"addTo": "Headers",
"key": "",
"value": ""
},
"preRequestScript": "pw.env.setd(\"HEADERS_TYPE2\", \"devblin_local2\");",
"testScript": "// Check status code is 200\npw.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(300);\n});\n\n// Check JSON response property\npw.test(\"Check JSON response property\", ()=> {\n pw.expect(pw.response.body.method).toBe(\"GET\");\n pw.expect(pw.response.body.headers).toBeType(\"object\");\n});",

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests":
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io/<<HEADERS_TYPE1>>",
"name": "fail",
"params": [],
@@ -12,7 +12,10 @@
"method": "GET",
"auth": {
"authType": "none",
"authActive": true
"authActive": true,
"addTo": "Headers",
"key": "",
"value": ""
},
"preRequestScript": "pw.env.set(\"HEADERS_TYPE1\", \"devblin_local1\");",
"testScript": "// Check status code is 200\npw.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(200);\n});\n\n// Check JSON response property\npw.test(\"Check JSON response property\", ()=> {\n pw.expect(pw.response.body.method).toBe(\"GET\");\n pw.expect(pw.response.body.headers).toBeType(\"string\");\n});",
@@ -23,7 +26,7 @@
"requestVariables": [],
},
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io/<<HEADERS_TYPE2>>",
"name": "success",
"params": [],
@@ -31,7 +34,10 @@
"method": "GET",
"auth": {
"authType": "none",
"authActive": true
"authActive": true,
"addTo": "Headers",
"key": "",
"value": ""
},
"preRequestScript": "pw.env.set(\"HEADERS_TYPE2\", \"devblin_local2\");",
"testScript": "// Check status code is 200\npw.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(300);\n});\n\n// Check JSON response property\npw.test(\"Check JSON response property\", ()=> {\n pw.expect(pw.response.body.method).toBe(\"GET\");\n pw.expect(pw.response.body.headers).toBeType(\"object\");\n});",

View File

@@ -5,7 +5,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io/<<HEADERS_TYPE1>>",
"name": "",
"params": [],
@@ -13,7 +13,10 @@
"method": "GET",
"auth": {
"authType": "none",
"authActive": true
"authActive": true,
"addTo": "Headers",
"key": "",
"value": ""
},
"preRequestScript": "pw.env.set(\"HEADERS_TYPE1\", \"devblin_local1\");",
"testScript": "// Check status code is 200\npw.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(200);\n});\n\n// Check JSON response property\npw.test(\"Check JSON response property\", ()=> {\n pw.expect(pw.response.body.method).toBe(\"GET\");\n pw.expect(pw.response.body.headers).toBeType(\"object\");\n});",
@@ -24,7 +27,7 @@
"requestVariables": []
},
{
"v": "3",
"v": "2",
"endpoint": "https://echo.hoppscotch.io/<<HEADERS_TYPE2>>",
"name": "success",
"params": [],
@@ -32,7 +35,10 @@
"method": "GET",
"auth": {
"authType": "none",
"authActive": true
"authActive": true,
"addTo": "Headers",
"key": "",
"value": ""
},
"preRequestScript": "pw.env.set(\"HEADERS_TYPE2\", \"devblin_local2\");",
"testScript": "// Check status code is 200\npw.test(\"Status code is 200\", ()=> {\n pw.expect(pw.response.status).toBe(200);\n});\n\n// Check JSON response property\npw.test(\"Check JSON response property\", ()=> {\n pw.expect(pw.response.body.method).toBe(\"GET\");\n pw.expect(pw.response.body.headers).toBeType(\"object\");\n});",

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"auth": { "authType": "none", "authActive": true },
"body": { "body": null, "contentType": null },
"name": "sample-req",

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"name": "test-request",
"endpoint": "https://echo.hoppscotch.io",
"method": "POST",

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"auth": { "authType": "none", "authActive": true },
"body": { "body": null, "contentType": null },
"name": "test-secret-headers",
@@ -23,7 +23,7 @@
"preRequestScript": "const secretHeaderValueFromPreReqScript = pw.env.get(\"secretHeaderValue\")\npw.env.set(\"secretHeaderValueFromPreReqScript\", secretHeaderValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": { "authType": "none", "authActive": true },
"body": {
"body": "{\n \"secretBodyKey\": \"<<secretBodyValue>>\"\n}",
@@ -39,7 +39,7 @@
"preRequestScript": "const secretBodyValueFromPreReqScript = pw.env.get(\"secretBodyValue\")\npw.env.set(\"secretBodyValueFromPreReqScript\", secretBodyValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": { "authType": "none", "authActive": true },
"body": { "body": null, "contentType": null },
"name": "test-secret-query-params",
@@ -58,7 +58,7 @@
"preRequestScript": "const secretQueryParamValueFromPreReqScript = pw.env.get(\"secretQueryParamValue\")\npw.env.set(\"secretQueryParamValueFromPreReqScript\", secretQueryParamValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": {
"authType": "basic",
"password": "<<secretBasicAuthPassword>>",
@@ -76,7 +76,7 @@
"preRequestScript": ""
},
{
"v": "3",
"v": "2",
"auth": {
"token": "<<secretBearerToken>>",
"authType": "bearer",
@@ -95,7 +95,7 @@
"preRequestScript": "const secretBearerToken = pw.env.get(\"secretBearerToken\")\npw.env.set(\"preReqSecretBearerToken\", secretBearerToken)"
},
{
"v": "3",
"v": "2",
"auth": { "authType": "none", "authActive": true },
"body": { "body": null, "contentType": null },
"name": "test-secret-fallback",

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"auth": {
"authType": "none",
"authActive": true
@@ -29,7 +29,7 @@
"preRequestScript": "pw.env.set(\"secretHeaderValue\", \"secret-header-value\")\n\nconst secretHeaderValueFromPreReqScript = pw.env.getResolve(\"secretHeaderValue\")\npw.env.set(\"secretHeaderValueFromPreReqScript\", secretHeaderValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": {
"authType": "none",
"authActive": true
@@ -54,7 +54,7 @@
"preRequestScript": "pw.env.set(\"secretHeaderValue\", \"secret-header-value-overriden\")\n\nconst secretHeaderValueFromPreReqScript = pw.env.getResolve(\"secretHeaderValue\")\npw.env.set(\"secretHeaderValueFromPreReqScript\", secretHeaderValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": {
"authType": "none",
"authActive": true
@@ -73,7 +73,7 @@
"preRequestScript": "const secretBodyValue = pw.env.get(\"secretBodyValue\")\n\nif (!secretBodyValue) { \n pw.env.set(\"secretBodyValue\", \"secret-body-value\")\n}\n\nconst secretBodyValueFromPreReqScript = pw.env.get(\"secretBodyValue\")\npw.env.set(\"secretBodyValueFromPreReqScript\", secretBodyValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": {
"authType": "none",
"authActive": true
@@ -98,7 +98,7 @@
"preRequestScript": "const secretQueryParamValue = pw.env.get(\"secretQueryParamValue\")\n\nif (!secretQueryParamValue) {\n pw.env.set(\"secretQueryParamValue\", \"secret-query-param-value\")\n}\n\nconst secretQueryParamValueFromPreReqScript = pw.env.get(\"secretQueryParamValue\")\npw.env.set(\"secretQueryParamValueFromPreReqScript\", secretQueryParamValueFromPreReqScript)"
},
{
"v": "3",
"v": "2",
"auth": {
"authType": "basic",
"password": "<<secretBasicAuthPassword>>",
@@ -119,7 +119,7 @@
"preRequestScript": "let secretBasicAuthUsername = pw.env.get(\"secretBasicAuthUsername\")\n\nlet secretBasicAuthPassword = pw.env.get(\"secretBasicAuthPassword\")\n\nif (!secretBasicAuthUsername) {\n pw.env.set(\"secretBasicAuthUsername\", \"test-user\")\n}\n\nif (!secretBasicAuthPassword) {\n pw.env.set(\"secretBasicAuthPassword\", \"test-pass\")\n}"
},
{
"v": "3",
"v": "2",
"auth": {
"token": "<<secretBearerToken>>",
"authType": "bearer",

View File

@@ -4,7 +4,7 @@
"folders": [],
"requests": [
{
"v": "3",
"v": "2",
"endpoint": "https://httpbin.org/post",
"name": "req",
"params": [],

View File

@@ -6,7 +6,7 @@ import { error } from "../../types/errors";
import {
HoppEnvKeyPairObject,
HoppEnvPair,
HoppEnvs,
HoppEnvs
} from "../../types/request";
import { readJsonFile } from "../../utils/mutators";
@@ -17,7 +17,7 @@ import { readJsonFile } from "../../utils/mutators";
*/
export async function parseEnvsData(path: string) {
const contents = await readJsonFile(path);
const envPairs: Array<HoppEnvPair | Record<string, string>> = [];
const envPairs: Array<Environment["variables"][number] | HoppEnvPair> = [];
// The legacy key-value pair format that is still supported
const HoppEnvKeyPairResult = HoppEnvKeyPairObject.safeParse(contents);
@@ -26,9 +26,7 @@ export async function parseEnvsData(path: string) {
const HoppEnvExportObjectResult = Environment.safeParse(contents);
// Shape of the bulk environment export object that is exported from the app
const HoppBulkEnvExportObjectResult = z
.array(entityReference(Environment))
.safeParse(contents);
const HoppBulkEnvExportObjectResult = z.array(entityReference(Environment)).safeParse(contents)
// CLI doesnt support bulk environments export
// Hence we check for this case and throw an error if it matches the format
@@ -38,10 +36,7 @@ export async function parseEnvsData(path: string) {
// Checks if the environment file is of the correct format
// If it doesnt match either of them, we throw an error
if (
!HoppEnvKeyPairResult.success &&
HoppEnvExportObjectResult.type === "err"
) {
if (!HoppEnvKeyPairResult.success && HoppEnvExportObjectResult.type === "err") {
throw error({ code: "MALFORMED_ENV_FILE", path, data: error });
}

View File

@@ -109,31 +109,18 @@ export function getEffectiveRESTRequest(
key: "Authorization",
value: `Basic ${btoa(`${username}:${password}`)}`,
});
} else if (request.auth.authType === "bearer") {
} else if (
request.auth.authType === "bearer" ||
request.auth.authType === "oauth-2"
) {
effectiveFinalHeaders.push({
active: true,
key: "Authorization",
value: `Bearer ${parseTemplateString(request.auth.token, envVariables)}`,
value: `Bearer ${parseTemplateString(
request.auth.token,
envVariables
)}`,
});
} else if (request.auth.authType === "oauth-2") {
const { addTo } = request.auth;
if (addTo === "HEADERS") {
effectiveFinalHeaders.push({
active: true,
key: "Authorization",
value: `Bearer ${parseTemplateString(request.auth.grantTypeInfo.token, envVariables)}`,
});
} else if (addTo === "QUERY_PARAMS") {
effectiveFinalParams.push({
active: true,
key: "access_token",
value: parseTemplateString(
request.auth.grantTypeInfo.token,
envVariables
),
});
}
} else if (request.auth.authType === "api-key") {
const { key, value, addTo } = request.auth;
if (addTo === "Headers") {

View File

@@ -41,10 +41,10 @@ const processVariables = (variable: Environment["variables"][number]) => {
...variable,
value:
"value" in variable ? variable.value : process.env[variable.key] || "",
};
}
}
return variable;
};
return variable
}
/**
* Processes given envs, which includes processing each variable in global
@@ -56,10 +56,10 @@ const processEnvs = (envs: HoppEnvs) => {
const processedEnvs = {
global: envs.global.map(processVariables),
selected: envs.selected.map(processVariables),
};
}
return processedEnvs;
};
return processedEnvs
}
/**
* Transforms given request data to request-config used by request-runner to
@@ -70,7 +70,7 @@ const processEnvs = (envs: HoppEnvs) => {
export const createRequest = (req: EffectiveHoppRESTRequest): RequestConfig => {
const config: RequestConfig = {
supported: true,
displayUrl: req.effectiveFinalDisplayURL,
displayUrl: req.effectiveFinalDisplayURL
};
const { finalBody, finalEndpoint, finalHeaders, finalParams } = getRequest;
const reqParams = finalParams(req);
@@ -131,7 +131,6 @@ export const requestRunner =
let status: number;
const baseResponse = await axios(requestConfig);
const { config } = baseResponse;
// PR-COMMENT: type error
const runnerResponse: RequestRunnerResponse = {
...baseResponse,
endpoint: getRequest.endpoint(config.url),
@@ -258,13 +257,10 @@ export const processRequest =
let updatedEnvs = <HoppEnvs>{};
// Fetch values for secret environment variables from system environment
const processedEnvs = processEnvs(envs);
const processedEnvs = processEnvs(envs)
// Executing pre-request-script
const preRequestRes = await preRequestScriptRunner(
request,
processedEnvs
)();
const preRequestRes = await preRequestScriptRunner(request, processedEnvs)();
if (E.isLeft(preRequestRes)) {
printPreRequestRunner.fail();
@@ -351,7 +347,7 @@ export const processRequest =
*/
export const preProcessRequest = (
request: HoppRESTRequest,
collection: HoppCollection
collection: HoppCollection,
): HoppRESTRequest => {
const tempRequest = Object.assign({}, request);
const { headers: parentHeaders, auth: parentAuth } = collection;
@@ -376,10 +372,8 @@ export const preProcessRequest = (
// Filter out header entries present in the parent (folder/collection) under the same name
// This ensures the child headers take precedence over the parent headers
const filteredEntries = parentHeaders.filter((parentHeaderEntries) => {
return !tempRequest.headers.some(
(reqHeaderEntries) => reqHeaderEntries.key === parentHeaderEntries.key
);
});
return !tempRequest.headers.some((reqHeaderEntries) => reqHeaderEntries.key === parentHeaderEntries.key)
})
tempRequest.headers.push(...filteredEntries);
} else if (!tempRequest.headers) {
tempRequest.headers = [];

View File

@@ -103,10 +103,8 @@
"auth": {
"account_exists": "Account exists with different credential - Login to link both accounts",
"all_sign_in_options": "All sign in options",
"continue_with_auth_provider": "Continue with {provider}",
"continue_with_email": "Continue with Email",
"continue_with_github": "Continue with GitHub",
"continue_with_github_enterprise": "Continue with GitHub Enterprise",
"continue_with_google": "Continue with Google",
"continue_with_microsoft": "Continue with Microsoft",
"email": "Email",
@@ -139,26 +137,7 @@
"redirect_no_token_endpoint": "No Token Endpoint Defined",
"something_went_wrong_on_oauth_redirect": "Something went wrong during OAuth Redirect",
"something_went_wrong_on_token_generation": "Something went wrong on token generation",
"token_generation_oidc_discovery_failed": "Failure on token generation: OpenID Connect Discovery Failed",
"grant_type": "Grant Type",
"grant_type_auth_code": "Authorization Code",
"token_fetched_successfully": "Token fetched successfully",
"token_fetch_failed": "Failed to fetch token",
"validation_failed": "Validation Failed, please check the form fields",
"label_authorization_endpoint": "Authorization Endpoint",
"label_client_id": "Client ID",
"label_client_secret": "Client Secret",
"label_code_challenge": "Code Challenge",
"label_code_challenge_method": "Code Challenge Method",
"label_code_verifier": "Code Verifier",
"label_scopes": "Scopes",
"label_token_endpoint": "Token Endpoint",
"label_use_pkce": "Use PKCE",
"label_implicit": "Implicit",
"label_password": "Password",
"label_username": "Username",
"label_auth_code": "Authorization Code",
"label_client_credentials": "Client Credentials"
"token_generation_oidc_discovery_failed": "Failure on token generation: OpenID Connect Discovery Failed"
},
"pass_key_by": "Pass by",
"password": "Password",
@@ -302,7 +281,7 @@
"updated": "Environment updated",
"value": "Value",
"variable": "Variable",
"variables": "Variables",
"variables":"Variables",
"variable_list": "Variable List"
},
"error": {
@@ -982,8 +961,7 @@
"success_invites": "Success invites",
"title": "Workspaces",
"we_sent_invite_link": "We sent an invite link to all invitees!",
"we_sent_invite_link_description": "Ask all invitees to check their inbox. Click on the link to join the workspace.",
"search_title": "Team Requests"
"we_sent_invite_link_description": "Ask all invitees to check their inbox. Click on the link to join the workspace."
},
"team_environment": {
"deleted": "Environment Deleted",

View File

@@ -1,7 +1,5 @@
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-components
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
import "@vue/runtime-core"

View File

@@ -1,32 +0,0 @@
<template>
<span class="flex flex-1 items-center space-x-2">
<template v-for="(title, index) in collectionTitles" :key="index">
<span class="block" :class="{ truncate: index !== 0 }">
{{ title }}
</span>
<icon-lucide-chevron-right class="flex flex-shrink-0" />
</template>
<span
v-if="request"
class="flex flex-shrink-0 truncate rounded-md border border-dividerDark px-1 text-tiny font-semibold"
:style="{ color: getMethodLabelColor(request.method) }"
>
{{ request.method.toUpperCase() }}
</span>
<span v-if="request" class="block">
{{ request.name }}
</span>
</span>
</template>
<script setup lang="ts">
import { getMethodLabelColor } from "~/helpers/rest/labelColoring"
defineProps<{
collectionTitles: string[]
request: {
name: string
method: string
}
}>()
</script>

View File

@@ -111,7 +111,6 @@ import { RequestSpotlightSearcherService } from "~/services/spotlight/searchers/
import { ResponseSpotlightSearcherService } from "~/services/spotlight/searchers/response.searcher"
import { SettingsSpotlightSearcherService } from "~/services/spotlight/searchers/settings.searcher"
import { TabSpotlightSearcherService } from "~/services/spotlight/searchers/tab.searcher"
import { TeamsSpotlightSearcherService } from "~/services/spotlight/searchers/teamRequest.searcher"
import { UserSpotlightSearcherService } from "~/services/spotlight/searchers/user.searcher"
import {
SwitchWorkspaceSpotlightSearcherService,
@@ -145,7 +144,6 @@ useService(SwitchEnvSpotlightSearcherService)
useService(WorkspaceSpotlightSearcherService)
useService(SwitchWorkspaceSpotlightSearcherService)
useService(InterceptorSpotlightSearcherService)
useService(TeamsSpotlightSearcherService)
platform.spotlight?.additionalSearchers?.forEach((searcher) =>
useService(searcher)

View File

@@ -8,7 +8,7 @@
>
<template #body>
<HoppSmartTabs
v-model="activeTab"
v-model="selectedOptionTab"
styles="sticky overflow-x-auto flex-shrink-0 bg-primary top-0 z-10 !-py-4"
render-inactive-tabs
>
@@ -16,6 +16,7 @@
<HttpHeaders
v-model="editableCollection"
:is-collection-property="true"
@change-tab="changeOptionTab"
/>
<div
class="bg-bannerInfo px-4 py-2 flex items-center sticky bottom-0"
@@ -33,7 +34,6 @@
:is-collection-property="true"
:is-root-collection="editingProperties?.isRootCollection"
:inherited-properties="editingProperties?.inheritedProperties"
:source="source"
/>
<div
class="bg-bannerInfo px-4 py-2 flex items-center sticky bottom-0"
@@ -64,20 +64,16 @@
</template>
<script setup lang="ts">
import { watch, ref } from "vue"
import { useI18n } from "@composables/i18n"
import { HoppCollection, HoppRESTAuth, HoppRESTHeaders } from "@hoppscotch/data"
import { RESTOptionTabs } from "../http/RequestOptions.vue"
import { clone } from "lodash-es"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { PersistenceService } from "~/services/persistence"
import { useService } from "dioc/vue"
import { ref, watch } from "vue"
import { useVModel } from "@vueuse/core"
const persistenceService = useService(PersistenceService)
const t = useI18n()
export type EditingProperties = {
type EditingProperties = {
collection: Partial<HoppCollection> | null
isRootCollection: boolean
path: string
@@ -89,8 +85,6 @@ const props = withDefaults(
show: boolean
loadingState: boolean
editingProperties: EditingProperties | null
source: "REST" | "GraphQL"
modelValue: string
}>(),
{
show: false,
@@ -105,7 +99,6 @@ const emit = defineEmits<{
newCollection: Omit<EditingProperties, "inheritedProperties">
): void
(e: "hide-modal"): void
(e: "update:modelValue"): void
}>()
const editableCollection = ref<{
@@ -119,27 +112,11 @@ const editableCollection = ref<{
},
})
watch(
editableCollection,
(updatedEditableCollection) => {
if (props.show) {
persistenceService.setLocalConfig(
"unsaved_collection_properties",
JSON.stringify(<EditingProperties>{
collection: updatedEditableCollection,
isRootCollection: props.editingProperties?.isRootCollection,
path: props.editingProperties?.path,
inheritedProperties: props.editingProperties?.inheritedProperties,
})
)
}
},
{
deep: true,
}
)
const selectedOptionTab = ref("headers")
const activeTab = useVModel(props, "modelValue", emit)
const changeOptionTab = (tab: RESTOptionTabs) => {
selectedOptionTab.value = tab
}
watch(
() => props.show,
@@ -159,8 +136,6 @@ watch(
authActive: false,
},
}
persistenceService.removeLocalConfig("unsaved_collection_properties")
}
}
)
@@ -177,11 +152,9 @@ const saveEditedCollection = () => {
isRootCollection: props.editingProperties.isRootCollection,
}
emit("set-collection-properties", collection as EditingProperties)
persistenceService.removeLocalConfig("unsaved_collection_properties")
}
const hideModal = () => {
persistenceService.removeLocalConfig("unsaved_collection_properties")
emit("hide-modal")
}
</script>

View File

@@ -9,7 +9,7 @@
"
>
<HoppButtonSecondary
v-if="hasNoTeamAccess || isShowingSearchResults"
v-if="hasNoTeamAccess"
v-tippy="{ theme: 'tooltip' }"
disabled
class="!rounded-none"
@@ -36,9 +36,8 @@
v-if="!saveRequest"
v-tippy="{ theme: 'tooltip' }"
:disabled="
(collectionsType.type === 'team-collections' &&
collectionsType.selectedTeam === undefined) ||
isShowingSearchResults
collectionsType.type === 'team-collections' &&
collectionsType.selectedTeam === undefined
"
:icon="IconImport"
:title="t('modal.import_export')"
@@ -59,7 +58,7 @@
:collections-type="collectionsType.type"
:is-open="isOpen"
:export-loading="exportLoading"
:has-no-team-access="hasNoTeamAccess || isShowingSearchResults"
:has-no-team-access="hasNoTeamAccess"
:collection-move-loading="collectionMoveLoading"
:is-last-item="node.data.isLastItem"
:is-selected="
@@ -129,14 +128,6 @@
})
}
"
@click="
() => {
handleCollectionClick({
collectionID: node.id,
isOpen,
})
}
"
/>
<CollectionsCollection
v-if="node.data.type === 'folders'"
@@ -146,7 +137,7 @@
:collections-type="collectionsType.type"
:is-open="isOpen"
:export-loading="exportLoading"
:has-no-team-access="hasNoTeamAccess || isShowingSearchResults"
:has-no-team-access="hasNoTeamAccess"
:collection-move-loading="collectionMoveLoading"
:is-last-item="node.data.isLastItem"
:is-selected="
@@ -218,15 +209,6 @@
})
}
"
@click="
() => {
handleCollectionClick({
// for the folders, we get a path, so we need to get the last part of the path which is the folder id
collectionID: node.id.split('/').pop() as string,
isOpen,
})
}
"
/>
<CollectionsRequest
v-if="node.data.type === 'requests'"
@@ -236,7 +218,7 @@
:collections-type="collectionsType.type"
:duplicate-loading="duplicateLoading"
:is-active="isActiveRequest(node.data.data.data.id)"
:has-no-team-access="hasNoTeamAccess || isShowingSearchResults"
:has-no-team-access="hasNoTeamAccess"
:request-move-loading="requestMoveLoading"
:is-last-item="node.data.isLastItem"
:is-selected="
@@ -301,15 +283,7 @@
</template>
<template #emptyNode="{ node }">
<HoppSmartPlaceholder
v-if="filterText.length !== 0 && teamCollectionList.length === 0"
:text="`${t('state.nothing_found')}${filterText}`"
>
<template #icon>
<icon-lucide-search class="svg-icons opacity-75" />
</template>
</HoppSmartPlaceholder>
<HoppSmartPlaceholder
v-else-if="node === null"
v-if="node === null"
:src="`/images/states/${colorMode.value}/pack.svg`"
:alt="`${t('empty.collections')}`"
:text="t('empty.collections')"
@@ -420,11 +394,6 @@ const props = defineProps({
default: () => ({ type: "my-collections", selectedTeam: undefined }),
required: true,
},
filterText: {
type: String as PropType<string>,
default: "",
required: true,
},
teamCollectionList: {
type: Array as PropType<TeamCollection[]>,
default: () => [],
@@ -467,8 +436,6 @@ const props = defineProps({
},
})
const isShowingSearchResults = computed(() => props.filterText.length > 0)
const emit = defineEmits<{
(
event: "add-request",
@@ -576,14 +543,6 @@ const emit = defineEmits<{
}
}
): void
(
event: "collection-click",
payload: {
// if the collection is open or not in the tree
isOpen: boolean
collectionID: string
}
): void
(event: "select", payload: Picked | null): void
(event: "expand-team-collection", payload: string): void
(event: "display-modal-add"): void
@@ -596,18 +555,6 @@ const getPath = (path: string) => {
return pathArray.join("/")
}
const handleCollectionClick = (payload: {
collectionID: string
isOpen: boolean
}) => {
const { collectionID, isOpen } = payload
emit("collection-click", {
collectionID,
isOpen,
})
}
const teamCollectionsList = toRef(props, "teamCollectionList")
const hasNoTeamAccess = computed(

View File

@@ -146,10 +146,8 @@
@hide-modal="displayModalImportExport(false)"
/>
<CollectionsProperties
v-model="collectionPropertiesModalActiveTab"
:show="showModalEditProperties"
:editing-properties="editingProperties"
source="GraphQL"
@hide-modal="displayModalEditProperties(false)"
@set-collection-properties="setCollectionProperties"
/>
@@ -157,7 +155,7 @@
</template>
<script setup lang="ts">
import { nextTick, onMounted, ref } from "vue"
import { nextTick, ref } from "vue"
import { clone, cloneDeep } from "lodash-es"
import {
graphqlCollections$,
@@ -180,7 +178,6 @@ import { GQLTabService } from "~/services/tab/graphql"
import { computed } from "vue"
import {
HoppCollection,
HoppGQLAuth,
HoppGQLRequest,
makeGQLRequest,
} from "@hoppscotch/data"
@@ -189,10 +186,6 @@ import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { updateInheritedPropertiesForAffectedRequests } from "~/helpers/collection/collection"
import { useToast } from "~/composables/toast"
import { getRequestsByPath } from "~/helpers/collection/request"
import { PersistenceService } from "~/services/persistence"
import { PersistedOAuthConfig } from "~/services/oauth/oauth.service"
import { GQLOptionTabs } from "~/components/graphql/RequestOptions.vue"
import { EditingProperties } from "../Properties.vue"
const t = useI18n()
const toast = useToast()
@@ -239,52 +232,6 @@ const editingProperties = ref<{
const filterText = ref("")
const persistenceService = useService(PersistenceService)
const collectionPropertiesModalActiveTab = ref<GQLOptionTabs>("headers")
onMounted(() => {
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
if (!localOAuthTempConfig) {
return
}
const { context, source, token }: PersistedOAuthConfig =
JSON.parse(localOAuthTempConfig)
if (source === "REST") {
return
}
if (context?.type === "collection-properties") {
// load the unsaved editing properties
const unsavedCollectionPropertiesString = persistenceService.getLocalConfig(
"unsaved_collection_properties"
)
if (unsavedCollectionPropertiesString) {
const unsavedCollectionProperties: EditingProperties<"GraphQL"> =
JSON.parse(unsavedCollectionPropertiesString)
const auth = unsavedCollectionProperties.collection?.auth
if (auth?.authType === "oauth-2") {
const grantTypeInfo = auth.grantTypeInfo
grantTypeInfo && (grantTypeInfo.token = token ?? "")
}
editingProperties.value = unsavedCollectionProperties
}
persistenceService.removeLocalConfig("oauth_temp_config")
collectionPropertiesModalActiveTab.value = "authorization"
showModalEditProperties.value = true
}
})
const filteredCollections = computed(() => {
const collectionsClone = clone(collections.value)

View File

@@ -24,6 +24,7 @@
autocomplete="off"
class="flex w-full bg-transparent px-4 py-2 h-8"
:placeholder="t('action.search')"
:disabled="collectionsType.type === 'team-collections'"
/>
</div>
<CollectionsMyCollections
@@ -57,15 +58,8 @@
<CollectionsTeamCollections
v-else
:collections-type="collectionsType"
:team-collection-list="
filterTexts.length > 0 ? teamsSearchResults : teamCollectionList
"
:team-loading-collections="
filterTexts.length > 0
? collectionsBeingLoadedFromSearch
: teamLoadingCollections
"
:filter-text="filterTexts"
:team-collection-list="teamCollectionList"
:team-loading-collections="teamLoadingCollections"
:export-loading="exportLoading"
:duplicate-loading="duplicateLoading"
:save-request="saveRequest"
@@ -93,7 +87,6 @@
@expand-team-collection="expandTeamCollection"
@display-modal-add="displayModalAdd(true)"
@display-modal-import-export="displayModalImportExport(true)"
@collection-click="handleCollectionClick"
/>
<div
class="py-15 hidden flex-1 flex-col items-center justify-center bg-primaryDark px-4 text-secondaryLight"
@@ -161,10 +154,8 @@
@hide-modal="displayTeamModalAdd(false)"
/>
<CollectionsProperties
v-model="collectionPropertiesModalActiveTab"
:show="showModalEditProperties"
:editing-properties="editingProperties"
source="REST"
@hide-modal="displayModalEditProperties(false)"
@set-collection-properties="setCollectionProperties"
/>
@@ -172,7 +163,7 @@
</template>
<script setup lang="ts">
import { computed, nextTick, onMounted, PropType, ref, watch } from "vue"
import { computed, nextTick, PropType, ref, watch } from "vue"
import { useToast } from "@composables/toast"
import { useI18n } from "@composables/i18n"
import { Picked } from "~/helpers/types/HoppPicked"
@@ -208,7 +199,7 @@ import {
HoppRESTRequest,
makeCollection,
} from "@hoppscotch/data"
import { cloneDeep, debounce, isEqual } from "lodash-es"
import { cloneDeep, isEqual } from "lodash-es"
import { GQLError } from "~/helpers/backend/GQLClient"
import {
createNewRootCollection,
@@ -249,11 +240,6 @@ import { WorkspaceService } from "~/services/workspace.service"
import { useService } from "dioc/vue"
import { RESTTabService } from "~/services/tab/rest"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { TeamSearchService } from "~/helpers/teams/TeamsSearch.service"
import { PersistenceService } from "~/services/persistence"
import { PersistedOAuthConfig } from "~/services/oauth/oauth.service"
import { RESTOptionTabs } from "../http/RequestOptions.vue"
import { EditingProperties } from "./Properties.vue"
const t = useI18n()
const toast = useToast()
@@ -305,7 +291,12 @@ const editingRequestName = ref("")
const editingRequestIndex = ref<number | null>(null)
const editingRequestID = ref<string | null>(null)
const editingProperties = ref<EditingProperties>({
const editingProperties = ref<{
collection: Partial<HoppCollection> | null
isRootCollection: boolean
path: string
inheritedProperties?: HoppInheritedProperty
}>({
collection: null,
isRootCollection: false,
path: "",
@@ -345,99 +336,6 @@ const teamLoadingCollections = useReadonlyStream(
[]
)
const {
cascadeParentCollectionForHeaderAuthForSearchResults,
searchTeams,
teamsSearchResults,
teamsSearchResultsLoading,
expandCollection,
expandingCollections,
} = useService(TeamSearchService)
watch(teamsSearchResults, (newSearchResults) => {
if (newSearchResults.length === 1 && filterTexts.value.length > 0) {
expandCollection(newSearchResults[0].id)
}
})
const debouncedSearch = debounce(searchTeams, 400)
const collectionsBeingLoadedFromSearch = computed(() => {
const collections = []
if (teamsSearchResultsLoading.value) {
collections.push("root")
}
collections.push(...expandingCollections.value)
return collections
})
watch(
filterTexts,
(newFilterText) => {
if (collectionsType.value.type === "team-collections") {
const selectedTeamID = collectionsType.value.selectedTeam?.id
selectedTeamID &&
debouncedSearch(newFilterText, selectedTeamID)?.catch((_) => {})
}
},
{
immediate: true,
}
)
const persistenceService = useService(PersistenceService)
const collectionPropertiesModalActiveTab = ref<RESTOptionTabs>("headers")
onMounted(() => {
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
if (!localOAuthTempConfig) {
return
}
const { context, source, token }: PersistedOAuthConfig =
JSON.parse(localOAuthTempConfig)
if (source === "GraphQL") {
return
}
if (context?.type === "collection-properties") {
// load the unsaved editing properties
const unsavedCollectionPropertiesString = persistenceService.getLocalConfig(
"unsaved_collection_properties"
)
if (unsavedCollectionPropertiesString) {
const unsavedCollectionProperties: EditingProperties<"REST"> = JSON.parse(
unsavedCollectionPropertiesString
)
// casting because the type `EditingProperties["collection"]["auth"] and the usage in Properties.vue is different. there it's casted as an any.
// FUTURE-TODO: look into this
// @ts-expect-error because of the above reason
const auth = unsavedCollectionProperties.collection?.auth as HoppRESTAuth
if (auth?.authType === "oauth-2") {
const grantTypeInfo = auth.grantTypeInfo
grantTypeInfo && (grantTypeInfo.token = token ?? "")
}
editingProperties.value = unsavedCollectionProperties
}
persistenceService.removeLocalConfig("oauth_temp_config")
collectionPropertiesModalActiveTab.value = "authorization"
showModalEditProperties.value = true
}
})
watch(
() => myTeams.value,
(newTeams) => {
@@ -466,28 +364,7 @@ const switchToMyCollections = () => {
teamCollectionAdapter.changeTeamID(null)
}
/**
* right now, for search results, we rely on collection click + isOpen to expand the collection
*/
const handleCollectionClick = (payload: {
collectionID: string
isOpen: boolean
}) => {
if (
filterTexts.value.length > 0 &&
teamsSearchResults.value.length &&
payload.isOpen
) {
expandCollection(payload.collectionID)
return
}
}
const expandTeamCollection = (collectionID: string) => {
if (filterTexts.value.length > 0 && teamsSearchResults.value) {
return
}
teamCollectionAdapter.expandCollection(collectionID)
}
@@ -1453,25 +1330,13 @@ const selectRequest = (selectedRequest: {
let possibleTab = null
if (collectionsType.value.type === "team-collections") {
let inheritedProperties: HoppInheritedProperty | undefined = undefined
const { auth, headers } =
teamCollectionAdapter.cascadeParentCollectionForHeaderAuth(folderPath)
if (filterTexts.value.length > 0) {
const collectionID = folderPath.split("/").at(-1)
if (!collectionID) return
inheritedProperties =
cascadeParentCollectionForHeaderAuthForSearchResults(collectionID)
} else {
inheritedProperties =
teamCollectionAdapter.cascadeParentCollectionForHeaderAuth(folderPath)
}
const possibleTab = tabs.getTabRefWithSaveContext({
possibleTab = tabs.getTabRefWithSaveContext({
originLocation: "team-collection",
requestID: requestIndex,
})
if (possibleTab) {
tabs.setActiveTab(possibleTab.value.id)
} else {
@@ -1483,7 +1348,10 @@ const selectRequest = (selectedRequest: {
requestID: requestIndex,
collectionID: folderPath,
},
inheritedProperties: inheritedProperties,
inheritedProperties: {
auth,
headers,
},
})
}
} else {

View File

@@ -23,10 +23,10 @@
@click="provider.action"
/>
<hr v-if="additionalLoginItems.length > 0" />
<hr v-if="additonalLoginItems.length > 0" />
<HoppSmartItem
v-for="loginItem in additionalLoginItems"
v-for="loginItem in additonalLoginItems"
:key="loginItem.id"
:icon="loginItem.icon"
:label="loginItem.text(t)"
@@ -170,7 +170,7 @@ type AuthProviderItem = {
}
let allowedAuthProviders: AuthProviderItem[] = []
const additionalLoginItems: LoginItemDef[] = []
let additonalLoginItems: LoginItemDef[] = []
const doAdditionalLoginItemClickAction = async (item: LoginItemDef) => {
await item.onClick()
@@ -199,33 +199,10 @@ onMounted(async () => {
allowedAuthProviders = enabledAuthProviders
// setup the additional login items
platform.auth.additionalLoginItems?.forEach((item) => {
if (res.right.includes(item.id)) {
additionalLoginItems.push(item)
}
// since the BE send the OIDC auth providers as OIDC:providerName,
// we need to split the string and use the providerName as the text
if (item.id === "OIDC") {
res.right
.filter((provider) => provider.startsWith("OIDC"))
.forEach((provider) => {
const OIDCName = provider.split(":")[1]
const loginItemText = OIDCName
? () =>
t("auth.continue_with_auth_provider", {
provider: OIDCName,
})
: item.text
const OIDCLoginItem = {
...item,
text: loginItemText,
}
additionalLoginItems.push(OIDCLoginItem)
})
}
})
additonalLoginItems =
platform.auth.additionalLoginItems?.filter((item) =>
res.right.includes(item.id)
) ?? []
isLoadingAllowedAuthProviders.value = false
})
@@ -334,14 +311,6 @@ const authProvidersAvailable: AuthProviderItem[] = [
action: signInWithGithub,
isLoading: signingInWithGitHub,
},
// the authprovider either send github or github:enterprise and both are handled by the same route
{
id: "GITHUB:ENTERPRISE",
icon: IconGithub,
label: t("auth.continue_with_github_enterprise"),
action: signInWithGithub,
isLoading: signingInWithGitHub,
},
{
id: "GOOGLE",
icon: IconGoogle,

View File

@@ -82,7 +82,7 @@
:active="authName === 'OAuth 2.0'"
@click="
() => {
selectOAuth2AuthType()
auth.authType = 'oauth-2'
hide()
}
"
@@ -189,12 +189,12 @@
<div v-if="auth.authType === 'oauth-2'">
<div class="flex flex-1 border-b border-dividerLight">
<SmartEnvInput
v-model="auth.grantTypeInfo.token"
v-model="auth.token"
:environment-highlights="false"
placeholder="Token"
/>
</div>
<HttpOAuth2Authorization v-model="auth" source="GraphQL" />
<HttpOAuth2Authorization v-model="auth" />
</div>
<div v-if="auth.authType === 'api-key'">
<HttpAuthorizationApiKey v-model="auth" />
@@ -220,22 +220,19 @@
</template>
<script setup lang="ts">
import { useI18n } from "@composables/i18n"
import { pluckRef } from "@composables/ref"
import { useColorMode } from "@composables/theming"
import { HoppGQLAuth, HoppGQLAuthOAuth2 } from "@hoppscotch/data"
import { useVModel } from "@vueuse/core"
import { computed, onMounted, ref } from "vue"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import IconCircle from "~icons/lucide/circle"
import IconCircleDot from "~icons/lucide/circle-dot"
import IconExternalLink from "~icons/lucide/external-link"
import IconHelpCircle from "~icons/lucide/help-circle"
import IconTrash2 from "~icons/lucide/trash-2"
import { getDefaultAuthCodeOauthFlowParams } from "~/services/oauth/flows/authCode"
import IconExternalLink from "~icons/lucide/external-link"
import IconCircleDot from "~icons/lucide/circle-dot"
import IconCircle from "~icons/lucide/circle"
import { computed, ref } from "vue"
import { HoppGQLAuth } from "@hoppscotch/data"
import { pluckRef } from "@composables/ref"
import { useI18n } from "@composables/i18n"
import { useColorMode } from "@composables/theming"
import { useVModel } from "@vueuse/core"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { onMounted } from "vue"
const t = useI18n()
@@ -283,30 +280,6 @@ const getAuthName = (type: HoppGQLAuth["authType"] | undefined) => {
return AUTH_KEY_NAME[type] ? AUTH_KEY_NAME[type] : "None"
}
const selectOAuth2AuthType = () => {
const defaultGrantTypeInfo: HoppGQLAuthOAuth2["grantTypeInfo"] = {
...getDefaultAuthCodeOauthFlowParams(),
grantType: "AUTHORIZATION_CODE",
token: "",
}
// @ts-expect-error - the existing grantTypeInfo might be in the auth object, typescript doesnt know that
const existingGrantTypeInfo = auth.value.grantTypeInfo as
| HoppGQLAuthOAuth2["grantTypeInfo"]
| undefined
const grantTypeInfo = existingGrantTypeInfo
? existingGrantTypeInfo
: defaultGrantTypeInfo
auth.value = <HoppGQLAuth>{
...auth.value,
authType: "oauth-2",
addTo: "HEADERS",
grantTypeInfo: grantTypeInfo,
}
}
const authActive = pluckRef(auth, "authActive")
const clearContent = () => {

View File

@@ -579,18 +579,12 @@ const getComputedAuthHeaders = (
})
} else if (
request.auth.authType === "bearer" ||
(request.auth.authType === "oauth-2" && request.auth.addTo === "HEADERS")
request.auth.authType === "oauth-2"
) {
const requestAuth = request.auth
const isOAuth2 = requestAuth.authType === "oauth-2"
const token = isOAuth2 ? requestAuth.grantTypeInfo.token : requestAuth.token
headers.push({
active: true,
key: "Authorization",
value: `Bearer ${token}`,
value: `Bearer ${request.auth.token}`,
})
} else if (request.auth.authType === "api-key") {
const { key, addTo } = request.auth

View File

@@ -82,7 +82,7 @@
:active="authName === 'OAuth 2.0'"
@click="
() => {
selectOAuth2AuthType()
auth.authType = 'oauth-2'
hide()
}
"
@@ -177,24 +177,15 @@
/>
</div>
</div>
<div v-if="auth.authType === 'oauth-2'" class="w-full">
<div v-if="auth.authType === 'oauth-2'">
<div class="flex flex-1 border-b border-dividerLight">
<!-- Ensure a new object is assigned here to avoid reactivity issues -->
<SmartEnvInput
:model-value="auth.grantTypeInfo.token"
v-model="auth.token"
placeholder="Token"
:envs="envs"
@update:model-value="
auth.grantTypeInfo = { ...auth.grantTypeInfo, token: $event }
"
/>
</div>
<HttpOAuth2Authorization
v-model="auth"
:is-collection-property="isCollectionProperty"
:envs="envs"
:source="source"
/>
<HttpOAuth2Authorization v-model="auth" :envs="envs" />
</div>
<div v-if="auth.authType === 'api-key'">
<HttpAuthorizationApiKey v-model="auth" :envs="envs" />
@@ -226,7 +217,7 @@ import IconExternalLink from "~icons/lucide/external-link"
import IconCircleDot from "~icons/lucide/circle-dot"
import IconCircle from "~icons/lucide/circle"
import { computed, ref } from "vue"
import { HoppRESTAuth, HoppRESTAuthOAuth2 } from "@hoppscotch/data"
import { HoppRESTAuth } from "@hoppscotch/data"
import { pluckRef } from "@composables/ref"
import { useI18n } from "@composables/i18n"
import { useColorMode } from "@composables/theming"
@@ -235,27 +226,17 @@ import { onMounted } from "vue"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { AggregateEnvironment } from "~/newstore/environments"
import { getDefaultAuthCodeOauthFlowParams } from "~/services/oauth/flows/authCode"
const t = useI18n()
const colorMode = useColorMode()
const props = withDefaults(
defineProps<{
modelValue: HoppRESTAuth
isCollectionProperty?: boolean
isRootCollection?: boolean
inheritedProperties?: HoppInheritedProperty
envs?: AggregateEnvironment[]
source?: "REST" | "GraphQL"
}>(),
{
source: "REST",
envs: undefined,
inheritedProperties: undefined,
}
)
const props = defineProps<{
modelValue: HoppRESTAuth
isCollectionProperty?: boolean
isRootCollection?: boolean
inheritedProperties?: HoppInheritedProperty
envs?: AggregateEnvironment[]
}>()
const emit = defineEmits<{
(e: "update:modelValue", value: HoppRESTAuth): void
@@ -291,30 +272,6 @@ const getAuthName = (type: HoppRESTAuth["authType"] | undefined) => {
return AUTH_KEY_NAME[type] ? AUTH_KEY_NAME[type] : "None"
}
const selectOAuth2AuthType = () => {
const defaultGrantTypeInfo: HoppRESTAuthOAuth2["grantTypeInfo"] = {
...getDefaultAuthCodeOauthFlowParams(),
grantType: "AUTHORIZATION_CODE",
token: "",
}
// @ts-expect-error - the existing grantTypeInfo might be in the auth object, typescript doesnt know that
const existingGrantTypeInfo = auth.value.grantTypeInfo as
| HoppRESTAuthOAuth2["grantTypeInfo"]
| undefined
const grantTypeInfo = existingGrantTypeInfo
? existingGrantTypeInfo
: defaultGrantTypeInfo
auth.value = <HoppRESTAuth>{
...auth.value,
authType: "oauth-2",
addTo: "HEADERS",
grantTypeInfo: grantTypeInfo,
}
}
const authActive = pluckRef(auth, "authActive")
const clearContent = () => {

View File

@@ -1,4 +1,4 @@
import { customRef, onBeforeUnmount, ref, Ref, UnwrapRef, watch } from "vue"
import { customRef, onBeforeUnmount, Ref, watch } from "vue"
export function pluckRef<T, K extends keyof T>(ref: Ref<T>, key: K): Ref<T[K]> {
return customRef((track, trigger) => {
@@ -31,16 +31,3 @@ export function pluckMultipleFromRef<T, K extends Array<keyof T>>(
): { [key in K[number]]: Ref<T[key]> } {
return Object.fromEntries(keys.map((x) => [x, pluckRef(sourceRef, x)])) as any
}
export const refWithCallbackOnChange = <T>(
initialValue: T,
callback: (value: UnwrapRef<T>) => void
) => {
const targetRef = ref(initialValue)
watch(targetRef, (value) => {
callback(value)
})
return targetRef
}

View File

@@ -66,20 +66,12 @@ export function getRequestsByPath(
let currentCollection = collections[pathArray[0]]
if (pathArray.length === 1) {
const latestVersionedRequests = currentCollection.requests.filter(
(req): req is HoppRESTRequest => req.v === "3"
)
return latestVersionedRequests
return currentCollection.requests
}
for (let i = 1; i < pathArray.length; i++) {
const folder = currentCollection.folders[pathArray[i]]
if (folder) currentCollection = folder
}
const latestVersionedRequests = currentCollection.requests.filter(
(req): req is HoppRESTRequest => req.v === "3"
)
return latestVersionedRequests
return currentCollection.requests
}

View File

@@ -269,16 +269,8 @@ export const runGQLOperation = async (options: RunQueryOptions) => {
const username = auth.username
const password = auth.password
finalHeaders.Authorization = `Basic ${btoa(`${username}:${password}`)}`
} else if (auth.authType === "bearer") {
} else if (auth.authType === "bearer" || auth.authType === "oauth-2") {
finalHeaders.Authorization = `Bearer ${auth.token}`
} else if (auth.authType === "oauth-2") {
const { addTo } = auth
if (addTo === "HEADERS") {
finalHeaders.Authorization = `Bearer ${auth.grantTypeInfo.token}`
} else if (addTo === "QUERY_PARAMS") {
params["access_token"] = auth.grantTypeInfo.token
}
} else if (auth.authType === "api-key") {
const { key, value, addTo } = auth
if (addTo === "Headers") {

View File

@@ -111,16 +111,12 @@ const getHoppReqAuth = (req: InsomniaRequestResource): HoppRESTAuth => {
return {
authType: "oauth-2",
authActive: !(auth.disabled ?? false),
grantTypeInfo: {
authEndpoint: replaceVarTemplating(auth.authorizationUrl ?? ""),
clientID: replaceVarTemplating(auth.clientId ?? ""),
clientSecret: "",
grantType: "AUTHORIZATION_CODE",
scopes: replaceVarTemplating(auth.scope ?? ""),
token: "",
isPKCE: false,
tokenEndpoint: replaceVarTemplating(auth.accessTokenUrl ?? ""),
},
accessTokenURL: replaceVarTemplating(auth.accessTokenUrl ?? ""),
authURL: replaceVarTemplating(auth.authorizationUrl ?? ""),
clientID: replaceVarTemplating(auth.clientId ?? ""),
oidcDiscoveryURL: "",
scope: replaceVarTemplating(auth.scope ?? ""),
token: "",
}
else if (auth.type === "bearer")
return {

View File

@@ -279,92 +279,67 @@ const resolveOpenAPIV3SecurityObj = (
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "AUTHORIZATION_CODE",
authEndpoint: scheme.flows.authorizationCode.authorizationUrl ?? "",
clientID: "",
scopes: _schemeData.join(" "),
token: "",
isPKCE: false,
tokenEndpoint: scheme.flows.authorizationCode.tokenUrl ?? "",
clientSecret: "",
},
addTo: "HEADERS",
accessTokenURL: scheme.flows.authorizationCode.tokenUrl ?? "",
authURL: scheme.flows.authorizationCode.authorizationUrl ?? "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.flows.implicit) {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "IMPLICIT",
authEndpoint: scheme.flows.implicit.authorizationUrl ?? "",
clientID: "",
token: "",
scopes: _schemeData.join(" "),
},
addTo: "HEADERS",
authURL: scheme.flows.implicit.authorizationUrl ?? "",
accessTokenURL: "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.flows.password) {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "PASSWORD",
clientID: "",
authEndpoint: scheme.flows.password.tokenUrl,
clientSecret: "",
password: "",
username: "",
token: "",
scopes: _schemeData.join(" "),
},
addTo: "HEADERS",
authURL: "",
accessTokenURL: scheme.flows.password.tokenUrl ?? "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.flows.clientCredentials) {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "CLIENT_CREDENTIALS",
authEndpoint: scheme.flows.clientCredentials.tokenUrl ?? "",
clientID: "",
clientSecret: "",
scopes: _schemeData.join(" "),
token: "",
},
addTo: "HEADERS",
accessTokenURL: scheme.flows.clientCredentials.tokenUrl ?? "",
authURL: "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
}
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "AUTHORIZATION_CODE",
authEndpoint: "",
clientID: "",
scopes: _schemeData.join(" "),
token: "",
isPKCE: false,
tokenEndpoint: "",
clientSecret: "",
},
addTo: "HEADERS",
accessTokenURL: "",
authURL: "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.type === "openIdConnect") {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "AUTHORIZATION_CODE",
authEndpoint: "",
clientID: "",
scopes: _schemeData.join(" "),
token: "",
isPKCE: false,
tokenEndpoint: "",
clientSecret: "",
},
addTo: "HEADERS",
accessTokenURL: "",
authURL: "",
clientID: "",
oidcDiscoveryURL: scheme.openIdConnectUrl ?? "",
scope: _schemeData.join(" "),
token: "",
}
}
@@ -441,76 +416,56 @@ const resolveOpenAPIV2SecurityScheme = (
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
authEndpoint: scheme.authorizationUrl ?? "",
clientID: "",
clientSecret: "",
grantType: "AUTHORIZATION_CODE",
scopes: _schemeData.join(" "),
token: "",
isPKCE: false,
tokenEndpoint: scheme.tokenUrl ?? "",
},
addTo: "HEADERS",
accessTokenURL: scheme.tokenUrl ?? "",
authURL: scheme.authorizationUrl ?? "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.flow === "implicit") {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
authEndpoint: scheme.authorizationUrl ?? "",
clientID: "",
grantType: "IMPLICIT",
scopes: _schemeData.join(" "),
token: "",
},
addTo: "HEADERS",
accessTokenURL: "",
authURL: scheme.authorizationUrl ?? "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.flow === "application") {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
authEndpoint: scheme.tokenUrl ?? "",
clientID: "",
clientSecret: "",
grantType: "CLIENT_CREDENTIALS",
scopes: _schemeData.join(" "),
token: "",
},
addTo: "HEADERS",
accessTokenURL: scheme.tokenUrl ?? "",
authURL: "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
} else if (scheme.flow === "password") {
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "PASSWORD",
authEndpoint: scheme.tokenUrl ?? "",
clientID: "",
clientSecret: "",
password: "",
scopes: _schemeData.join(" "),
token: "",
username: "",
},
addTo: "HEADERS",
accessTokenURL: scheme.tokenUrl ?? "",
authURL: "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
}
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
authEndpoint: "",
clientID: "",
clientSecret: "",
grantType: "AUTHORIZATION_CODE",
scopes: _schemeData.join(" "),
token: "",
isPKCE: false,
tokenEndpoint: "",
},
addTo: "HEADERS",
accessTokenURL: "",
authURL: "",
clientID: "",
oidcDiscoveryURL: "",
scope: _schemeData.join(" "),
token: "",
}
}

View File

@@ -162,36 +162,25 @@ const getHoppReqAuth = (item: Item): HoppRESTAuth => {
),
}
} else if (auth.type === "oauth2") {
const accessTokenURL = replacePMVarTemplating(
getVariableValue(auth.oauth2, "accessTokenUrl") ?? ""
)
const authURL = replacePMVarTemplating(
getVariableValue(auth.oauth2, "authUrl") ?? ""
)
const clientId = replacePMVarTemplating(
getVariableValue(auth.oauth2, "clientId") ?? ""
)
const scope = replacePMVarTemplating(
getVariableValue(auth.oauth2, "scope") ?? ""
)
const token = replacePMVarTemplating(
getVariableValue(auth.oauth2, "accessToken") ?? ""
)
return {
authType: "oauth-2",
authActive: true,
grantTypeInfo: {
grantType: "AUTHORIZATION_CODE",
authEndpoint: authURL,
clientID: clientId,
scopes: scope,
token: token,
tokenEndpoint: accessTokenURL,
clientSecret: "",
isPKCE: false,
},
addTo: "HEADERS",
accessTokenURL: replacePMVarTemplating(
getVariableValue(auth.oauth2, "accessTokenUrl") ?? ""
),
authURL: replacePMVarTemplating(
getVariableValue(auth.oauth2, "authUrl") ?? ""
),
clientID: replacePMVarTemplating(
getVariableValue(auth.oauth2, "clientId") ?? ""
),
scope: replacePMVarTemplating(
getVariableValue(auth.oauth2, "scope") ?? ""
),
token: replacePMVarTemplating(
getVariableValue(auth.oauth2, "accessToken") ?? ""
),
oidcDiscoveryURL: "",
}
}

View File

@@ -1,610 +0,0 @@
import { ref } from "vue"
import { runGQLQuery } from "../backend/GQLClient"
import {
GetCollectionChildrenDocument,
GetCollectionRequestsDocument,
GetSingleCollectionDocument,
GetSingleRequestDocument,
} from "../backend/graphql"
import { TeamCollection } from "./TeamCollection"
import { HoppRESTAuth, HoppRESTHeader } from "@hoppscotch/data"
import * as E from "fp-ts/Either"
import { HoppInheritedProperty } from "../types/HoppInheritedProperties"
import { TeamRequest } from "./TeamRequest"
import { Service } from "dioc"
import axios from "axios"
import { Ref } from "vue"
type CollectionSearchMeta = {
isSearchResult?: boolean
insertedWhileExpanding?: boolean
}
type CollectionSearchNode =
| {
type: "request"
title: string
method: string
id: string
// parent collections
path: CollectionSearchNode[]
}
| {
type: "collection"
title: string
id: string
// parent collections
path: CollectionSearchNode[]
}
type _SearchCollection = TeamCollection & {
parentID: string | null
meta?: CollectionSearchMeta
}
type _SearchRequest = {
id: string
collectionID: string
title: string
request: {
name: string
method: string
}
meta?: CollectionSearchMeta
}
function convertToTeamCollection(
node: CollectionSearchNode & {
meta?: CollectionSearchMeta
},
existingCollections: Record<string, _SearchCollection>,
existingRequests: Record<string, _SearchRequest>
) {
if (node.type === "request") {
existingRequests[node.id] = {
id: node.id,
collectionID: node.path[0].id,
title: node.title,
request: {
name: node.title,
method: node.method,
},
meta: {
isSearchResult: node.meta?.isSearchResult || false,
},
}
if (node.path[0]) {
// add parent collections to the collections array recursively
convertToTeamCollection(
node.path[0],
existingCollections,
existingRequests
)
}
} else {
existingCollections[node.id] = {
id: node.id,
title: node.title,
children: [],
requests: [],
data: null,
parentID: node.path[0]?.id,
meta: {
isSearchResult: node.meta?.isSearchResult || false,
},
}
if (node.path[0]) {
// add parent collections to the collections array recursively
convertToTeamCollection(
node.path[0],
existingCollections,
existingRequests
)
}
}
return {
existingCollections,
existingRequests,
}
}
function convertToTeamTree(
collections: (TeamCollection & { parentID: string | null })[],
requests: TeamRequest[]
) {
const collectionTree: TeamCollection[] = []
collections.forEach((collection) => {
const parentCollection = collection.parentID
? collections.find((c) => c.id === collection.parentID)
: null
const isAlreadyInserted = parentCollection?.children?.find(
(c) => c.id === collection.id
)
if (isAlreadyInserted) return
if (parentCollection) {
parentCollection.children = parentCollection.children || []
parentCollection.children.push(collection)
} else {
collectionTree.push(collection)
}
})
requests.forEach((request) => {
const parentCollection = collections.find(
(c) => c.id === request.collectionID
)
const isAlreadyInserted = parentCollection?.requests?.find(
(r) => r.id === request.id
)
if (isAlreadyInserted) return
if (parentCollection) {
parentCollection.requests = parentCollection.requests || []
parentCollection.requests.push({
id: request.id,
collectionID: request.collectionID,
title: request.title,
request: request.request,
})
}
})
return collectionTree
}
export class TeamSearchService extends Service {
public static readonly ID = "TeamSearchService"
public endpoint = import.meta.env.VITE_BACKEND_API_URL
public teamsSearchResultsLoading = ref(false)
public teamsSearchResults = ref<TeamCollection[]>([])
public teamsSearchResultsFormattedForSpotlight = ref<
{
collectionTitles: string[]
request: {
id: string
name: string
method: string
}
}[]
>([])
searchResultsCollections: Record<string, _SearchCollection> = {}
searchResultsRequests: Record<string, _SearchRequest> = {}
expandingCollections: Ref<string[]> = ref([])
expandedCollections: Ref<string[]> = ref([])
// FUTURE-TODO: ideally this should return the search results / formatted results instead of directly manipulating the result set
// eg: do the spotlight formatting in the spotlight searcher and not here
searchTeams = async (query: string, teamID: string) => {
if (!query.length) {
return
}
this.teamsSearchResultsLoading.value = true
this.searchResultsCollections = {}
this.searchResultsRequests = {}
this.expandedCollections.value = []
try {
const searchResponse = await axios.get(
`${
this.endpoint
}/team-collection/search/${teamID}?searchQuery=${encodeURIComponent(
query
)}}`,
{
withCredentials: true,
}
)
if (searchResponse.status !== 200) {
return
}
const searchResults = searchResponse.data.data as CollectionSearchNode[]
searchResults
.map((node) => {
const { existingCollections, existingRequests } =
convertToTeamCollection(
{
...node,
meta: {
isSearchResult: true,
},
},
{},
{}
)
return {
collections: existingCollections,
requests: existingRequests,
}
})
.forEach(({ collections, requests }) => {
this.searchResultsCollections = {
...this.searchResultsCollections,
...collections,
}
this.searchResultsRequests = {
...this.searchResultsRequests,
...requests,
}
})
const collectionFetchingPromises = Object.values(
this.searchResultsCollections
).map((col) => {
return getSingleCollection(col.id)
})
const requestFetchingPromises = Object.values(
this.searchResultsRequests
).map((req) => {
return getSingleRequest(req.id)
})
const collectionResponses = await Promise.all(collectionFetchingPromises)
const requestResponses = await Promise.all(requestFetchingPromises)
requestResponses.map((res) => {
if (E.isLeft(res)) {
return
}
const request = res.right.request
if (!request) return
this.searchResultsRequests[request.id] = {
id: request.id,
title: request.title,
request: JSON.parse(request.request) as TeamRequest["request"],
collectionID: request.collectionID,
}
})
collectionResponses.map((res) => {
if (E.isLeft(res)) {
return
}
const collection = res.right.collection
if (!collection) return
this.searchResultsCollections[collection.id].data =
collection.data ?? null
})
const collectionTree = convertToTeamTree(
Object.values(this.searchResultsCollections),
// asserting because we've already added the missing properties after fetching the full details
Object.values(this.searchResultsRequests) as TeamRequest[]
)
this.teamsSearchResults.value = collectionTree
this.teamsSearchResultsFormattedForSpotlight.value = Object.values(
this.searchResultsRequests
).map((request) => {
return formatTeamsSearchResultsForSpotlight(
{
collectionID: request.collectionID,
name: request.title,
method: request.request.method,
id: request.id,
},
Object.values(this.searchResultsCollections)
)
})
} catch (error) {
console.error(error)
}
this.teamsSearchResultsLoading.value = false
}
cascadeParentCollectionForHeaderAuthForSearchResults = (
collectionID: string
): HoppInheritedProperty => {
const defaultInheritedAuth: HoppInheritedProperty["auth"] = {
parentID: "",
parentName: "",
inheritedAuth: {
authType: "none",
authActive: true,
},
}
const defaultInheritedHeaders: HoppInheritedProperty["headers"] = []
const collection = Object.values(this.searchResultsCollections).find(
(col) => col.id === collectionID
)
if (!collection)
return { auth: defaultInheritedAuth, headers: defaultInheritedHeaders }
const inheritedAuthData = this.findInheritableParentAuth(collectionID)
const inheritedHeadersData = this.findInheritableParentHeaders(collectionID)
return {
auth: E.isRight(inheritedAuthData)
? inheritedAuthData.right
: defaultInheritedAuth,
headers: E.isRight(inheritedHeadersData)
? Object.values(inheritedHeadersData.right)
: defaultInheritedHeaders,
}
}
findInheritableParentAuth = (
collectionID: string
): E.Either<
string,
{
parentID: string
parentName: string
inheritedAuth: HoppRESTAuth
}
> => {
const collection = Object.values(this.searchResultsCollections).find(
(col) => col.id === collectionID
)
if (!collection) {
return E.left("PARENT_NOT_FOUND" as const)
}
// has inherited data
if (collection.data) {
const parentInheritedData = JSON.parse(collection.data) as {
auth: HoppRESTAuth
headers: HoppRESTHeader[]
}
const inheritedAuth = parentInheritedData.auth
if (inheritedAuth.authType !== "inherit") {
return E.right({
parentID: collectionID,
parentName: collection.title,
inheritedAuth: inheritedAuth,
})
}
}
if (!collection.parentID) {
return E.left("PARENT_INHERITED_DATA_NOT_FOUND")
}
return this.findInheritableParentAuth(collection.parentID)
}
findInheritableParentHeaders = (
collectionID: string,
existingHeaders: Record<
string,
HoppInheritedProperty["headers"][number]
> = {}
): E.Either<
string,
Record<string, HoppInheritedProperty["headers"][number]>
> => {
const collection = Object.values(this.searchResultsCollections).find(
(col) => col.id === collectionID
)
if (!collection) {
return E.left("PARENT_NOT_FOUND" as const)
}
// see if it has headers to inherit, if yes, add it to the existing headers
if (collection.data) {
const parentInheritedData = JSON.parse(collection.data) as {
auth: HoppRESTAuth
headers: HoppRESTHeader[]
}
const inheritedHeaders = parentInheritedData.headers
if (inheritedHeaders) {
inheritedHeaders.forEach((header) => {
if (!existingHeaders[header.key]) {
existingHeaders[header.key] = {
parentID: collection.id,
parentName: collection.title,
inheritedHeader: header,
}
}
})
}
}
if (collection.parentID) {
return this.findInheritableParentHeaders(
collection.parentID,
existingHeaders
)
}
return E.right(existingHeaders)
}
expandCollection = async (collectionID: string) => {
if (this.expandingCollections.value.includes(collectionID)) return
const collectionToExpand = Object.values(
this.searchResultsCollections
).find((col) => col.id === collectionID)
const isAlreadyExpanded =
this.expandedCollections.value.includes(collectionID)
// only allow search result collections to be expanded
if (
isAlreadyExpanded ||
!collectionToExpand ||
!(
collectionToExpand.meta?.isSearchResult ||
collectionToExpand.meta?.insertedWhileExpanding
)
)
return
this.expandingCollections.value.push(collectionID)
const childCollectionsPromise = getCollectionChildCollections(collectionID)
const childRequestsPromise = getCollectionChildRequests(collectionID)
const [childCollections, childRequests] = await Promise.all([
childCollectionsPromise,
childRequestsPromise,
])
if (E.isLeft(childCollections)) {
return
}
if (E.isLeft(childRequests)) {
return
}
childCollections.right.collection?.children
.map((child) => ({
id: child.id,
title: child.title,
data: child.data ?? null,
children: [],
requests: [],
}))
.forEach((child) => {
this.searchResultsCollections[child.id] = {
...child,
parentID: collectionID,
meta: {
isSearchResult: false,
insertedWhileExpanding: true,
},
}
})
childRequests.right.requestsInCollection
.map((request) => ({
id: request.id,
collectionID: collectionID,
title: request.title,
request: JSON.parse(request.request) as TeamRequest["request"],
}))
.forEach((request) => {
this.searchResultsRequests[request.id] = {
...request,
meta: {
isSearchResult: false,
insertedWhileExpanding: true,
},
}
})
this.teamsSearchResults.value = convertToTeamTree(
Object.values(this.searchResultsCollections),
// asserting because we've already added the missing properties after fetching the full details
Object.values(this.searchResultsRequests) as TeamRequest[]
)
// remove the collection after expanding
this.expandingCollections.value = this.expandingCollections.value.filter(
(colID) => colID !== collectionID
)
this.expandedCollections.value.push(collectionID)
}
}
const getSingleCollection = (collectionID: string) =>
runGQLQuery({
query: GetSingleCollectionDocument,
variables: {
collectionID,
},
})
const getSingleRequest = (requestID: string) =>
runGQLQuery({
query: GetSingleRequestDocument,
variables: {
requestID,
},
})
const getCollectionChildCollections = (collectionID: string) =>
runGQLQuery({
query: GetCollectionChildrenDocument,
variables: {
collectionID,
},
})
const getCollectionChildRequests = (collectionID: string) =>
runGQLQuery({
query: GetCollectionRequestsDocument,
variables: {
collectionID,
},
})
const formatTeamsSearchResultsForSpotlight = (
request: {
collectionID: string
name: string
method: string
id: string
},
parentCollections: (TeamCollection & { parentID: string | null })[]
) => {
let collectionTitles: string[] = []
let parentCollectionID: string | null = request.collectionID
while (true) {
if (!parentCollectionID) {
break
}
const parentCollection = parentCollections.find(
(col) => col.id === parentCollectionID
)
if (!parentCollection) {
break
}
collectionTitles = [parentCollection.title, ...collectionTitles]
parentCollectionID = parentCollection.parentID
}
return {
collectionTitles,
request: {
name: request.name,
method: request.method,
id: request.id,
},
}
}

View File

@@ -82,17 +82,16 @@ export const getComputedAuthHeaders = (
})
} else if (
request.auth.authType === "bearer" ||
(request.auth.authType === "oauth-2" && request.auth.addTo === "HEADERS")
request.auth.authType === "oauth-2"
) {
const token =
request.auth.authType === "bearer"
? request.auth.token
: request.auth.grantTypeInfo.token
headers.push({
active: true,
key: "Authorization",
value: `Bearer ${parse ? parseTemplateString(token, envVars) : token}`,
value: `Bearer ${
parse
? parseTemplateString(request.auth.token, envVars)
: request.auth.token
}`,
})
} else if (request.auth.authType === "api-key") {
const { key, addTo } = request.auth
@@ -197,40 +196,17 @@ export const getComputedParams = (
): ComputedParam[] => {
// When this gets complex, its best to split this function off (like with getComputedHeaders)
// API-key auth can be added to query params
if (!req.auth || !req.auth.authActive) {
return []
}
if (req.auth.authType !== "api-key" && req.auth.authType !== "oauth-2") {
return []
}
if (req.auth.addTo !== "QUERY_PARAMS") {
return []
}
if (req.auth.authType === "api-key") {
return [
{
source: "auth" as const,
param: {
active: true,
key: parseTemplateString(req.auth.key, envVars),
value: parseTemplateString(req.auth.value, envVars),
},
},
]
}
const { grantTypeInfo } = req.auth
if (!req.auth || !req.auth.authActive) return []
if (req.auth.authType !== "api-key") return []
if (req.auth.addTo !== "Query params") return []
return [
{
source: "auth",
param: {
active: true,
key: "access_token",
value: parseTemplateString(grantTypeInfo.token, envVars),
key: parseTemplateString(req.auth.key, envVars),
value: parseTemplateString(req.auth.value, envVars),
},
},
]

View File

@@ -5,31 +5,23 @@
</template>
<script setup lang="ts">
import { useI18n } from "~/composables/i18n"
import { handleOAuthRedirect } from "~/helpers/oauth"
import { useToast } from "~/composables/toast"
import { useI18n } from "~/composables/i18n"
import { useService } from "dioc/vue"
import * as E from "fp-ts/Either"
import { onMounted } from "vue"
import { useService } from "dioc/vue"
import { RESTTabService } from "~/services/tab/rest"
import { onMounted } from "vue"
import { useRouter } from "vue-router"
import {
PersistedOAuthConfig,
routeOAuthRedirect,
} from "~/services/oauth/oauth.service"
import { PersistenceService } from "~/services/persistence"
import { GQLTabService } from "~/services/tab/graphql"
const t = useI18n()
const router = useRouter()
const toast = useToast()
const gqlTabs = useService(GQLTabService)
const persistenceService = useService(PersistenceService)
const restTabs = useService(RESTTabService)
const tabs = useService(RESTTabService)
function translateOAuthRedirectError(error: string) {
switch (error) {
@@ -68,58 +60,22 @@ function translateOAuthRedirectError(error: string) {
}
onMounted(async () => {
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
const tokenInfo = await handleOAuthRedirect()
if (!localOAuthTempConfig) {
toast.error(t("authorization.oauth.something_went_wrong_on_oauth_redirect"))
if (E.isLeft(tokenInfo)) {
toast.error(translateOAuthRedirectError(tokenInfo.left))
router.push("/")
return
}
const persistedOAuthConfig: PersistedOAuthConfig =
JSON.parse(localOAuthTempConfig)
const { context, source } = persistedOAuthConfig
const tokenInfo = await routeOAuthRedirect()
if (E.isLeft(tokenInfo)) {
toast.error(translateOAuthRedirectError(tokenInfo.left))
router.push(source === "REST" ? "/" : "/graphql")
return
}
// Indicates the access token generation flow originated from the modal for setting authorization/headers at the collection level
if (context?.type === "collection-properties") {
// Set the access token in `localStorage` to retrieve from the modal while redirecting back
persistenceService.setLocalConfig(
"oauth_temp_config",
JSON.stringify(<PersistedOAuthConfig>{
...persistedOAuthConfig,
token: tokenInfo.right.access_token,
})
)
toast.success(t("authorization.oauth.token_fetched_successfully"))
router.push(source === "REST" ? "/" : "/graphql")
return
}
const routeToRedirect = source === "GraphQL" ? "/graphql" : "/"
const tabService = source === "GraphQL" ? gqlTabs : restTabs
if (
tabService.currentActiveTab.value.document.request.auth.authType ===
"oauth-2"
tabs.currentActiveTab.value.document.request.auth.authType === "oauth-2"
) {
tabService.currentActiveTab.value.document.request.auth.grantTypeInfo.token =
tabs.currentActiveTab.value.document.request.auth.token =
tokenInfo.right.access_token
toast.success(t("authorization.oauth.token_fetched_successfully"))
router.push("/")
return
}
router.push(routeToRedirect)
})
</script>

View File

@@ -231,7 +231,6 @@ export class ExtensionInterceptorService
try {
const result = await extensionHook.sendRequest({
...req,
headers: req.headers ?? {},
wantsBinary: true,
})

View File

@@ -1,293 +0,0 @@
import { PersistenceService } from "~/services/persistence"
import {
OauthAuthService,
PersistedOAuthConfig,
createFlowConfig,
decodeResponseAsJSON,
generateRandomString,
} from "../oauth.service"
import { z } from "zod"
import { getService } from "~/modules/dioc"
import * as E from "fp-ts/Either"
import { InterceptorService } from "~/services/interceptor.service"
import { AuthCodeGrantTypeParams } from "@hoppscotch/data"
const persistenceService = getService(PersistenceService)
const interceptorService = getService(InterceptorService)
const AuthCodeOauthFlowParamsSchema = AuthCodeGrantTypeParams.pick({
authEndpoint: true,
tokenEndpoint: true,
clientID: true,
clientSecret: true,
scopes: true,
isPKCE: true,
codeVerifierMethod: true,
})
.refine(
(params) => {
return (
params.authEndpoint.length >= 1 &&
params.tokenEndpoint.length >= 1 &&
params.clientID.length >= 1 &&
params.clientSecret.length >= 1 &&
(!params.scopes || params.scopes.trim().length >= 1)
)
},
{
message: "Minimum length requirement not met for one or more parameters",
}
)
.refine((params) => (params.isPKCE ? !!params.codeVerifierMethod : true), {
message: "codeVerifierMethod is required when using PKCE",
path: ["codeVerifierMethod"],
})
export type AuthCodeOauthFlowParams = z.infer<
typeof AuthCodeOauthFlowParamsSchema
>
export const getDefaultAuthCodeOauthFlowParams =
(): AuthCodeOauthFlowParams => ({
authEndpoint: "",
tokenEndpoint: "",
clientID: "",
clientSecret: "",
scopes: undefined,
isPKCE: false,
codeVerifierMethod: "S256",
})
const initAuthCodeOauthFlow = async ({
tokenEndpoint,
clientID,
clientSecret,
scopes,
authEndpoint,
isPKCE,
codeVerifierMethod,
}: AuthCodeOauthFlowParams) => {
const state = generateRandomString()
let codeVerifier: string | undefined
let codeChallenge: string | undefined
if (isPKCE) {
codeVerifier = generateCodeVerifier()
codeChallenge = await generateCodeChallenge(
codeVerifier,
codeVerifierMethod
)
}
let oauthTempConfig: {
state: string
grant_type: "AUTHORIZATION_CODE"
authEndpoint: string
tokenEndpoint: string
clientSecret: string
clientID: string
isPKCE: boolean
codeVerifier?: string
codeVerifierMethod?: string
codeChallenge?: string
scopes?: string
} = {
state,
grant_type: "AUTHORIZATION_CODE",
authEndpoint,
tokenEndpoint,
clientSecret,
clientID,
isPKCE,
codeVerifierMethod,
scopes,
}
if (codeVerifier && codeChallenge) {
oauthTempConfig = {
...oauthTempConfig,
codeVerifier,
codeChallenge,
}
}
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
const persistedOAuthConfig: PersistedOAuthConfig = localOAuthTempConfig
? { ...JSON.parse(localOAuthTempConfig) }
: {}
const { grant_type, ...rest } = oauthTempConfig
// persist the state so we can compare it when we get redirected back
// also persist the grant_type,tokenEndpoint and clientSecret so we can use them when we get redirected back
persistenceService.setLocalConfig(
"oauth_temp_config",
JSON.stringify(<PersistedOAuthConfig>{
...persistedOAuthConfig,
fields: rest,
grant_type,
})
)
let url: URL
try {
url = new URL(authEndpoint)
} catch (e) {
return E.left("INVALID_AUTH_ENDPOINT")
}
url.searchParams.set("grant_type", "authorization_code")
url.searchParams.set("client_id", clientID)
url.searchParams.set("state", state)
url.searchParams.set("response_type", "code")
url.searchParams.set("redirect_uri", OauthAuthService.redirectURI)
if (scopes) url.searchParams.set("scope", scopes)
if (codeVerifierMethod && codeChallenge) {
url.searchParams.set("code_challenge", codeChallenge)
url.searchParams.set("code_challenge_method", codeVerifierMethod)
}
// Redirect to the authorization server
window.location.assign(url.toString())
return E.right(undefined)
}
const handleRedirectForAuthCodeOauthFlow = async (localConfig: string) => {
// parse the query string
const params = new URLSearchParams(window.location.search)
const code = params.get("code")
const state = params.get("state")
const error = params.get("error")
if (error) {
return E.left("AUTH_SERVER_RETURNED_ERROR")
}
if (!code) {
return E.left("AUTH_TOKEN_REQUEST_FAILED")
}
const expectedSchema = z.object({
source: z.optional(z.string()),
state: z.string(),
tokenEndpoint: z.string(),
clientSecret: z.string(),
clientID: z.string(),
codeVerifier: z.string().optional(),
codeChallenge: z.string().optional(),
})
const decodedLocalConfig = expectedSchema.safeParse(
JSON.parse(localConfig).fields
)
if (!decodedLocalConfig.success) {
return E.left("INVALID_LOCAL_CONFIG")
}
// check if the state matches
if (decodedLocalConfig.data.state !== state) {
return E.left("INVALID_STATE")
}
// exchange the code for a token
const formData = new URLSearchParams()
formData.append("grant_type", "authorization_code")
formData.append("code", code)
formData.append("client_id", decodedLocalConfig.data.clientID)
formData.append("client_secret", decodedLocalConfig.data.clientSecret)
formData.append("redirect_uri", OauthAuthService.redirectURI)
if (decodedLocalConfig.data.codeVerifier) {
formData.append("code_verifier", decodedLocalConfig.data.codeVerifier)
}
const { response } = interceptorService.runRequest({
url: decodedLocalConfig.data.tokenEndpoint,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
data: formData.toString(),
})
const res = await response
if (E.isLeft(res)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const responsePayload = decodeResponseAsJSON(res.right)
if (E.isLeft(responsePayload)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const withAccessTokenSchema = z.object({
access_token: z.string(),
})
const parsedTokenResponse = withAccessTokenSchema.safeParse(
responsePayload.right
)
return parsedTokenResponse.success
? E.right(parsedTokenResponse.data)
: E.left("AUTH_TOKEN_REQUEST_INVALID_RESPONSE" as const)
}
const generateCodeVerifier = () => {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
const length = Math.floor(Math.random() * (128 - 43 + 1)) + 43 // Random length between 43 and 128
let codeVerifier = ""
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length)
codeVerifier += characters[randomIndex]
}
return codeVerifier
}
const generateCodeChallenge = async (
codeVerifier: string,
strategy: AuthCodeOauthFlowParams["codeVerifierMethod"]
) => {
if (strategy === "plain") {
return codeVerifier
}
const encoder = new TextEncoder()
const data = encoder.encode(codeVerifier)
const buffer = await crypto.subtle.digest("SHA-256", data)
return encodeArrayBufferAsUrlEncodedBase64(buffer)
}
const encodeArrayBufferAsUrlEncodedBase64 = (buffer: ArrayBuffer) => {
const hashArray = Array.from(new Uint8Array(buffer))
const hashBase64URL = btoa(String.fromCharCode(...hashArray))
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_")
return hashBase64URL
}
export default createFlowConfig(
"AUTHORIZATION_CODE" as const,
AuthCodeOauthFlowParamsSchema,
initAuthCodeOauthFlow,
handleRedirectForAuthCodeOauthFlow
)

View File

@@ -1,183 +0,0 @@
import {
OauthAuthService,
createFlowConfig,
decodeResponseAsJSON,
} from "../oauth.service"
import { z } from "zod"
import { getService } from "~/modules/dioc"
import * as E from "fp-ts/Either"
import { InterceptorService } from "~/services/interceptor.service"
import { useToast } from "~/composables/toast"
import { ClientCredentialsGrantTypeParams } from "@hoppscotch/data"
const interceptorService = getService(InterceptorService)
const ClientCredentialsFlowParamsSchema = ClientCredentialsGrantTypeParams.pick(
{
authEndpoint: true,
clientID: true,
clientSecret: true,
scopes: true,
}
).refine(
(params) => {
return (
params.authEndpoint.length >= 1 &&
params.clientID.length >= 1 &&
params.clientSecret.length >= 1 &&
(!params.scopes || params.scopes.length >= 1)
)
},
{
message: "Minimum length requirement not met for one or more parameters",
}
)
export type ClientCredentialsFlowParams = z.infer<
typeof ClientCredentialsFlowParamsSchema
>
export const getDefaultClientCredentialsFlowParams =
(): ClientCredentialsFlowParams => ({
authEndpoint: "",
clientID: "",
clientSecret: "",
scopes: undefined,
})
const initClientCredentialsOAuthFlow = async ({
clientID,
clientSecret,
scopes,
authEndpoint,
}: ClientCredentialsFlowParams) => {
const toast = useToast()
const formData = new URLSearchParams()
formData.append("grant_type", "client_credentials")
formData.append("client_id", clientID)
formData.append("client_secret", clientSecret)
if (scopes) {
formData.append("scope", scopes)
}
const { response } = interceptorService.runRequest({
url: authEndpoint,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
data: formData.toString(),
})
const res = await response
if (E.isLeft(res)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const responsePayload = decodeResponseAsJSON(res.right)
if (E.isLeft(responsePayload)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const withAccessTokenSchema = z.object({
access_token: z.string(),
})
const parsedTokenResponse = withAccessTokenSchema.safeParse(
responsePayload.right
)
if (!parsedTokenResponse.success) {
toast.error("AUTH_TOKEN_REQUEST_INVALID_RESPONSE")
}
return parsedTokenResponse.success
? E.right(parsedTokenResponse.data)
: E.left("AUTH_TOKEN_REQUEST_INVALID_RESPONSE" as const)
}
const handleRedirectForAuthCodeOauthFlow = async (localConfig: string) => {
// parse the query string
const params = new URLSearchParams(window.location.search)
const code = params.get("code")
const state = params.get("state")
const error = params.get("error")
if (error) {
return E.left("AUTH_SERVER_RETURNED_ERROR")
}
if (!code) {
return E.left("AUTH_TOKEN_REQUEST_FAILED")
}
const expectedSchema = z.object({
state: z.string(),
tokenEndpoint: z.string(),
clientSecret: z.string(),
clientID: z.string(),
})
const decodedLocalConfig = expectedSchema.safeParse(JSON.parse(localConfig))
if (!decodedLocalConfig.success) {
return E.left("INVALID_LOCAL_CONFIG")
}
// check if the state matches
if (decodedLocalConfig.data.state !== state) {
return E.left("INVALID_STATE")
}
// exchange the code for a token
const formData = new URLSearchParams()
formData.append("code", code)
formData.append("client_id", decodedLocalConfig.data.clientID)
formData.append("client_secret", decodedLocalConfig.data.clientSecret)
formData.append("redirect_uri", OauthAuthService.redirectURI)
const { response } = interceptorService.runRequest({
url: decodedLocalConfig.data.tokenEndpoint,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
data: formData.toString(),
})
const res = await response
if (E.isLeft(res)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const responsePayload = new TextDecoder("utf-8")
.decode(res.right.data as any)
.replaceAll("\x00", "")
const withAccessTokenSchema = z.object({
access_token: z.string(),
})
const parsedTokenResponse = withAccessTokenSchema.safeParse(
JSON.parse(responsePayload)
)
return parsedTokenResponse.success
? E.right(parsedTokenResponse.data)
: E.left("AUTH_TOKEN_REQUEST_INVALID_RESPONSE" as const)
}
export default createFlowConfig(
"CLIENT_CREDENTIALS" as const,
ClientCredentialsFlowParamsSchema,
initClientCredentialsOAuthFlow,
handleRedirectForAuthCodeOauthFlow
)

View File

@@ -1,135 +0,0 @@
import { PersistenceService } from "~/services/persistence"
import {
OauthAuthService,
PersistedOAuthConfig,
createFlowConfig,
generateRandomString,
} from "../oauth.service"
import { z } from "zod"
import { getService } from "~/modules/dioc"
import * as E from "fp-ts/Either"
import { ImplicitOauthFlowParams } from "@hoppscotch/data"
const persistenceService = getService(PersistenceService)
const ImplicitOauthFlowParamsSchema = ImplicitOauthFlowParams.pick({
authEndpoint: true,
clientID: true,
scopes: true,
}).refine((params) => {
return (
params.authEndpoint.length >= 1 &&
params.clientID.length >= 1 &&
(params.scopes === undefined || params.scopes.length >= 1)
)
})
export type ImplicitOauthFlowParams = z.infer<
typeof ImplicitOauthFlowParamsSchema
>
export const getDefaultImplicitOauthFlowParams =
(): ImplicitOauthFlowParams => ({
authEndpoint: "",
clientID: "",
scopes: undefined,
})
const initImplicitOauthFlow = async ({
clientID,
scopes,
authEndpoint,
}: ImplicitOauthFlowParams) => {
const state = generateRandomString()
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
const persistedOAuthConfig: PersistedOAuthConfig = localOAuthTempConfig
? { ...JSON.parse(localOAuthTempConfig) }
: {}
// Persist the necessary information for retrieval while getting redirected back
persistenceService.setLocalConfig(
"oauth_temp_config",
JSON.stringify(<PersistedOAuthConfig>{
...persistedOAuthConfig,
fields: {
clientID,
authEndpoint,
scopes,
state,
},
grant_type: "IMPLICIT",
})
)
let url: URL
try {
url = new URL(authEndpoint)
} catch {
return E.left("INVALID_AUTH_ENDPOINT")
}
url.searchParams.set("client_id", clientID)
url.searchParams.set("state", state)
url.searchParams.set("response_type", "token")
url.searchParams.set("redirect_uri", OauthAuthService.redirectURI)
if (scopes) url.searchParams.set("scope", scopes)
// Redirect to the authorization server
window.location.assign(url.toString())
return E.right(undefined)
}
const handleRedirectForAuthCodeOauthFlow = async (localConfig: string) => {
// parse the query string
const params = new URLSearchParams(window.location.search)
const paramsFromHash = new URLSearchParams(window.location.hash.substring(1))
const accessToken =
params.get("access_token") || paramsFromHash.get("access_token")
const state = params.get("state") || paramsFromHash.get("state")
const error = params.get("error") || paramsFromHash.get("error")
if (error) {
return E.left("AUTH_SERVER_RETURNED_ERROR")
}
if (!accessToken) {
return E.left("AUTH_TOKEN_REQUEST_FAILED")
}
const expectedSchema = z.object({
source: z.optional(z.string()),
state: z.string(),
clientID: z.string(),
})
const decodedLocalConfig = expectedSchema.safeParse(
JSON.parse(localConfig).fields
)
if (!decodedLocalConfig.success) {
return E.left("INVALID_LOCAL_CONFIG")
}
// check if the state matches
if (decodedLocalConfig.data.state !== state) {
return E.left("INVALID_STATE")
}
return E.right({
access_token: accessToken,
})
}
export default createFlowConfig(
"IMPLICIT" as const,
ImplicitOauthFlowParamsSchema,
initImplicitOauthFlow,
handleRedirectForAuthCodeOauthFlow
)

View File

@@ -1,189 +0,0 @@
import {
OauthAuthService,
createFlowConfig,
decodeResponseAsJSON,
} from "../oauth.service"
import { z } from "zod"
import { getService } from "~/modules/dioc"
import * as E from "fp-ts/Either"
import { InterceptorService } from "~/services/interceptor.service"
import { useToast } from "~/composables/toast"
import { PasswordGrantTypeParams } from "@hoppscotch/data"
const interceptorService = getService(InterceptorService)
const PasswordFlowParamsSchema = PasswordGrantTypeParams.pick({
authEndpoint: true,
clientID: true,
clientSecret: true,
scopes: true,
username: true,
password: true,
}).refine(
(params) => {
return (
params.authEndpoint.length >= 1 &&
params.clientID.length >= 1 &&
params.clientSecret.length >= 1 &&
params.username.length >= 1 &&
params.password.length >= 1 &&
(!params.scopes || params.scopes.length >= 1)
)
},
{
message: "Minimum length requirement not met for one or more parameters",
}
)
export type PasswordFlowParams = z.infer<typeof PasswordFlowParamsSchema>
export const getDefaultPasswordFlowParams = (): PasswordFlowParams => ({
authEndpoint: "",
clientID: "",
clientSecret: "",
scopes: undefined,
username: "",
password: "",
})
const initPasswordOauthFlow = async ({
password,
username,
clientID,
clientSecret,
scopes,
authEndpoint,
}: PasswordFlowParams) => {
const toast = useToast()
const formData = new URLSearchParams()
formData.append("grant_type", "password")
formData.append("client_id", clientID)
formData.append("client_secret", clientSecret)
formData.append("username", username)
formData.append("password", password)
if (scopes) {
formData.append("scope", scopes)
}
const { response } = interceptorService.runRequest({
url: authEndpoint,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
data: formData.toString(),
})
const res = await response
if (E.isLeft(res) || res.right.status !== 200) {
toast.error("AUTH_TOKEN_REQUEST_FAILED")
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const responsePayload = new TextDecoder("utf-8")
.decode(res.right.data as any)
.replaceAll("\x00", "")
const withAccessTokenSchema = z.object({
access_token: z.string(),
})
const parsedTokenResponse = withAccessTokenSchema.safeParse(
JSON.parse(responsePayload)
)
if (!parsedTokenResponse.success) {
toast.error("AUTH_TOKEN_REQUEST_INVALID_RESPONSE")
}
return parsedTokenResponse.success
? E.right(parsedTokenResponse.data)
: E.left("AUTH_TOKEN_REQUEST_INVALID_RESPONSE" as const)
}
const handleRedirectForAuthCodeOauthFlow = async (localConfig: string) => {
// parse the query string
const params = new URLSearchParams(window.location.search)
const code = params.get("code")
const state = params.get("state")
const error = params.get("error")
if (error) {
return E.left("AUTH_SERVER_RETURNED_ERROR")
}
if (!code) {
return E.left("AUTH_TOKEN_REQUEST_FAILED")
}
const expectedSchema = z.object({
state: z.string(),
tokenEndpoint: z.string(),
clientSecret: z.string(),
clientID: z.string(),
})
const decodedLocalConfig = expectedSchema.safeParse(JSON.parse(localConfig))
if (!decodedLocalConfig.success) {
return E.left("INVALID_LOCAL_CONFIG")
}
// check if the state matches
if (decodedLocalConfig.data.state !== state) {
return E.left("INVALID_STATE")
}
// exchange the code for a token
const formData = new URLSearchParams()
formData.append("code", code)
formData.append("client_id", decodedLocalConfig.data.clientID)
formData.append("client_secret", decodedLocalConfig.data.clientSecret)
formData.append("redirect_uri", OauthAuthService.redirectURI)
const { response } = interceptorService.runRequest({
url: decodedLocalConfig.data.tokenEndpoint,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
data: formData.toString(),
})
const res = await response
if (E.isLeft(res)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const responsePayload = decodeResponseAsJSON(res.right)
if (E.isLeft(responsePayload)) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
const withAccessTokenSchema = z.object({
access_token: z.string(),
})
const parsedTokenResponse = withAccessTokenSchema.safeParse(
responsePayload.right
)
return parsedTokenResponse.success
? E.right(parsedTokenResponse.data)
: E.left("AUTH_TOKEN_REQUEST_INVALID_RESPONSE" as const)
}
export default createFlowConfig(
"PASSWORD" as const,
PasswordFlowParamsSchema,
initPasswordOauthFlow,
handleRedirectForAuthCodeOauthFlow
)

View File

@@ -1,124 +0,0 @@
import { Service } from "dioc"
import { PersistenceService } from "../persistence"
import { ZodType, z } from "zod"
import * as E from "fp-ts/Either"
import authCode, { AuthCodeOauthFlowParams } from "./flows/authCode"
import implicit, { ImplicitOauthFlowParams } from "./flows/implicit"
import { getService } from "~/modules/dioc"
import { HoppCollection } from "@hoppscotch/data"
import { TeamCollection } from "~/helpers/backend/graphql"
export type PersistedOAuthConfig = {
source: "REST" | "GraphQL"
context?: {
type: "collection-properties" | "request-tab"
metadata: {
collection?: HoppCollection | TeamCollection
collectionID?: string
}
}
grant_type: string
fields?: (AuthCodeOauthFlowParams | ImplicitOauthFlowParams) & {
state: string
}
token?: string
}
const persistenceService = getService(PersistenceService)
export const grantTypesInvolvingRedirect = ["AUTHORIZATION_CODE", "IMPLICIT"]
export const routeOAuthRedirect = async () => {
// get the temp data from the local storage
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
if (!localOAuthTempConfig) {
return E.left("INVALID_STATE")
}
const expectedSchema = z.object({
source: z.optional(z.string()),
grant_type: z.string(),
})
const decodedLocalConfig = expectedSchema.safeParse(
JSON.parse(localOAuthTempConfig)
)
if (!decodedLocalConfig.success) {
return E.left("INVALID_STATE")
}
// route the request to the correct flow
const flowConfig = [authCode, implicit].find(
(flow) => flow.flow === decodedLocalConfig.data.grant_type
)
if (!flowConfig) {
return E.left("INVALID_STATE")
}
return flowConfig?.onRedirectReceived(localOAuthTempConfig)
}
export function createFlowConfig<
Flow extends string,
AuthParams extends Record<string, unknown>,
InitFuncReturnObject extends Record<string, unknown>,
>(
flow: Flow,
params: ZodType<AuthParams>,
init: (
params: AuthParams
) =>
| E.Either<string, InitFuncReturnObject>
| Promise<E.Either<string, InitFuncReturnObject>>
| E.Either<string, undefined>
| Promise<E.Either<string, undefined>>,
onRedirectReceived: (localConfig: string) => Promise<
E.Either<
string,
{
access_token: string
}
>
>
) {
return {
flow,
params,
init,
onRedirectReceived,
}
}
export const decodeResponseAsJSON = (response: { data: any }) => {
try {
const responsePayload = new TextDecoder("utf-8")
.decode(response.data as any)
.replaceAll("\x00", "")
return E.right(JSON.parse(responsePayload) as Record<string, unknown>)
} catch (error) {
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
}
export class OauthAuthService extends Service {
public static readonly ID = "OAUTH_AUTH_SERVICE"
static redirectURI = `${window.location.origin}/oauth`
constructor() {
super()
}
}
export const generateRandomString = () => {
const length = 64
const possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
const values = crypto.getRandomValues(new Uint8Array(length))
return values.reduce((acc, x) => acc + possible[x % possible.length], "")
}

View File

@@ -25,7 +25,7 @@ export const REST_COLLECTIONS_MOCK: HoppCollection[] = [
folders: [],
requests: [
{
v: "3",
v: "2",
endpoint: "https://echo.hoppscotch.io",
name: "Echo test",
params: [],
@@ -50,7 +50,7 @@ export const GQL_COLLECTIONS_MOCK: HoppCollection[] = [
folders: [],
requests: [
{
v: 3,
v: 2,
name: "Echo test",
url: "https://echo.hoppscotch.io/graphql",
headers: [],
@@ -138,7 +138,7 @@ export const REST_HISTORY_MOCK: RESTHistoryEntry[] = [
preRequestScript: "",
testScript: "",
requestVariables: [],
v: "3",
v: "2",
},
responseMeta: { duration: 807, statusCode: 200 },
star: false,
@@ -150,7 +150,7 @@ export const GQL_HISTORY_MOCK: GQLHistoryEntry[] = [
{
v: 1,
request: {
v: 3,
v: 2,
name: "Untitled",
url: "https://echo.hoppscotch.io/graphql",
query: "query Request { url }",
@@ -171,7 +171,7 @@ export const GQL_TAB_STATE_MOCK: PersistableTabState<HoppGQLDocument> = {
tabID: "5edbe8d4-65c9-4381-9354-5f1bf05d8ccc",
doc: {
request: {
v: 3,
v: 2,
name: "Untitled",
url: "https://echo.hoppscotch.io/graphql",
headers: [],
@@ -194,7 +194,7 @@ export const REST_TAB_STATE_MOCK: PersistableTabState<HoppRESTDocument> = {
tabID: "e6e8d800-caa8-44a2-a6a6-b4765a3167aa",
doc: {
request: {
v: "3",
v: "2",
endpoint: "https://echo.hoppscotch.io",
name: "Echo test",
params: [],

View File

@@ -1,140 +0,0 @@
import { Service } from "dioc"
import {
SpotlightSearcher,
SpotlightSearcherResult,
SpotlightSearcherSessionState,
SpotlightService,
} from ".."
import { getI18n } from "~/modules/i18n"
import { Ref, computed, effectScope, markRaw, watch } from "vue"
import { TeamSearchService } from "~/helpers/teams/TeamsSearch.service"
import { cloneDeep, debounce } from "lodash-es"
import IconFolder from "~icons/lucide/folder"
import { WorkspaceService } from "~/services/workspace.service"
import RESTTeamRequestEntry from "~/components/app/spotlight/entry/RESTTeamRequestEntry.vue"
import { RESTTabService } from "~/services/tab/rest"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { HoppRESTRequest } from "@hoppscotch/data"
export class TeamsSpotlightSearcherService
extends Service
implements SpotlightSearcher
{
public static readonly ID = "TEAMS_SPOTLIGHT_SEARCHER_SERVICE"
private t = getI18n()
public searcherID = "teams"
public searcherSectionTitle = this.t("team.search_title")
private readonly spotlight = this.bind(SpotlightService)
private readonly teamsSearch = this.bind(TeamSearchService)
private readonly workspaceService = this.bind(WorkspaceService)
private readonly tabs = this.bind(RESTTabService)
constructor() {
super()
this.spotlight.registerSearcher(this)
}
createSearchSession(
query: Readonly<Ref<string>>
): [Ref<SpotlightSearcherSessionState>, () => void] {
const isTeamWorkspace = computed(
() => this.workspaceService.currentWorkspace.value.type === "team"
)
const scopeHandle = effectScope()
scopeHandle.run(() => {
const debouncedSearch = debounce(this.teamsSearch.searchTeams, 400)
watch(
query,
(query) => {
if (this.workspaceService.currentWorkspace.value.type === "team") {
const teamID = this.workspaceService.currentWorkspace.value.teamID
debouncedSearch(query, teamID)?.catch((_) => {})
}
},
{
immediate: true,
}
)
})
const onSessionEnd = () => {
scopeHandle.stop()
}
const resultObj = computed<SpotlightSearcherSessionState>(() => {
return isTeamWorkspace.value
? {
loading: this.teamsSearch.teamsSearchResultsLoading.value,
results:
this.teamsSearch.teamsSearchResultsFormattedForSpotlight.value.map(
(result) => ({
id: result.request.id,
icon: markRaw(IconFolder),
score: 1, // make a better scoring system for this
text: {
type: "custom",
component: markRaw(RESTTeamRequestEntry),
componentProps: {
collectionTitles: result.collectionTitles,
request: result.request,
},
},
})
),
}
: {
loading: false,
results: [],
}
})
return [resultObj, onSessionEnd]
}
onResultSelect(result: SpotlightSearcherResult): void {
let inheritedProperties: HoppInheritedProperty | undefined = undefined
const selectedRequest = this.teamsSearch.searchResultsRequests[result.id]
if (!selectedRequest) return
const collectionID = result.id
if (!collectionID) return
inheritedProperties =
this.teamsSearch.cascadeParentCollectionForHeaderAuthForSearchResults(
collectionID
)
const possibleTab = this.tabs.getTabRefWithSaveContext({
originLocation: "team-collection",
requestID: result.id,
})
if (possibleTab) {
this.tabs.setActiveTab(possibleTab.value.id)
} else {
this.tabs.createNewTab({
request: cloneDeep(selectedRequest.request as HoppRESTRequest),
isDirty: false,
saveContext: {
originLocation: "team-collection",
requestID: selectedRequest.id,
collectionID: selectedRequest.collectionID,
},
inheritedProperties: inheritedProperties,
})
}
}
}

View File

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

View File

@@ -71,7 +71,7 @@ export const HoppGQLAuth = z
export type HoppGQLAuth = z.infer<typeof HoppGQLAuth>
export const V2_SCHEMA = z.object({
const V2_SCHEMA = z.object({
id: z.optional(z.string()),
v: z.literal(2),

View File

@@ -1,77 +0,0 @@
import { z } from "zod"
import { defineVersion } from "verzod"
import { HoppRESTAuthOAuth2 } from "../../rest"
import {
HoppGQLAuthAPIKey,
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthInherit,
HoppGQLAuthNone,
V2_SCHEMA,
} from "./2"
export { HoppRESTAuthOAuth2 as HoppGQLAuthOAuth2 } from "../../rest"
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 V3_SCHEMA = V2_SCHEMA.extend({
v: z.literal(3),
auth: HoppGQLAuth,
})
export default defineVersion({
initial: false,
schema: V3_SCHEMA,
up(old: z.infer<typeof V2_SCHEMA>) {
if (old.auth.authType === "oauth-2") {
const { token, accessTokenURL, scope, clientID, authURL } = old.auth
return {
...old,
v: 3 as const,
auth: {
...old.auth,
authType: "oauth-2" as const,
grantTypeInfo: {
grantType: "AUTHORIZATION_CODE" as const,
authEndpoint: authURL,
tokenEndpoint: accessTokenURL,
clientID: clientID,
clientSecret: "",
scopes: scope,
isPKCE: false,
token,
},
addTo: "HEADERS" as const,
},
}
}
return {
...old,
v: 3 as const,
auth: {
...old.auth,
},
}
},
})

View File

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

View File

@@ -18,7 +18,7 @@ export const HoppRESTRequestVariables = z.array(
export type HoppRESTRequestVariables = z.infer<typeof HoppRESTRequestVariables>
export const V2_SCHEMA = V1_SCHEMA.extend({
const V2_SCHEMA = V1_SCHEMA.extend({
v: z.literal("2"),
requestVariables: HoppRESTRequestVariables,
})

View File

@@ -1,127 +0,0 @@
import { z } from "zod"
import {
HoppRESTAuthAPIKey,
HoppRESTAuthBasic,
HoppRESTAuthBearer,
HoppRESTAuthInherit,
HoppRESTAuthNone,
} from "./1"
import { V2_SCHEMA } from "./2"
import { defineVersion } from "verzod"
export const AuthCodeGrantTypeParams = z.object({
grantType: z.literal("AUTHORIZATION_CODE"),
authEndpoint: z.string().trim(),
tokenEndpoint: z.string().trim(),
clientID: z.string().trim(),
clientSecret: z.string().trim(),
scopes: z.string().trim().optional(),
token: z.string().catch(""),
isPKCE: z.boolean(),
codeVerifierMethod: z
.union([z.literal("plain"), z.literal("S256")])
.optional(),
})
export const ClientCredentialsGrantTypeParams = z.object({
grantType: z.literal("CLIENT_CREDENTIALS"),
authEndpoint: z.string().trim(),
clientID: z.string().trim(),
clientSecret: z.string().trim(),
scopes: z.string().trim().optional(),
token: z.string().catch(""),
})
export const PasswordGrantTypeParams = z.object({
grantType: z.literal("PASSWORD"),
authEndpoint: z.string().trim(),
clientID: z.string().trim(),
clientSecret: z.string().trim(),
scopes: z.string().trim().optional(),
username: z.string().trim(),
password: z.string().trim(),
token: z.string().catch(""),
})
export const ImplicitOauthFlowParams = z.object({
grantType: z.literal("IMPLICIT"),
authEndpoint: z.string().trim(),
clientID: z.string().trim(),
scopes: z.string().trim().optional(),
token: z.string().catch(""),
})
export const HoppRESTAuthOAuth2 = z.object({
authType: z.literal("oauth-2"),
grantTypeInfo: z.discriminatedUnion("grantType", [
AuthCodeGrantTypeParams,
ClientCredentialsGrantTypeParams,
PasswordGrantTypeParams,
ImplicitOauthFlowParams,
]),
addTo: z.enum(["HEADERS", "QUERY_PARAMS"]).catch("HEADERS"),
})
export type HoppRESTAuthOAuth2 = z.infer<typeof HoppRESTAuthOAuth2>
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>
// V2_SCHEMA has one change in HoppRESTAuthOAuth2, we'll add the grant_type field
export const V3_SCHEMA = V2_SCHEMA.extend({
v: z.literal("3"),
auth: HoppRESTAuth,
})
export default defineVersion({
initial: false,
schema: V3_SCHEMA,
up(old: z.infer<typeof V2_SCHEMA>) {
if (old.auth.authType === "oauth-2") {
const { token, accessTokenURL, scope, clientID, authURL } = old.auth
return {
...old,
v: "3" as const,
auth: {
...old.auth,
authType: "oauth-2" as const,
grantTypeInfo: {
grantType: "AUTHORIZATION_CODE" as const,
authEndpoint: authURL,
tokenEndpoint: accessTokenURL,
clientID: clientID,
clientSecret: "",
scopes: scope,
isPKCE: false,
token,
},
addTo: "HEADERS" as const,
},
}
}
return {
...old,
v: "3" as const,
auth: {
...old.auth,
},
}
},
})

1
pnpm-lock.yaml generated
View File

@@ -24543,4 +24543,3 @@ packages:
/zod@3.22.4:
resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==}
dev: false