Search in sources :

Example 21 with AccessTokenResponse

use of org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse in project keycloak by keycloak.

the class RealmTest method deleteSession.

@Test
public void deleteSession() {
    setupTestAppAndUser();
    oauth.doLogin("testuser", "password");
    AccessTokenResponse tokenResponse = oauth.doAccessTokenRequest(oauth.getCurrentQuery().get(OAuth2Constants.CODE), "secret");
    assertEquals(200, tokenResponse.getStatusCode());
    EventRepresentation event = events.poll();
    assertNotNull(event);
    realm.deleteSession(event.getSessionId());
    assertAdminEvents.assertEvent(realmId, OperationType.DELETE, AdminEventPaths.deleteSessionPath(event.getSessionId()), ResourceType.USER_SESSION);
    try {
        realm.deleteSession(event.getSessionId());
        fail("Expected 404");
    } catch (NotFoundException e) {
        // Expected
        assertAdminEvents.assertEmpty();
    }
    tokenResponse = oauth.doRefreshTokenRequest(tokenResponse.getRefreshToken(), "secret");
    assertEquals(400, tokenResponse.getStatusCode());
    assertEquals("Session not active", tokenResponse.getErrorDescription());
}
Also used : EventRepresentation(org.keycloak.representations.idm.EventRepresentation) AdminEventRepresentation(org.keycloak.representations.idm.AdminEventRepresentation) NotFoundException(javax.ws.rs.NotFoundException) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) AbstractAdminTest(org.keycloak.testsuite.admin.AbstractAdminTest) Test(org.junit.Test)

Example 22 with AccessTokenResponse

use of org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse in project keycloak by keycloak.

the class AbstractGroupTest method login.

AccessToken login(String login, String clientId, String clientSecret, String userId) throws Exception {
    AccessTokenResponse tokenResponse = oauth.doGrantAccessTokenRequest("test", login, "password", null, clientId, clientSecret);
    String accessToken = tokenResponse.getAccessToken();
    String refreshToken = tokenResponse.getRefreshToken();
    PublicKey publicKey = PemUtils.decodePublicKey(ApiUtil.findActiveSigningKey(adminClient.realm("test")).getPublicKey());
    AccessToken accessTokenRepresentation = RSATokenVerifier.verifyToken(accessToken, publicKey, getAuthServerContextRoot() + "/auth/realms/test");
    JWSInput jws = new JWSInput(refreshToken);
    RefreshToken refreshTokenRepresentation = jws.readJsonContent(RefreshToken.class);
    events.expectLogin().client(clientId).user(userId).detail(Details.GRANT_TYPE, OAuth2Constants.PASSWORD).detail(Details.TOKEN_ID, accessTokenRepresentation.getId()).detail(Details.REFRESH_TOKEN_ID, refreshTokenRepresentation.getId()).detail(Details.USERNAME, login).removeDetail(Details.CODE_ID).removeDetail(Details.REDIRECT_URI).removeDetail(Details.CONSENT).assertEvent();
    return accessTokenRepresentation;
}
Also used : RefreshToken(org.keycloak.representations.RefreshToken) PublicKey(java.security.PublicKey) AccessToken(org.keycloak.representations.AccessToken) JWSInput(org.keycloak.jose.jws.JWSInput) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse)

Example 23 with AccessTokenResponse

use of org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse in project keycloak by keycloak.

the class ClientTest method offlineUserSessions.

@Test
public void offlineUserSessions() throws IOException {
    ClientRepresentation client = createAppClient();
    String id = client.getId();
    Response response = realm.users().create(UserBuilder.create().username("testuser").build());
    String userId = ApiUtil.getCreatedId(response);
    response.close();
    realm.users().get(userId).resetPassword(CredentialBuilder.create().password("password").build());
    Map<String, Long> offlineSessionCount = realm.clients().get(id).getOfflineSessionCount();
    assertEquals(new Long(0), offlineSessionCount.get("count"));
    List<UserSessionRepresentation> userSessions = realm.users().get(userId).getOfflineSessions(id);
    assertEquals("There should be no offline sessions", 0, userSessions.size());
    oauth.realm(REALM_NAME);
    oauth.redirectUri(client.getRedirectUris().get(0));
    oauth.scope(OAuth2Constants.OFFLINE_ACCESS);
    oauth.doLogin("testuser", "password");
    AccessTokenResponse accessTokenResponse = oauth.doAccessTokenRequest(oauth.getCurrentQuery().get("code"), "secret");
    assertEquals(200, accessTokenResponse.getStatusCode());
    offlineSessionCount = realm.clients().get(id).getOfflineSessionCount();
    assertEquals(new Long(1), offlineSessionCount.get("count"));
    List<UserSessionRepresentation> offlineUserSessions = realm.clients().get(id).getOfflineUserSessions(0, 100);
    assertEquals(1, offlineUserSessions.size());
    assertEquals("testuser", offlineUserSessions.get(0).getUsername());
    org.hamcrest.MatcherAssert.assertThat(offlineUserSessions.get(0).getLastAccess(), allOf(greaterThan(Time.currentTimeMillis() - 10000L), lessThan(Time.currentTimeMillis())));
    userSessions = realm.users().get(userId).getOfflineSessions(id);
    assertEquals("There should be one offline session", 1, userSessions.size());
    assertOfflineSession(offlineUserSessions.get(0), userSessions.get(0));
}
Also used : AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Response(javax.ws.rs.core.Response) UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 24 with AccessTokenResponse

use of org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse in project keycloak by keycloak.

the class TokenIntrospectionTest method testIntrospectRefreshTokenAfterRefreshTokenRequest.

@Test
public void testIntrospectRefreshTokenAfterRefreshTokenRequest() throws Exception {
    RealmRepresentation realm = adminClient.realm(oauth.getRealm()).toRepresentation();
    realm.setRevokeRefreshToken(true);
    realm.setRefreshTokenMaxReuse(1);
    adminClient.realm(oauth.getRealm()).update(realm);
    try {
        oauth.doLogin("test-user@localhost", "password");
        String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
        AccessTokenResponse accessTokenResponse = oauth.doAccessTokenRequest(code, "password");
        String oldRefreshToken = accessTokenResponse.getRefreshToken();
        setTimeOffset(1);
        accessTokenResponse = oauth.doRefreshTokenRequest(oldRefreshToken, "password");
        accessTokenResponse = oauth.doRefreshTokenRequest(oldRefreshToken, "password");
        String newRefreshToken = accessTokenResponse.getRefreshToken();
        String tokenResponse = oauth.introspectRefreshTokenWithClientCredential("confidential-cli", "secret1", newRefreshToken);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(tokenResponse);
        assertTrue(jsonNode.get("active").asBoolean());
        accessTokenResponse = oauth.doRefreshTokenRequest(newRefreshToken, "password");
        tokenResponse = oauth.introspectRefreshTokenWithClientCredential("confidential-cli", "secret1", oldRefreshToken);
        jsonNode = objectMapper.readTree(tokenResponse);
        assertFalse(jsonNode.get("active").asBoolean());
    } finally {
        realm.setRevokeRefreshToken(false);
        realm.setRefreshTokenMaxReuse(0);
        adminClient.realm(oauth.getRealm()).update(realm);
    }
}
Also used : RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) JsonNode(com.fasterxml.jackson.databind.JsonNode) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractOIDCScopeTest(org.keycloak.testsuite.oidc.AbstractOIDCScopeTest) OIDCScopeTest(org.keycloak.testsuite.oidc.OIDCScopeTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 25 with AccessTokenResponse

use of org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse in project keycloak by keycloak.

the class TokenIntrospectionTest method testIntrospectAccessTokenExpired.

@Test
public void testIntrospectAccessTokenExpired() throws Exception {
    oauth.doLogin("test-user@localhost", "password");
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    AccessTokenResponse accessTokenResponse = oauth.doAccessTokenRequest(code, "password");
    setTimeOffset(adminClient.realm(oauth.getRealm()).toRepresentation().getAccessTokenLifespan() + 1);
    String tokenResponse = oauth.introspectAccessTokenWithClientCredential("confidential-cli", "secret1", accessTokenResponse.getAccessToken());
    TokenMetadataRepresentation rep = JsonSerialization.readValue(tokenResponse, TokenMetadataRepresentation.class);
    assertFalse(rep.isActive());
    assertNull(rep.getUserName());
    assertNull(rep.getClientId());
    assertNull(rep.getSubject());
}
Also used : TokenMetadataRepresentation(org.keycloak.representations.oidc.TokenMetadataRepresentation) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) AbstractOIDCScopeTest(org.keycloak.testsuite.oidc.AbstractOIDCScopeTest) OIDCScopeTest(org.keycloak.testsuite.oidc.OIDCScopeTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Aggregations

AccessTokenResponse (org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse)45 Test (org.junit.Test)29 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)24 AccessToken (org.keycloak.representations.AccessToken)14 AbstractOIDCScopeTest (org.keycloak.testsuite.oidc.AbstractOIDCScopeTest)14 OIDCScopeTest (org.keycloak.testsuite.oidc.OIDCScopeTest)14 TokenMetadataRepresentation (org.keycloak.representations.oidc.TokenMetadataRepresentation)13 EventRepresentation (org.keycloak.representations.idm.EventRepresentation)12 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)10 IOException (java.io.IOException)9 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)9 RefreshTokenTest (org.keycloak.testsuite.oauth.RefreshTokenTest)9 OAuthClient (org.keycloak.testsuite.util.OAuthClient)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 RefreshToken (org.keycloak.representations.RefreshToken)4 Response (javax.ws.rs.core.Response)3 JWSInput (org.keycloak.jose.jws.JWSInput)3 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)3 OAuth2ErrorRepresentation (org.keycloak.representations.idm.OAuth2ErrorRepresentation)3