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