feat: oauth revamp + support for multiple grant types in oauth (#3885)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -2,29 +2,32 @@ 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 const GQL_REQ_SCHEMA_VERSION = 2
|
||||
export { HoppGQLAuth } from "./v/3"
|
||||
export { HoppGQLAuthOAuth2 } from "./v/3"
|
||||
|
||||
export const GQL_REQ_SCHEMA_VERSION = 3
|
||||
|
||||
const versionedObject = z.object({
|
||||
v: z.number(),
|
||||
})
|
||||
|
||||
export const HoppGQLRequest = createVersionedEntity({
|
||||
latestVersion: 2,
|
||||
latestVersion: 3,
|
||||
versionMap: {
|
||||
1: V1_VERSION,
|
||||
2: V2_VERSION,
|
||||
3: V3_VERSION,
|
||||
},
|
||||
getVersion(x) {
|
||||
const result = versionedObject.safeParse(x)
|
||||
|
||||
@@ -71,7 +71,7 @@ export const HoppGQLAuth = z
|
||||
|
||||
export type HoppGQLAuth = z.infer<typeof HoppGQLAuth>
|
||||
|
||||
const V2_SCHEMA = z.object({
|
||||
export const V2_SCHEMA = z.object({
|
||||
id: z.optional(z.string()),
|
||||
v: z.literal(2),
|
||||
|
||||
|
||||
77
packages/hoppscotch-data/src/graphql/v/3.ts
Normal file
77
packages/hoppscotch-data/src/graphql/v/3.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
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,
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -4,32 +4,39 @@ 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 {
|
||||
HoppRESTAuth,
|
||||
HoppRESTReqBody,
|
||||
HoppRESTHeaders,
|
||||
HoppRESTParams,
|
||||
} from "./v/1"
|
||||
|
||||
import { HoppRESTReqBody, HoppRESTHeaders, HoppRESTParams } from "./v/1"
|
||||
|
||||
import { HoppRESTAuth } from "./v/3"
|
||||
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({
|
||||
@@ -38,11 +45,12 @@ const versionedObject = z.object({
|
||||
})
|
||||
|
||||
export const HoppRESTRequest = createVersionedEntity({
|
||||
latestVersion: 2,
|
||||
latestVersion: 3,
|
||||
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
|
||||
@@ -84,7 +92,7 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
|
||||
),
|
||||
})
|
||||
|
||||
export const RESTReqSchemaVersion = "2"
|
||||
export const RESTReqSchemaVersion = "3"
|
||||
|
||||
export type HoppRESTParam = HoppRESTRequest["params"][number]
|
||||
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
|
||||
@@ -179,7 +187,7 @@ export function makeRESTRequest(
|
||||
|
||||
export function getDefaultRESTRequest(): HoppRESTRequest {
|
||||
return {
|
||||
v: "2",
|
||||
v: "3",
|
||||
endpoint: "https://echo.hoppscotch.io",
|
||||
name: "Untitled",
|
||||
params: [],
|
||||
|
||||
@@ -18,7 +18,7 @@ export const HoppRESTRequestVariables = z.array(
|
||||
|
||||
export type HoppRESTRequestVariables = z.infer<typeof HoppRESTRequestVariables>
|
||||
|
||||
const V2_SCHEMA = V1_SCHEMA.extend({
|
||||
export const V2_SCHEMA = V1_SCHEMA.extend({
|
||||
v: z.literal("2"),
|
||||
requestVariables: HoppRESTRequestVariables,
|
||||
})
|
||||
|
||||
127
packages/hoppscotch-data/src/rest/v/3.ts
Normal file
127
packages/hoppscotch-data/src/rest/v/3.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
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,
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user