Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Last active October 23, 2018 07:11
Show Gist options
  • Save jamesdavidson/e7f7e8e1d80194707d858a860bc76d5e to your computer and use it in GitHub Desktop.
Save jamesdavidson/e7f7e8e1d80194707d858a860bc76d5e to your computer and use it in GitHub Desktop.
Could some bits from the SAML Plugin for Jenkins (particularly SamlSecurityRealm.java) be reused to implement a Service Provider in Clojure?
(def idpMetadata (slurp "FederationMetadata.xml"))
(defn encode64 [b]
(. (new sun.misc.BASE64Encoder) encode b))
(defn str-to-bytes [s] (.getBytes s))
(def saml-response (encode64 (str-to-bytes (slurp "saml-response.xml"))))
(def sts-cookie "MSISAuth=AAEasdfasdfasdfqp")
(def saml-request "asdf")
(def saml-response
(-> (str "https://idp.example.com/adfs/ls/?SAMLRequest=" saml-request "&RelayState=https%3A%2F%2Fjenkins%2FsecurityRealm%2FfinishLogin")
(clj-http.client/post {:headers {"Cookie" sts-cookie}})
:body
clojure.data.xml/parse-str :content second :content first :content first :attrs :value))
;(def saml-response (-> saml-response decode64 str-from-bytes (.replace "jamesd" "jamesp") str-to-bytes encode64))
;(def saml-response (-> saml-response decode64 str-from-bytes (.replace "Z" "") str-to-bytes encode64))
;(defn asdf [el] (if (= :SignatureValue (:tag el)) (new clojure.data.xml.Element :SignatureValue {} '("asdf")) el))
;(def saml-response (->> saml-response decode64 str-from-bytes clojure.data.xml/parse-str (clojure.walk/postwalk asdf) clojure.data.xml/emit-str str-to-bytes encode64))
(def context (reify WebContext (setSessionAttribute [this o1 o2] nil) (getFullRequestURL [this] "https://jenkins/securityRealm/finishLogin") (getRequestMethod [this] "POST") (getRequestParameter [this k] (get {"SAMLResponse" saml-response} k))))
(.setCallbackUrl client "https://jenkins/securityRealm/finishLogin")
(.setIdpMetadata client idpMetadata)
(.getCredentials client context)
(def credentials (.getCredentials client context))
(def profile (.getUserProfile client credentials context))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment