Skip to content

Commit

Permalink
KEYCLOAK-15429 Add CORS origin on permission request with invalid acc…
Browse files Browse the repository at this point in the history
…ess token
  • Loading branch information
Frode Ingebrigtsen authored and pedroigor committed Sep 22, 2020
1 parent 053f5ba commit 0a0b7da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.keycloak.OAuth2Constants;
import org.keycloak.OAuthErrorException;
import org.keycloak.TokenVerifier;
import org.keycloak.authentication.AuthenticationProcessor;
import org.keycloak.authorization.AuthorizationProvider;
import org.keycloak.authorization.authorization.AuthorizationTokenService;
Expand All @@ -36,10 +37,13 @@
import org.keycloak.broker.provider.IdentityProviderMapperSyncModeDelegate;
import org.keycloak.common.ClientConnection;
import org.keycloak.common.Profile;
import org.keycloak.common.VerificationException;
import org.keycloak.common.constants.ServiceAccountConstants;
import org.keycloak.common.util.Base64Url;
import org.keycloak.common.util.KeycloakUriBuilder;
import org.keycloak.constants.AdapterConstants;
import org.keycloak.crypto.SignatureProvider;
import org.keycloak.crypto.SignatureVerifierContext;
import org.keycloak.events.Details;
import org.keycloak.events.Errors;
import org.keycloak.events.EventBuilder;
Expand Down Expand Up @@ -1209,6 +1213,14 @@ public Response permissionGrant() {
AccessToken accessToken = Tokens.getAccessToken(session);

if (accessToken == null) {
try {
// In case the access token is invalid because it's expired or the user is disabled, identify the client
// from the access token anyway in order to set correct CORS headers.
AccessToken invalidToken = new JWSInput(accessTokenString).readJsonContent(AccessToken.class);
ClientModel client = realm.getClientByClientId(invalidToken.getIssuedFor());
cors.allowedOrigins(session, client);
} catch (JWSInputException ignore) {
}
throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_GRANT, "Invalid bearer token", Status.UNAUTHORIZED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import static org.keycloak.testsuite.util.OAuthClient.AUTH_SERVER_ROOT;

import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.client.Client;
Expand All @@ -39,6 +42,12 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import com.google.common.base.Charsets;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.OAuth2Constants;
Expand All @@ -51,6 +60,7 @@
import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
import org.keycloak.representations.AccessToken;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.representations.idm.authorization.AuthorizationResponse;
import org.keycloak.representations.idm.authorization.JSPolicyRepresentation;
import org.keycloak.representations.idm.authorization.Permission;
Expand All @@ -61,6 +71,7 @@
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
import org.keycloak.testsuite.util.OAuthClient;
import org.keycloak.testsuite.util.UserBuilder;
import org.keycloak.util.BasicAuthHelper;
import org.keycloak.util.JsonSerialization;

Expand Down Expand Up @@ -361,6 +372,33 @@ public void testObtainRptUsingAccessToken() throws Exception {
assertTrue(permissions.isEmpty());
}

@Test
public void testCORSHeadersInFailedRptRequest() throws Exception {
AccessTokenResponse accessTokenResponse = getAuthzClient().obtainAccessToken("marta", "password");

UserRepresentation userRepresentation = getRealm().users().search("marta").get(0);
UserRepresentation updatedUser = UserBuilder.edit(userRepresentation).enabled(false).build();
getRealm().users().get(userRepresentation.getId()).update(updatedUser);

PermissionRequest permissions = new PermissionRequest("Resource A", "ScopeA", "ScopeB");
String ticket = getAuthzClient().protection().permission().create(Arrays.asList(permissions)).getTicket();

String tokenEndpoint = getAuthzClient().getServerConfiguration().getTokenEndpoint();
HttpPost post = new HttpPost(tokenEndpoint);
post.addHeader("Origin", "http://localhost");
post.addHeader("Authorization", "Bearer " + accessTokenResponse.getToken());
List<NameValuePair> parameters = new LinkedList<>();
parameters.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.UMA_GRANT_TYPE));
parameters.add(new BasicNameValuePair("ticket", ticket));

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, Charsets.UTF_8);
post.setEntity(formEntity);

CloseableHttpResponse response = oauth.getHttpClient().get().execute(post);
assertEquals(401, response.getStatusLine().getStatusCode());
assertEquals("http://localhost", response.getFirstHeader("Access-Control-Allow-Origin").getValue());
}

@Test
public void testRefreshRpt() {
AccessTokenResponse accessTokenResponse = getAuthzClient().obtainAccessToken("marta", "password");
Expand Down

0 comments on commit 0a0b7da

Please sign in to comment.