This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
spnego.clj
45 lines (41 loc) · 1.92 KB
/
spnego.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
;;
;; Copyright (c) Two Sigma Open Source, LLC
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;
(ns token-syncer.spnego
(:require [clojure.tools.logging :as log])
(:import (java.net URI)
(org.apache.commons.codec.binary Base64)
(org.eclipse.jetty.client.api Authentication$Result Request)
(org.eclipse.jetty.http HttpHeader)
(org.ietf.jgss GSSContext GSSManager GSSName Oid)))
(def ^Oid spnego-oid (Oid. "1.3.6.1.5.5.2"))
(def ^Base64 base64 (Base64.))
(defn spnego-authentication
"Returns an Authentication$Result for endpoint which will use SPNEGO to generate an Authorization header"
[^URI endpoint]
(reify Authentication$Result
(getURI [_] endpoint)
(^void apply [_ ^Request request]
(try
(let [gss-manager (GSSManager/getInstance)
server-princ (str "HTTP@" (.getHost endpoint))
server-name (.createName gss-manager server-princ GSSName/NT_HOSTBASED_SERVICE spnego-oid)
gss-context (.createContext gss-manager server-name spnego-oid nil GSSContext/DEFAULT_LIFETIME)
_ (.requestMutualAuth gss-context true)
token (.initSecContext gss-context (make-array Byte/TYPE 0) 0 0)
header (str "Negotiate " (String. (.encode base64 token)))]
(.header request HttpHeader/AUTHORIZATION header))
(catch Exception e
(log/error e "failure during spnego authentication"))))))