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

first shot at async handlers #90

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
61 changes: 37 additions & 24 deletions src/friend_oauth2/workflow.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,45 @@
:client-config (config/->client-cfg cfg)
:uri-config (config/->uri-cfg cfg)))))

(defn- apply-workflow
"Workflow for OAuth2"
[cfg request]
(when (is-oauth2-callback? cfg request)
;; Extracts code from request if we are getting here via OAuth2 callback.
;; http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1.2
(let [{:keys [state code error]} (:params request)
session-state (util/extract-anti-forgery-token request)]
(log/debug "state:" state)
(log/debug "code:" code)
(log/debug "error:" error)
(log/debug "session-state:" session-state)
(if (and (not (nil? code))
(= state session-state))
(when-let [access-token (request-token cfg code)]
(when-let [auth-map ((:credential-fn cfg
default-credential-fn)
{:access-token access-token})]
(vary-meta auth-map merge {::friend/workflow :oauth2
::friend/redirect-on-auth? true
:type ::friend/auth})))
(let [auth-error-fn (:auth-error-fn cfg)]
(if (and error auth-error-fn)
(auth-error-fn error)
(redirect-to-provider! cfg request)))))))

(defn workflow
"Workflow for OAuth2"
[args]
(let [cfg (process-config args)]
(log/trace "Config:\n" (logger/pprint cfg))
(fn [request]
(log/trace "Request:\n" (logger/pprint request))
(when (is-oauth2-callback? cfg request)
;; Extracts code from request if we are getting here via OAuth2 callback.
;; http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1.2
(let [{:keys [state code error]} (:params request)
session-state (util/extract-anti-forgery-token request)]
(log/debug "state:" state)
(log/debug "code:" code)
(log/debug "error:" error)
(log/debug "session-state:" session-state)
(if (and (not (nil? code))
(= state session-state))
(when-let [access-token (request-token cfg code)]
(when-let [auth-map ((:credential-fn cfg
default-credential-fn)
{:access-token access-token})]
(vary-meta auth-map merge {::friend/workflow :oauth2
::friend/redirect-on-auth? true
:type ::friend/auth})))
(let [auth-error-fn (:auth-error-fn cfg)]
(if (and error auth-error-fn)
(auth-error-fn error)
(redirect-to-provider! cfg request)))))))))
(fn
([request]
(log/trace "Request:\n" (logger/pprint request))
(apply-workflow cfg request))
([request respond raise]
(log/trace "Request:\n" (logger/pprint request))
(try
(let [response (apply-workflow cfg request)]
(respond response))
(catch Exception e
(raise e)))))))