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());
}
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;
}
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));
}
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);
}
}
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());
}
Aggregations