|
| 1 | +package com.onelogin.saml; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.nio.charset.Charset; |
| 5 | +import java.text.SimpleDateFormat; |
| 6 | +import java.util.Date; |
| 7 | +import java.util.UUID; |
| 8 | + |
| 9 | +import javax.xml.stream.XMLOutputFactory; |
| 10 | +import javax.xml.stream.XMLStreamException; |
| 11 | +import javax.xml.stream.XMLStreamWriter; |
| 12 | + |
| 13 | +import org.apache.commons.codec.binary.Base64; |
| 14 | + |
| 15 | +import com.onelogin.AccountSettings; |
| 16 | +import com.onelogin.AppSettings; |
| 17 | + |
| 18 | +public class AuthRequest { |
| 19 | + |
| 20 | + private String id; |
| 21 | + private String issueInstant; |
| 22 | + private AppSettings appSettings; |
| 23 | + public static final int base64 = 1; |
| 24 | + |
| 25 | + public AuthRequest(AppSettings appSettings, AccountSettings accountSettings){ |
| 26 | + this.appSettings = appSettings; |
| 27 | + id="_"+UUID.randomUUID().toString(); |
| 28 | + SimpleDateFormat simpleDf = new SimpleDateFormat("yyyy-MM-dd'T'H:mm:ss"); |
| 29 | + issueInstant = simpleDf.format(new Date()); |
| 30 | + } |
| 31 | + |
| 32 | + public String getRequest(int format) throws XMLStreamException { |
| 33 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 34 | + XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
| 35 | + XMLStreamWriter writer = factory.createXMLStreamWriter(baos); |
| 36 | + |
| 37 | + writer.writeStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol"); |
| 38 | + writer.writeNamespace("samlp","urn:oasis:names:tc:SAML:2.0:protocol"); |
| 39 | + |
| 40 | + writer.writeAttribute("ID", id); |
| 41 | + writer.writeAttribute("Version", "2.0"); |
| 42 | + writer.writeAttribute("IssueInstant", this.issueInstant); |
| 43 | + writer.writeAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"); |
| 44 | + writer.writeAttribute("AssertionConsumerServiceURL", this.appSettings.getAssertionConsumerServiceUrl()); |
| 45 | + |
| 46 | + writer.writeStartElement("saml","Issuer","urn:oasis:names:tc:SAML:2.0:assertion"); |
| 47 | + writer.writeNamespace("saml","urn:oasis:names:tc:SAML:2.0:assertion"); |
| 48 | + writer.writeCharacters(this.appSettings.getIssuer()); |
| 49 | + writer.writeEndElement(); |
| 50 | + |
| 51 | + writer.writeStartElement("samlp", "NameIDPolicy", "urn:oasis:names:tc:SAML:2.0:protocol"); |
| 52 | + |
| 53 | + writer.writeAttribute("Format", "urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified"); |
| 54 | + writer.writeAttribute("AllowCreate", "true"); |
| 55 | + writer.writeEndElement(); |
| 56 | + |
| 57 | + writer.writeStartElement("samlp","RequestedAuthnContext","urn:oasis:names:tc:SAML:2.0:protocol"); |
| 58 | + |
| 59 | + writer.writeAttribute("Comparison", "exact"); |
| 60 | + writer.writeEndElement(); |
| 61 | + |
| 62 | + writer.writeStartElement("saml","AuthnContextClassRef","urn:oasis:names:tc:SAML:2.0:assertion"); |
| 63 | + writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion"); |
| 64 | + writer.writeCharacters("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"); |
| 65 | + writer.writeEndElement(); |
| 66 | + |
| 67 | + writer.writeEndElement(); |
| 68 | + writer.flush(); |
| 69 | + |
| 70 | + if (format == base64) { |
| 71 | + byte [] encoded = Base64.encodeBase64Chunked(baos.toByteArray()); |
| 72 | + String result = new String(encoded,Charset.forName("UTF-8")); |
| 73 | + |
| 74 | + return result; |
| 75 | + } |
| 76 | + |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + public static String getRidOfCRLF(String what) { |
| 81 | + String lf = "%0D"; |
| 82 | + String cr = "%0A"; |
| 83 | + String now = lf; |
| 84 | + |
| 85 | + int index = what.indexOf(now); |
| 86 | + StringBuffer r = new StringBuffer(); |
| 87 | + |
| 88 | + while (index!=-1) { |
| 89 | + r.append(what.substring(0,index)); |
| 90 | + what = what.substring(index+3,what.length()); |
| 91 | + |
| 92 | + if (now.equals(lf)) { |
| 93 | + now = cr; |
| 94 | + } else { |
| 95 | + now = lf; |
| 96 | + } |
| 97 | + |
| 98 | + index = what.indexOf(now); |
| 99 | + } |
| 100 | + return r.toString(); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments