forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Select auth flow via acr using client policies (keycloak#36441)
Closes keycloak#24297 Co-authored-by: Ben Cresitello-Dittmar <[email protected]> Signed-off-by: Giuseppe Graziano <[email protected]>
- Loading branch information
Showing
16 changed files
with
938 additions
and
5 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
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
87 changes: 87 additions & 0 deletions
87
services/src/main/java/org/keycloak/services/clientpolicy/condition/AcrCondition.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,87 @@ | ||
/* | ||
* Copyright 2021 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.services.clientpolicy.condition; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.keycloak.models.Constants; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.protocol.oidc.utils.AcrUtils; | ||
import org.keycloak.representations.idm.ClientPolicyConditionConfigurationRepresentation; | ||
import org.keycloak.services.clientpolicy.ClientPolicyContext; | ||
import org.keycloak.services.clientpolicy.ClientPolicyEvent; | ||
import org.keycloak.services.clientpolicy.ClientPolicyException; | ||
import org.keycloak.services.clientpolicy.ClientPolicyVote; | ||
import org.keycloak.services.clientpolicy.context.AuthorizationRequestContext; | ||
|
||
import java.util.List; | ||
|
||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Giuseppe Graziano</a> | ||
*/ | ||
public class AcrCondition extends AbstractClientPolicyConditionProvider<AcrCondition.Configuration> { | ||
|
||
public AcrCondition(KeycloakSession session) { | ||
super(session); | ||
} | ||
|
||
public static class Configuration extends ClientPolicyConditionConfigurationRepresentation { | ||
|
||
@JsonProperty("acr-property") | ||
protected String acrProperty; | ||
|
||
public String getAcrProperty() { | ||
return acrProperty; | ||
} | ||
|
||
public void setAcrProperty(String acrProperty) { | ||
this.acrProperty = acrProperty; | ||
} | ||
} | ||
|
||
@Override | ||
public Class<Configuration> getConditionConfigurationClass() { | ||
return Configuration.class; | ||
} | ||
|
||
@Override | ||
public String getProviderId() { | ||
return AnyClientConditionFactory.PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPolicyException { | ||
if (context.getEvent() == ClientPolicyEvent.AUTHORIZATION_REQUEST) { | ||
AuthorizationRequestContext authorizationRequestContext = ((AuthorizationRequestContext) context); | ||
if (containsAcr(authorizationRequestContext)) { | ||
authorizationRequestContext.getAuthenticationSession().setAuthNote(Constants.CLIENT_POLICY_REQUESTED_ACR, configuration.getAcrProperty()); | ||
return ClientPolicyVote.YES; | ||
} | ||
else { | ||
return ClientPolicyVote.NO; | ||
} | ||
} | ||
return ClientPolicyVote.ABSTAIN; | ||
} | ||
|
||
private boolean containsAcr(AuthorizationRequestContext context) { | ||
List<String> acrValues = AcrUtils.getAcrValues(context.getAuthorizationEndpointRequest().getClaims(), context.getAuthorizationEndpointRequest().getAcr(), session.getContext().getClient()); | ||
return acrValues != null && !acrValues.isEmpty() && acrValues.contains(configuration.getAcrProperty()); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
services/src/main/java/org/keycloak/services/clientpolicy/condition/AcrConditionFactory.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,64 @@ | ||
/* | ||
* Copyright 2021 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.services.clientpolicy.condition; | ||
|
||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Giuseppe Graziano</a> | ||
*/ | ||
public class AcrConditionFactory extends AbstractClientPolicyConditionProviderFactory { | ||
|
||
public static final String PROVIDER_ID = "acr-condition"; | ||
|
||
public static final String ACR_PROPERTY = "acr-property"; | ||
|
||
private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>(); | ||
|
||
static { | ||
ProviderConfigProperty property = new ProviderConfigProperty(ACR_PROPERTY, "ACR", | ||
"ACR to be requested to satisfy the condition", | ||
ProviderConfigProperty.STRING_TYPE, null); | ||
configProperties.add(property); | ||
} | ||
|
||
@Override | ||
public ClientPolicyConditionProvider create(KeycloakSession session) { | ||
return new AcrCondition(session); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return "The condition is satisfied when configured acr value is requested"; | ||
} | ||
|
||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigProperties() { | ||
return configProperties; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import org.keycloak.protocol.oidc.utils.OIDCResponseType; | ||
import org.keycloak.services.clientpolicy.ClientPolicyContext; | ||
import org.keycloak.services.clientpolicy.ClientPolicyEvent; | ||
import org.keycloak.sessions.AuthenticationSessionModel; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Takashi Norimatsu</a> | ||
|
@@ -35,14 +36,18 @@ public class AuthorizationRequestContext implements ClientPolicyContext { | |
private final String redirectUri; | ||
private final MultivaluedMap<String, String> requestParameters; | ||
|
||
private final AuthenticationSessionModel authenticationSession; | ||
|
||
public AuthorizationRequestContext(OIDCResponseType parsedResponseType, | ||
AuthorizationEndpointRequest request, | ||
String redirectUri, | ||
MultivaluedMap<String, String> requestParameters) { | ||
AuthorizationEndpointRequest request, | ||
String redirectUri, | ||
MultivaluedMap<String, String> requestParameters, | ||
AuthenticationSessionModel authenticationSession) { | ||
this.parsedResponseType = parsedResponseType; | ||
this.request = request; | ||
this.redirectUri = redirectUri; | ||
this.requestParameters = requestParameters; | ||
this.authenticationSession = authenticationSession; | ||
} | ||
|
||
@Override | ||
|
@@ -69,4 +74,8 @@ public MultivaluedMap<String, String> getRequestParameters() { | |
public boolean isParRequest() { | ||
return requestParameters.containsKey(OIDCLoginProtocol.REQUEST_URI_PARAM); | ||
} | ||
|
||
public AuthenticationSessionModel getAuthenticationSession() { | ||
return authenticationSession; | ||
} | ||
} |
Oops, something went wrong.