feat: ability to refresh tokens for oauth flows (#4302)

Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com>
This commit is contained in:
Akash K
2024-08-29 13:27:31 +05:30
committed by GitHub
parent 2ed7221182
commit 181ad098e0
20 changed files with 488 additions and 68 deletions

View File

@@ -5,11 +5,14 @@ import { GQLHeader as GQLHeaderV1 } from "../../graphql/v/1"
import { GQLHeader as GQLHeaderV2 } from "../../graphql/v/6"
import { HoppRESTHeaders as V1_HoppRESTHeaders } from "../../rest/v/1"
import { HoppRESTHeaders as V2_HoppRESTHeaders } from "../../rest/v/7"
import { HoppRESTAuth } from "../../rest/v/7"
import { HoppGQLAuth } from "../../graphql/v/6"
import { v2_baseCollectionSchema, V2_SCHEMA } from "./2"
const v3_baseCollectionSchema = v2_baseCollectionSchema.extend({
v: z.literal(3),
headers: z.union([V2_HoppRESTHeaders, z.array(GQLHeaderV2)]),
auth: z.union([HoppRESTAuth, HoppGQLAuth]),
})
type Input = z.input<typeof v3_baseCollectionSchema> & {

View File

@@ -15,7 +15,7 @@ export {
} from "./v/2"
export { GQLHeader } from "./v/6"
export { HoppGQLAuth, HoppGQLAuthOAuth2 } from "./v/5"
export { HoppGQLAuth, HoppGQLAuthOAuth2 } from "./v/6"
export { HoppGQLAuthAPIKey } from "./v/4"

View File

@@ -1,7 +1,33 @@
import { defineVersion } from "verzod"
import { z } from "zod"
import { V5_SCHEMA } from "./5"
import { HoppRESTAuthOAuth2 } from "./../../rest/v/7"
import {
HoppGQLAuthBasic,
HoppGQLAuthBearer,
HoppGQLAuthInherit,
HoppGQLAuthNone,
} from "./2"
import { HoppGQLAuthAPIKey } from "./4"
export { HoppRESTAuthOAuth2 as HoppGQLAuthOAuth2 } from "../../rest/v/7"
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 GQLHeader = z.object({
key: z.string().catch(""),
@@ -15,6 +41,7 @@ export type GQLHeader = z.infer<typeof GQLHeader>
export const V6_SCHEMA = V5_SCHEMA.extend({
v: z.literal(6),
headers: z.array(GQLHeader).catch([]),
auth: HoppGQLAuth,
})
export default defineVersion({

View File

@@ -13,12 +13,10 @@ import V3_VERSION from "./v/3"
import V4_VERSION from "./v/4"
import V5_VERSION from "./v/5"
import V6_VERSION, { HoppRESTReqBody } from "./v/6"
import V7_VERSION, { HoppRESTAuth } from "./v/7"
import { HoppRESTHeaders, HoppRESTParams } from "./v/7"
import V7_VERSION from "./v/7"
import { HoppRESTRequestVariables } from "./v/2"
import { HoppRESTAuth } from "./v/5"
export * from "./content-types"
@@ -37,11 +35,8 @@ export {
PasswordGrantTypeParams,
} from "./v/3"
export {
AuthCodeGrantTypeParams,
HoppRESTAuth,
HoppRESTAuthOAuth2,
} from "./v/5"
export { AuthCodeGrantTypeParams } from "./v/5"
export { HoppRESTAuthOAuth2, HoppRESTAuth } from "./v/7"
export { HoppRESTAuthAPIKey } from "./v/4"
@@ -159,6 +154,7 @@ export function safelyExtractRESTRequest(
const result = HoppRESTAuth.safeParse(x.auth)
if (result.success) {
// @ts-ignore
req.auth = result.data
}
}

View File

@@ -3,6 +3,56 @@ import { z } from "zod"
import { V6_SCHEMA } from "./6"
import { AuthCodeGrantTypeParams as AuthCodeGrantTypeParamsOld } from "./5"
import {
ClientCredentialsGrantTypeParams,
ImplicitOauthFlowParams,
PasswordGrantTypeParams,
} from "./3"
import {
HoppRESTAuthAPIKey,
HoppRESTAuthBasic,
HoppRESTAuthBearer,
HoppRESTAuthInherit,
HoppRESTAuthNone,
} from "./1"
// Add refreshToken to all grant types except Implicit
export const AuthCodeGrantTypeParams = AuthCodeGrantTypeParamsOld.extend({
refreshToken: z.string().optional(),
})
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>
export const HoppRESTParams = z.array(
z.object({
key: z.string().catch(""),
@@ -29,6 +79,7 @@ export const V7_SCHEMA = V6_SCHEMA.extend({
v: z.literal("7"),
params: HoppRESTParams,
headers: HoppRESTHeaders,
auth: HoppRESTAuth,
})
export default defineVersion({
@@ -54,6 +105,7 @@ export default defineVersion({
v: "7" as const,
params,
headers,
// no need to update anything for HoppRESTAuth, because the newly added refreshToken is optional
}
},
})