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:
parent
6beca5c787
commit
f9a1d65ad9
7 changed files with 52 additions and 49 deletions
|
|
@ -12,26 +12,24 @@ import * as E from "fp-ts/Either"
|
||||||
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
|
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
|
||||||
import { content } from "@hoppscotch/kernel"
|
import { content } from "@hoppscotch/kernel"
|
||||||
import { refreshToken, OAuth2ParamSchema } from "../utils"
|
import { refreshToken, OAuth2ParamSchema } from "../utils"
|
||||||
|
import {
|
||||||
|
AuthCodeGrantTypeParams,
|
||||||
|
OAuth2AuthRequestParam,
|
||||||
|
} from "@hoppscotch/data"
|
||||||
|
|
||||||
const persistenceService = getService(PersistenceService)
|
const persistenceService = getService(PersistenceService)
|
||||||
const interceptorService = getService(KernelInterceptorService)
|
const interceptorService = getService(KernelInterceptorService)
|
||||||
|
|
||||||
const AuthCodeOauthFlowParamsSchema = z
|
// Use the existing schema from hoppscotch-data but ensure required arrays
|
||||||
.object({
|
const AuthCodeOauthFlowParamsSchema = AuthCodeGrantTypeParams.omit({
|
||||||
authEndpoint: z.string(),
|
grantType: true,
|
||||||
tokenEndpoint: z.string(),
|
token: true,
|
||||||
clientID: z.string(),
|
})
|
||||||
clientSecret: z.string().optional(),
|
.extend({
|
||||||
scopes: z.string().optional(),
|
// Override optional arrays to be required for the service layer
|
||||||
isPKCE: z.boolean(),
|
authRequestParams: z.array(OAuth2AuthRequestParam),
|
||||||
codeVerifierMethod: z.enum(["plain", "S256"]).optional(),
|
|
||||||
authRequestParams: z.array(
|
|
||||||
OAuth2ParamSchema.omit({
|
|
||||||
sendIn: true,
|
|
||||||
})
|
|
||||||
),
|
|
||||||
refreshRequestParams: z.array(OAuth2ParamSchema),
|
|
||||||
tokenRequestParams: z.array(OAuth2ParamSchema),
|
tokenRequestParams: z.array(OAuth2ParamSchema),
|
||||||
|
refreshRequestParams: z.array(OAuth2ParamSchema),
|
||||||
})
|
})
|
||||||
.refine(
|
.refine(
|
||||||
(params) => {
|
(params) => {
|
||||||
|
|
|
||||||
|
|
@ -12,16 +12,20 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
|
||||||
import { RelayRequest, content } from "@hoppscotch/kernel"
|
import { RelayRequest, content } from "@hoppscotch/kernel"
|
||||||
import { parseBytesToJSON } from "~/helpers/functional/json"
|
import { parseBytesToJSON } from "~/helpers/functional/json"
|
||||||
import { refreshToken, OAuth2ParamSchema } from "../utils"
|
import { refreshToken, OAuth2ParamSchema } from "../utils"
|
||||||
|
import { ClientCredentialsGrantTypeParams } from "@hoppscotch/data"
|
||||||
|
|
||||||
const interceptorService = getService(KernelInterceptorService)
|
const interceptorService = getService(KernelInterceptorService)
|
||||||
|
|
||||||
const ClientCredentialsFlowParamsSchema = z
|
// Use the existing schema from hoppscotch-data but add client authentication mode
|
||||||
.object({
|
const ClientCredentialsFlowParamsSchema = ClientCredentialsGrantTypeParams.omit(
|
||||||
authEndpoint: z.string(),
|
{
|
||||||
clientID: z.string(),
|
grantType: true,
|
||||||
clientSecret: z.string().optional(),
|
token: true,
|
||||||
scopes: z.string().optional(),
|
}
|
||||||
|
)
|
||||||
|
.extend({
|
||||||
clientAuthentication: z.enum(["AS_BASIC_AUTH_HEADERS", "IN_BODY"]),
|
clientAuthentication: z.enum(["AS_BASIC_AUTH_HEADERS", "IN_BODY"]),
|
||||||
|
// Override optional arrays to be required for the service layer
|
||||||
tokenRequestParams: z.array(OAuth2ParamSchema),
|
tokenRequestParams: z.array(OAuth2ParamSchema),
|
||||||
refreshRequestParams: z.array(OAuth2ParamSchema),
|
refreshRequestParams: z.array(OAuth2ParamSchema),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -8,20 +8,22 @@ import {
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { getService } from "~/modules/dioc"
|
import { getService } from "~/modules/dioc"
|
||||||
import * as E from "fp-ts/Either"
|
import * as E from "fp-ts/Either"
|
||||||
|
import {
|
||||||
|
ImplicitOauthFlowParams as ImplicitOauthFlowParamsData,
|
||||||
|
OAuth2AuthRequestParam,
|
||||||
|
} from "@hoppscotch/data"
|
||||||
import { OAuth2ParamSchema } from "../utils"
|
import { OAuth2ParamSchema } from "../utils"
|
||||||
|
|
||||||
const persistenceService = getService(PersistenceService)
|
const persistenceService = getService(PersistenceService)
|
||||||
|
|
||||||
const ImplicitOauthFlowParamsSchema = z
|
// Use the existing schema from hoppscotch-data
|
||||||
.object({
|
const ImplicitOauthFlowParamsSchema = ImplicitOauthFlowParamsData.omit({
|
||||||
authEndpoint: z.string(),
|
grantType: true,
|
||||||
clientID: z.string(),
|
token: true,
|
||||||
scopes: z.string().optional(),
|
})
|
||||||
authRequestParams: z.array(
|
.extend({
|
||||||
OAuth2ParamSchema.omit({
|
// Override optional arrays to be required for the service layer
|
||||||
sendIn: true,
|
authRequestParams: z.array(OAuth2AuthRequestParam),
|
||||||
})
|
|
||||||
),
|
|
||||||
refreshRequestParams: z.array(OAuth2ParamSchema),
|
refreshRequestParams: z.array(OAuth2ParamSchema),
|
||||||
})
|
})
|
||||||
.refine((params) => {
|
.refine((params) => {
|
||||||
|
|
|
||||||
|
|
@ -11,18 +11,18 @@ import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
|
||||||
import { useToast } from "~/composables/toast"
|
import { useToast } from "~/composables/toast"
|
||||||
import { content } from "@hoppscotch/kernel"
|
import { content } from "@hoppscotch/kernel"
|
||||||
import { parseBytesToJSON } from "~/helpers/functional/json"
|
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 interceptorService = getService(KernelInterceptorService)
|
||||||
|
|
||||||
const PasswordFlowParamsSchema = z
|
// Use the existing schema from hoppscotch-data
|
||||||
.object({
|
const PasswordFlowParamsSchema = PasswordGrantTypeParams.omit({
|
||||||
authEndpoint: z.string(),
|
grantType: true,
|
||||||
clientID: z.string(),
|
token: true,
|
||||||
clientSecret: z.string().optional(),
|
})
|
||||||
scopes: z.string().optional(),
|
.extend({
|
||||||
username: z.string(),
|
// Override optional arrays to be required for the service layer
|
||||||
password: z.string(),
|
|
||||||
tokenRequestParams: z.array(OAuth2ParamSchema),
|
tokenRequestParams: z.array(OAuth2ParamSchema),
|
||||||
refreshRequestParams: z.array(OAuth2ParamSchema),
|
refreshRequestParams: z.array(OAuth2ParamSchema),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { getService } from "~/modules/dioc"
|
||||||
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
|
import { KernelInterceptorService } from "~/services/kernel-interceptor.service"
|
||||||
import { content } from "@hoppscotch/kernel"
|
import { content } from "@hoppscotch/kernel"
|
||||||
import { decodeResponseAsJSON } from "./oauth.service"
|
import { decodeResponseAsJSON } from "./oauth.service"
|
||||||
|
import { OAuth2AdvancedParam } from "@hoppscotch/data"
|
||||||
|
|
||||||
const interceptorService = getService(KernelInterceptorService)
|
const interceptorService = getService(KernelInterceptorService)
|
||||||
|
|
||||||
|
|
@ -110,13 +111,9 @@ export const refreshToken = async ({
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common OAuth2 parameter schema with all possible fields
|
* Common OAuth2 parameter schema
|
||||||
* Used as base for both auth requests and advanced token/refresh requests
|
* Used for all OAuth parameter types - omit sendIn field where not needed
|
||||||
*/
|
*/
|
||||||
export const OAuth2ParamSchema = z.object({
|
export const OAuth2ParamSchema = OAuth2AdvancedParam.extend({
|
||||||
id: z.number(),
|
|
||||||
key: z.string(),
|
|
||||||
value: z.string(),
|
|
||||||
active: z.boolean(),
|
|
||||||
sendIn: z.enum(["headers", "url", "body"]).optional(),
|
sendIn: z.enum(["headers", "url", "body"]).optional(),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,8 @@ export { ImplicitOauthFlowParams } from "./v/15/auth"
|
||||||
export {
|
export {
|
||||||
HoppRESTAuthOAuth2,
|
HoppRESTAuthOAuth2,
|
||||||
ClientCredentialsGrantTypeParams,
|
ClientCredentialsGrantTypeParams,
|
||||||
|
OAuth2AdvancedParam,
|
||||||
|
OAuth2AuthRequestParam,
|
||||||
} from "./v/15/auth"
|
} from "./v/15/auth"
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ import { ImplicitOauthFlowParams as ImplicitOauthFlowParamsOld } from "../3"
|
||||||
export { HoppRESTAuthJWT } from "../13/auth"
|
export { HoppRESTAuthJWT } from "../13/auth"
|
||||||
|
|
||||||
// Define the OAuth2 advanced parameter structure
|
// Define the OAuth2 advanced parameter structure
|
||||||
const OAuth2AdvancedParam = z.object({
|
export const OAuth2AdvancedParam = z.object({
|
||||||
id: z.number(),
|
id: z.number(),
|
||||||
key: z.string(),
|
key: z.string(),
|
||||||
value: z.string(),
|
value: z.string(),
|
||||||
|
|
@ -29,7 +29,7 @@ const OAuth2AdvancedParam = z.object({
|
||||||
})
|
})
|
||||||
|
|
||||||
// omit sendIn from OAuth2AuthRequestParam
|
// omit sendIn from OAuth2AuthRequestParam
|
||||||
const OAuth2AuthRequestParam = OAuth2AdvancedParam.omit({ sendIn: true })
|
export const OAuth2AuthRequestParam = OAuth2AdvancedParam.omit({ sendIn: true })
|
||||||
|
|
||||||
export const AuthCodeGrantTypeParams = AuthCodeGrantTypeParamsOld.extend({
|
export const AuthCodeGrantTypeParams = AuthCodeGrantTypeParamsOld.extend({
|
||||||
authRequestParams: z.array(OAuth2AuthRequestParam).optional().default([]),
|
authRequestParams: z.array(OAuth2AuthRequestParam).optional().default([]),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue