-
Notifications
You must be signed in to change notification settings - Fork 74
Add LDAP integration #509
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
base: main
Are you sure you want to change the base?
Add LDAP integration #509
Changes from all commits
872aada
e86bce0
da0c864
f72b4f2
8f10861
6efece1
72bd970
5529f91
093a7ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # LDAP Configuration for CTFNote | ||
| # Set LDAP_ENABLED=true to enable LDAP authentication | ||
|
|
||
| # Basic LDAP Settings | ||
| LDAP_ENABLED=true | ||
| LDAP_SERVER=ldap.forumsys.com | ||
| LDAP_PORT=389 | ||
| LDAP_BASE_DN=dc=example,dc=com | ||
|
|
||
| # LDAP Bind Configuration | ||
| LDAP_BIND_DN=cn=read-only-admin,dc=example,dc=com | ||
| LDAP_BIND_PASSWORD=password | ||
|
|
||
| # User Search Configuration | ||
| LDAP_USER_SEARCH_BASE=dc=example,dc=com | ||
| LDAP_USER_SEARCH_FILTER=(uid={0}) | ||
|
|
||
| # Role Mapping | ||
| LDAP_DEFAULT_ROLE=user_guest | ||
| LDAP_MATHEMATICIANS_ROLE=user_member | ||
| LDAP_SCIENTISTS_ROLE=user_member | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file seems to be for an earlier version of this PR and seems wrong for the current version |
||
|
|
||
| # Test Users Available: | ||
| # Mathematicians: riemann, gauss, euler, euclid (password: password) | ||
| # Scientists: einstein, newton, galieleo, tesla (password: password) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| -- Add LDAP user authentication function | ||
| -- This function will be called from the LDAP plugin to handle user creation/update | ||
|
|
||
| CREATE OR REPLACE FUNCTION ctfnote.login_ldap( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be a private function, because if it is public, I can use it to request a JWT for arbitrary (non-existing) users with arbitrary roles: [{"operationName":"LoginLdap","variables":{"username":"test","userRole":"USER_ADMIN"},"query":"mutation LoginLdap($username: String!, $userRole: Role!) {\n loginLdap(input: {username: $username, userRole: $userRole}) {\n jwt\n __typename\n }\n}"}] |
||
| "username" text, | ||
| "user_role" ctfnote.role, | ||
| "ldap_data" jsonb DEFAULT '{}'::jsonb | ||
| ) | ||
| RETURNS ctfnote.jwt | ||
| AS $$ | ||
| DECLARE | ||
| existing_user ctfnote_private.user; | ||
| new_user ctfnote_private.user; | ||
| BEGIN | ||
| -- Check if user already exists | ||
| SELECT * INTO existing_user | ||
| FROM ctfnote_private.user | ||
| WHERE login = username; | ||
|
|
||
| IF existing_user.id IS NOT NULL THEN | ||
| -- User exists, update role if different | ||
| IF existing_user.role != user_role THEN | ||
| UPDATE ctfnote_private.user | ||
| SET role = user_role | ||
| WHERE id = existing_user.id; | ||
| END IF; | ||
|
|
||
| -- Return token for existing user | ||
| RETURN (ctfnote_private.new_token(existing_user.id))::ctfnote.jwt; | ||
| ELSE | ||
| -- Create new user with LDAP marker in password field | ||
| INSERT INTO ctfnote_private.user ("login", "password", "role") | ||
| VALUES (username, 'ldap_user', user_role) | ||
| RETURNING * INTO new_user; | ||
|
|
||
| -- Create profile | ||
| INSERT INTO ctfnote.profile ("id", "username") | ||
| VALUES (new_user.id, username); | ||
|
|
||
| -- Return token for new user | ||
| RETURN (ctfnote_private.new_token(new_user.id))::ctfnote.jwt; | ||
| END IF; | ||
| EXCEPTION | ||
| WHEN unique_violation THEN | ||
| RAISE EXCEPTION 'Username already taken'; | ||
| END; | ||
| $$ | ||
| LANGUAGE plpgsql | ||
| STRICT | ||
| SECURITY DEFINER; | ||
|
|
||
| -- Grant permission to execute this function to anonymous users (for login) | ||
| GRANT EXECUTE ON FUNCTION ctfnote.login_ldap(text, ctfnote.role, jsonb) TO user_anonymous; | ||
|
|
||
| -- Add a function to check if LDAP is enabled (can be used by frontend) | ||
| CREATE OR REPLACE FUNCTION ctfnote.ldap_enabled() | ||
| RETURNS boolean | ||
| AS $$ | ||
| BEGIN | ||
| -- This will be determined by environment variables in the plugin | ||
| -- For now, return false by default | ||
| RETURN false; | ||
| END; | ||
| $$ | ||
| LANGUAGE plpgsql | ||
| STABLE | ||
| SECURITY DEFINER; | ||
|
|
||
| GRANT EXECUTE ON FUNCTION ctfnote.ldap_enabled() TO user_anonymous; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should either use singular or support specifying multiple groups