Skip to content

Commit 30b43e1

Browse files
committed
Explicitly add imports for java classes
1 parent 86ae2eb commit 30b43e1

File tree

7 files changed

+155
-119
lines changed

7 files changed

+155
-119
lines changed

src/saml20_clj/coerce.clj

Lines changed: 86 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,28 @@
66
[page :as h.page]]
77
[saml20-clj
88
[encode-decode :as encode-decode]
9-
[xml :as saml.xml]]))
9+
[xml :as saml.xml]])
10+
(:import [clojure.lang IPersistentMap IPersistentVector]
11+
[java.io ByteArrayInputStream StringWriter]
12+
[java.security KeyFactory KeyStore PrivateKey PublicKey Security]
13+
[java.security.cert CertificateFactory X509Certificate]
14+
java.security.interfaces.RSAPrivateCrtKey
15+
[java.security.spec PKCS8EncodedKeySpec RSAPublicKeySpec]
16+
javax.crypto.spec.SecretKeySpec
17+
[javax.xml.transform OutputKeys TransformerFactory]
18+
javax.xml.transform.dom.DOMSource
19+
javax.xml.transform.stream.StreamResult
20+
org.bouncycastle.jce.provider.BouncyCastleProvider
21+
org.opensaml.core.config.InitializationService
22+
org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport
23+
org.opensaml.core.xml.XMLObject
24+
org.opensaml.saml.common.SignableSAMLObject
25+
org.opensaml.saml.saml2.core.Response
26+
[org.opensaml.security.credential BasicCredential Credential]
27+
org.opensaml.security.x509.BasicX509Credential
28+
org.opensaml.security.x509.impl.KeyStoreX509CredentialAdapter
29+
org.opensaml.xmlsec.config.impl.JavaCryptoValidationInitializer
30+
[org.w3c.dom Document Element Node]))
1031

1132
;; these have to be initialized before using.
1233
;;
@@ -15,44 +36,44 @@
1536
(defonce ^:private -init
1637
(do
1738
;; add BouncyCastle as a security provider.
18-
(java.security.Security/addProvider (org.bouncycastle.jce.provider.BouncyCastleProvider.))
39+
(Security/addProvider (BouncyCastleProvider.))
1940
;; initialize OpenSAML
20-
(org.opensaml.core.config.InitializationService/initialize)
41+
(InitializationService/initialize)
2142
;; verify that OpenSAML has the crypto classes it needs
22-
(.init (org.opensaml.xmlsec.config.impl.JavaCryptoValidationInitializer.))
43+
(.init (JavaCryptoValidationInitializer.))
2344
nil))
2445

2546
(defprotocol CoerceToPrivateKey
2647
(->PrivateKey
27-
^java.security.PrivateKey [this]
28-
^java.security.PrivateKey [this ^String algorithm]
48+
^PrivateKey [this]
49+
^PrivateKey [this ^String algorithm]
2950
"Coerce something such as a base-64-encoded string or byte array to a `PrivateKey`. This isn't used directly by
3051
OpenSAML -- the key must be passed as part of an OpenSAML `Credential`. See `->Credential`."))
3152

3253
(defprotocol CoerceToX509Certificate
33-
(->X509Certificate ^java.security.cert.X509Certificate [this]
54+
(->X509Certificate ^X509Certificate [this]
3455
"Coerce something such as a base-64-encoded string or byte array to a `java.security.cert.X509Certificate`. This
3556
class isn't used directly by OpenSAML; instead, certificate must be coerced to an OpenSAML `Credential`. See
3657
`->Credential`."))
3758

3859
(defprotocol CoerceToCredential
3960
(->Credential
40-
^org.opensaml.security.credential.Credential [this]
41-
^org.opensaml.security.credential.Credential [public-key private-key]
61+
^Credential [this]
62+
^Credential [public-key private-key]
4263
"Coerce something such as a byte array or base-64-encoded String to an OpenSAML `Credential`. Typically, you'd use
4364
the credential with just the public key for the IdP's credentials, for encrypting requests (in combination with SP
4465
credentails) or verifying signature(s) in the response. A credential with both public and private keys would
4566
typically contain *your* public and private keys, for encrypting requests (in combination with IdP credentials) or
4667
for decrypting encrypted assertions in the response."))
4768

4869
(defprotocol CoerceToElement
49-
(->Element ^org.w3c.dom.Element [this]))
70+
(->Element ^Element [this]))
5071

5172
(defprotocol CoerceToSAMLObject
52-
(->SAMLObject ^org.opensaml.saml.common.SignableSAMLObject [this]))
73+
(->SAMLObject ^SignableSAMLObject [this]))
5374

5475
(defprotocol CoerceToResponse
55-
(->Response ^org.opensaml.saml.saml2.core.Response [this]))
76+
(->Response ^Response [this]))
5677

5778
(defprotocol SerializeXMLString
5879
(->xml-string ^String [this]))
@@ -61,32 +82,32 @@
6182
;;; ------------------------------------------------------ Impl ------------------------------------------------------
6283

6384
(defn keystore
64-
^java.security.KeyStore [{:keys [keystore ^String filename ^String password]}]
85+
^KeyStore [{:keys [keystore ^String filename ^String password]}]
6586
(or keystore
6687
(when (some-> filename io/as-file .exists)
6788
(with-open [is (io/input-stream filename)]
68-
(doto (java.security.KeyStore/getInstance "JKS")
89+
(doto (KeyStore/getInstance "JKS")
6990
(.load is (.toCharArray password)))))))
7091

7192
(defmulti bytes->PrivateKey
7293
"Generate a private key from a byte array using the given `algorithm`.
7394
7495
(bytes->PrivateKey my-byte-array :rsa) ;; -> ..."
75-
{:arglists '(^java.security.PrivateKey [^bytes key-bytes algorithm])}
96+
{:arglists '(^PrivateKey [^bytes key-bytes algorithm])}
7697
(fn [_ algorithm]
7798
(keyword algorithm)))
7899

79100
(defmethod bytes->PrivateKey :default
80101
[^bytes key-bytes algorithm]
81-
(.generatePrivate (java.security.KeyFactory/getInstance (str/upper-case (name algorithm)), "BC")
82-
(java.security.spec.PKCS8EncodedKeySpec. key-bytes)))
102+
(.generatePrivate (KeyFactory/getInstance (str/upper-case (name algorithm)), "BC")
103+
(PKCS8EncodedKeySpec. key-bytes)))
83104

84105
(defmethod bytes->PrivateKey :aes
85106
[^bytes key-bytes _]
86-
(javax.crypto.spec.SecretKeySpec. key-bytes
87-
0
88-
(count key-bytes)
89-
"AES"))
107+
(SecretKeySpec. key-bytes
108+
0
109+
(count key-bytes)
110+
"AES"))
90111

91112
;; I don't think we can use the "class name" of a byte array in `extend-protocol`
92113
(extend (Class/forName "[B")
@@ -109,29 +130,29 @@
109130
([s] (->PrivateKey s :rsa))
110131
([s algorithm] (->PrivateKey (encode-decode/base64-credential->bytes s) algorithm)))
111132

112-
java.security.PrivateKey
133+
PrivateKey
113134
(->PrivateKey
114135
([this] this)
115136
([this _] this))
116137

117-
org.opensaml.security.credential.Credential
138+
Credential
118139
(->PrivateKey
119140
([this]
120141
(.getPrivateKey this))
121142
([this _]
122143
(->PrivateKey this)))
123144

124-
clojure.lang.IPersistentMap
145+
IPersistentMap
125146
(->PrivateKey
126147
([{^String key-alias :alias, ^String password :password, :as m}]
127148
(when-let [keystore (keystore m)]
128149
(when-let [key (.getKey keystore key-alias (.toCharArray password))]
129-
(assert (instance? java.security.PrivateKey key))
150+
(assert (instance? PrivateKey key))
130151
key)))
131152
([this _]
132153
(->PrivateKey this)))
133154

134-
clojure.lang.IPersistentVector
155+
IPersistentVector
135156
(->PrivateKey
136157
([[_ k]]
137158
(->PrivateKey k))
@@ -142,9 +163,8 @@
142163
CoerceToX509Certificate
143164
{:->X509Certificate
144165
(fn [^bytes this]
145-
(let [cert-factory (java.security.cert.CertificateFactory/getInstance
146-
"X.509")]
147-
(with-open [is (java.io.ByteArrayInputStream. this)]
166+
(let [cert-factory (CertificateFactory/getInstance "X.509")]
167+
(with-open [is (ByteArrayInputStream. this)]
148168
(.generateCertificate cert-factory is))))})
149169

150170
(extend-protocol CoerceToX509Certificate
@@ -155,14 +175,14 @@
155175
(->X509Certificate [s]
156176
(->X509Certificate (encode-decode/base64-credential->bytes s)))
157177

158-
java.security.cert.X509Certificate
178+
X509Certificate
159179
(->X509Certificate [this] this)
160180

161-
org.opensaml.security.x509.BasicX509Credential
181+
BasicX509Credential
162182
(->X509Certificate [this]
163183
(.getEntityCertificate this))
164184

165-
clojure.lang.IPersistentMap
185+
IPersistentMap
166186
(->X509Certificate
167187
[{^String key-alias :alias, ^String password :password, :as m}]
168188
(when (and key-alias password)
@@ -182,53 +202,53 @@
182202
([public-key private-key]
183203
(let [cert (->X509Certificate public-key)]
184204
(if private-key
185-
(org.opensaml.security.x509.BasicX509Credential. cert (->PrivateKey private-key))
186-
(org.opensaml.security.x509.BasicX509Credential. cert)))))
205+
(BasicX509Credential. cert (->PrivateKey private-key))
206+
(BasicX509Credential. cert)))))
187207

188-
clojure.lang.IPersistentMap
208+
IPersistentMap
189209
(->Credential
190210
([{^String key-alias :alias, ^String password :password, :as m}]
191211
(when (and key-alias password)
192212
(when-let [keystore (keystore m)]
193-
(org.opensaml.security.x509.impl.KeyStoreX509CredentialAdapter. keystore key-alias (.toCharArray password)))))
213+
(KeyStoreX509CredentialAdapter. keystore key-alias (.toCharArray password)))))
194214
([m private-key]
195215
(let [credential (->Credential m)
196216
public-key (.getPublicKey credential)]
197217
(->Credential public-key private-key))))
198218

199-
clojure.lang.IPersistentVector
219+
IPersistentVector
200220
(->Credential [[public-key private-key]]
201221
(->Credential public-key private-key))
202222

203-
java.security.PublicKey
223+
PublicKey
204224
(->Credential [this]
205-
(org.opensaml.security.credential.BasicCredential. this))
225+
(BasicCredential. this))
206226

207-
javax.crypto.spec.SecretKeySpec
227+
SecretKeySpec
208228
(->Credential [this]
209-
(org.opensaml.security.credential.BasicCredential. this))
229+
(BasicCredential. this))
210230

211-
java.security.interfaces.RSAPrivateCrtKey
231+
RSAPrivateCrtKey
212232
(->Credential [this]
213-
(org.opensaml.security.credential.BasicCredential.
214-
(.generatePublic (java.security.KeyFactory/getInstance "RSA")
215-
(java.security.spec.RSAPublicKeySpec. (.getModulus this) (.getPublicExponent this)))
233+
(BasicCredential.
234+
(.generatePublic (KeyFactory/getInstance "RSA")
235+
(RSAPublicKeySpec. (.getModulus this) (.getPublicExponent this)))
216236
this)))
217237

218238
(extend-protocol CoerceToElement
219239
nil
220240
(->Element [_] nil)
221241

222-
org.w3c.dom.Element
242+
Element
223243
(->Element [this] this)
224244

225-
org.w3c.dom.Document
245+
Document
226246
(->Element [this]
227247
(.getDocumentElement this))
228248

229-
org.opensaml.core.xml.XMLObject
249+
XMLObject
230250
(->Element [this]
231-
(let [marshaller-factory (org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport/getMarshallerFactory)
251+
(let [marshaller-factory (XMLObjectProviderRegistrySupport/getMarshallerFactory)
232252
marshaller (.getMarshaller marshaller-factory this)]
233253
(when-not marshaller
234254
(throw (ex-info (format "Don't know how to marshall %s" (.getCanonicalName (class this)))
@@ -241,27 +261,27 @@
241261

242262
;; hiccup-style xml element
243263
;; TODO -- it's a little inefficient to serialize this to a string and then back to an element
244-
clojure.lang.IPersistentVector
264+
IPersistentVector
245265
(->Element [this]
246266
(->Element (->xml-string this))))
247267

248268
(extend-protocol CoerceToSAMLObject
249269
nil
250270
(->SAMLObject [_] nil)
251271

252-
org.opensaml.saml.common.SignableSAMLObject
272+
SignableSAMLObject
253273
(->SAMLObject [this] this)
254274

255-
org.w3c.dom.Element
275+
Element
256276
(->SAMLObject [this]
257-
(let [unmarshaller-factory (org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport/getUnmarshallerFactory)
277+
(let [unmarshaller-factory (XMLObjectProviderRegistrySupport/getUnmarshallerFactory)
258278
unmarshaller (.getUnmarshaller unmarshaller-factory this)]
259279
(when-not unmarshaller
260280
(throw (ex-info (format "Don't know how to unmarshall %s" (.getCanonicalName (class this)))
261281
{:object this})))
262282
(.unmarshall unmarshaller this)))
263283

264-
org.w3c.dom.Document
284+
Document
265285
(->SAMLObject [this]
266286
(->SAMLObject (.getDocumentElement this)))
267287

@@ -273,10 +293,10 @@
273293
nil
274294
(->Response [_] nil)
275295

276-
org.opensaml.saml.saml2.core.Response
296+
Response
277297
(->Response [this] this)
278298

279-
org.opensaml.saml.common.SignableSAMLObject
299+
SignableSAMLObject
280300
(->Response [this]
281301
(throw (ex-info (format "Don't know how to coerce a %s to a Response" (.getCanonicalName (class this)))
282302
{:object this})))
@@ -292,25 +312,25 @@
292312
String
293313
(->xml-string [this] this)
294314

295-
clojure.lang.IPersistentVector
315+
IPersistentVector
296316
(->xml-string [this]
297317
(str
298318
(h.page/xml-declaration "UTF-8")
299319
(hiccup/html this)))
300320

301-
org.w3c.dom.Node
321+
Node
302322
(->xml-string [this]
303-
(let [transformer (doto (.. javax.xml.transform.TransformerFactory newInstance newTransformer)
304-
#_(.setOutputProperty javax.xml.transform.OutputKeys/OMIT_XML_DECLARATION "yes")
305-
(.setOutputProperty javax.xml.transform.OutputKeys/ENCODING "UTF-8")
306-
(.setOutputProperty javax.xml.transform.OutputKeys/INDENT "yes")
323+
(let [transformer (doto (.. TransformerFactory newInstance newTransformer)
324+
#_(.setOutputProperty OutputKeys/OMIT_XML_DECLARATION "yes")
325+
(.setOutputProperty OutputKeys/ENCODING "UTF-8")
326+
(.setOutputProperty OutputKeys/INDENT "yes")
307327
(.setOutputProperty "{http://xml.apache.org/xslt}indent-amount" "2"))
308-
dom-source (javax.xml.transform.dom.DOMSource. this)]
309-
(with-open [w (java.io.StringWriter.)]
310-
(let [stream-result (javax.xml.transform.stream.StreamResult. w)]
328+
dom-source (DOMSource. this)]
329+
(with-open [w (StringWriter.)]
330+
(let [stream-result (StreamResult. w)]
311331
(.transform transformer dom-source stream-result))
312332
(.toString w))))
313333

314-
org.opensaml.core.xml.XMLObject
334+
XMLObject
315335
(->xml-string [this]
316336
(->xml-string (.getDOM this))))

0 commit comments

Comments
 (0)