Permalink
Browse files

Breaking change. use keyword-params.

Some cleanups (as I learn more clojure)
  • Loading branch information...
1 parent adec65e commit 60bf83b93c9779e6da3142674d08723783eb549c @pelle committed Apr 15, 2012
View
@@ -23,7 +23,7 @@ The following bearer tokens are implemented:
Add the following dependency to your `project.clj` file:
- [clauth "1.0.0-beta2"]
+ [clauth "1.0.0-beta3"]
## Usage
View
@@ -1,4 +1,4 @@
-(defproject clauth "1.0.0-beta2"
+(defproject clauth "1.0.0-beta3"
:description "OAuth2 based authentication library for Ring"
:url "http://github.com/pelle/clauth"
View
@@ -1,6 +1,6 @@
(ns clauth.client
- (:use [clauth.token])
- (:use [clauth.store]))
+ (:use [clauth.token]
+ [clauth.store]))
(defonce client-store (atom (create-memory-store)))
View
@@ -1,18 +1,19 @@
(ns clauth.demo
- (:use [clauth.middleware])
- (:use [clauth.endpoints])
- (:use [clauth.client])
- (:use [clauth.token])
- (:use [clauth.store.redis])
(:require [redis.core :as redis])
- (:use [ring.adapter.jetty])
- (:use [ring.middleware.cookies])
- (:use [ring.middleware.session])
- (:use [ring.middleware.params])
- (:use [hiccup.bootstrap.middleware])
- (:use [hiccup.bootstrap.page])
- (:use [hiccup.page])
- (:use [hiccup.element]))
+ (:use [clauth.middleware]
+ [clauth.endpoints]
+ [clauth.client]
+ [clauth.token]
+ [clauth.store.redis]
+ [ring.adapter.jetty]
+ [ring.middleware.cookies]
+ [ring.middleware.session]
+ [ring.middleware.params]
+ [ring.middleware.keyword-params]
+ [hiccup.bootstrap.middleware]
+ [hiccup.bootstrap.page]
+ [hiccup.page]
+ [hiccup.element]))
(defn nav-menu [req]
(if (logged-in? req)
@@ -104,6 +105,7 @@
(println "http://127.0.0.1:3000/login")
(run-jetty (-> (routes client)
+ (wrap-keyword-params)
(wrap-params)
(wrap-cookies)
(wrap-session)
View
@@ -1,12 +1,12 @@
(ns clauth.endpoints
- (:use [clauth.token])
- (:use [clauth.client])
- (:use [clauth.user])
- (:use [clauth.middleware :only [csrf-protect! require-user-session!]])
- (:use [clauth.views :only [login-form-handler authorization-form-handler error-page]])
- (:use [hiccup.util :only [url-encode]])
- (:use [ring.util.response])
- (:use [cheshire.core])
+ (:use [clauth.token]
+ [clauth.client]
+ [clauth.user]
+ [clauth.middleware :only [csrf-protect! require-user-session!]]
+ [clauth.views :only [login-form-handler authorization-form-handler error-page]]
+ [hiccup.util :only [url-encode]]
+ [ring.util.response]
+ [cheshire.core])
(:import [org.apache.commons.codec.binary Base64]))
@@ -59,16 +59,16 @@
If authenticate-client returns a client map it runs success function with the request and the client."
[req authenticator success]
(let [ basic (basic-authentication-credentials req)
- client_id (if basic (first basic) ((req :params ) "client_id"))
- client_secret (if basic (last basic) ((req :params) "client_secret"))
+ client_id (if basic (first basic) ((req :params ) :client_id))
+ client_secret (if basic (last basic) ((req :params) :client_secret))
client (authenticator client_id client_secret)]
(if client
(success req client)
(error-response "invalid_client"))))
(defn grant-type
"extract grant type from request"
- [req _ _] ((req :params) "grant_type"))
+ [req _ _] ((req :params) :grant_type))
(defmulti token-request-handler grant-type)
@@ -82,7 +82,7 @@
(client-authenticated-request
req
client-authenticator
- (fn [req client] (if-let [user (user-authenticator ((req :params) "username") ((req :params) "password"))]
+ (fn [req client] (if-let [user (user-authenticator ((req :params) :username) ((req :params) :password))]
(respond-with-new-token client client)
(error-response "invalid_grant")))))
@@ -109,7 +109,7 @@
(fn [req]
(if (= :get (req :request-method))
(login-form req)
- (if-let [user (user-authenticator ((req :params) "username") ((req :params) "password"))]
+ (if-let [user (user-authenticator ((req :params) :username) ((req :params) :password))]
(let
[ destination ((req :session {}) :return-to "/")
session ( dissoc (assoc (req :session) :access_token (:token (create-token client user))) :return-to )
@@ -139,32 +139,32 @@
(defn response-type
"extract grant type from request"
- [req] ((req :params) "response_type"))
+ [req] ((req :params) :response_type))
(defn authorization-response
"Create a proper redirection response depending on response_type"
[req response_params ]
(let [ params (req :params)
- redirect_uri (params "redirect_uri")]
+ redirect_uri (params :redirect_uri)]
(redirect (str redirect_uri
- (if (= (params "response_type") "token")
+ (if (= (params :response_type) "token")
"#"
"?")
- (url-encode (merge response_params (filter val (select-keys (req :params) ["state"]))))
+ (url-encode (merge response_params (filter val (select-keys (req :params) [:state]))))
))))
(defn authorization-error-response
"redirect to client with error code"
[req error]
- (if ((req :params) "redirect_uri")
+ (if ((req :params) :redirect_uri)
(authorization-response req { "error" error })
(error-page error)))
(defmulti authorization-request-handler response-type)
(defmethod authorization-request-handler "token" [req]
(let [ params (req :params)
- client (fetch-client (params "client_id"))
+ client (fetch-client (params :client_id))
user ( :subject (fetch-token (:access_token (req :session))))
token (create-token client user)]
(authorization-response req {:access_token (:token token) :token_type "bearer"})))
@@ -181,9 +181,9 @@
(csrf-protect!
(fn [req]
(let [params (req :params)]
- (if (and (params "response_type") (params "client_id"))
+ (if (and (params :response_type) (params :client_id))
- (if (= (params "response_type") "token")
+ (if (= (params :response_type) "token")
(if (= :get (req :request-method))
(authorization-form-handler req)
(authorization-request-handler req)
@@ -1,6 +1,6 @@
(ns clauth.middleware
- (:use [clauth.token])
- (:use [ring.util.response :only [redirect]]))
+ (:use [clauth.token]
+ [ring.util.response :only [redirect]]))
(defn wrap-bearer-token
"Wrap request with a OAuth2 bearer token as defined in http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-08.
@@ -24,6 +24,7 @@
(let [auth ((:headers req {}) "authorization")
token (or (last
(re-find #"^Bearer (.*)$" (str auth)))
+ ((:params req {}) :access_token)
((:params req {}) "access_token")
((:session req {}) :access_token)
(((:cookies req {}) "access_token" {}) :value )
@@ -88,7 +89,7 @@
session (req :session)]
(if (or
(= (:request-method req) :get)
- (= token ((req :params {}) "csrf-token")))
+ (= token ((req :params {}) :csrf-token)))
(let [response (app req)
session (assoc (response :session (req :session)) :csrf-token token)]
(assoc response :session session))
@@ -1,7 +1,7 @@
(ns clauth.store.redis
(:use [clauth.store])
- (:require [redis.core :as redis])
- (:require [cheshire.core]))
+ (:require [redis.core :as redis]
+ [cheshire.core]))
(defn namespaced-keys
View
@@ -1,8 +1,8 @@
(ns clauth.token
(:use [clauth.store])
- (:require [crypto.random])
- (:require [clj-time.core :as time])
- (:require [cheshire.core]))
+ (:require [crypto.random]
+ [clj-time.core :as time]
+ [cheshire.core]))
(defprotocol Expirable
"Check if object is valid"
View
@@ -1,8 +1,8 @@
(ns clauth.views
- (:use ring.util.response)
- (:use [clauth.middleware :only [csrf-token]])
- (:use hiccup.core)
- (:use hiccup.form))
+ (:use [ring.util.response]
+ [clauth.middleware :only [csrf-token]]
+ [hiccup.core]
+ [hiccup.form]))
(defn csrf-field
"hidden form field containing csrf-token"
@@ -1,6 +1,6 @@
(ns clauth.test.client
- (:use [clauth.client])
- (:use [clojure.test]))
+ (:use [clauth.client]
+ [clojure.test]))
(deftest client-registration
(reset-client-store!)
Oops, something went wrong.

0 comments on commit 60bf83b

Please sign in to comment.