forked from huahaiy/saml20-clj
/
shared.clj
219 lines (185 loc) · 6.96 KB
/
shared.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
(ns saml20-clj.shared
(:import (java.io FileInputStream InputStreamReader BufferedReader))
(:require [clj-time.core :as ctime]
[clj-time.format :as ctimeformat]
[clojure.data.codec.base64 :as b64]
[ring.util.codec :refer [form-encode url-encode base64-encode]]
[hiccup.util :refer [escape-html]]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.zip]
[clojure.xml])
(:import [java.io ByteArrayInputStream]))
(def instant-format (ctimeformat/formatters :date-time-no-ms))
(def charset-format (java.nio.charset.Charset/forName "UTF-8"))
(def status-code-success "urn:oasis:names:tc:SAML:2.0:status:Success")
(defn saml-successful?
[id-str]
(if (= id-str status-code-success)
true false))
(defn read-to-end
[stream]
(let [sb (StringBuilder.)]
(with-open [reader (-> stream
InputStreamReader.
BufferedReader.)]
(loop [c (.read reader)]
(if (neg? c)
(str sb)
(do
(.append sb (char c))
(recur (.read reader))))))))
(defn jcert->public-key
"Extracts a public key object from a java cert object."
[java-cert-obj]
(.getPublicKey java-cert-obj))
(defn parse-xml-str
[xml-str]
(clojure.zip/xml-zip (clojure.xml/parse (java.io.ByteArrayInputStream. (.getBytes xml-str)))))
(defn clean-x509-filter
"Turns a base64 string into a byte array to be decoded, which includes sanitization."
[x509-string]
(-> x509-string
(str/replace #"[\n ]" "")
((partial map byte))
byte-array
bytes))
(defn certificate-x509
"Takes in a raw X.509 certificate string, parses it, and creates a Java certificate."
[x509-string]
(let [x509-byte-array (clean-x509-filter x509-string)
fty (java.security.cert.CertificateFactory/getInstance "X.509")
bais (new java.io.ByteArrayInputStream (bytes (b64/decode x509-byte-array)))]
(.generateCertificate fty bais)))
(defn jcert->public-key
"Extracts a public key object from a java cert object."
[java-cert-obj]
(.getPublicKey java-cert-obj))
(defn str->inputstream
"Unravels a string into an input stream so we can work with Java constructs."
[unravel]
(ByteArrayInputStream. (.getBytes unravel charset-format)))
(defn make-issue-instant
"Converts a date-time to a SAML 2.0 time string."
[ii-date]
(ctimeformat/unparse instant-format ii-date))
(defn str->bytes
[some-string]
(.getBytes some-string charset-format))
(defn bytes->str
[some-bytes]
(String. some-bytes charset-format))
(defn byte-deflate
[str-bytes]
(let [out (java.io.ByteArrayOutputStream.)
deflater (java.util.zip.DeflaterOutputStream.
out
(java.util.zip.Deflater. -1 true) 1024)]
(.write deflater str-bytes)
(.close deflater)
(.toByteArray out)))
(defn byte-inflate
[comp-bytes]
(let [input (java.io.ByteArrayInputStream. comp-bytes)
inflater (java.util.zip.InflaterInputStream.
input (java.util.zip.Inflater. true) 1024)
result (read-to-end inflater)]
(.close inflater)
result))
(defn str->base64
[base64able-string]
(-> base64able-string str->bytes b64/encode bytes->str))
(defn base64->str
[stringable-base64]
(-> stringable-base64 str->bytes b64/decode bytes->str))
(defn str->deflate->base64
[deflatable-str]
(let [byte-str (str->bytes deflatable-str)]
(bytes->str (b64/encode (byte-deflate byte-str)))))
(defn base64->inflate->str
[string]
(let [byte-str (str->bytes string)]
(bytes->str (b64/decode byte-str))))
(defn random-bytes
([size]
(let [ba (byte-array size)
r (new java.util.Random)]
(.nextBytes r ba)
ba) )
([]
(random-bytes 20)))
(def bytes->hex
(let [digits (into {} (map-indexed vector "0123456789ABCDEF") )]
(fn [^bytes bytes-str]
(let [ret (char-array (* 2 (alength bytes-str)))]
(loop [idx 0]
(if (< idx (alength bytes-str))
(let [pos (* 2 idx)
b (aget bytes-str idx)
d1 (unsigned-bit-shift-right (bit-and 0xF0 b) 4)
d2 (bit-and 0x0F b)]
(aset-char ret pos (digits d1))
(aset-char ret (unchecked-inc pos) (digits d2))
(recur (unchecked-inc idx)))
(String. ret)))))))
(defn new-secret-key-spec []
(new javax.crypto.spec.SecretKeySpec (random-bytes) "HmacSHA1"))
(defn hmac-str [^javax.crypto.spec.SecretKeySpec key-spec ^String string]
(let [mac (doto (javax.crypto.Mac/getInstance "HmacSHA1")
(.init key-spec))
hs (.doFinal mac (.getBytes string "UTF-8"))]
(bytes->hex hs)))
(defn uri-query-str
[clean-hash]
(form-encode clean-hash))
(defn form-encode-b64
[req]
(into {}
(map
(fn [[k v]] [k (str->base64 v)])
req)))
(defn saml-form-encode [form]
(-> form
form-encode-b64
form-encode))
(defn time-since
[time-span]
(ctime/minus (ctime/now) time-span))
(defn make-timeout-filter-fn
"Creates a function for clojure.core/filter to keep all dates after
a given date."
[timespan]
(fn [i]
(ctime/after? (second i) (time-since timespan))))
(defn load-key-store [keystore-filename keystore-password]
(when (and (not (nil? keystore-filename))
(.exists (io/as-file keystore-filename)))
(with-open [is (clojure.java.io/input-stream keystore-filename)]
(doto (java.security.KeyStore/getInstance "JKS")
(.load is (.toCharArray keystore-password))))))
(defn get-certificate-b64 [keystore-filename keystore-password cert-alias]
(when-let [ks (load-key-store keystore-filename keystore-password)]
(-> ks (.getCertificate cert-alias) (.getEncoded) b64/encode (String. "UTF-8"))))
;; https://www.purdue.edu/apps/account/docs/Shibboleth/Shibboleth_information.jsp
;; Or
;; https://wiki.library.ucsf.edu/display/IAM/EDS+Attributes
(def saml2-attr->name
(let [names {"urn:oid:0.9.2342.19200300.100.1.1" "uid"
"urn:oid:0.9.2342.19200300.100.1.3" "mail"
"urn:oid:2.16.840.1.113730.3.1.241" "displayName"
"urn:oid:2.5.4.3" "cn"
"urn:oid:2.5.4.4" "sn"
"urn:oid:2.5.4.12" "title"
"urn:oid:2.5.4.20" "phone"
"urn:oid:2.5.4.42" "givenName"
"urn:oid:2.5.6.8" "organizationalRole"
"urn:oid:2.16.840.1.113730.3.1.3" "employeeNumber"
"urn:oid:2.16.840.1.113730.3.1.4" "employeeType"
"urn:oid:1.3.6.1.4.1.5923.1.1.1.1" "eduPersonAffiliation"
"urn:oid:1.3.6.1.4.1.5923.1.1.1.2" "eduPersonNickname"
"urn:oid:1.3.6.1.4.1.5923.1.1.1.6" "eduPersonPrincipalName"
"urn:oid:1.3.6.1.4.1.5923.1.1.1.9" "eduPersonScopedAffiliation"
"urn:oid:1.3.6.1.4.1.5923.1.1.1.10" "eduPersonTargetedID"
"urn:oid:1.3.6.1.4.1.5923.1.6.1.1" "eduCourseOffering"}]
(fn [attr-oid]
(get names attr-oid attr-oid) )))