// Path: mujina-common/src/main/java/nl/surfnet/mujina/model/AuthenticationMethod.java
// @XmlRootElement
// public class AuthenticationMethod implements Serializable {
//     private static final long serialVersionUID = 1L;
// 
//     private String value;
// 
//     public String getValue() {
//         return value;
//     }
// 
//     @XmlElement
//     public void setValue(final String value) {
//         this.value = value;
//     }
// 
//     public enum Method {
//         USER, ALL
//     }
// }
// 
// Path: mujina-idp/src/main/java/nl/surfnet/mujina/model/IdpConfiguration.java
// public interface IdpConfiguration extends CommonConfiguration {
//     Map<String, String> getAttributes();
// 
//     Collection<SimpleAuthentication> getUsers();
// 
//     AuthenticationMethod.Method getAuthentication();
// 
//     void setAuthentication(AuthenticationMethod.Method method);
// }
// 
// Path: mujina-idp/src/main/java/nl/surfnet/mujina/model/SimpleAuthentication.java
// public class SimpleAuthentication implements Authentication {
// 
//     private String username;
//     private String password;
//     private Collection<GrantedAuthority> authorities;
// 
// 
//     public SimpleAuthentication(String username,
//                                 String password,
//                                 Collection<GrantedAuthority> authorities) {
//         this.username = username;
//         this.password = password;
//         this.authorities = authorities;
//     }
// 
//     @Override
//     public Collection<GrantedAuthority> getAuthorities() {
//         return authorities;
//     }
// 
//     @Override
//     public Object getCredentials() {
//         return password;
//     }
// 
//     @Override
//     public Object getDetails() {
//         return null;
//     }
// 
//     @Override
//     public Object getPrincipal() {
//         return username;
//     }
// 
//     @Override
//     public boolean isAuthenticated() {
//         return true;
//     }
// 
//     @Override
//     public void setAuthenticated(final boolean isAuthenticated) throws IllegalArgumentException {
//     }
// 
//     @Override
//     public String toString() {
//         return username + " / " + password;
//     }
// 
//     @Override
//     public String getName() {
//         return username;
//     }
// 
//     @Override
//     public boolean equals(final Object o) {
//         if (this == o) {
//             return true;
//         }
//         if (o == null || getClass() != o.getClass()) {
//             return false;
//         }
// 
//         final SimpleAuthentication that = (SimpleAuthentication) o;
// 
//         if (authorities != null ? !authorities.equals(that.authorities) : that.authorities != null) {
//             return false;
//         }
//         if (password != null ? !password.equals(that.password) : that.password != null) {
//             return false;
//         }
//         if (username != null ? !username.equals(that.username) : that.username != null) {
//             return false;
//         }
// 
//         return true;
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = username != null ? username.hashCode() : 0;
//         result = 31 * result + (password != null ? password.hashCode() : 0);
//         result = 31 * result + (authorities != null ? authorities.hashCode() : 0);
//         return result;
//     }
// }
// 
// Path: mujina-common/src/main/java/nl/surfnet/mujina/util/IDService.java
// public class IDService {
// 
//     public String generateID() {
//         return UUID.randomUUID().toString();
//     }
// 
// }
// 
// Path: mujina-common/src/main/java/nl/surfnet/mujina/util/TimeService.java
// public class TimeService {
// 
//     public DateTime getCurrentDateTime() {
//         return new DateTime();
//     }
// 
// }


import java.util.HashMap;
import java.util.Map;
import org.joda.time.DateTime;
import org.opensaml.saml2.core.Assertion;
import org.opensaml.saml2.core.AuthnStatement;
import org.opensaml.saml2.core.Issuer;
import org.opensaml.saml2.core.Subject;
import org.opensaml.saml2.core.impl.AssertionBuilder;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.io.MarshallingException;
import org.opensaml.xml.security.credential.Credential;
import org.opensaml.xml.signature.Signature;
import org.opensaml.xml.signature.SignatureConstants;
import org.opensaml.xml.signature.SignatureException;
import org.opensaml.xml.signature.Signer;
import nl.surfnet.mujina.model.AuthenticationMethod;
import nl.surfnet.mujina.model.IdpConfiguration;
import nl.surfnet.mujina.model.SimpleAuthentication;
import nl.surfnet.mujina.util.IDService;
import nl.surfnet.mujina.util.TimeService;


        AssertionBuilder assertionBuilder = (AssertionBuilder) builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME);
        Assertion assertion = assertionBuilder.buildObject();

        Subject subject = subjectGenerator.generateSubject(recepientAssertionConsumerURL, validForInSeconds, authToken.getName(), inResponseTo, remoteIP);

        Issuer issuer = issuerGenerator.generateIssuer();

        AuthnStatement authnStatement = authnStatementGenerator.generateAuthnStatement(authnInstant);

        assertion.setIssuer(issuer);
        assertion.getAuthnStatements().add(authnStatement);
        assertion.setSubject(subject);

        // extends this
        // assertion.getAttributeStatements().add(attributeStatementGenerator.generateAttributeStatement(authToken.getAuthorities()));

        final Map<String,String> attributes = new HashMap<String, String>();
        attributes.putAll(idpConfiguration.getAttributes());

        if (idpConfiguration.getAuthentication() == AuthenticationMethod.Method.ALL) {
            attributes.put("urn:mace:dir:attribute-def:uid", authToken.getName());
        }

        assertion.getAttributeStatements().add(attributeStatementGenerator.generateAttributeStatement(attributes));

        assertion.setID(idService.generateID());
        assertion.setIssueInstant(timeService.getCurrentDateTime());

// Path: mujina-common/src/main/java/nl/surfnet/mujina/model/AuthenticationMethod.java
// @XmlRootElement
// public class AuthenticationMethod implements Serializable {
//     private static final long serialVersionUID = 1L;
// 
//     private String value;
// 
//     public String getValue() {
//         return value;
//     }
// 
//     @XmlElement
//     public void setValue(final String value) {
//         this.value = value;
//     }
// 
//     public enum Method {
//         USER, ALL
//     }
// }
// 
// Path: mujina-idp/src/main/java/nl/surfnet/mujina/model/IdpConfiguration.java
// public interface IdpConfiguration extends CommonConfiguration {
//     Map<String, String> getAttributes();
// 
//     Collection<SimpleAuthentication> getUsers();
// 
//     AuthenticationMethod.Method getAuthentication();
// 
//     void setAuthentication(AuthenticationMethod.Method method);
// }
// 
// Path: mujina-idp/src/main/java/nl/surfnet/mujina/model/SimpleAuthentication.java
// public class SimpleAuthentication implements Authentication {
// 
//     private String username;
//     private String password;
//     private Collection<GrantedAuthority> authorities;
// 
// 
//     public SimpleAuthentication(String username,
//                                 String password,
//                                 Collection<GrantedAuthority> authorities) {
//         this.username = username;
//         this.password = password;
//         this.authorities = authorities;
//     }
// 
//     @Override
//     public Collection<GrantedAuthority> getAuthorities() {
//         return authorities;
//     }
// 
//     @Override
//     public Object getCredentials() {
//         return password;
//     }
// 
//     @Override
//     public Object getDetails() {
//         return null;
//     }
// 
//     @Override
//     public Object getPrincipal() {
//         return username;
//     }
// 
//     @Override
//     public boolean isAuthenticated() {
//         return true;
//     }
// 
//     @Override
//     public void setAuthenticated(final boolean isAuthenticated) throws IllegalArgumentException {
//     }
// 
//     @Override
//     public String toString() {
//         return username + " / " + password;
//     }
// 
//     @Override
//     public String getName() {
//         return username;
//     }
// 
//     @Override
//     public boolean equals(final Object o) {
//         if (this == o) {
//             return true;
//         }
//         if (o == null || getClass() != o.getClass()) {
//             return false;
//         }
// 
//         final SimpleAuthentication that = (SimpleAuthentication) o;
// 
//         if (authorities != null ? !authorities.equals(that.authorities) : that.authorities != null) {
//             return false;
//         }
//         if (password != null ? !password.equals(that.password) : that.password != null) {
//             return false;
//         }
//         if (username != null ? !username.equals(that.username) : that.username != null) {
//             return false;
//         }
// 
//         return true;
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = username != null ? username.hashCode() : 0;
//         result = 31 * result + (password != null ? password.hashCode() : 0);
//         result = 31 * result + (authorities != null ? authorities.hashCode() : 0);
//         return result;
//     }
// }
// 
// Path: mujina-common/src/main/java/nl/surfnet/mujina/util/IDService.java
// public class IDService {
// 
//     public String generateID() {
//         return UUID.randomUUID().toString();
//     }
// 
// }
// 
// Path: mujina-common/src/main/java/nl/surfnet/mujina/util/TimeService.java
// public class TimeService {
// 
//     public DateTime getCurrentDateTime() {
//         return new DateTime();
//     }
// 
// }


// Path: mujina-idp/src/main/java/nl/surfnet/mujina/saml/xml/AssertionGenerator.java
import java.util.HashMap;
import java.util.Map;
import org.joda.time.DateTime;
import org.opensaml.saml2.core.Assertion;
import org.opensaml.saml2.core.AuthnStatement;
import org.opensaml.saml2.core.Issuer;
import org.opensaml.saml2.core.Subject;
import org.opensaml.saml2.core.impl.AssertionBuilder;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.io.MarshallingException;
import org.opensaml.xml.security.credential.Credential;
import org.opensaml.xml.signature.Signature;
import org.opensaml.xml.signature.SignatureConstants;
import org.opensaml.xml.signature.SignatureException;
import org.opensaml.xml.signature.Signer;
import nl.surfnet.mujina.model.AuthenticationMethod;
import nl.surfnet.mujina.model.IdpConfiguration;
import nl.surfnet.mujina.model.SimpleAuthentication;
import nl.surfnet.mujina.util.IDService;
import nl.surfnet.mujina.util.TimeService;



        AssertionBuilder assertionBuilder = (AssertionBuilder) builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME);
        Assertion assertion = assertionBuilder.buildObject();

        Subject subject = subjectGenerator.generateSubject(recepientAssertionConsumerURL, validForInSeconds, authToken.getName(), inResponseTo, remoteIP);

        Issuer issuer = issuerGenerator.generateIssuer();

        AuthnStatement authnStatement = authnStatementGenerator.generateAuthnStatement(authnInstant);

        assertion.setIssuer(issuer);
        assertion.getAuthnStatements().add(authnStatement);
        assertion.setSubject(subject);

        // extends this
        // assertion.getAttributeStatements().add(attributeStatementGenerator.generateAttributeStatement(authToken.getAuthorities()));

        final Map<String,String> attributes = new HashMap<String, String>();
        attributes.putAll(idpConfiguration.getAttributes());

        if (idpConfiguration.getAuthentication() == AuthenticationMethod.Method.ALL) {
            attributes.put("urn:mace:dir:attribute-def:uid", authToken.getName());
        }

        assertion.getAttributeStatements().add(attributeStatementGenerator.generateAttributeStatement(attributes));

        assertion.setID(idService.generateID());
        assertion.setIssueInstant(timeService.getCurrentDateTime());

        signAssertion(assertion);