fix: addressed requested changes

This commit is contained in:
Daniel Hougaard
2025-05-08 23:13:46 +04:00
parent a4b648ad95
commit a3b7df4e6b
12 changed files with 166 additions and 146 deletions

View File

@@ -152,7 +152,7 @@ declare module "fastify" {
identityId: string;
user: {
uid: string;
mail: string;
mail?: string;
};
};
kmipUser: {

View File

@@ -23,7 +23,6 @@ export async function up(knex: Knex): Promise<void> {
t.string("url").notNullable();
t.string("searchBase").notNullable();
t.string("searchFilter").notNullable();
t.string("uniqueAttribute").notNullable();
t.jsonb("allowedFields").nullable();

View File

@@ -22,7 +22,6 @@ export const IdentityLdapAuthsSchema = z.object({
url: z.string(),
searchBase: z.string(),
searchFilter: z.string(),
uniqueAttribute: z.string(),
allowedFields: z.unknown().nullable().optional(),
createdAt: z.date(),
updatedAt: z.date()

View File

@@ -1056,7 +1056,7 @@ interface LoginIdentityLdapAuthEvent {
metadata: {
identityId: string;
ldapUsername: string;
ldapEmail: string;
ldapEmail?: string;
};
}

View File

@@ -185,6 +185,49 @@ export const UNIVERSAL_AUTH = {
}
} as const;
export const LDAP_AUTH = {
LOGIN: {
identityId: "The ID of the identity to login.",
username: "The username of the LDAP user to login.",
password: "The password of the LDAP user to login."
},
ATTACH: {
identityId: "The ID of the identity to attach the configuration onto.",
url: "The URL of the LDAP server.",
allowedFields:
"The comma-separated array of key/value pairs of required fields that the LDAP entry must have in order to authenticate.",
searchBase: "The base DN to search for the LDAP user.",
searchFilter: "The filter to use to search for the LDAP user.",
bindDN: "The DN of the user to bind to the LDAP server.",
bindPass: "The password of the user to bind to the LDAP server.",
ldapCaCertificate: "The PEM-encoded CA certificate for the LDAP server.",
accessTokenTTL: "The lifetime for an access token in seconds.",
accessTokenMaxTTL: "The maximum lifetime for an access token in seconds.",
accessTokenNumUsesLimit: "The maximum number of times that an access token can be used.",
accessTokenTrustedIps: "The IPs or CIDR ranges that access tokens can be used from."
},
UPDATE: {
identityId: "The ID of the identity to update the configuration for.",
url: "The new URL of the LDAP server.",
allowedFields: "The comma-separated list of allowed fields to return from the LDAP user.",
searchBase: "The new base DN to search for the LDAP user.",
searchFilter: "The new filter to use to search for the LDAP user.",
bindDN: "The new DN of the user to bind to the LDAP server.",
bindPass: "The new password of the user to bind to the LDAP server.",
ldapCaCertificate: "The new PEM-encoded CA certificate for the LDAP server.",
accessTokenTTL: "The new lifetime for an access token in seconds.",
accessTokenMaxTTL: "The new maximum lifetime for an access token in seconds.",
accessTokenNumUsesLimit: "The new maximum number of times that an access token can be used.",
accessTokenTrustedIps: "The new IPs or CIDR ranges that access tokens can be used from."
},
RETRIEVE: {
identityId: "The ID of the identity to retrieve the configuration for."
},
REVOKE: {
identityId: "The ID of the identity to revoke the configuration for."
}
} as const;
export const AWS_AUTH = {
LOGIN: {
identityId: "The ID of the identity to login.",

View File

@@ -15,7 +15,7 @@ import { z } from "zod";
import { IdentityLdapAuthsSchema } from "@app/db/schemas/identity-ldap-auths";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { ApiDocsTags } from "@app/lib/api-docs";
import { ApiDocsTags, LDAP_AUTH } from "@app/lib/api-docs";
import { getConfig } from "@app/lib/config/env";
import { UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
@@ -45,6 +45,7 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
...ldapConfig,
isActive: true,
groupSearchBase: "",
uniqueUserAttribute: "",
groupSearchFilter: ""
};
@@ -120,9 +121,9 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
tags: [ApiDocsTags.LdapAuth],
description: "Login with LDAP Auth",
body: z.object({
identityId: z.string().trim(),
username: z.string(),
password: z.string()
identityId: z.string().trim().describe(LDAP_AUTH.LOGIN.identityId),
username: z.string().describe(LDAP_AUTH.LOGIN.username),
password: z.string().describe(LDAP_AUTH.LOGIN.password)
}),
response: {
200: z.object({
@@ -147,11 +148,7 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
},
handler: async (req) => {
if (
!req.passportMachineIdentity?.identityId ||
!req.passportMachineIdentity.user.mail ||
!req.passportMachineIdentity.user.uid
) {
if (!req.passportMachineIdentity?.identityId || !req.passportMachineIdentity.user.uid) {
throw new UnauthorizedError({ message: "Invalid request. Missing identity ID or LDAP entry details." });
}
@@ -200,29 +197,40 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
}
],
params: z.object({
identityId: z.string().trim()
identityId: z.string().trim().describe(LDAP_AUTH.ATTACH.identityId)
}),
body: z
.object({
url: z.string().trim().min(1),
bindDN: z.string().trim().min(1),
bindPass: z.string().trim().min(1),
searchBase: z.string().trim().min(1),
uniqueAttribute: z.string().trim().min(1).default("uidNumber"),
searchFilter: z.string().trim().min(1).default("(uid={{username}})"),
allowedFields: AllowedFieldsSchema.array().optional(),
ldapCaCertificate: z.string().trim().optional(),
url: z.string().trim().min(1).describe(LDAP_AUTH.ATTACH.url),
bindDN: z.string().trim().min(1).describe(LDAP_AUTH.ATTACH.bindDN),
bindPass: z.string().trim().min(1).describe(LDAP_AUTH.ATTACH.bindPass),
searchBase: z.string().trim().min(1).describe(LDAP_AUTH.ATTACH.searchBase),
searchFilter: z.string().trim().min(1).default("(uid={{username}})").describe(LDAP_AUTH.ATTACH.searchFilter),
allowedFields: AllowedFieldsSchema.array().optional().describe(LDAP_AUTH.ATTACH.allowedFields),
ldapCaCertificate: z.string().trim().optional().describe(LDAP_AUTH.ATTACH.ldapCaCertificate),
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
})
.array()
.min(1)
.default([{ ipAddress: "0.0.0.0/0" }, { ipAddress: "::/0" }]),
accessTokenTTL: z.number().int().min(0).max(315360000).default(2592000),
accessTokenMaxTTL: z.number().int().min(1).max(315360000).default(2592000),
accessTokenNumUsesLimit: z.number().int().min(0).default(0)
.default([{ ipAddress: "0.0.0.0/0" }, { ipAddress: "::/0" }])
.describe(LDAP_AUTH.ATTACH.accessTokenTrustedIps),
accessTokenTTL: z
.number()
.int()
.min(0)
.max(315360000)
.default(2592000)
.describe(LDAP_AUTH.ATTACH.accessTokenTTL),
accessTokenMaxTTL: z
.number()
.int()
.min(1)
.max(315360000)
.default(2592000)
.describe(LDAP_AUTH.ATTACH.accessTokenMaxTTL),
accessTokenNumUsesLimit: z.number().int().min(0).default(0).describe(LDAP_AUTH.ATTACH.accessTokenNumUsesLimit)
})
.refine(
(val) => val.accessTokenTTL <= val.accessTokenMaxTTL,
@@ -286,27 +294,38 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
}
],
params: z.object({
identityId: z.string()
identityId: z.string().trim().describe(LDAP_AUTH.UPDATE.identityId)
}),
body: z
.object({
url: z.string().trim().min(1),
bindDN: z.string().trim().min(1),
bindPass: z.string().trim().min(1),
searchBase: z.string().trim().min(1),
uniqueAttribute: z.string().trim().min(1).default("uidNumber"),
searchFilter: z.string().trim().min(1).default("(uid={{username}})"),
allowedFields: AllowedFieldsSchema.array().optional(),
url: z.string().trim().min(1).describe(LDAP_AUTH.UPDATE.url),
bindDN: z.string().trim().min(1).describe(LDAP_AUTH.UPDATE.bindDN),
bindPass: z.string().trim().min(1).describe(LDAP_AUTH.UPDATE.bindPass),
searchBase: z.string().trim().min(1).describe(LDAP_AUTH.UPDATE.searchBase),
searchFilter: z.string().trim().min(1).default("(uid={{username}})").describe(LDAP_AUTH.UPDATE.searchFilter),
allowedFields: AllowedFieldsSchema.array().optional().describe(LDAP_AUTH.UPDATE.allowedFields),
accessTokenTrustedIps: z
.object({
ipAddress: z.string().trim()
})
.array()
.min(1)
.optional(),
accessTokenTTL: z.number().int().min(0).max(315360000).optional(),
accessTokenNumUsesLimit: z.number().int().min(0).optional(),
accessTokenMaxTTL: z.number().int().max(315360000).min(0).optional()
.optional()
.describe(LDAP_AUTH.UPDATE.accessTokenTrustedIps),
accessTokenTTL: z.number().int().min(0).max(315360000).optional().describe(LDAP_AUTH.UPDATE.accessTokenTTL),
accessTokenNumUsesLimit: z
.number()
.int()
.min(0)
.optional()
.describe(LDAP_AUTH.UPDATE.accessTokenNumUsesLimit),
accessTokenMaxTTL: z
.number()
.int()
.max(315360000)
.min(0)
.optional()
.describe(LDAP_AUTH.UPDATE.accessTokenMaxTTL)
})
.refine(
(val) => (val.accessTokenMaxTTL && val.accessTokenTTL ? val.accessTokenTTL <= val.accessTokenMaxTTL : true),
@@ -370,7 +389,7 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
}
],
params: z.object({
identityId: z.string()
identityId: z.string().trim().describe(LDAP_AUTH.RETRIEVE.identityId)
}),
response: {
200: z.object({
@@ -427,7 +446,7 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
}
],
params: z.object({
identityId: z.string()
identityId: z.string().trim().describe(LDAP_AUTH.REVOKE.identityId)
}),
response: {
200: z.object({

View File

@@ -88,7 +88,6 @@ export const identityLdapAuthServiceFactory = ({
url: ldapAuth.url,
bindDN,
bindPass,
uniqueUserAttribute: ldapAuth.uniqueAttribute,
searchBase: ldapAuth.searchBase,
searchFilter: ldapAuth.searchFilter,
caCert: ldapCaCertificate || "",
@@ -100,9 +99,8 @@ export const identityLdapAuthServiceFactory = ({
url: ldapAuth.url,
bindDN,
bindCredentials: bindPass,
uniqueUserAttribute: ldapAuth.uniqueAttribute,
searchBase: ldapAuth.searchBase,
searchFilter: ldapAuth.searchFilter || "(uid={{username}})",
searchFilter: ldapAuth.searchFilter,
...(ldapCaCertificate
? {
tlsOptions: {
@@ -178,7 +176,6 @@ export const identityLdapAuthServiceFactory = ({
url,
searchBase,
searchFilter,
uniqueAttribute,
bindDN,
bindPass,
ldapCaCertificate,
@@ -287,7 +284,6 @@ export const identityLdapAuthServiceFactory = ({
encryptedBindPass,
searchBase,
searchFilter,
uniqueAttribute,
url,
encryptedLdapCaCertificate,
accessTokenMaxTTL,
@@ -308,7 +304,6 @@ export const identityLdapAuthServiceFactory = ({
url,
searchBase,
searchFilter,
uniqueAttribute,
bindDN,
bindPass,
ldapCaCertificate,
@@ -428,7 +423,6 @@ export const identityLdapAuthServiceFactory = ({
url,
searchBase,
searchFilter,
uniqueAttribute,
encryptedBindDN,
encryptedBindPass,
encryptedLdapCaCertificate,

View File

@@ -17,7 +17,6 @@ export type TAttachLdapAuthDTO = {
url: string;
searchBase: string;
searchFilter: string;
uniqueAttribute: string;
bindDN: string;
bindPass: string;
ldapCaCertificate?: string;
@@ -34,7 +33,6 @@ export type TUpdateLdapAuthDTO = {
url?: string;
searchBase?: string;
searchFilter?: string;
uniqueAttribute?: string;
bindDN?: string;
bindPass?: string;
allowedFields?: TAllowedFields[];