mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-17 01:12:59 +00:00
rebase
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export enum Auth0ConnectionMethod {
|
||||
ClientCredentials = "client-credentials"
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { request } from "@app/lib/config/request";
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
import { removeTrailingSlash } from "@app/lib/fn";
|
||||
import { blockLocalAndPrivateIpAddresses } from "@app/lib/validator";
|
||||
import { TAppConnectionDALFactory } from "@app/services/app-connection/app-connection-dal";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import { encryptAppConnectionCredentials } from "@app/services/app-connection/app-connection-fns";
|
||||
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
|
||||
|
||||
import { Auth0ConnectionMethod } from "./auth0-connection-enums";
|
||||
import { TAuth0AccessTokenResponse, TAuth0Connection, TAuth0ConnectionConfig } from "./auth0-connection-types";
|
||||
|
||||
export const getAuth0ConnectionListItem = () => {
|
||||
return {
|
||||
name: "Auth0" as const,
|
||||
app: AppConnection.Auth0 as const,
|
||||
methods: Object.values(Auth0ConnectionMethod) as [Auth0ConnectionMethod.ClientCredentials]
|
||||
};
|
||||
};
|
||||
|
||||
const authorizeAuth0Connection = async ({
|
||||
clientId,
|
||||
clientSecret,
|
||||
domain,
|
||||
audience
|
||||
}: TAuth0ConnectionConfig["credentials"]) => {
|
||||
const instanceUrl = domain.startsWith("http") ? domain : `https://${domain}`;
|
||||
await blockLocalAndPrivateIpAddresses(instanceUrl);
|
||||
|
||||
const { data } = await request.request<TAuth0AccessTokenResponse>({
|
||||
method: "POST",
|
||||
url: `${removeTrailingSlash(instanceUrl)}/oauth/token`,
|
||||
headers: { "content-type": "application/x-www-form-urlencoded" },
|
||||
data: new URLSearchParams({
|
||||
grant_type: "client_credentials", // this will need to be resolved if we support methods other than client credentials
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
audience
|
||||
})
|
||||
});
|
||||
|
||||
if (data.token_type !== "Bearer") {
|
||||
throw new Error(`Unhandled token type: ${data.token_type}`);
|
||||
}
|
||||
|
||||
return { accessToken: data.access_token, expiresAt: data.expires_in * 1000 + Date.now() };
|
||||
};
|
||||
|
||||
export const getAuth0ConnectionAccessToken = async (
|
||||
{ id, orgId, credentials }: TAuth0Connection,
|
||||
appConnectionDAL: Pick<TAppConnectionDALFactory, "updateById">,
|
||||
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">
|
||||
) => {
|
||||
// just getting a new auth token every time because if permissions change the previous token doesn't have the changes
|
||||
|
||||
const authData = await authorizeAuth0Connection(credentials);
|
||||
|
||||
const updatedCredentials: TAuth0Connection["credentials"] = {
|
||||
...credentials,
|
||||
...authData
|
||||
};
|
||||
|
||||
const encryptedCredentials = await encryptAppConnectionCredentials({
|
||||
credentials: updatedCredentials,
|
||||
orgId,
|
||||
kmsService
|
||||
});
|
||||
|
||||
await appConnectionDAL.updateById(id, { encryptedCredentials });
|
||||
|
||||
return authData.accessToken;
|
||||
};
|
||||
|
||||
export const validateAuth0ConnectionCredentials = async ({ credentials }: TAuth0ConnectionConfig) => {
|
||||
try {
|
||||
const { accessToken, expiresAt } = await authorizeAuth0Connection(credentials);
|
||||
|
||||
return {
|
||||
...credentials,
|
||||
accessToken,
|
||||
expiresAt
|
||||
};
|
||||
} catch (e: unknown) {
|
||||
throw new BadRequestError({
|
||||
message: (e as AxiosError).message ?? `Unable to validate connection: verify credentials`
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { AppConnections } from "@app/lib/api-docs";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
import {
|
||||
BaseAppConnectionSchema,
|
||||
GenericCreateAppConnectionFieldsSchema,
|
||||
GenericUpdateAppConnectionFieldsSchema
|
||||
} from "@app/services/app-connection/app-connection-schemas";
|
||||
|
||||
import { Auth0ConnectionMethod } from "./auth0-connection-enums";
|
||||
|
||||
export const Auth0ConnectionClientCredentialsInputCredentialsSchema = z.object({
|
||||
domain: z.string().trim().min(1, "Domain required").describe(AppConnections.CREDENTIALS.AUTH0_CONNECTION.domain),
|
||||
clientId: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Client ID required")
|
||||
.describe(AppConnections.CREDENTIALS.AUTH0_CONNECTION.clientId),
|
||||
clientSecret: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Client Secret required")
|
||||
.describe(AppConnections.CREDENTIALS.AUTH0_CONNECTION.clientSecret),
|
||||
audience: z
|
||||
.string()
|
||||
.trim()
|
||||
.url()
|
||||
.min(1, "Audience required")
|
||||
.describe(AppConnections.CREDENTIALS.AUTH0_CONNECTION.audience)
|
||||
});
|
||||
|
||||
const Auth0ConnectionClientCredentialsOutputCredentialsSchema = z
|
||||
.object({
|
||||
accessToken: z.string(),
|
||||
expiresAt: z.number()
|
||||
})
|
||||
.merge(Auth0ConnectionClientCredentialsInputCredentialsSchema);
|
||||
|
||||
const BaseAuth0ConnectionSchema = BaseAppConnectionSchema.extend({
|
||||
app: z.literal(AppConnection.Auth0)
|
||||
});
|
||||
|
||||
export const Auth0ConnectionSchema = z.intersection(
|
||||
BaseAuth0ConnectionSchema,
|
||||
z.discriminatedUnion("method", [
|
||||
z.object({
|
||||
method: z.literal(Auth0ConnectionMethod.ClientCredentials),
|
||||
credentials: Auth0ConnectionClientCredentialsOutputCredentialsSchema
|
||||
})
|
||||
])
|
||||
);
|
||||
|
||||
export const SanitizedAuth0ConnectionSchema = z.discriminatedUnion("method", [
|
||||
BaseAuth0ConnectionSchema.extend({
|
||||
method: z.literal(Auth0ConnectionMethod.ClientCredentials),
|
||||
credentials: Auth0ConnectionClientCredentialsInputCredentialsSchema.pick({
|
||||
domain: true,
|
||||
clientId: true,
|
||||
audience: true
|
||||
})
|
||||
})
|
||||
]);
|
||||
|
||||
export const ValidateAuth0ConnectionCredentialsSchema = z.discriminatedUnion("method", [
|
||||
z.object({
|
||||
method: z
|
||||
.literal(Auth0ConnectionMethod.ClientCredentials)
|
||||
.describe(AppConnections.CREATE(AppConnection.Auth0).method),
|
||||
credentials: Auth0ConnectionClientCredentialsInputCredentialsSchema.describe(
|
||||
AppConnections.CREATE(AppConnection.Auth0).credentials
|
||||
)
|
||||
})
|
||||
]);
|
||||
|
||||
export const CreateAuth0ConnectionSchema = ValidateAuth0ConnectionCredentialsSchema.and(
|
||||
GenericCreateAppConnectionFieldsSchema(AppConnection.Auth0)
|
||||
);
|
||||
|
||||
export const UpdateAuth0ConnectionSchema = z
|
||||
.object({
|
||||
credentials: Auth0ConnectionClientCredentialsInputCredentialsSchema.optional().describe(
|
||||
AppConnections.UPDATE(AppConnection.Auth0).credentials
|
||||
)
|
||||
})
|
||||
.and(GenericUpdateAppConnectionFieldsSchema(AppConnection.Auth0));
|
||||
|
||||
export const Auth0ConnectionListItemSchema = z.object({
|
||||
name: z.literal("Auth0"),
|
||||
app: z.literal(AppConnection.Auth0),
|
||||
// the below is preferable but currently breaks with our zod to json schema parser
|
||||
// methods: z.tuple([z.literal(AwsConnectionMethod.ServicePrincipal), z.literal(AwsConnectionMethod.AccessKey)]),
|
||||
methods: z.nativeEnum(Auth0ConnectionMethod).array()
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { DiscriminativePick } from "@app/lib/types";
|
||||
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
|
||||
|
||||
import {
|
||||
Auth0ConnectionSchema,
|
||||
CreateAuth0ConnectionSchema,
|
||||
ValidateAuth0ConnectionCredentialsSchema
|
||||
} from "./auth0-connection-schemas";
|
||||
|
||||
export type TAuth0Connection = z.infer<typeof Auth0ConnectionSchema>;
|
||||
|
||||
export type TAuth0ConnectionInput = z.infer<typeof CreateAuth0ConnectionSchema> & {
|
||||
app: AppConnection.Auth0;
|
||||
};
|
||||
|
||||
export type TValidateAuth0ConnectionCredentialsSchema = typeof ValidateAuth0ConnectionCredentialsSchema;
|
||||
|
||||
export type TAuth0ConnectionConfig = DiscriminativePick<TAuth0Connection, "method" | "app" | "credentials"> & {
|
||||
orgId: string;
|
||||
};
|
||||
|
||||
export type TAuth0AccessTokenResponse = {
|
||||
access_token: string;
|
||||
expires_in: number;
|
||||
scope: string;
|
||||
token_type: string;
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from "./auth0-connection-enums";
|
||||
export * from "./auth0-connection-fns";
|
||||
export * from "./auth0-connection-schemas";
|
||||
export * from "./auth0-connection-types";
|
||||
Reference in New Issue
Block a user