Add docs for Azure auth

This commit is contained in:
Tuan Dang
2024-05-15 23:13:19 -07:00
parent 265932df20
commit 9352e8bca0
11 changed files with 211 additions and 16 deletions
@@ -6,6 +6,7 @@ import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { TIdentityTrustedIp } from "@app/services/identity/identity-types";
import { validateAzureAuthField } from "@app/services/identity-azure-auth/identity-azure-auth-validators";
export const registerIdentityAzureAuthRouter = async (server: FastifyZodProvider) => {
server.route({
@@ -75,7 +76,7 @@ export const registerIdentityAzureAuthRouter = async (server: FastifyZodProvider
body: z.object({
tenantId: z.string().trim(),
resource: z.string().trim(),
allowedServicePrincipalIds: z.string().trim(),
allowedServicePrincipalIds: validateAzureAuthField,
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
@@ -157,7 +158,7 @@ export const registerIdentityAzureAuthRouter = async (server: FastifyZodProvider
body: z.object({
tenantId: z.string().trim().optional(),
resource: z.string().trim().optional(),
allowedServicePrincipalIds: z.string().trim().optional(),
allowedServicePrincipalIds: validateAzureAuthField.optional(),
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
@@ -160,9 +160,9 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider)
}),
body: z.object({
type: z.enum(["iam", "gce"]).optional(),
allowedServiceAccounts: validateGcpAuthField,
allowedProjects: validateGcpAuthField,
allowedZones: validateGcpAuthField,
allowedServiceAccounts: validateGcpAuthField.optional(),
allowedProjects: validateGcpAuthField.optional(),
allowedZones: validateGcpAuthField.optional(),
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
@@ -63,7 +63,7 @@ export const identityAzureAuthServiceFactory = ({
const isServicePrincipalAllowed = identityAzureAuth.allowedServicePrincipalIds
.split(",")
.map((servicePrincipalId) => servicePrincipalId.trim())
.some((servicePrincipalId) => servicePrincipalId === azureIdentity.appid);
.some((servicePrincipalId) => servicePrincipalId === azureIdentity.oid);
if (!isServicePrincipalAllowed) throw new UnauthorizedError();
}
@@ -79,7 +79,7 @@ type TUserPayload = {
xms_tcdt: number;
};
type TAppRegistrationPayload = {
type TAppPayload = {
aud: string;
iss: string;
iat: number;
@@ -90,7 +90,7 @@ type TAppRegistrationPayload = {
appidacr: string;
idp: string;
idtyp: string;
oid: string;
oid: string; // service principal id
rh: string;
sub: string;
tid: string;
@@ -103,7 +103,7 @@ type TAppRegistrationPayload = {
xms_tcdt: number;
};
export type TAzureAuthJwtPayload = TUserPayload | TAppRegistrationPayload;
export type TAzureAuthJwtPayload = TUserPayload | TAppPayload;
export type TDecodedAzureAuthJwt = {
header: {
@@ -0,0 +1,14 @@
import { z } from "zod";
export const validateAzureAuthField = z
.string()
.trim()
.default("")
.transform((data) => {
if (data === "") return "";
// Trim each ID and join with ', ' to ensure formatting
return data
.split(",")
.map((id) => id.trim())
.join(", ");
});