Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 86 authorization scheme must be case insensitive #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/buddy/auth/backends/httpbasic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Given a request, try to extract and parse
the http basic header."
[request]
(let [pattern (re-pattern "^Basic (.+)$")
(let [pattern (re-pattern "^(?i)Basic (.+)$")
decoded (some->> (http/-get-header request "authorization")
(re-find pattern)
(second)
Expand Down
2 changes: 1 addition & 1 deletion src/buddy/auth/backends/token.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
(defn- parse-header
[request token-name]
(some->> (http/-get-header request "authorization")
(re-find (re-pattern (str "^" token-name " (.+)$")))
(re-find (re-pattern (str "^(?i)" token-name " (.+)$")))
(second)))

(defn jws-backend
Expand Down
23 changes: 19 additions & 4 deletions test/buddy/auth/backends/httpbasic_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
[buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]))

(defn make-header
[username password]
(format "Basic %s" (-> (b64/encode (format "%s:%s" username password))
(bytes->str))))
([schema username password]
(format "%s %s" schema (-> (b64/encode (format "%s:%s" username password))
(bytes->str)))))

(defn make-request
([] {:headers {}})
([username password]
(let [auth (make-header username password)]
(make-request "Basic" username password))
([schema username password]
(let [auth (make-header schema username password)]
{:headers {"auThorIzation" auth "lala" "2"}})))

(defn auth-fn
Expand Down Expand Up @@ -44,6 +46,19 @@
(is (not (nil? parsed)))
(is (= (:password parsed) "bar:baz"))
(is (= (:username parsed) "foo")))))
(testing "Parsing httpbasic header as case insensitive schema"
(let [parse #'httpbasic/parse-header
request (make-request "BASIC" "Ufoo" "Ubar")
parsed (parse request)]
(is (not (nil? parsed)))
(is (= (:password parsed) "Ubar"))
(is (= (:username parsed) "Ufoo")))
(let [parse #'httpbasic/parse-header
request (make-request "basic" "lfoo" "lbar")
parsed (parse request)]
(is (not (nil? parsed)))
(is (= (:password parsed) "lbar"))
(is (= (:username parsed) "lfoo"))))

(deftest httpbasic-auth-backend
(testing "Testing anon request"
Expand Down
10 changes: 9 additions & 1 deletion test/buddy/auth/backends/token_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@
(testing "Parse authorization header different header name yields nil"
(let [parse #'token/parse-header
parsed (parse (make-request "foo") "MyToken")]
(is (= parsed nil)))))
(is (= parsed nil))))

(testing "Token authorization schema is case insensitive"
(let [request (make-request "foo")
parse #'token/parse-header
parsed-small (parse request "token")
parsed-caps (parse request "token")]
(is (= parsed-small "foo"))
(is (= parsed-caps "foo")))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests: JWS
Expand Down