use of org.opensaml.saml.saml2.core.SessionIndex in project ddf by codice.
the class LogoutRequestServiceTest method insertLogoutRequest.
private void insertLogoutRequest() throws XMLStreamException, LogoutSecurityException {
LogoutRequest logoutRequest = mock(LogoutRequest.class);
LogoutWrapper logoutRequestWrapper = mock(LogoutWrapper.class);
doReturn(logoutRequest).when(logoutRequestWrapper).getMessage();
SessionIndex sessionIndex = mock(SessionIndex.class);
doReturn(SESSION_INDEX).when(sessionIndex).getSessionIndex();
doReturn((Collections.singletonList(sessionIndex))).when(logoutRequest).getSessionIndexes();
doReturn(DateTime.now()).when(logoutRequest).getIssueInstant();
doReturn(SAMLVersion.VERSION_20).when(logoutRequest).getVersion();
doReturn(ID).when(logoutRequest).getID();
doReturn(logoutRequestWrapper).when(logoutMessage).extractSamlLogoutRequest(eq(UNENCODED_SAML_REQUEST));
}
use of org.opensaml.saml.saml2.core.SessionIndex in project ddf by codice.
the class LogoutRequestServiceTest method testPostLogoutRequestResponseNotParsable.
@Test
public void testPostLogoutRequestResponseNotParsable() throws Exception {
LogoutRequest logoutRequest = mock(LogoutRequest.class);
when(logoutRequest.getIssueInstant()).thenReturn(DateTime.now());
SessionIndex sessionIndex = mock(SessionIndex.class);
when(sessionIndex.getSessionIndex()).thenReturn(SESSION_INDEX);
when(logoutRequest.getSessionIndexes()).thenReturn(Collections.singletonList(sessionIndex));
String encodedSamlResponse = "encodedSamlRequest";
when(logoutMessage.extractSamlLogoutResponse(any(String.class))).thenReturn(null);
logoutRequestService.setLogoutMessage(logoutMessage);
insertLogoutRequest();
Response response = logoutRequestService.postLogoutRequest(null, encodedSamlResponse, relayState);
assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
String msg = LogoutRequestService.UNABLE_TO_PARSE_LOGOUT_RESPONSE.replaceAll(" ", "+");
assertTrue("Expected message containing " + msg, response.getLocation().getQuery().contains(msg));
}
use of org.opensaml.saml.saml2.core.SessionIndex in project pac4j by pac4j.
the class SAML2DefaultResponseValidator method buildSAML2Credentials.
protected final SAML2Credentials buildSAML2Credentials(final SAML2MessageContext context) {
final NameID nameId = context.getSAMLSubjectNameIdentifierContext().getSAML2SubjectNameID();
final Assertion subjectAssertion = context.getSubjectAssertion();
final String sessionIndex = getSessionIndex(subjectAssertion);
final String issuerEntityId = subjectAssertion.getIssuer().getValue();
List<AuthnStatement> authnStatements = subjectAssertion.getAuthnStatements();
List<String> authnContexts = new ArrayList<String>();
for (AuthnStatement authnStatement : authnStatements) {
authnContexts.add(authnStatement.getAuthnContext().getAuthnContextClassRef().getAuthnContextClassRef());
}
final List<Attribute> attributes = new ArrayList<Attribute>();
for (final AttributeStatement attributeStatement : subjectAssertion.getAttributeStatements()) {
for (final Attribute attribute : attributeStatement.getAttributes()) {
attributes.add(attribute);
}
if (!attributeStatement.getEncryptedAttributes().isEmpty()) {
if (decrypter == null) {
logger.warn("Encrypted attributes returned, but no keystore was provided.");
} else {
for (final EncryptedAttribute encryptedAttribute : attributeStatement.getEncryptedAttributes()) {
try {
attributes.add(decrypter.decrypt(encryptedAttribute));
} catch (final DecryptionException e) {
logger.warn("Decryption of attribute failed, continue with the next one", e);
}
}
}
}
}
return new SAML2Credentials(nameId, issuerEntityId, attributes, subjectAssertion.getConditions(), sessionIndex, authnContexts);
}
use of org.opensaml.saml.saml2.core.SessionIndex in project spring-security by spring-projects.
the class OpenSamlLogoutRequestResolver method resolve.
Saml2LogoutRequest resolve(HttpServletRequest request, Authentication authentication, BiConsumer<RelyingPartyRegistration, LogoutRequest> logoutRequestConsumer) {
String registrationId = getRegistrationId(authentication);
RelyingPartyRegistration registration = this.relyingPartyRegistrationResolver.resolve(request, registrationId);
if (registration == null) {
return null;
}
if (registration.getAssertingPartyDetails().getSingleLogoutServiceLocation() == null) {
return null;
}
LogoutRequest logoutRequest = this.logoutRequestBuilder.buildObject();
logoutRequest.setDestination(registration.getAssertingPartyDetails().getSingleLogoutServiceLocation());
Issuer issuer = this.issuerBuilder.buildObject();
issuer.setValue(registration.getEntityId());
logoutRequest.setIssuer(issuer);
NameID nameId = this.nameIdBuilder.buildObject();
nameId.setValue(authentication.getName());
logoutRequest.setNameID(nameId);
if (authentication.getPrincipal() instanceof Saml2AuthenticatedPrincipal) {
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal) authentication.getPrincipal();
for (String index : principal.getSessionIndexes()) {
SessionIndex sessionIndex = this.sessionIndexBuilder.buildObject();
sessionIndex.setSessionIndex(index);
logoutRequest.getSessionIndexes().add(sessionIndex);
}
}
logoutRequestConsumer.accept(registration, logoutRequest);
if (logoutRequest.getID() == null) {
logoutRequest.setID("LR" + UUID.randomUUID());
}
String relayState = UUID.randomUUID().toString();
Saml2LogoutRequest.Builder result = Saml2LogoutRequest.withRelyingPartyRegistration(registration).id(logoutRequest.getID());
if (registration.getAssertingPartyDetails().getSingleLogoutServiceBinding() == Saml2MessageBinding.POST) {
String xml = serialize(OpenSamlSigningUtils.sign(logoutRequest, registration));
String samlRequest = Saml2Utils.samlEncode(xml.getBytes(StandardCharsets.UTF_8));
return result.samlRequest(samlRequest).relayState(relayState).build();
} else {
String xml = serialize(logoutRequest);
String deflatedAndEncoded = Saml2Utils.samlEncode(Saml2Utils.samlDeflate(xml));
result.samlRequest(deflatedAndEncoded);
QueryParametersPartial partial = OpenSamlSigningUtils.sign(registration).param(Saml2ParameterNames.SAML_REQUEST, deflatedAndEncoded).param(Saml2ParameterNames.RELAY_STATE, relayState);
return result.parameters((params) -> params.putAll(partial.parameters())).build();
}
}
use of org.opensaml.saml.saml2.core.SessionIndex in project ddf by codice.
the class LogoutRequestServiceTest method testPostLogoutRequest.
@Test
public void testPostLogoutRequest() throws Exception {
String encodedSamlRequest = "encodedSamlRequest";
String issuerStr = "issuer";
LogoutRequest logoutRequest = mock(LogoutRequest.class);
when(logoutRequest.getIssueInstant()).thenReturn(DateTime.now());
SessionIndex sessionIndex = mock(SessionIndex.class);
when(sessionIndex.getSessionIndex()).thenReturn(SESSION_INDEX);
when(logoutRequest.getSessionIndexes()).thenReturn(Collections.singletonList(sessionIndex));
LogoutWrapper<LogoutRequest> requestLogoutWrapper = new LogoutWrapperImpl<>(logoutRequest);
when(logoutMessage.extractSamlLogoutRequest(any(String.class))).thenReturn(requestLogoutWrapper);
Issuer issuer = mock(Issuer.class);
OpenSAMLUtil.initSamlEngine();
LogoutResponse logoutResponse = new LogoutResponseBuilder().buildObject();
when(logoutRequest.getIssuer()).thenReturn(issuer);
when(logoutRequest.getIssueInstant()).thenReturn(new DateTime());
when(logoutRequest.getVersion()).thenReturn(SAMLVersion.VERSION_20);
when(logoutRequest.getID()).thenReturn("id");
when(issuer.getValue()).thenReturn(issuerStr);
LogoutWrapper<LogoutResponse> responseLogoutWrapper = new LogoutWrapperImpl<>(logoutResponse);
when(logoutMessage.buildLogoutResponse(eq(issuerStr), eq(StatusCode.SUCCESS), anyString())).thenReturn(responseLogoutWrapper);
logoutRequestService.setLogoutMessage(logoutMessage);
when(idpMetadata.getSingleLogoutBinding()).thenReturn(SamlProtocol.POST_BINDING);
when(idpMetadata.getSingleLogoutLocation()).thenReturn(postLogoutUrl);
Response response = logoutRequestService.postLogoutRequest(encodedSamlRequest, null, relayState);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertTrue("Expected logout url of " + postLogoutUrl, response.getEntity().toString().contains(postLogoutUrl));
}
Aggregations