Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from mschristiansen/master

Use 'official' Clojure lib for base64 encoding/decoding
  • Loading branch information...
commit b3f7c86d8ac1c137ea5ff7f9cee0a9590990ebe3 2 parents 13e958d + 80dfcf9
@remvee authored
View
1  .gitignore
@@ -4,3 +4,4 @@
/classes
/autodoc
/.lein-deps-sum
+/.lein-failures
View
4 project.clj
@@ -1,8 +1,8 @@
-(defproject ring-basic-authentication "0.0.3"
+(defproject mschristiansen/ring-basic-authentication "0.0.3"
:description "Ring middleware for basic authentication."
:dependencies [[org.clojure/clojure "1.3.0"]
- [clj-base64 "0.0.2"]]
+ [org.clojure/data.codec "0.1.0"]]
:autodoc {:description "Ring middleware to enforce basic authentication as described in RFC2617 section 2."
:copyright "Copyright (c) Remco van 't Veer."
View
27 src/remvee/ring/middleware/basic_authentication.clj → src/ring/middleware/basic_authentication.clj
@@ -6,11 +6,26 @@
;; terms of this license. You must not remove this notice, or any other, from
;; this software.
-(ns remvee.ring.middleware.basic-authentication
+(ns ring.middleware.basic-authentication
"HTTP basis authentication middleware for ring."
{:author "Remco van 't Veer"}
- (:use clojure.test
- [remvee.base64 :as base64]))
+ (:use clojure.test)
+ (:require [clojure.data.codec.base64 :as base64]))
+
+(defn- byte-transform
+ "Used to encode and decode strings."
+ [direction-fn string]
+ (reduce str (map char (direction-fn (.getBytes string)))))
+
+(defn encode
+ "Will do a base64 encoding of a string and return a string."
+ [^String string]
+ (byte-transform base64/encode string))
+
+(defn decode
+ "Will do a base64 decoding of a string and return a string."
+ [^String string]
+ (byte-transform base64/decode string))
(defn wrap-basic-authentication
"Wrap response with a basic authentication challenge as described in
@@ -38,7 +53,7 @@
#(and (= %1 "tester")
(= %2 "secret")))
{:headers {"authorization"
- (str "Basic " (base64/encode-str "tester:secret"))}})))
+ (str "Basic " (encode "tester:secret"))}})))
;; authorization success adds basic-authentication on request map
(is (= "token" (:basic-authentication
@@ -47,7 +62,7 @@
(= %2 "secret")
"token"))
{:headers {"authorization"
- (str "Basic " (base64/encode-str "tester:secret"))}}))))
+ (str "Basic " (encode "tester:secret"))}}))))
;; authorization failure
(let [f (wrap-basic-authentication (fn [_] :pass)
@@ -80,7 +95,7 @@
(fn [req]
(let [auth ((:headers req) "authorization")
cred (and auth
- (base64/decode-str
+ (decode
(last
(re-find #"^Basic (.*)$" auth))))
user (and cred
View
4 test/run.clj
@@ -1,6 +1,6 @@
(ns run
- (:use [remvee.ring.middleware.basic-authentication] :reload-all)
+ (:use [ring.middleware.basic-authentication] :reload-all)
(:use [clojure.test]))
(deftest all
- (run-tests 'remvee.ring.middleware.basic-authentication))
+ (run-tests 'ring.middleware.basic-authentication))
Please sign in to comment.
Something went wrong with that request. Please try again.