use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project asgardeo-java-oidc-sdk by asgardeo.
the class OIDCRequestResolverTest method testIsAuthorizationCodeResponse.
@Test
public void testIsAuthorizationCodeResponse() throws IOException, ParseException, URISyntaxException {
MockedStatic<AuthorizationResponse> mockedAuthorizationResponse = mockStatic(AuthorizationResponse.class);
MockedStatic<ServletUtils> mockedServletUtils = mockStatic(ServletUtils.class);
HTTPRequest httpRequest = mock(HTTPRequest.class);
AuthorizationResponse authorizationResponse = mock(AuthorizationResponse.class);
AuthorizationSuccessResponse authorizationSuccessResponse = mock(AuthorizationSuccessResponse.class);
AuthorizationCode authzCode = new AuthorizationCode("auth-code");
when(ServletUtils.createHTTPRequest(request)).thenReturn(httpRequest);
when(AuthorizationResponse.parse(httpRequest)).thenReturn(authorizationResponse);
when(authorizationResponse.indicatesSuccess()).thenReturn(true);
when(authorizationResponse.toSuccessResponse()).thenReturn(authorizationSuccessResponse);
when(authorizationSuccessResponse.getAuthorizationCode()).thenReturn(authzCode);
OIDCRequestResolver resolver = new OIDCRequestResolver(request, oidcAgentConfig);
assertTrue(resolver.isAuthorizationCodeResponse());
mockedAuthorizationResponse.close();
mockedServletUtils.close();
}
use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project asgardeo-java-oidc-sdk by asgardeo.
the class DefaultOIDCManager method handleAuthentication.
private boolean handleAuthentication(final HttpServletRequest request, SessionContext authenticationInfo, Nonce nonce) throws SSOAgentServerException {
AuthorizationResponse authorizationResponse;
AuthorizationCode authorizationCode;
AuthorizationSuccessResponse successResponse;
TokenRequest tokenRequest;
TokenResponse tokenResponse;
try {
authorizationResponse = AuthorizationResponse.parse(ServletUtils.createHTTPRequest(request));
if (!authorizationResponse.indicatesSuccess()) {
handleErrorAuthorizationResponse(authorizationResponse);
return false;
}
successResponse = authorizationResponse.toSuccessResponse();
authorizationCode = successResponse.getAuthorizationCode();
tokenRequest = getTokenRequest(authorizationCode);
tokenResponse = getTokenResponse(tokenRequest);
if (!tokenResponse.indicatesSuccess()) {
handleErrorTokenResponse(tokenRequest, tokenResponse);
return false;
}
handleSuccessTokenResponse(tokenResponse, authenticationInfo, nonce);
return true;
} catch (com.nimbusds.oauth2.sdk.ParseException | SSOAgentServerException | IOException e) {
throw new SSOAgentServerException(e.getMessage(), e);
}
}
use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project sandbox by backpaper0.
the class OAuth2Filter method handleAuthorization.
private Optional<AuthorizationSuccessResponse> handleAuthorization(HttpServletRequest req) throws Exception {
final Map<String, List<String>> params = req.getParameterMap().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, a -> List.of(a.getValue())));
final URI redirectURI = URI.create(req.getRequestURI());
final AuthorizationResponse response = AuthorizationResponse.parse(redirectURI, params);
if (response.indicatesSuccess() == false) {
return Optional.empty();
}
return Optional.of((AuthorizationSuccessResponse) response);
}
use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project sandbox by backpaper0.
the class OAuth2Filter method doCallback.
private void doCallback(HttpServletRequest req, HttpServletResponse resp) throws Exception {
final var authorizationResponse = handleAuthorization(req);
if (authorizationResponse.isEmpty()) {
resp.sendError(401);
return;
}
final AuthorizationSuccessResponse response = authorizationResponse.get();
final var requestedURI = extractRequestedURI(req, response.getState());
if (requestedURI.isEmpty()) {
resp.sendError(401);
return;
}
final var accessTokenResponse = getAccessToken(response.getAuthorizationCode());
if (accessTokenResponse.isEmpty()) {
resp.sendError(401);
return;
}
final var username = getUsername(accessTokenResponse.get().getTokens());
if (username.isEmpty()) {
resp.sendError(401);
return;
}
req.getSession().setAttribute("user", username.get());
resp.sendRedirect(requestedURI.get());
}
use of com.nimbusds.oauth2.sdk.AuthorizationSuccessResponse in project Kustvakt by KorAP.
the class OpenIdAuthorizationService method requestAuthorizationCode.
public URI requestAuthorizationCode(MultivaluedMap<String, String> map, String username, boolean isAuthentication, ZonedDateTime authenticationTime) throws KustvaktException, ParseException {
AuthorizationCode code = new AuthorizationCode();
URI redirectUri = null;
if (isAuthentication) {
AuthenticationRequest authRequest = null;
authRequest = AuthenticationRequest.parse((Map<String, List<String>>) map);
redirectUri = handleAuthenticationRequest(authRequest, code, username, authenticationTime);
return new AuthenticationSuccessResponse(redirectUri, code, null, null, authRequest.getState(), null, null).toURI();
} else {
AuthorizationRequest authzRequest = AuthorizationRequest.parse((Map<String, List<String>>) map);
redirectUri = handleAuthorizationRequest(authzRequest, code, username, authenticationTime, null);
return new AuthorizationSuccessResponse(redirectUri, code, null, authzRequest.getState(), null).toURI();
}
}
Aggregations