Search in sources :

Example 1 with TokenRequest

use of com.auth0.net.TokenRequest in project auth0-java by auth0.

the class TokenRequestTest method shouldCreateRequest.

@Test
public void shouldCreateRequest() throws Exception {
    TokenRequest request = new TokenRequest(client, server.getBaseUrl());
    request.addParameter("non_empty", "body");
    assertThat(request, is(notNullValue()));
    server.jsonResponse(AUTH_TOKENS, 200);
    TokenHolder response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod(), is("POST"));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TokenHolder(com.auth0.json.auth.TokenHolder) Test(org.junit.Test)

Example 2 with TokenRequest

use of com.auth0.net.TokenRequest in project auth0-java-mvc-common by auth0.

the class RequestProcessorTest method shouldReturnTokensOnProcessIfCodeRequestPassesIdTokenVerification.

@Test
public void shouldReturnTokensOnProcessIfCodeRequestPassesIdTokenVerification() throws Exception {
    doNothing().when(tokenVerifier).verify(eq("backIdToken"), eq(verifyOptions));
    Map<String, Object> params = new HashMap<>();
    params.put("code", "abc123");
    params.put("state", "1234");
    MockHttpServletRequest request = getRequest(params);
    request.setCookies(new Cookie("com.auth0.state", "1234"));
    TokenRequest codeExchangeRequest = mock(TokenRequest.class);
    TokenHolder tokenHolder = mock(TokenHolder.class);
    when(tokenHolder.getIdToken()).thenReturn("backIdToken");
    when(tokenHolder.getAccessToken()).thenReturn("backAccessToken");
    when(tokenHolder.getRefreshToken()).thenReturn("backRefreshToken");
    when(codeExchangeRequest.execute()).thenReturn(tokenHolder);
    when(client.exchangeCode("abc123", "https://me.auth0.com:80/callback")).thenReturn(codeExchangeRequest);
    RequestProcessor handler = new RequestProcessor.Builder(client, "code", verifyOptions).withIdTokenVerifier(tokenVerifier).build();
    Tokens tokens = handler.process(request, response);
    verify(tokenVerifier).verify("backIdToken", verifyOptions);
    verifyNoMoreInteractions(tokenVerifier);
    assertThat(tokens, is(notNullValue()));
    assertThat(tokens.getIdToken(), is("backIdToken"));
    assertThat(tokens.getAccessToken(), is("backAccessToken"));
    assertThat(tokens.getRefreshToken(), is("backRefreshToken"));
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TokenRequest(com.auth0.net.TokenRequest) TokenHolder(com.auth0.json.auth.TokenHolder) Test(org.junit.jupiter.api.Test)

Example 3 with TokenRequest

use of com.auth0.net.TokenRequest in project auth0-java-mvc-common by auth0.

the class RequestProcessorTest method shouldReturnTokensOnProcessIfIdTokenCodeRequestPassesIdTokenVerification.

@Test
public void shouldReturnTokensOnProcessIfIdTokenCodeRequestPassesIdTokenVerification() throws Exception {
    doNothing().when(tokenVerifier).verify(eq("frontIdToken"), eq(verifyOptions));
    Map<String, Object> params = new HashMap<>();
    params.put("code", "abc123");
    params.put("state", "1234");
    params.put("id_token", "frontIdToken");
    params.put("expires_in", "8400");
    params.put("token_type", "frontTokenType");
    MockHttpServletRequest request = getRequest(params);
    request.setCookies(new Cookie("com.auth0.state", "1234"));
    TokenRequest codeExchangeRequest = mock(TokenRequest.class);
    TokenHolder tokenHolder = mock(TokenHolder.class);
    when(tokenHolder.getIdToken()).thenReturn("backIdToken");
    when(tokenHolder.getExpiresIn()).thenReturn(4800L);
    when(tokenHolder.getTokenType()).thenReturn("backTokenType");
    when(codeExchangeRequest.execute()).thenReturn(tokenHolder);
    when(client.exchangeCode("abc123", "https://me.auth0.com:80/callback")).thenReturn(codeExchangeRequest);
    RequestProcessor handler = new RequestProcessor.Builder(client, "id_token code", verifyOptions).withIdTokenVerifier(tokenVerifier).build();
    Tokens tokens = handler.process(request, response);
    // Should not verify the ID Token twice
    verify(tokenVerifier).verify("frontIdToken", verifyOptions);
    verify(tokenVerifier, never()).verify("backIdToken", verifyOptions);
    verifyNoMoreInteractions(tokenVerifier);
    assertThat(tokens, is(notNullValue()));
    assertThat(tokens.getIdToken(), is("frontIdToken"));
    assertThat(tokens.getType(), is("frontTokenType"));
    assertThat(tokens.getExpiresIn(), is(8400L));
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TokenRequest(com.auth0.net.TokenRequest) TokenHolder(com.auth0.json.auth.TokenHolder) Test(org.junit.jupiter.api.Test)

Example 4 with TokenRequest

use of com.auth0.net.TokenRequest in project auth0-java-mvc-common by auth0.

the class RequestProcessorTest method shouldThrowOnProcessIfCodeRequestFailsToExecuteCodeExchange.

@Test
public void shouldThrowOnProcessIfCodeRequestFailsToExecuteCodeExchange() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("code", "abc123");
    params.put("state", "1234");
    MockHttpServletRequest request = getRequest(params);
    request.setCookies(new Cookie("com.auth0.state", "1234"));
    TokenRequest codeExchangeRequest = mock(TokenRequest.class);
    when(codeExchangeRequest.execute()).thenThrow(Auth0Exception.class);
    when(client.exchangeCode("abc123", "https://me.auth0.com:80/callback")).thenReturn(codeExchangeRequest);
    RequestProcessor handler = new RequestProcessor.Builder(client, "code", verifyOptions).withIdTokenVerifier(tokenVerifier).build();
    IdentityVerificationException e = assertThrows(IdentityVerificationException.class, () -> handler.process(request, response));
    assertThat(e, IdentityVerificationExceptionMatcher.hasCode("a0.api_error"));
    assertEquals("An error occurred while exchanging the authorization code.", e.getMessage());
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TokenRequest(com.auth0.net.TokenRequest) Test(org.junit.jupiter.api.Test)

Example 5 with TokenRequest

use of com.auth0.net.TokenRequest in project auth0-java-mvc-common by auth0.

the class AuthenticationControllerTest method shouldCheckSessionFallbackWhenHandleCalledWithRequest.

@Test
public void shouldCheckSessionFallbackWhenHandleCalledWithRequest() throws Exception {
    AuthenticationController controller = builderSpy.withResponseType("code").build();
    TokenRequest codeExchangeRequest = mock(TokenRequest.class);
    TokenHolder tokenHolder = mock(TokenHolder.class);
    when(codeExchangeRequest.execute()).thenReturn(tokenHolder);
    when(client.exchangeCode("abc123", "http://localhost")).thenReturn(codeExchangeRequest);
    AuthorizeUrlBuilder mockBuilder = mock(AuthorizeUrlBuilder.class);
    when(mockBuilder.withResponseType("code")).thenReturn(mockBuilder);
    when(mockBuilder.withScope("openid")).thenReturn(mockBuilder);
    when(client.authorizeUrl("https://redirect.uri/here")).thenReturn(mockBuilder);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    // build auth URL using request and response, which stores state and nonce in cookies and also session as a fallback
    String authUrl = controller.buildAuthorizeUrl(request, response, "https://redirect.uri/here").withState("state").withNonce("nonce").build();
    String state = (String) request.getSession().getAttribute("com.auth0.state");
    String nonce = (String) request.getSession().getAttribute("com.auth0.nonce");
    assertThat(state, is("state"));
    assertThat(nonce, is("nonce"));
    request.setParameter("state", "state");
    request.setParameter("nonce", "nonce");
    request.setParameter("code", "abc123");
    // handle called with request, which should use session
    controller.handle(request);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TokenRequest(com.auth0.net.TokenRequest) TokenHolder(com.auth0.json.auth.TokenHolder) AuthorizeUrlBuilder(com.auth0.client.auth.AuthorizeUrlBuilder) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

TokenHolder (com.auth0.json.auth.TokenHolder)8 TokenRequest (com.auth0.net.TokenRequest)8 Test (org.junit.jupiter.api.Test)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 HashMap (java.util.HashMap)6 Cookie (javax.servlet.http.Cookie)6 AuthorizeUrlBuilder (com.auth0.client.auth.AuthorizeUrlBuilder)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1