Search in sources :

Example 1 with AuthnStatement

use of org.opensaml.saml2.core.AuthnStatement in project cas by apereo.

the class GoogleAccountsServiceResponseBuilder method constructSamlResponse.

/**
     * Construct SAML response.
     * <a href="http://bit.ly/1uI8Ggu">See this reference for more info.</a>
     *
     * @param service the service
     * @return the SAML response
     */
protected String constructSamlResponse(final GoogleAccountsService service) {
    final ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneOffset.UTC);
    final ZonedDateTime notBeforeIssueInstant = ZonedDateTime.parse("2003-04-17T00:46:02Z");
    final RegisteredService registeredService = servicesManager.findServiceBy(service);
    if (registeredService == null || !registeredService.getAccessStrategy().isServiceAccessAllowed()) {
        throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE);
    }
    final String userId = registeredService.getUsernameAttributeProvider().resolveUsername(service.getPrincipal(), service);
    final org.opensaml.saml.saml2.core.Response response = this.samlObjectBuilder.newResponse(this.samlObjectBuilder.generateSecureRandomId(), currentDateTime, service.getId(), service);
    response.setStatus(this.samlObjectBuilder.newStatus(StatusCode.SUCCESS, null));
    final String sessionIndex = '_' + String.valueOf(Math.abs(new SecureRandom().nextLong()));
    final AuthnStatement authnStatement = this.samlObjectBuilder.newAuthnStatement(AuthnContext.PASSWORD_AUTHN_CTX, currentDateTime, sessionIndex);
    final Assertion assertion = this.samlObjectBuilder.newAssertion(authnStatement, casServerPrefix, notBeforeIssueInstant, this.samlObjectBuilder.generateSecureRandomId());
    final Conditions conditions = this.samlObjectBuilder.newConditions(notBeforeIssueInstant, currentDateTime.plusSeconds(this.skewAllowance), service.getId());
    assertion.setConditions(conditions);
    final Subject subject = this.samlObjectBuilder.newSubject(NameID.EMAIL, userId, service.getId(), currentDateTime.plusSeconds(this.skewAllowance), service.getRequestId());
    assertion.setSubject(subject);
    response.getAssertions().add(assertion);
    final StringWriter writer = new StringWriter();
    this.samlObjectBuilder.marshalSamlXmlObject(response, writer);
    final String result = writer.toString();
    LOGGER.debug("Generated Google SAML response: [{}]", result);
    return result;
}
Also used : RegisteredService(org.apereo.cas.services.RegisteredService) Assertion(org.opensaml.saml.saml2.core.Assertion) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) SecureRandom(java.security.SecureRandom) Conditions(org.opensaml.saml.saml2.core.Conditions) Subject(org.opensaml.saml.saml2.core.Subject) StringWriter(java.io.StringWriter) ZonedDateTime(java.time.ZonedDateTime) AuthnStatement(org.opensaml.saml.saml2.core.AuthnStatement)

Example 2 with AuthnStatement

use of org.opensaml.saml2.core.AuthnStatement in project ddf by codice.

the class SecurityAssertionImpl method toString.

/*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    result.append("Principal: ");
    result.append(getPrincipal());
    result.append(", Attributes: ");
    for (AttributeStatement attributeStatement : getAttributeStatements()) {
        for (Attribute attr : attributeStatement.getAttributes()) {
            result.append("[ ");
            result.append(attr.getName());
            result.append(" : ");
            for (int i = 0; i < attr.getAttributeValues().size(); i++) {
                result.append(((XSString) attr.getAttributeValues().get(i)).getValue());
            }
            result.append("] ");
        }
    }
    // add this back in when we support parsing this information
    result.append(", AuthnStatements: ");
    for (AuthnStatement authStatement : getAuthnStatements()) {
        result.append("[ ");
        result.append(authStatement.getAuthnInstant());
        result.append(" : ");
        result.append(authStatement.getAuthnContext().getAuthnContextClassRef().getAuthnContextClassRef());
        result.append("] ");
    }
    //        }
    return result.toString();
}
Also used : Attribute(org.opensaml.saml.saml2.core.Attribute) EncryptedAttribute(org.opensaml.saml.saml2.core.EncryptedAttribute) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) AuthnStatement(org.opensaml.saml.saml2.core.AuthnStatement)

Example 3 with AuthnStatement

use of org.opensaml.saml2.core.AuthnStatement in project cas by apereo.

the class SamlProfileSamlAuthNStatementBuilder method buildAuthnStatement.

/**
 * Creates an authentication statement for the current request.
 *
 * @param assertion    the assertion
 * @param authnRequest the authn request
 * @param adaptor      the adaptor
 * @param service      the service
 * @param binding      the binding
 * @return constructed authentication statement
 * @throws SamlException the saml exception
 */
private AuthnStatement buildAuthnStatement(final Object casAssertion, final RequestAbstractType authnRequest, final SamlRegisteredServiceServiceProviderMetadataFacade adaptor, final SamlRegisteredService service, final String binding) throws SamlException {
    final Assertion assertion = Assertion.class.cast(casAssertion);
    final String authenticationMethod = this.authnContextClassRefBuilder.build(assertion, authnRequest, adaptor, service);
    final String id = '_' + String.valueOf(Math.abs(RandomUtils.getNativeInstance().nextLong()));
    final AuthnStatement statement = newAuthnStatement(authenticationMethod, DateTimeUtils.zonedDateTimeOf(assertion.getAuthenticationDate()), id);
    if (assertion.getValidUntilDate() != null) {
        final ZonedDateTime dt = DateTimeUtils.zonedDateTimeOf(assertion.getValidUntilDate());
        statement.setSessionNotOnOrAfter(DateTimeUtils.dateTimeOf(dt.plusSeconds(casProperties.getAuthn().getSamlIdp().getResponse().getSkewAllowance())));
    }
    statement.setSubjectLocality(buildSubjectLocality(assertion, authnRequest, adaptor, binding));
    return statement;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Assertion(org.jasig.cas.client.validation.Assertion) AuthnStatement(org.opensaml.saml.saml2.core.AuthnStatement)

Example 4 with AuthnStatement

use of org.opensaml.saml2.core.AuthnStatement in project webcert by sklintyg.

the class BaseFakeAuthenticationProvider method attachAuthenticationContext.

protected void attachAuthenticationContext(Assertion assertion, String authContextRef) {
    AuthnStatement authnStatement = new AuthnStatementBuilder().buildObject();
    AuthnContext authnContext = new AuthnContextBuilder().buildObject();
    AuthnContextClassRef authnContextClassRef = new AuthnContextClassRefBuilder().buildObject();
    authnContextClassRef.setAuthnContextClassRef(authContextRef);
    authnContext.setAuthnContextClassRef(authnContextClassRef);
    authnStatement.setAuthnContext(authnContext);
    assertion.getAuthnStatements().add(authnStatement);
}
Also used : AuthnContextBuilder(org.opensaml.saml2.core.impl.AuthnContextBuilder) AuthnStatement(org.opensaml.saml2.core.AuthnStatement) AuthnContextClassRef(org.opensaml.saml2.core.AuthnContextClassRef) AuthnStatementBuilder(org.opensaml.saml2.core.impl.AuthnStatementBuilder) AuthnContextClassRefBuilder(org.opensaml.saml2.core.impl.AuthnContextClassRefBuilder) AuthnContext(org.opensaml.saml2.core.AuthnContext)

Example 5 with AuthnStatement

use of org.opensaml.saml2.core.AuthnStatement in project spring-security by spring-projects.

the class TestOpenSamlObjects method assertion.

static Assertion assertion(String username, String issuerEntityId, String recipientEntityId, String recipientUri) {
    Assertion assertion = build(Assertion.DEFAULT_ELEMENT_NAME);
    assertion.setID("A" + UUID.randomUUID().toString());
    assertion.setVersion(SAMLVersion.VERSION_20);
    assertion.setIssuer(issuer(issuerEntityId));
    assertion.setSubject(subject(username));
    assertion.setConditions(conditions());
    SubjectConfirmation subjectConfirmation = subjectConfirmation();
    subjectConfirmation.setMethod(SubjectConfirmation.METHOD_BEARER);
    SubjectConfirmationData confirmationData = subjectConfirmationData(recipientEntityId);
    confirmationData.setRecipient(recipientUri);
    subjectConfirmation.setSubjectConfirmationData(confirmationData);
    assertion.getSubject().getSubjectConfirmations().add(subjectConfirmation);
    AuthnStatement statement = build(AuthnStatement.DEFAULT_ELEMENT_NAME);
    statement.setSessionIndex("session-index");
    assertion.getAuthnStatements().add(statement);
    return assertion;
}
Also used : SubjectConfirmation(org.opensaml.saml.saml2.core.SubjectConfirmation) EncryptedAssertion(org.opensaml.saml.saml2.core.EncryptedAssertion) Assertion(org.opensaml.saml.saml2.core.Assertion) AuthnStatement(org.opensaml.saml.saml2.core.AuthnStatement) SubjectConfirmationData(org.opensaml.saml.saml2.core.SubjectConfirmationData)

Aggregations

AuthnStatement (org.opensaml.saml.saml2.core.AuthnStatement)17 Assertion (org.opensaml.saml.saml2.core.Assertion)11 Test (org.junit.jupiter.api.Test)5 AttributeStatement (org.opensaml.saml.saml2.core.AttributeStatement)5 AuthnContext (org.opensaml.saml.saml2.core.AuthnContext)5 DateTime (org.joda.time.DateTime)4 Attribute (org.opensaml.saml.saml2.core.Attribute)4 ZonedDateTime (java.time.ZonedDateTime)3 AuthnContextClassRef (org.opensaml.saml.saml2.core.AuthnContextClassRef)3 Conditions (org.opensaml.saml.saml2.core.Conditions)3 NameID (org.opensaml.saml.saml2.core.NameID)3 Subject (org.opensaml.saml.saml2.core.Subject)3 SubjectConfirmation (org.opensaml.saml.saml2.core.SubjectConfirmation)3 SecureRandom (java.security.SecureRandom)2 ArrayList (java.util.ArrayList)2 EncryptedAssertion (org.opensaml.saml.saml2.core.EncryptedAssertion)2 EncryptedAttribute (org.opensaml.saml.saml2.core.EncryptedAttribute)2 Issuer (org.opensaml.saml.saml2.core.Issuer)2 AssertionBuilder (org.opensaml.saml.saml2.core.impl.AssertionBuilder)2 AttributeBuilder (org.opensaml.saml.saml2.core.impl.AttributeBuilder)2