Skip to content

Commit

Permalink
Merge branch '0.1.0-pre' of https://github.com/ddellacosta/friend-oauth2
Browse files Browse the repository at this point in the history
 into 0.1.0-pre
  • Loading branch information
ddellacosta committed Nov 12, 2013
2 parents b607ce6 + d2d7471 commit 4085c8a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -23,7 +23,10 @@ Somewhere in your code, maybe in your handler:
Set your OAuth2 provider settings (using [Google APIs OAuth2](https://developers.google.com/accounts/docs/OAuth2) as an example):

```clojure
(def config-auth {:roles #{::oauth2-user}})
(defn credential-fn
[token]
;;lookup token in DB or whatever to fetch appropriate :roles
{:identity token :roles #{::user}})

(def client-config
{:client-id "123456789012.apps.googleusercontent.com"
Expand Down Expand Up @@ -52,7 +55,7 @@ Then add the workflow to your handler per normal Friend configuration:
:workflows [(oauth2/workflow
{:client-config client-config
:uri-config uri-config
:config-auth config-auth})]})
:credential-fn credential-fn})]})

(def app
(-> ring-app
Expand Down
18 changes: 8 additions & 10 deletions src/friend_oauth2/workflow.clj
Expand Up @@ -5,13 +5,9 @@
[clj-http.client :as client]
[ring.util.request :as request]))

(defn make-auth
"Creates the auth-map for Friend"
[identity]
(with-meta identity
{:type ::friend/auth
::friend/workflow :email-login
::friend/redirect-on-auth? true}))
(defn- default-credential-fn
[creds]
{:identity (:access-token creds)})

(defn- is-oauth2-callback?
[config request]
Expand Down Expand Up @@ -53,7 +49,9 @@
(if (and (not (nil? code))
(= state session-state))
(when-let [access-token (request-token config code)]
(make-auth (merge {:identity access-token
:access_token access-token}
(:config-auth config))))
(when-let [auth-map ((:credential-fn config default-credential-fn)
{:access-token access-token})]
(vary-meta auth-map merge {::friend/workflow :oauth2
::friend/redirect-on-auth? true
:type ::friend/auth})))
(redirect-to-provider! config request))))))
4 changes: 3 additions & 1 deletion test/friend_oauth2/test_helpers.clj
Expand Up @@ -62,7 +62,9 @@
:workflows [(oauth2/workflow
{:client-config client-config-fixture
:uri-config uri-config-fixture
:config-auth {:roles #{::user}}})]})))
:credential-fn (fn [token]
{:identity token
:roles #{::user}})})]})))

(defn setup-valid-state
"Initiates login to provide valid state for later requests.
Expand Down
8 changes: 0 additions & 8 deletions test/friend_oauth2/workflow_facts.clj
Expand Up @@ -10,14 +10,6 @@
[ring.util.response :refer [get-header]]
[ring.mock.request :as ring-mock]))

(fact
"Creates the auth-map for Friend with proper meta-data"
(meta (oauth2/make-auth identity-fixture))
=>
{:type ::friend/auth
::friend/workflow :email-login
::friend/redirect-on-auth? true})

(fact
"A user can authenticate via OAuth2
(tests 'happy path' for the whole process)."
Expand Down

0 comments on commit 4085c8a

Please sign in to comment.