forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-19540 FAPI 2.0 Baseline : Reject Resource Owner Password Cre…
…dentials Grant
- Loading branch information
Showing
8 changed files
with
288 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
45 changes: 45 additions & 0 deletions
45
...a/org/keycloak/services/clientpolicy/context/ResourceOwnerPasswordCredentialsContext.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,45 @@ | ||
/* | ||
* 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.context; | ||
|
||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import org.keycloak.services.clientpolicy.ClientPolicyContext; | ||
import org.keycloak.services.clientpolicy.ClientPolicyEvent; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Takashi Norimatsu</a> | ||
*/ | ||
public class ResourceOwnerPasswordCredentialsContext implements ClientPolicyContext { | ||
|
||
private final MultivaluedMap<String, String> params; | ||
|
||
public ResourceOwnerPasswordCredentialsContext(MultivaluedMap<String, String> params) { | ||
this.params = params; | ||
} | ||
|
||
@Override | ||
public ClientPolicyEvent getEvent() { | ||
return ClientPolicyEvent.RESOURCE_OWNER_PASSWORD_CREDENTIALS_REQUEST; | ||
} | ||
|
||
public MultivaluedMap<String, String> getParams() { | ||
return params; | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
...k/services/clientpolicy/executor/RejectResourceOwnerPasswordCredentialsGrantExecutor.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,108 @@ | ||
/* | ||
* 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.executor; | ||
|
||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import org.keycloak.OAuthErrorException; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.representations.idm.ClientPolicyExecutorConfigurationRepresentation; | ||
import org.keycloak.representations.idm.ClientRepresentation; | ||
import org.keycloak.services.clientpolicy.ClientPolicyContext; | ||
import org.keycloak.services.clientpolicy.ClientPolicyException; | ||
import org.keycloak.services.clientpolicy.context.ClientCRUDContext; | ||
import org.keycloak.services.clientpolicy.context.ResourceOwnerPasswordCredentialsContext; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Takashi Norimatsu</a> | ||
*/ | ||
public class RejectResourceOwnerPasswordCredentialsGrantExecutor implements ClientPolicyExecutorProvider<RejectResourceOwnerPasswordCredentialsGrantExecutor.Configuration> { | ||
|
||
private final KeycloakSession session; | ||
private Configuration configuration; | ||
|
||
public RejectResourceOwnerPasswordCredentialsGrantExecutor(KeycloakSession session) { | ||
this.session = session; | ||
} | ||
|
||
@Override | ||
public void setupConfiguration(Configuration config) { | ||
this.configuration = config; | ||
} | ||
|
||
@Override | ||
public Class<Configuration> getExecutorConfigurationClass() { | ||
return Configuration.class; | ||
} | ||
|
||
public static class Configuration extends ClientPolicyExecutorConfigurationRepresentation { | ||
@JsonProperty("auto-configure") | ||
protected Boolean autoConfigure; | ||
|
||
public Boolean isAutoConfigure() { | ||
return autoConfigure; | ||
} | ||
|
||
public void setAutoConfigure(Boolean autoConfigure) { | ||
this.autoConfigure = autoConfigure; | ||
} | ||
} | ||
|
||
@Override | ||
public String getProviderId() { | ||
return RejectResourceOwnerPasswordCredentialsGrantExecutorFactory.PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public void executeOnEvent(ClientPolicyContext context) throws ClientPolicyException { | ||
switch (context.getEvent()) { | ||
case REGISTER: | ||
case UPDATE: | ||
ClientCRUDContext clientUpdateContext = (ClientCRUDContext)context; | ||
autoConfigure(clientUpdateContext.getProposedClientRepresentation()); | ||
validate(clientUpdateContext.getProposedClientRepresentation()); | ||
break; | ||
case RESOURCE_OWNER_PASSWORD_CREDENTIALS_REQUEST: | ||
ResourceOwnerPasswordCredentialsContext ropcContext = (ResourceOwnerPasswordCredentialsContext)context; | ||
executeOnAuthorizationRequest(ropcContext.getParams()); | ||
return; | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
private void autoConfigure(ClientRepresentation rep) { | ||
if (configuration.isAutoConfigure()) | ||
rep.setDirectAccessGrantsEnabled(Boolean.FALSE); | ||
} | ||
|
||
private void validate(ClientRepresentation rep) throws ClientPolicyException { | ||
boolean isResourceOwnerPasswordCredentialsGrantEnabled = rep.isDirectAccessGrantsEnabled().booleanValue(); | ||
if (!isResourceOwnerPasswordCredentialsGrantEnabled) return; | ||
throw new ClientPolicyException(OAuthErrorException.INVALID_CLIENT_METADATA, "Invalid client metadata: resource owner password credentials grant enabled"); | ||
} | ||
|
||
private void executeOnAuthorizationRequest(MultivaluedMap<String, String> params) throws ClientPolicyException { | ||
// Before client policies operation, Token Endpoint logic has already checked whether resource owner password credentials grant is activated for a client. | ||
// This method rejects resource owner password credentials grant regardless of client setting for allowing resource owner password credentials grant. | ||
throw new ClientPolicyException(OAuthErrorException.INVALID_GRANT, "resource owner password credentials grant is prohibited."); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...ces/clientpolicy/executor/RejectResourceOwnerPasswordCredentialsGrantExecutorFactory.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,72 @@ | ||
/* | ||
* 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.executor; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.keycloak.Config.Scope; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
import org.keycloak.provider.ProviderConfigProperty; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Takashi Norimatsu</a> | ||
*/ | ||
public class RejectResourceOwnerPasswordCredentialsGrantExecutorFactory implements ClientPolicyExecutorProviderFactory { | ||
|
||
public static final String PROVIDER_ID = "reject-ropc-grant"; | ||
|
||
public static final String AUTO_CONFIGURE = "auto-configure"; | ||
|
||
private static final ProviderConfigProperty AUTO_CONFIGURE_PROPERTY = new ProviderConfigProperty( | ||
AUTO_CONFIGURE, "Auto-configure", "If On, then the during client creation or update, the configuration of the client will be auto-configured to reject a resource owner password credentials grant.", ProviderConfigProperty.BOOLEAN_TYPE, false); | ||
|
||
@Override | ||
public ClientPolicyExecutorProvider create(KeycloakSession session) { | ||
return new RejectResourceOwnerPasswordCredentialsGrantExecutor(session); | ||
} | ||
|
||
@Override | ||
public void init(Scope config) { | ||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory factory) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public String getHelpText() { | ||
return "It makes keycloak to reject a resource owner password credentials grant."; | ||
} | ||
|
||
@Override | ||
public List<ProviderConfigProperty> getConfigProperties() { | ||
return Collections.singletonList(AUTO_CONFIGURE_PROPERTY); | ||
} | ||
|
||
} |
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