Skip to content
Browse files

Added client certificates to ring-servlet

  • Loading branch information...
1 parent 890ffee commit 1619fd3b57418696ca0856c2e5a0ad5a3faf90cb @weavejester weavejester committed
Showing with 28 additions and 16 deletions.
  1. +6 −0 ring-servlet/src/ring/util/servlet.clj
  2. +22 −16 ring-servlet/test/ring/util/test/servlet.clj
View
6 ring-servlet/src/ring/util/servlet.clj
@@ -26,6 +26,11 @@
(let [length (.getContentLength request)]
(if (>= length 0) length)))
+(defn get-client-cert
+ "Returns the SSL client certificate of the reqest, if one exists."
+ [^HttpServletRequest request]
+ (first (.getAttribute request "javax.servlet.request.X509Certificate")))
+
(defn build-request-map
"Create the request map from the HttpServletRequest object."
[^HttpServletRequest request]
@@ -40,6 +45,7 @@
:content-type (.getContentType request)
:content-length (get-content-length request)
:character-encoding (.getCharacterEncoding request)
+ :ssl-client-cert (get-client-cert request)
:body (.getInputStream request)})
(defn merge-servlet-keys
View
38 ring-servlet/test/ring/util/test/servlet.clj
@@ -9,20 +9,23 @@
(nextElement [] (let [f (first @e)] (swap! e rest) f)))))
(defn- servlet-request [request]
- (proxy [javax.servlet.http.HttpServletRequest] []
- (getServerPort [] (request :server-port))
- (getServerName [] (request :server-name))
- (getRemoteAddr [] (request :remote-addr))
- (getRequestURI [] (request :uri))
- (getQueryString [] (request :query-string))
- (getScheme [] (name (request :scheme)))
- (getMethod [] (-> request :request-method name .toUpperCase))
- (getHeaderNames [] (enumeration (keys (request :headers))))
- (getHeaders [name] (enumeration (get-in request [:headers name])))
- (getContentType [] (request :content-type))
- (getContentLength [] (or (request :content-length) -1))
- (getCharacterEncoding [] (request :character-encoding))
- (getInputStream [] (request :body))))
+ (let [attributes {"javax.servlet.request.X509Certificate"
+ [(request :ssl-client-cert)]}]
+ (proxy [javax.servlet.http.HttpServletRequest] []
+ (getServerPort [] (request :server-port))
+ (getServerName [] (request :server-name))
+ (getRemoteAddr [] (request :remote-addr))
+ (getRequestURI [] (request :uri))
+ (getQueryString [] (request :query-string))
+ (getScheme [] (name (request :scheme)))
+ (getMethod [] (-> request :request-method name .toUpperCase))
+ (getHeaderNames [] (enumeration (keys (request :headers))))
+ (getHeaders [name] (enumeration (get-in request [:headers name])))
+ (getContentType [] (request :content-type))
+ (getContentLength [] (or (request :content-length) -1))
+ (getCharacterEncoding [] (request :character-encoding))
+ (getAttribute [k] (attributes k))
+ (getInputStream [] (request :body)))))
(defn- servlet-response [response]
(proxy [javax.servlet.http.HttpServletResponse] []
@@ -47,6 +50,7 @@
(deftest servlet-test
(let [body (proxy [javax.servlet.ServletInputStream] [])
+ cert (proxy [java.security.cert.X509Certificate] [])
request {:server-port 8080
:server-name "foobar"
:remote-addr "127.0.0.1"
@@ -59,7 +63,8 @@
:content-type "text/plain"
:content-length 10
:character-encoding "UTF-8"
- :body body}
+ :ssl-client-cert cert
+ :body body}
response (atom {})]
(testing "request"
(letfn [(handler [r]
@@ -76,7 +81,8 @@
:content-type "text/plain"
:content-length 10
:character-encoding "UTF-8"
- :body body)
+ :ssl-client-cert cert
+ :body body)
{:status 200, :headers {}})]
(run-servlet handler request response)))
(testing "response"

0 comments on commit 1619fd3

Please sign in to comment.
Something went wrong with that request. Please try again.