KEYCLOAK-15429 Add CORS origin on permission request with invalid access token
This commit is contained in:
parent
053f5bad1f
commit
0a0b7da53e
2 changed files with 50 additions and 0 deletions
|
@ -23,6 +23,7 @@ import org.jboss.resteasy.spi.HttpResponse;
|
||||||
import org.jboss.resteasy.spi.ResteasyProviderFactory;
|
import org.jboss.resteasy.spi.ResteasyProviderFactory;
|
||||||
import org.keycloak.OAuth2Constants;
|
import org.keycloak.OAuth2Constants;
|
||||||
import org.keycloak.OAuthErrorException;
|
import org.keycloak.OAuthErrorException;
|
||||||
|
import org.keycloak.TokenVerifier;
|
||||||
import org.keycloak.authentication.AuthenticationProcessor;
|
import org.keycloak.authentication.AuthenticationProcessor;
|
||||||
import org.keycloak.authorization.AuthorizationProvider;
|
import org.keycloak.authorization.AuthorizationProvider;
|
||||||
import org.keycloak.authorization.authorization.AuthorizationTokenService;
|
import org.keycloak.authorization.authorization.AuthorizationTokenService;
|
||||||
|
@ -36,10 +37,13 @@ import org.keycloak.broker.provider.IdentityProviderMapper;
|
||||||
import org.keycloak.broker.provider.IdentityProviderMapperSyncModeDelegate;
|
import org.keycloak.broker.provider.IdentityProviderMapperSyncModeDelegate;
|
||||||
import org.keycloak.common.ClientConnection;
|
import org.keycloak.common.ClientConnection;
|
||||||
import org.keycloak.common.Profile;
|
import org.keycloak.common.Profile;
|
||||||
|
import org.keycloak.common.VerificationException;
|
||||||
import org.keycloak.common.constants.ServiceAccountConstants;
|
import org.keycloak.common.constants.ServiceAccountConstants;
|
||||||
import org.keycloak.common.util.Base64Url;
|
import org.keycloak.common.util.Base64Url;
|
||||||
import org.keycloak.common.util.KeycloakUriBuilder;
|
import org.keycloak.common.util.KeycloakUriBuilder;
|
||||||
import org.keycloak.constants.AdapterConstants;
|
import org.keycloak.constants.AdapterConstants;
|
||||||
|
import org.keycloak.crypto.SignatureProvider;
|
||||||
|
import org.keycloak.crypto.SignatureVerifierContext;
|
||||||
import org.keycloak.events.Details;
|
import org.keycloak.events.Details;
|
||||||
import org.keycloak.events.Errors;
|
import org.keycloak.events.Errors;
|
||||||
import org.keycloak.events.EventBuilder;
|
import org.keycloak.events.EventBuilder;
|
||||||
|
@ -1209,6 +1213,14 @@ public class TokenEndpoint {
|
||||||
AccessToken accessToken = Tokens.getAccessToken(session);
|
AccessToken accessToken = Tokens.getAccessToken(session);
|
||||||
|
|
||||||
if (accessToken == null) {
|
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);
|
throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_GRANT, "Invalid bearer token", Status.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,10 @@ import static org.junit.Assert.fail;
|
||||||
import static org.keycloak.testsuite.util.OAuthClient.AUTH_SERVER_ROOT;
|
import static org.keycloak.testsuite.util.OAuthClient.AUTH_SERVER_ROOT;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.ws.rs.client.Client;
|
import javax.ws.rs.client.Client;
|
||||||
|
@ -39,6 +42,12 @@ import javax.ws.rs.core.HttpHeaders;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.UriBuilder;
|
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.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.keycloak.OAuth2Constants;
|
import org.keycloak.OAuth2Constants;
|
||||||
|
@ -51,6 +60,7 @@ import org.keycloak.models.utils.KeycloakModelUtils;
|
||||||
import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
|
import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
|
||||||
import org.keycloak.representations.AccessToken;
|
import org.keycloak.representations.AccessToken;
|
||||||
import org.keycloak.representations.AccessTokenResponse;
|
import org.keycloak.representations.AccessTokenResponse;
|
||||||
|
import org.keycloak.representations.idm.UserRepresentation;
|
||||||
import org.keycloak.representations.idm.authorization.AuthorizationResponse;
|
import org.keycloak.representations.idm.authorization.AuthorizationResponse;
|
||||||
import org.keycloak.representations.idm.authorization.JSPolicyRepresentation;
|
import org.keycloak.representations.idm.authorization.JSPolicyRepresentation;
|
||||||
import org.keycloak.representations.idm.authorization.Permission;
|
import org.keycloak.representations.idm.authorization.Permission;
|
||||||
|
@ -61,6 +71,7 @@ import org.keycloak.representations.idm.authorization.ScopePermissionRepresentat
|
||||||
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
|
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
|
||||||
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
|
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
|
||||||
import org.keycloak.testsuite.util.OAuthClient;
|
import org.keycloak.testsuite.util.OAuthClient;
|
||||||
|
import org.keycloak.testsuite.util.UserBuilder;
|
||||||
import org.keycloak.util.BasicAuthHelper;
|
import org.keycloak.util.BasicAuthHelper;
|
||||||
import org.keycloak.util.JsonSerialization;
|
import org.keycloak.util.JsonSerialization;
|
||||||
|
|
||||||
|
@ -361,6 +372,33 @@ public class UmaGrantTypeTest extends AbstractResourceServerTest {
|
||||||
assertTrue(permissions.isEmpty());
|
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
|
@Test
|
||||||
public void testRefreshRpt() {
|
public void testRefreshRpt() {
|
||||||
AccessTokenResponse accessTokenResponse = getAuthzClient().obtainAccessToken("marta", "password");
|
AccessTokenResponse accessTokenResponse = getAuthzClient().obtainAccessToken("marta", "password");
|
||||||
|
|
Loading…
Reference in a new issue