Search in sources :

Example 1 with AuthorizeUrlBuilder

use of com.auth0.client.auth.AuthorizeUrlBuilder 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)

Example 2 with AuthorizeUrlBuilder

use of com.auth0.client.auth.AuthorizeUrlBuilder in project auth0-java-mvc-common by auth0.

the class AuthenticationControllerTest method shouldCheckSessionFallbackWhenHandleCalledWithRequestAndResponse.

@Test
public void shouldCheckSessionFallbackWhenHandleCalledWithRequestAndResponse() 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 deprecated method, which stores state and nonce in session
    String authUrl = controller.buildAuthorizeUrl(request, "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 and response, which should use cookies but fallback to session
    controller.handle(request, response);
}
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

AuthorizeUrlBuilder (com.auth0.client.auth.AuthorizeUrlBuilder)2 TokenHolder (com.auth0.json.auth.TokenHolder)2 TokenRequest (com.auth0.net.TokenRequest)2 Test (org.junit.jupiter.api.Test)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2