refactor: streamline duplicated advanced param type definitions for oauth grant types (#5401)

Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com>
This commit is contained in:
Anwarul Islam 2025-09-25 21:33:13 +06:00 committed by GitHub
parent 6beca5c787
commit f9a1d65ad9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 52 additions and 49 deletions

View file

@ -12,26 +12,24 @@ import * as E from "fp-ts/Either"
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { content } from "@hoppscotch/kernel"
import { refreshToken, OAuth2ParamSchema } from "../utils"
import {
AuthCodeGrantTypeParams,
OAuth2AuthRequestParam,
} from "@hoppscotch/data"
const persistenceService = getService(PersistenceService)
const interceptorService = getService(KernelInterceptorService)
const AuthCodeOauthFlowParamsSchema = z
.object({
authEndpoint: z.string(),
tokenEndpoint: z.string(),
clientID: z.string(),
clientSecret: z.string().optional(),
scopes: z.string().optional(),
isPKCE: z.boolean(),
codeVerifierMethod: z.enum(["plain", "S256"]).optional(),
authRequestParams: z.array(
OAuth2ParamSchema.omit({
sendIn: true,
})
),
refreshRequestParams: z.array(OAuth2ParamSchema),
// Use the existing schema from hoppscotch-data but ensure required arrays
const AuthCodeOauthFlowParamsSchema = AuthCodeGrantTypeParams.omit({
grantType: true,
token: true,
})
.extend({
// Override optional arrays to be required for the service layer
authRequestParams: z.array(OAuth2AuthRequestParam),
tokenRequestParams: z.array(OAuth2ParamSchema),
refreshRequestParams: z.array(OAuth2ParamSchema),
})
.refine(
(params) => {

View file

@ -12,16 +12,20 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { RelayRequest, content } from "@hoppscotch/kernel"
import { parseBytesToJSON } from "~/helpers/functional/json"
import { refreshToken, OAuth2ParamSchema } from "../utils"
import { ClientCredentialsGrantTypeParams } from "@hoppscotch/data"
const interceptorService = getService(KernelInterceptorService)
const ClientCredentialsFlowParamsSchema = z
.object({
authEndpoint: z.string(),
clientID: z.string(),
clientSecret: z.string().optional(),
scopes: z.string().optional(),
// Use the existing schema from hoppscotch-data but add client authentication mode
const ClientCredentialsFlowParamsSchema = ClientCredentialsGrantTypeParams.omit(
{
grantType: true,
token: true,
}
)
.extend({
clientAuthentication: z.enum(["AS_BASIC_AUTH_HEADERS", "IN_BODY"]),
// Override optional arrays to be required for the service layer
tokenRequestParams: z.array(OAuth2ParamSchema),
refreshRequestParams: z.array(OAuth2ParamSchema),
})

View file

@ -8,20 +8,22 @@ import {
import { z } from "zod"
import { getService } from "~/modules/dioc"
import * as E from "fp-ts/Either"
import {
ImplicitOauthFlowParams as ImplicitOauthFlowParamsData,
OAuth2AuthRequestParam,
} from "@hoppscotch/data"
import { OAuth2ParamSchema } from "../utils"
const persistenceService = getService(PersistenceService)
const ImplicitOauthFlowParamsSchema = z
.object({
authEndpoint: z.string(),
clientID: z.string(),
scopes: z.string().optional(),
authRequestParams: z.array(
OAuth2ParamSchema.omit({
sendIn: true,
})
),
// Use the existing schema from hoppscotch-data
const ImplicitOauthFlowParamsSchema = ImplicitOauthFlowParamsData.omit({
grantType: true,
token: true,
})
.extend({
// Override optional arrays to be required for the service layer
authRequestParams: z.array(OAuth2AuthRequestParam),
refreshRequestParams: z.array(OAuth2ParamSchema),
})
.refine((params) => {

View file

@ -11,18 +11,18 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { useToast } from "~/composables/toast"
import { content } from "@hoppscotch/kernel"
import { parseBytesToJSON } from "~/helpers/functional/json"
import { OAuth2ParamSchema, refreshToken } from "../utils"
import { refreshToken, OAuth2ParamSchema } from "../utils"
import { PasswordGrantTypeParams } from "@hoppscotch/data"
const interceptorService = getService(KernelInterceptorService)
const PasswordFlowParamsSchema = z
.object({
authEndpoint: z.string(),
clientID: z.string(),
clientSecret: z.string().optional(),
scopes: z.string().optional(),
username: z.string(),
password: z.string(),
// Use the existing schema from hoppscotch-data
const PasswordFlowParamsSchema = PasswordGrantTypeParams.omit({
grantType: true,
token: true,
})
.extend({
// Override optional arrays to be required for the service layer
tokenRequestParams: z.array(OAuth2ParamSchema),
refreshRequestParams: z.array(OAuth2ParamSchema),
})

View file

@ -4,6 +4,7 @@ import { getService } from "~/modules/dioc"
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
import { content } from "@hoppscotch/kernel"
import { decodeResponseAsJSON } from "./oauth.service"
import { OAuth2AdvancedParam } from "@hoppscotch/data"
const interceptorService = getService(KernelInterceptorService)
@ -110,13 +111,9 @@ export const refreshToken = async ({
}
/**
* Common OAuth2 parameter schema with all possible fields
* Used as base for both auth requests and advanced token/refresh requests
* Common OAuth2 parameter schema
* Used for all OAuth parameter types - omit sendIn field where not needed
*/
export const OAuth2ParamSchema = z.object({
id: z.number(),
key: z.string(),
value: z.string(),
active: z.boolean(),
export const OAuth2ParamSchema = OAuth2AdvancedParam.extend({
sendIn: z.enum(["headers", "url", "body"]).optional(),
})

View file

@ -62,6 +62,8 @@ export { ImplicitOauthFlowParams } from "./v/15/auth"
export {
HoppRESTAuthOAuth2,
ClientCredentialsGrantTypeParams,
OAuth2AdvancedParam,
OAuth2AuthRequestParam,
} from "./v/15/auth"
export {

View file

@ -20,7 +20,7 @@ import { ImplicitOauthFlowParams as ImplicitOauthFlowParamsOld } from "../3"
export { HoppRESTAuthJWT } from "../13/auth"
// Define the OAuth2 advanced parameter structure
const OAuth2AdvancedParam = z.object({
export const OAuth2AdvancedParam = z.object({
id: z.number(),
key: z.string(),
value: z.string(),
@ -29,7 +29,7 @@ const OAuth2AdvancedParam = z.object({
})
// omit sendIn from OAuth2AuthRequestParam
const OAuth2AuthRequestParam = OAuth2AdvancedParam.omit({ sendIn: true })
export const OAuth2AuthRequestParam = OAuth2AdvancedParam.omit({ sendIn: true })
export const AuthCodeGrantTypeParams = AuthCodeGrantTypeParamsOld.extend({
authRequestParams: z.array(OAuth2AuthRequestParam).optional().default([]),