Search in sources :

Example 1 with AuthorizationEndpointResponse

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

the class CookieTest method testCookieValue.

private void testCookieValue(String cookieName) throws Exception {
    final String accountClientId = realmsResouce().realm("test").clients().findByClientId("account").get(0).getId();
    final String clientSecret = realmsResouce().realm("test").clients().get(accountClientId).getSecret().getValue();
    AuthorizationEndpointResponse codeResponse = oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).doLogin("test-user@localhost", "password");
    OAuthClient.AccessTokenResponse accTokenResp = oauth.doAccessTokenRequest(codeResponse.getCode(), clientSecret);
    String accessToken = accTokenResp.getAccessToken();
    accountPage.navigateTo();
    accountPage.assertCurrent();
    try (CloseableHttpClient hc = OAuthClient.newCloseableHttpClient()) {
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie(cookieName, accessToken);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        HttpGet get = new HttpGet(oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).getLoginFormUrl());
        try (CloseableHttpResponse resp = hc.execute(get, localContext)) {
            final String pageContent = EntityUtils.toString(resp.getEntity());
            // Ensure that we did not get to the account page ...
            assertThat(pageContent, not(containsString("First name")));
            assertThat(pageContent, not(containsString("Last name")));
            // ... but were redirected to login page
            assertThat(pageContent, containsString("Sign In"));
            assertThat(pageContent, containsString("Forgot Password?"));
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthorizationEndpointResponse(org.keycloak.testsuite.util.OAuthClient.AuthorizationEndpointResponse) OAuthClient(org.keycloak.testsuite.util.OAuthClient) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 2 with AuthorizationEndpointResponse

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

the class CookieTest method testCookieValueLoggedOut.

@Test
public void testCookieValueLoggedOut() throws Exception {
    final String accountClientId = realmsResouce().realm("test").clients().findByClientId("account").get(0).getId();
    final String clientSecret = realmsResouce().realm("test").clients().get(accountClientId).getSecret().getValue();
    AuthorizationEndpointResponse codeResponse = oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).doLogin("test-user@localhost", "password");
    OAuthClient.AccessTokenResponse accTokenResp = oauth.doAccessTokenRequest(codeResponse.getCode(), clientSecret);
    String accessToken = accTokenResp.getAccessToken();
    accountPage.navigateTo();
    accountPage.assertCurrent();
    accountPage.logOut();
    try (CloseableHttpClient hc = OAuthClient.newCloseableHttpClient()) {
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie(KEYCLOAK_IDENTITY_COOKIE, accessToken);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        HttpGet get = new HttpGet(oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).getLoginFormUrl());
        try (CloseableHttpResponse resp = hc.execute(get, localContext)) {
            final String pageContent = EntityUtils.toString(resp.getEntity());
            // Ensure that we did not get to the account page ...
            assertThat(pageContent, not(containsString("First name")));
            assertThat(pageContent, not(containsString("Last name")));
            // ... but were redirected to login page
            assertThat(pageContent, containsString("Sign In"));
            assertThat(pageContent, containsString("Forgot Password?"));
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthorizationEndpointResponse(org.keycloak.testsuite.util.OAuthClient.AuthorizationEndpointResponse) OAuthClient(org.keycloak.testsuite.util.OAuthClient) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 3 with AuthorizationEndpointResponse

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

the class ConsentsTest method clientConsentRequiredAfterLogin.

@Test
public void clientConsentRequiredAfterLogin() {
    oauth.realm(TEST_REALM_NAME).clientId("test-app");
    AuthorizationEndpointResponse response = oauth.doLogin("test-user@localhost", "password");
    AccessTokenResponse accessTokenResponse = oauth.doAccessTokenRequest(response.getCode(), "password");
    Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
    Assert.assertNotNull(oauth.getCurrentQuery().get(OAuth2Constants.CODE));
    EventRepresentation loginEvent = events.expectLogin().detail(Details.USERNAME, "test-user@localhost").assertEvent();
    String sessionId = loginEvent.getSessionId();
    ClientRepresentation clientRepresentation = adminClient.realm(TEST_REALM_NAME).clients().findByClientId("test-app").get(0);
    try {
        clientRepresentation.setConsentRequired(true);
        adminClient.realm(TEST_REALM_NAME).clients().get(clientRepresentation.getId()).update(clientRepresentation);
        events.clear();
        // try to refresh the token
        // this fails as client no longer has requested consent from user
        AccessTokenResponse refreshTokenResponse = oauth.doRefreshTokenRequest(accessTokenResponse.getRefreshToken(), "password");
        Assert.assertEquals(OAuthErrorException.INVALID_SCOPE, refreshTokenResponse.getError());
        Assert.assertEquals("Client no longer has requested consent from user", refreshTokenResponse.getErrorDescription());
        events.expectRefresh(accessTokenResponse.getRefreshToken(), sessionId).clearDetails().error(Errors.INVALID_TOKEN).assertEvent();
    } finally {
        clientRepresentation.setConsentRequired(false);
        adminClient.realm(TEST_REALM_NAME).clients().get(clientRepresentation.getId()).update(clientRepresentation);
    }
}
Also used : AuthorizationEndpointResponse(org.keycloak.testsuite.util.OAuthClient.AuthorizationEndpointResponse) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

AuthorizationEndpointResponse (org.keycloak.testsuite.util.OAuthClient.AuthorizationEndpointResponse)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)2 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)2 HttpContext (org.apache.http.protocol.HttpContext)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)2 OAuthClient (org.keycloak.testsuite.util.OAuthClient)2 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1 EventRepresentation (org.keycloak.representations.idm.EventRepresentation)1 AccessTokenResponse (org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse)1