use of org.gluu.oxauth.client.EndSessionRequest in project oxTrust by GluuFederation.
the class OpenIdClient method getLogoutRedirectionUrl.
@Override
public String getLogoutRedirectionUrl(WebContext context) {
init();
final String state = RandomStringUtils.randomAlphanumeric(10);
final String postLogoutRedirectUri = this.appConfiguration.getOpenIdPostLogoutRedirectUri();
String idToken = (String) context.getSessionAttribute(getName() + SESSION_ID_TOKEN_PARAMETER);
// Allow to send logout request if session is expired
if (idToken == null) {
idToken = "";
}
final EndSessionRequest endSessionRequest = new EndSessionRequest(idToken, postLogoutRedirectUri, state);
final String redirectionUrl = this.openIdConfiguration.getEndSessionEndpoint() + "?" + endSessionRequest.getQueryString();
logger.debug("oxAuth redirection Url: '{}'", redirectionUrl);
return redirectionUrl;
}
use of org.gluu.oxauth.client.EndSessionRequest in project oxTrust by GluuFederation.
the class AuthenticationSessionService method sessionDestroyed.
@PreDestroy
public void sessionDestroyed() {
OauthData oauthData = identity.getOauthData();
if ((oauthData == null) || StringHelper.isEmpty(oauthData.getSessionState())) {
return;
}
String userUid = oauthData.getUserUid();
log.debug("Calling oxAuth logout method at the end of HTTP session. User: '{}'", userUid);
try {
String endSessionState = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest = new EndSessionRequest(oauthData.getIdToken(), appConfiguration.getLogoutRedirectUrl(), endSessionState);
endSessionRequest.setSid(oauthData.getSessionState());
EndSessionClient endSessionClient = new EndSessionClient(openIdService.getOpenIdConfiguration().getEndSessionEndpoint());
endSessionClient.setRequest(endSessionRequest);
EndSessionResponse endSessionResponse = endSessionClient.exec();
if ((endSessionResponse == null) || (endSessionResponse.getStatus() != 302)) {
log.error("Invalid response code at oxAuth logout. User: '{}'", userUid);
}
} catch (Exception ex) {
log.error("Exception happened at oxAuth logout. User: '{}'", userUid, ex);
}
}
use of org.gluu.oxauth.client.EndSessionRequest in project oxAuth by GluuFederation.
the class EndSessionRestWebServiceHttpTest method requestEndSession.
@Parameters({ "userId", "userSecret", "redirectUri", "redirectUris", "postLogoutRedirectUri", "logoutUri", "sectorIdentifierUri" })
@Test
public void requestEndSession(final String userId, final String userSecret, final String redirectUri, final String redirectUris, final String postLogoutRedirectUri, final String logoutUri, final String sectorIdentifierUri) throws Exception {
showTitle("requestEndSession by id_token");
// 1. OpenID Connect Dynamic Registration
RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app", StringUtils.spaceSeparatedToList(redirectUris));
registerRequest.setResponseTypes(Arrays.asList(ResponseType.TOKEN, ResponseType.ID_TOKEN));
registerRequest.setPostLogoutRedirectUris(Arrays.asList(postLogoutRedirectUri));
registerRequest.setFrontChannelLogoutUris(Lists.newArrayList(logoutUri));
registerRequest.setSectorIdentifierUri(sectorIdentifierUri);
RegisterClient registerClient = new RegisterClient(registrationEndpoint);
registerClient.setRequest(registerRequest);
RegisterResponse response = registerClient.exec();
showClient(registerClient);
assertEquals(response.getStatus(), 200, "Unexpected response code: " + response.getEntity());
assertNotNull(response.getClientId());
assertNotNull(response.getClientSecret());
assertNotNull(response.getRegistrationAccessToken());
assertNotNull(response.getClientSecretExpiresAt());
String clientId = response.getClientId();
// 2. Request authorization
List<ResponseType> responseTypes = new ArrayList<ResponseType>();
responseTypes.add(ResponseType.TOKEN);
responseTypes.add(ResponseType.ID_TOKEN);
List<String> scopes = new ArrayList<String>();
scopes.add("openid");
scopes.add("profile");
scopes.add("address");
scopes.add("email");
String nonce = UUID.randomUUID().toString();
String state = UUID.randomUUID().toString();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId, scopes, redirectUri, nonce);
authorizationRequest.setState(state);
AuthorizationResponse authorizationResponse = authenticateResourceOwnerAndGrantAccess(authorizationEndpoint, authorizationRequest, userId, userSecret);
assertNotNull(authorizationResponse.getLocation(), "The location is null");
assertNotNull(authorizationResponse.getAccessToken(), "The access token is null");
assertEquals(authorizationResponse.getState(), state);
assertNotNull(authorizationResponse.getTokenType(), "The token type is null");
assertNotNull(authorizationResponse.getExpiresIn(), "The expires in value is null");
assertNotNull(authorizationResponse.getScope(), "The scope must be null");
assertNotNull(authorizationResponse.getSessionId(), "The session_id is null");
assertNotNull(authorizationResponse.getSid(), "The sid is null");
String idToken = authorizationResponse.getIdToken();
// 3. End session
String endSessionId1 = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest1 = new EndSessionRequest(idToken, postLogoutRedirectUri, endSessionId1);
endSessionRequest1.setSid(authorizationResponse.getSid());
EndSessionClient endSessionClient = new EndSessionClient(endSessionEndpoint);
endSessionClient.setRequest(endSessionRequest1);
EndSessionResponse endSessionResponse1 = endSessionClient.exec();
showClient(endSessionClient);
assertEquals(endSessionResponse1.getStatus(), 200);
assertNotNull(endSessionResponse1.getHtmlPage(), "The HTML page is null");
// silly validation of html content returned by server but at least it verifies that logout_uri and post_logout_uri are present
assertTrue(endSessionResponse1.getHtmlPage().contains("<html>"), "The HTML page is null");
assertTrue(endSessionResponse1.getHtmlPage().contains(logoutUri), "logout_uri is not present on html page");
assertTrue(endSessionResponse1.getHtmlPage().contains(postLogoutRedirectUri), "postLogoutRedirectUri is not present on html page");
// assertEquals(endSessionResponse.getState(), endSessionId); // commented out, for http-based logout we get html page
// 4. End session with an already ended session
String endSessionId2 = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest2 = new EndSessionRequest(idToken, postLogoutRedirectUri, endSessionId2);
endSessionRequest2.setSid(authorizationResponse.getSid());
EndSessionClient endSessionClient2 = new EndSessionClient(endSessionEndpoint);
endSessionClient2.setRequest(endSessionRequest2);
EndSessionResponse endSessionResponse2 = endSessionClient2.exec();
showClient(endSessionClient2);
assertStatusOrRedirect(endSessionResponse2.getStatus(), Status.BAD_REQUEST.getStatusCode());
assertEquals(endSessionResponse2.getErrorType(), EndSessionErrorResponseType.INVALID_GRANT_AND_SESSION);
}
use of org.gluu.oxauth.client.EndSessionRequest in project oxAuth by GluuFederation.
the class EndSessionRestWebServiceHttpTest method requestEndSessionWithSessionId.
@Parameters({ "userId", "userSecret", "redirectUri", "redirectUris", "postLogoutRedirectUri", "logoutUri", "sectorIdentifierUri" })
@Test
public void requestEndSessionWithSessionId(final String userId, final String userSecret, final String redirectUri, final String redirectUris, final String postLogoutRedirectUri, final String logoutUri, final String sectorIdentifierUri) throws Exception {
showTitle("requestEndSession by session_id");
// 1. OpenID Connect Dynamic Registration
RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app", StringUtils.spaceSeparatedToList(redirectUris));
registerRequest.setResponseTypes(Arrays.asList(ResponseType.TOKEN, ResponseType.ID_TOKEN));
registerRequest.setPostLogoutRedirectUris(Arrays.asList(postLogoutRedirectUri));
registerRequest.setFrontChannelLogoutUris(Lists.newArrayList(logoutUri));
registerRequest.setSectorIdentifierUri(sectorIdentifierUri);
RegisterClient registerClient = new RegisterClient(registrationEndpoint);
registerClient.setRequest(registerRequest);
RegisterResponse response = registerClient.exec();
showClient(registerClient);
assertEquals(response.getStatus(), 200, "Unexpected response code: " + response.getEntity());
assertNotNull(response.getClientId());
assertNotNull(response.getClientSecret());
assertNotNull(response.getRegistrationAccessToken());
assertNotNull(response.getClientSecretExpiresAt());
String clientId = response.getClientId();
// 2. Request authorization
List<ResponseType> responseTypes = new ArrayList<ResponseType>();
responseTypes.add(ResponseType.TOKEN);
responseTypes.add(ResponseType.ID_TOKEN);
List<String> scopes = new ArrayList<String>();
scopes.add("openid");
scopes.add("profile");
scopes.add("address");
scopes.add("email");
String nonce = UUID.randomUUID().toString();
String state = UUID.randomUUID().toString();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId, scopes, redirectUri, nonce);
authorizationRequest.setState(state);
AuthorizationResponse authorizationResponse = authenticateResourceOwnerAndGrantAccess(authorizationEndpoint, authorizationRequest, userId, userSecret);
assertNotNull(authorizationResponse.getLocation(), "The location is null");
assertNotNull(authorizationResponse.getAccessToken(), "The access token is null");
assertEquals(authorizationResponse.getState(), state);
assertNotNull(authorizationResponse.getTokenType(), "The token type is null");
assertNotNull(authorizationResponse.getExpiresIn(), "The expires in value is null");
assertNotNull(authorizationResponse.getScope(), "The scope must be null");
assertNotNull(authorizationResponse.getSessionId(), "The session_id is null");
// 3. End session
String endSessionId1 = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest1 = new EndSessionRequest(null, postLogoutRedirectUri, endSessionId1);
endSessionRequest1.setSid(authorizationResponse.getSid());
EndSessionClient endSessionClient = new EndSessionClient(endSessionEndpoint);
endSessionClient.setRequest(endSessionRequest1);
EndSessionResponse endSessionResponse1 = endSessionClient.exec();
showClient(endSessionClient);
assertEquals(endSessionResponse1.getStatus(), 200);
assertNotNull(endSessionResponse1.getHtmlPage(), "The HTML page is null");
// silly validation of html content returned by server but at least it verifies that logout_uri and post_logout_uri are present
assertTrue(endSessionResponse1.getHtmlPage().contains("<html>"), "The HTML page is null");
assertTrue(endSessionResponse1.getHtmlPage().contains(logoutUri), "logout_uri is not present on html page");
assertTrue(endSessionResponse1.getHtmlPage().contains(postLogoutRedirectUri), "postLogoutRedirectUri is not present on html page");
// assertEquals(endSessionResponse.getState(), endSessionId); // commented out, for http-based logout we get html page
// 4. End session with an already ended session
String endSessionId2 = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest2 = new EndSessionRequest(null, postLogoutRedirectUri, endSessionId2);
endSessionRequest2.setSid(authorizationResponse.getSid());
EndSessionClient endSessionClient2 = new EndSessionClient(endSessionEndpoint);
endSessionClient2.setRequest(endSessionRequest2);
EndSessionResponse endSessionResponse2 = endSessionClient2.exec();
showClient(endSessionClient2);
assertStatusOrRedirect(endSessionResponse2.getStatus(), Status.BAD_REQUEST.getStatusCode());
assertEquals(endSessionResponse2.getErrorType(), EndSessionErrorResponseType.INVALID_GRANT_AND_SESSION);
}
use of org.gluu.oxauth.client.EndSessionRequest in project oxAuth by GluuFederation.
the class GrantTypesRestrictionHttpTest method grantTypesRestriction.
@Test(dataProvider = "grantTypesRestrictionDataProvider")
public void grantTypesRestriction(final List<ResponseType> responseTypes, final List<ResponseType> expectedResponseTypes, final List<GrantType> grantTypes, final List<GrantType> expectedGrantTypes, final String userId, final String userSecret, final String redirectUris, final String redirectUri, final String sectorIdentifierUri, final String postLogoutRedirectUri, final String logoutUri) throws Exception {
showTitle("grantTypesRestriction");
List<String> scopes = Arrays.asList("openid", "profile", "address", "email", "user_name");
// 1. Register client
RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app", StringUtils.spaceSeparatedToList(redirectUris));
registerRequest.setResponseTypes(responseTypes);
registerRequest.setGrantTypes(grantTypes);
registerRequest.setScope(scopes);
registerRequest.setSubjectType(SubjectType.PAIRWISE);
registerRequest.setSectorIdentifierUri(sectorIdentifierUri);
registerRequest.setPostLogoutRedirectUris(Arrays.asList(postLogoutRedirectUri));
registerRequest.setFrontChannelLogoutUris(Lists.newArrayList(logoutUri));
RegisterClient registerClient = new RegisterClient(registrationEndpoint);
registerClient.setRequest(registerRequest);
RegisterResponse registerResponse = registerClient.exec();
showClient(registerClient);
assertEquals(registerResponse.getStatus(), 200);
assertNotNull(registerResponse.getClientId());
assertNotNull(registerResponse.getClientSecret());
assertNotNull(registerResponse.getRegistrationAccessToken());
assertNotNull(registerResponse.getClientIdIssuedAt());
assertNotNull(registerResponse.getClientSecretExpiresAt());
assertNotNull(registerResponse.getResponseTypes());
assertTrue(registerResponse.getResponseTypes().containsAll(expectedResponseTypes));
assertNotNull(registerResponse.getGrantTypes());
assertTrue(registerResponse.getGrantTypes().containsAll(expectedGrantTypes));
String clientId = registerResponse.getClientId();
String clientSecret = registerResponse.getClientSecret();
String registrationAccessToken = registerResponse.getRegistrationAccessToken();
String registrationClientUri = registerResponse.getRegistrationClientUri();
// 2. Client read
RegisterRequest readRequest = new RegisterRequest(registrationAccessToken);
RegisterClient readClient = new RegisterClient(registrationClientUri);
readClient.setRequest(readRequest);
RegisterResponse readResponse = registerClient.exec();
showClient(registerClient);
assertEquals(readResponse.getStatus(), 200);
assertNotNull(readResponse.getClientId());
assertNotNull(readResponse.getClientSecret());
assertNotNull(readResponse.getRegistrationAccessToken());
assertNotNull(readResponse.getRegistrationClientUri());
assertNotNull(readResponse.getClientSecretExpiresAt());
assertNotNull(readResponse.getClaims().get(APPLICATION_TYPE.toString()));
assertNotNull(readResponse.getClaims().get(SCOPE.toString()));
assertNotNull(readResponse.getResponseTypes());
assertTrue(readResponse.getResponseTypes().containsAll(expectedResponseTypes));
assertNotNull(readResponse.getGrantTypes());
assertTrue(readResponse.getGrantTypes().containsAll(expectedGrantTypes));
// 3. Request authorization
String nonce = UUID.randomUUID().toString();
String state = UUID.randomUUID().toString();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(expectedResponseTypes, clientId, scopes, redirectUri, nonce);
authorizationRequest.setState(state);
if (expectedResponseTypes.size() == 0) {
AuthorizeClient authorizeClient = new AuthorizeClient(authorizationEndpoint);
authorizeClient.setRequest(authorizationRequest);
AuthorizationResponse authorizationResponse = authorizeClient.exec();
showClient(authorizeClient);
assertEquals(authorizationResponse.getStatus(), 302);
assertNotNull(authorizationResponse.getLocation());
assertNotNull(authorizationResponse.getErrorType());
assertNotNull(authorizationResponse.getErrorDescription());
assertNotNull(authorizationResponse.getState());
return;
}
AuthorizationResponse authorizationResponse = authenticateResourceOwnerAndGrantAccess(authorizationEndpoint, authorizationRequest, userId, userSecret);
String scope = authorizationResponse.getScope();
String authorizationCode = null;
String accessToken = null;
String idToken = null;
String refreshToken = null;
assertNotNull(authorizationResponse.getLocation());
assertNotNull(authorizationResponse.getState());
assertNotNull(authorizationResponse.getScope());
if (expectedResponseTypes.contains(ResponseType.CODE)) {
assertNotNull(authorizationResponse.getCode());
authorizationCode = authorizationResponse.getCode();
}
if (expectedResponseTypes.contains(ResponseType.TOKEN)) {
assertNotNull(authorizationResponse.getAccessToken());
accessToken = authorizationResponse.getAccessToken();
}
if (expectedResponseTypes.contains(ResponseType.ID_TOKEN)) {
assertNotNull(authorizationResponse.getIdToken());
idToken = authorizationResponse.getIdToken();
// 4. Validate id_token
Jwt jwt = Jwt.parse(idToken);
assertNotNull(jwt.getHeader().getClaimAsString(JwtHeaderName.TYPE));
assertNotNull(jwt.getHeader().getClaimAsString(JwtHeaderName.ALGORITHM));
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.ISSUER));
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.AUDIENCE));
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.EXPIRATION_TIME));
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.ISSUED_AT));
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.SUBJECT_IDENTIFIER));
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.AUTHENTICATION_TIME));
RSAPublicKey publicKey = JwkClient.getRSAPublicKey(jwksUri, jwt.getHeader().getClaimAsString(JwtHeaderName.KEY_ID));
RSASigner rsaSigner = new RSASigner(SignatureAlgorithm.RS256, publicKey);
assertTrue(rsaSigner.validate(jwt));
if (expectedResponseTypes.contains(ResponseType.CODE)) {
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.CODE_HASH));
assertTrue(rsaSigner.validateAuthorizationCode(authorizationCode, jwt));
}
if (expectedResponseTypes.contains(ResponseType.TOKEN)) {
assertNotNull(jwt.getClaims().getClaimAsString(JwtClaimName.ACCESS_TOKEN_HASH));
assertTrue(rsaSigner.validateAccessToken(accessToken, jwt));
}
}
if (expectedResponseTypes.contains(ResponseType.CODE)) {
// 5. Request access token using the authorization code.
TokenRequest tokenRequest = new TokenRequest(GrantType.AUTHORIZATION_CODE);
tokenRequest.setCode(authorizationCode);
tokenRequest.setRedirectUri(redirectUri);
tokenRequest.setAuthUsername(clientId);
tokenRequest.setAuthPassword(clientSecret);
tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_BASIC);
TokenClient tokenClient = new TokenClient(tokenEndpoint);
tokenClient.setRequest(tokenRequest);
TokenResponse tokenResponse = tokenClient.exec();
showClient(tokenClient);
assertEquals(tokenResponse.getStatus(), 200);
assertNotNull(tokenResponse.getEntity());
assertNotNull(tokenResponse.getAccessToken());
assertNotNull(tokenResponse.getExpiresIn());
assertNotNull(tokenResponse.getTokenType());
if (expectedGrantTypes.contains(GrantType.REFRESH_TOKEN)) {
assertNotNull(tokenResponse.getRefreshToken());
refreshToken = tokenResponse.getRefreshToken();
// 6. Request new access token using the refresh token.
TokenClient refreshTokenClient = new TokenClient(tokenEndpoint);
TokenResponse refreshTokenResponse = refreshTokenClient.execRefreshToken(scope, refreshToken, clientId, clientSecret);
showClient(refreshTokenClient);
assertEquals(refreshTokenResponse.getStatus(), 200);
assertNotNull(refreshTokenResponse.getEntity());
assertNotNull(refreshTokenResponse.getAccessToken());
assertNotNull(refreshTokenResponse.getTokenType());
assertNotNull(refreshTokenResponse.getRefreshToken());
assertNotNull(refreshTokenResponse.getScope());
accessToken = refreshTokenResponse.getAccessToken();
} else {
assertNull(tokenResponse.getRefreshToken());
}
}
if (accessToken != null) {
// 7. Request user info
UserInfoClient userInfoClient = new UserInfoClient(userInfoEndpoint);
UserInfoResponse userInfoResponse = userInfoClient.execUserInfo(accessToken);
showClient(userInfoClient);
assertEquals(userInfoResponse.getStatus(), 200);
assertNotNull(userInfoResponse.getClaim(JwtClaimName.SUBJECT_IDENTIFIER));
assertNotNull(userInfoResponse.getClaim(JwtClaimName.NAME));
assertNotNull(userInfoResponse.getClaim(JwtClaimName.FAMILY_NAME));
assertNotNull(userInfoResponse.getClaim(JwtClaimName.EMAIL));
assertNotNull(userInfoResponse.getClaim(JwtClaimName.ADDRESS));
if (idToken != null) {
// 8. End session
String endSessionId = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest = new EndSessionRequest(idToken, postLogoutRedirectUri, endSessionId);
endSessionRequest.setSid(authorizationResponse.getSid());
EndSessionClient endSessionClient = new EndSessionClient(endSessionEndpoint);
endSessionClient.setRequest(endSessionRequest);
EndSessionResponse endSessionResponse = endSessionClient.exec();
showClient(endSessionClient);
assertEquals(endSessionResponse.getStatus(), 200);
assertNotNull(endSessionResponse.getHtmlPage());
// silly validation of html content returned by server but at least it verifies that logout_uri and post_logout_uri are present
assertTrue(endSessionResponse.getHtmlPage().contains("<html>"));
assertTrue(endSessionResponse.getHtmlPage().contains(logoutUri));
assertTrue(endSessionResponse.getHtmlPage().contains(postLogoutRedirectUri));
// assertEquals(endSessionResponse.getState(), endSessionId); // commented out, for http-based logout we get html page
}
}
}
Aggregations