forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add limited support for LDAP password policy control
Send password policy control during LDAP bind operation and receive response according to chapter 9.1 of [1]. When server responds with error "changeAfterReset" then prompt user to update their password by adding UPDATE_PASSWORD required action Following preconditions need to be met: - import mode enabled, otherwise required actions cannot be persisted. - edit mode is set writable, so that users can modify their passwords. Without these preconditions changeAfterReset is treated as failed authentication. [1] https://datatracker.ietf.org/doc/html/draft-behera-ldap-password-policy-11 Signed-off-by: Tero Saarni <tero.saarni@est.tech>
- Loading branch information
Showing
15 changed files
with
381 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...src/main/java/org/keycloak/storage/ldap/idm/store/ldap/control/PasswordPolicyControl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.storage.ldap.idm.store.ldap.control; | ||
|
||
import java.math.BigInteger; | ||
|
||
import javax.naming.ldap.Control; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.wildfly.security.asn1.ASN1; | ||
import org.wildfly.security.asn1.ASN1Exception; | ||
import org.wildfly.security.asn1.DERDecoder; | ||
|
||
/** | ||
* Implements (parts of) draft-behera-ldap-password-policy | ||
*/ | ||
public class PasswordPolicyControl implements Control { | ||
|
||
/* https://datatracker.ietf.org/doc/html/draft-behera-ldap-password-policy-11#section-6.1 */ | ||
public static final String OID = "1.3.6.1.4.1.42.2.27.8.5.1"; | ||
|
||
private static final Logger logger = Logger.getLogger(PasswordPolicyControl.class); | ||
|
||
private static final int ERROR_CHANGE_AFTER_RESET = 2; | ||
|
||
private boolean changeAfterReset; | ||
|
||
/* | ||
* https://datatracker.ietf.org/doc/html/draft-behera-ldap-password-policy-11#section-6.2 | ||
* | ||
* PasswordPolicyResponseValue ::= SEQUENCE { | ||
* warning [0] CHOICE { | ||
* timeBeforeExpiration [0] INTEGER (0 .. maxInt), | ||
* graceAuthNsRemaining [1] INTEGER (0 .. maxInt) } OPTIONAL, | ||
* error [1] ENUMERATED { | ||
* passwordExpired (0), | ||
* accountLocked (1), | ||
* changeAfterReset (2), | ||
* passwordModNotAllowed (3), | ||
* mustSupplyOldPassword (4), | ||
* insufficientPasswordQuality (5), | ||
* passwordTooShort (6), | ||
* passwordTooYoung (7), | ||
* passwordInHistory (8), | ||
* passwordTooLong (9) } OPTIONAL } | ||
*/ | ||
|
||
PasswordPolicyControl(byte[] encodedValue) { | ||
DERDecoder der = new DERDecoder(encodedValue); | ||
|
||
try { | ||
der.startSequence(); // PasswordPolicyResponseValue ::= SEQUENCE | ||
if (der.isNextType(ASN1.CONTEXT_SPECIFIC_MASK, 0, false)) { // warning [0] CHOICE | ||
der.skipElement(); | ||
} | ||
if (der.isNextType(ASN1.CONTEXT_SPECIFIC_MASK, 1, false)) { // error [1] ENUMERATED | ||
der.decodeImplicit(1); | ||
int error = new BigInteger(der.drainElementValue()).intValue(); | ||
this.changeAfterReset = error == ERROR_CHANGE_AFTER_RESET; | ||
} | ||
der.endSequence(); | ||
} catch (ASN1Exception ignored) { | ||
logger.errorf("Failed to parse PasswordPolicyResponseValue: %s", ignored.getMessage()); | ||
} | ||
} | ||
|
||
public boolean changeAfterReset() { | ||
return changeAfterReset; | ||
} | ||
|
||
@Override | ||
public String getID() { | ||
return OID; | ||
} | ||
|
||
@Override | ||
public boolean isCritical() { | ||
return Control.NONCRITICAL; | ||
} | ||
|
||
@Override | ||
public byte[] getEncodedValue() { | ||
return new byte[0]; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...n/java/org/keycloak/storage/ldap/idm/store/ldap/control/PasswordPolicyControlFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2022 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.storage.ldap.idm.store.ldap.control; | ||
|
||
import javax.naming.NamingException; | ||
import javax.naming.ldap.Control; | ||
import javax.naming.ldap.ControlFactory; | ||
|
||
public class PasswordPolicyControlFactory extends ControlFactory { | ||
|
||
@Override | ||
public Control getControlInstance(Control ctl) throws NamingException { | ||
if (ctl.getID().equals(PasswordPolicyControl.OID)) { | ||
return new PasswordPolicyControl(ctl.getEncodedValue()); | ||
} | ||
return null; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...g/keycloak/storage/ldap/idm/store/ldap/control/PasswordPolicyPasswordChangeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.storage.ldap.idm.store.ldap.control; | ||
|
||
import javax.naming.AuthenticationException; | ||
|
||
/** | ||
* PasswordPolicyPasswordChangeException is thrown when LDAP password policy response control indicates error "changeAfterReset". | ||
*/ | ||
public class PasswordPolicyPasswordChangeException extends AuthenticationException { | ||
|
||
public PasswordPolicyPasswordChangeException() { | ||
super(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.