Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ldap search scope #1948

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { createEnv } = require('@t3-oss/env-nextjs');
const trueStrings = ['1', 't', 'T', 'TRUE', 'true', 'True'];
const falseStrings = ['0', 'f', 'F', 'FALSE', 'false', 'False'];

const ldapSearchScope = z.enum(['base', 'one', 'sub']).default('base');

const zodParsedBoolean = () =>
z
.enum([...trueStrings, ...falseStrings])
Expand Down Expand Up @@ -52,6 +54,7 @@ const env = createEnv({
AUTH_LDAP_BIND_DN: z.string(),
AUTH_LDAP_BIND_PASSWORD: z.string(),
AUTH_LDAP_BASE: z.string(),
AUTH_LDAP_SEARCH_SCOPE: z.enum(['base', 'one', 'sub']).default('base'),
AUTH_LDAP_USERNAME_ATTRIBUTE: z.string().default('uid'),
AUTH_LDAP_GROUP_CLASS: z.string().default('groupOfUniqueNames'),
AUTH_LDAP_GROUP_MEMBER_ATTRIBUTE: z.string().default('member'),
Expand Down Expand Up @@ -115,6 +118,7 @@ const env = createEnv({
AUTH_LDAP_BIND_DN: process.env.AUTH_LDAP_BIND_DN,
AUTH_LDAP_BIND_PASSWORD: process.env.AUTH_LDAP_BIND_PASSWORD,
AUTH_LDAP_BASE: process.env.AUTH_LDAP_BASE,
AUTH_LDAP_SEARCH_SCOPE: process.env.AUTH_LDAP_SEARCH_SCOPE?.toLowerCase(),
AUTH_LDAP_USERNAME_ATTRIBUTE: process.env.AUTH_LDAP_USERNAME_ATTRIBUTE,
AUTH_LDAP_GROUP_CLASS: process.env.AUTH_LDAP_GROUP_CLASS,
AUTH_LDAP_GROUP_MEMBER_ATTRIBUTE: process.env.AUTH_LDAP_GROUP_MEMBER_ATTRIBUTE,
Expand Down
8 changes: 6 additions & 2 deletions src/utils/auth/ldap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type InferrableSearchOptions<
type SearchResultIndex<Attributes extends AttributeConstraint> = Attributes extends string
? Attributes
: Attributes extends readonly string[]
? Attributes[number]
: string;
? Attributes[number]
: string;

type SearchResult<
Attributes extends AttributeConstraint,
Expand Down Expand Up @@ -101,18 +101,22 @@ export default Credentials({
const ldapUser = (
await ldapSearch(client, env.AUTH_LDAP_BASE, {
filter: `(uid=${data.name})`,
scope: env.AUTH_LDAP_SEARCH_SCOPE,
// as const for inference
attributes: ['uid', 'mail'] as const,
})
)[0];

if (!ldapUser) throw new Error('User not found in LDAP');

await ldapLogin(ldapUser.dn, data.password).then((client) => client.destroy());

const userGroups = (
await ldapSearch(client, env.AUTH_LDAP_BASE, {
filter: `(&(objectclass=${env.AUTH_LDAP_GROUP_CLASS})(${
env.AUTH_LDAP_GROUP_MEMBER_ATTRIBUTE
}=${ldapUser[env.AUTH_LDAP_GROUP_MEMBER_USER_ATTRIBUTE as 'dn' | 'uid']}))`,
scope: env.AUTH_LDAP_SEARCH_SCOPE,
// as const for inference
attributes: 'cn',
})
Expand Down
Loading