Skip to content

Commit 458e35a

Browse files
committed
Make r.m.authorization/parse-authorization public
Make the existing ring.middleware.authorization/parse-authorization function public and make it take the Authorization header value as input. According to RFC 7235 Section 2 and RFC 9110 Section 11, the value credentials of the Authorization HTTP request header has the same structure as each of the comma-separated challenges of the WWW-Authenticate HTTP response header, which allows to reuse this function also for parsing responses: * https://datatracker.ietf.org/doc/html/rfc7235#section-2 * https://datatracker.ietf.org/doc/html/rfc9110#section-11
1 parent 990a379 commit 458e35a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/ring/middleware/authorization.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
{}
1717
(str/split auth-params #"\s*,\s*")))
1818

19-
(defn- parse-authorization [request]
19+
(defn parse-credentials
20+
"Parse `credentials` as used in the `Authorization` header of an HTTP request.
21+
22+
Note: The `WWW-Authenticate` header of an HTTP response contains a comma-separated list of `challenge`s,
23+
which each happen to have the same structure as the single `credentials` in the `Authorization` header."
24+
[credentials]
2025
(when-let [[auth-scheme token-or-params]
21-
(some-> (get-in request [:headers "authorization"])
22-
(str/split #"\s" 2))]
26+
(some-> credentials (str/split #"\s" 2))]
2327
(cond
2428
(empty? token-or-params)
2529
{:scheme (str/lower-case auth-scheme)}
@@ -37,7 +41,7 @@
3741
[request]
3842
(if (:authorization request)
3943
request
40-
(assoc request :authorization (parse-authorization request))))
44+
(assoc request :authorization (parse-credentials (get-in request [:headers "authorization"])))))
4145

4246
(defn wrap-authorization
4347
"Parses the Authorization header in the request map, then assocs the result

0 commit comments

Comments
 (0)