use of io.jans.as.client.EndSessionResponse in project jans by JanssenProject.
the class EndSessionRestWebServiceHttpTest method requestEndSessionFail1.
@Test
public void requestEndSessionFail1() throws Exception {
showTitle("requestEndSessionFail1");
EndSessionClient endSessionClient = new EndSessionClient(endSessionEndpoint);
EndSessionResponse response = endSessionClient.execEndSession(null, null, null);
showClient(endSessionClient);
assertEquals(response.getStatus(), 400, "Unexpected response code. Entity: " + response.getEntity());
assertNotNull(response.getEntity(), "The entity is null");
assertNotNull(response.getErrorType(), "The error type is null");
assertNotNull(response.getErrorDescription(), "The error description is null");
}
use of io.jans.as.client.EndSessionResponse in project jans by JanssenProject.
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, "jans test app", StringUtils.spaceSeparatedToList(redirectUris));
registerRequest.setResponseTypes(Arrays.asList(ResponseType.TOKEN, ResponseType.ID_TOKEN));
registerRequest.setPostLogoutRedirectUris(Arrays.asList(postLogoutRedirectUri));
registerRequest.setFrontChannelLogoutUri(logoutUri);
registerRequest.setSectorIdentifierUri(sectorIdentifierUri);
RegisterClient registerClient = new RegisterClient(registrationEndpoint);
registerClient.setRequest(registerRequest);
RegisterResponse response = registerClient.exec();
showClient(registerClient);
assertRegisterResponseOk(response, 201, true);
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");
String sid = Jwt.parseOrThrow(authorizationResponse.getIdToken()).getClaims().getClaimAsString("sid");
// 3. End session
String endSessionId1 = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest1 = new EndSessionRequest(null, postLogoutRedirectUri, endSessionId1);
endSessionRequest1.setSid(sid);
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(sid);
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 io.jans.as.client.EndSessionResponse in project jans by JanssenProject.
the class EndSessionRestWebServiceHttpTest method requestEndSessionFail2.
@Parameters({ "postLogoutRedirectUri" })
@Test
public void requestEndSessionFail2(final String postLogoutRedirectUri) throws Exception {
showTitle("requestEndSessionFail2");
String state = UUID.randomUUID().toString();
EndSessionClient endSessionClient = new EndSessionClient(endSessionEndpoint);
EndSessionResponse response = endSessionClient.execEndSession("INVALID_ACCESS_TOKEN", postLogoutRedirectUri, state);
showClient(endSessionClient);
assertStatusOrRedirect(response.getStatus(), Status.BAD_REQUEST.getStatusCode());
assertNotNull(response.getEntity(), "The entity is null");
assertNotNull(response.getErrorType(), "The error type is null");
assertNotNull(response.getErrorDescription(), "The error description is null");
}
use of io.jans.as.client.EndSessionResponse in project jans by JanssenProject.
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, "jans test app", StringUtils.spaceSeparatedToList(redirectUris));
registerRequest.setResponseTypes(Arrays.asList(ResponseType.TOKEN, ResponseType.ID_TOKEN));
registerRequest.setPostLogoutRedirectUris(Arrays.asList(postLogoutRedirectUri));
registerRequest.setFrontChannelLogoutUri(logoutUri);
registerRequest.setSectorIdentifierUri(sectorIdentifierUri);
RegisterClient registerClient = new RegisterClient(registrationEndpoint);
registerClient.setRequest(registerRequest);
RegisterResponse response = registerClient.exec();
showClient(registerClient);
assertRegisterResponseOk(response, 201, true);
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");
String idToken = authorizationResponse.getIdToken();
String sid = Jwt.parse(idToken).getClaims().getClaimAsString("sid");
assertNotNull(sid, "The sid is null");
// 3. End session
String state1 = UUID.randomUUID().toString();
EndSessionRequest endSessionRequest1 = new EndSessionRequest(idToken, postLogoutRedirectUri, state1);
endSessionRequest1.setSid(sid);
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(sid);
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 io.jans.as.client.EndSessionResponse in project jans by JanssenProject.
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, "jans 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.setFrontChannelLogoutUri(logoutUri);
RegisterClient registerClient = new RegisterClient(registrationEndpoint);
registerClient.setRequest(registerRequest);
RegisterResponse registerResponse = registerClient.exec();
showClient(registerClient);
assertRegisterResponseOk(registerResponse, 201, true);
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);
readRequest.setHttpMethod(HttpMethod.GET);
RegisterClient readClient = new RegisterClient(registrationClientUri);
readClient.setRequest(readRequest);
RegisterResponse readResponse = readClient.exec();
showClient(readClient);
assertRegisterResponseOk(readResponse, 200, true);
assertRegisterResponseClaimsNotNull(readResponse, APPLICATION_TYPE, SCOPE);
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);
assertJwtStandarClaimsNotNull(jwt, false);
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);
assertTokenResponseOk(tokenResponse, false, false);
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);
assertTokenResponseOk(refreshTokenResponse, true, false);
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();
String sid = Jwt.parseOrThrow(idToken).getClaims().getClaimAsString("sid");
EndSessionRequest endSessionRequest = new EndSessionRequest(idToken, postLogoutRedirectUri, endSessionId);
endSessionRequest.setSid(sid);
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