Search in sources :

Example 51 with AccessToken

use of org.keycloak.representations.AccessToken in project keycloak by keycloak.

the class HolderOfKeyEnforcerExecutor method checkUserInfo.

private void checkUserInfo(UserInfoRequestContext context, HttpRequest request) throws ClientPolicyException {
    String encodedAccessToken = context.getTokenString();
    AccessToken accessToken = session.tokens().decode(encodedAccessToken, AccessToken.class);
    if (accessToken == null) {
        // this executor does not treat this error case.
        return;
    }
    if (!MtlsHoKTokenUtil.verifyTokenBindingWithClientCertificate(accessToken, request, session)) {
        throw new ClientPolicyException(Errors.NOT_ALLOWED, MtlsHoKTokenUtil.CERT_VERIFY_ERROR_DESC, Response.Status.UNAUTHORIZED);
    }
}
Also used : AccessToken(org.keycloak.representations.AccessToken) ClientPolicyException(org.keycloak.services.clientpolicy.ClientPolicyException)

Example 52 with AccessToken

use of org.keycloak.representations.AccessToken in project keycloak by keycloak.

the class CompositeRoleTest method testRealmOnlyWithUserRoleAppComposite.

@Test
public void testRealmOnlyWithUserRoleAppComposite() throws Exception {
    oauth.realm("test");
    oauth.clientId("REALM_COMPOSITE_1_APPLICATION");
    oauth.doLogin("REALM_ROLE_1_USER", "password");
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    AccessTokenResponse response = oauth.doAccessTokenRequest(code, "password");
    Assert.assertEquals(200, response.getStatusCode());
    Assert.assertEquals("Bearer", response.getTokenType());
    AccessToken token = oauth.verifyToken(response.getAccessToken());
    Assert.assertEquals(getUserId("REALM_ROLE_1_USER"), token.getSubject());
    Assert.assertEquals(1, token.getRealmAccess().getRoles().size());
    Assert.assertTrue(token.getRealmAccess().isUserInRole("REALM_ROLE_1"));
    AccessTokenResponse refreshResponse = oauth.doRefreshTokenRequest(response.getRefreshToken(), "password");
    Assert.assertEquals(200, refreshResponse.getStatusCode());
}
Also used : AccessToken(org.keycloak.representations.AccessToken) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Test(org.junit.Test)

Example 53 with AccessToken

use of org.keycloak.representations.AccessToken in project keycloak by keycloak.

the class CompositeRoleTest method testAppCompositeUser.

@Test
public void testAppCompositeUser() throws Exception {
    oauth.realm("test");
    oauth.clientId("APP_COMPOSITE_APPLICATION");
    oauth.doLogin("APP_COMPOSITE_USER", "password");
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    AccessTokenResponse response = oauth.doAccessTokenRequest(code, "password");
    Assert.assertEquals(200, response.getStatusCode());
    Assert.assertEquals("Bearer", response.getTokenType());
    AccessToken token = oauth.verifyToken(response.getAccessToken());
    Assert.assertEquals(getUserId("APP_COMPOSITE_USER"), token.getSubject());
    Assert.assertEquals(1, token.getResourceAccess("APP_ROLE_APPLICATION").getRoles().size());
    Assert.assertEquals(1, token.getRealmAccess().getRoles().size());
    Assert.assertTrue(token.getResourceAccess("APP_ROLE_APPLICATION").isUserInRole("APP_ROLE_1"));
    Assert.assertTrue(token.getRealmAccess().isUserInRole("REALM_ROLE_1"));
    AccessTokenResponse refreshResponse = oauth.doRefreshTokenRequest(response.getRefreshToken(), "password");
    Assert.assertEquals(200, refreshResponse.getStatusCode());
}
Also used : AccessToken(org.keycloak.representations.AccessToken) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Test(org.junit.Test)

Example 54 with AccessToken

use of org.keycloak.representations.AccessToken in project keycloak by keycloak.

the class CompositeRoleTest method testRealmAppCompositeUser.

@Test
public void testRealmAppCompositeUser() throws Exception {
    oauth.realm("test");
    oauth.clientId("APP_ROLE_APPLICATION");
    oauth.doLogin("REALM_APP_COMPOSITE_USER", "password");
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    AccessTokenResponse response = oauth.doAccessTokenRequest(code, "password");
    Assert.assertEquals(200, response.getStatusCode());
    Assert.assertEquals("Bearer", response.getTokenType());
    AccessToken token = oauth.verifyToken(response.getAccessToken());
    Assert.assertEquals(getUserId("REALM_APP_COMPOSITE_USER"), token.getSubject());
    Assert.assertEquals(1, token.getResourceAccess("APP_ROLE_APPLICATION").getRoles().size());
    Assert.assertTrue(token.getResourceAccess("APP_ROLE_APPLICATION").isUserInRole("APP_ROLE_1"));
    AccessTokenResponse refreshResponse = oauth.doRefreshTokenRequest(response.getRefreshToken(), "password");
    Assert.assertEquals(200, refreshResponse.getStatusCode());
}
Also used : AccessToken(org.keycloak.representations.AccessToken) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Test(org.junit.Test)

Example 55 with AccessToken

use of org.keycloak.representations.AccessToken in project keycloak by keycloak.

the class CompositeImportRoleTest method testRealmOnlyWithUserRoleAppComposite.

@Test
public void testRealmOnlyWithUserRoleAppComposite() throws Exception {
    oauth.realm("test");
    oauth.clientId("REALM_COMPOSITE_1_APPLICATION");
    oauth.doLogin("REALM_ROLE_1_USER", "password");
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    AccessTokenResponse response = oauth.doAccessTokenRequest(code, "password");
    Assert.assertEquals(200, response.getStatusCode());
    Assert.assertEquals("Bearer", response.getTokenType());
    AccessToken token = oauth.verifyToken(response.getAccessToken());
    Assert.assertEquals(getUserId("REALM_ROLE_1_USER"), token.getSubject());
    Assert.assertEquals(1, token.getRealmAccess().getRoles().size());
    Assert.assertTrue(token.getRealmAccess().isUserInRole("REALM_ROLE_1"));
}
Also used : AccessToken(org.keycloak.representations.AccessToken) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Test(org.junit.Test)

Aggregations

AccessToken (org.keycloak.representations.AccessToken)230 Test (org.junit.Test)129 OAuthClient (org.keycloak.testsuite.util.OAuthClient)104 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)54 RefreshToken (org.keycloak.representations.RefreshToken)45 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)37 JWSInput (org.keycloak.jose.jws.JWSInput)29 Permission (org.keycloak.representations.idm.authorization.Permission)28 EventRepresentation (org.keycloak.representations.idm.EventRepresentation)27 Response (javax.ws.rs.core.Response)26 ClientResource (org.keycloak.admin.client.resource.ClientResource)22 VerificationException (org.keycloak.common.VerificationException)19 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)19 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)18 IDToken (org.keycloak.representations.IDToken)18 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)17 IOException (java.io.IOException)15 AuthzClient (org.keycloak.authorization.client.AuthzClient)15 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)14 ArrayList (java.util.ArrayList)13