Skip to content

Instantly share code, notes, and snippets.

@slagyr
Created December 12, 2012 16:01
Show Gist options
  • Save slagyr/4268974 to your computer and use it in GitHub Desktop.
Save slagyr/4268974 to your computer and use it in GitHub Desktop.
(ns doppler.auth.auth-controller
(:require [cheshire.core :refer [parse-string]]
[clj-oauth2.client :as oauth2]
[compojure.core :refer [defroutes GET context]]
[joodo.env :refer [env]]
[joodo.middleware.request :refer [*request*]]
[ring.util.response :refer [redirect]]))
(def google-com-oauth2 (merge (env :google-oauth )
{:authorization-uri "https://accounts.google.com/o/oauth2/auth"
:access-token-uri "https://accounts.google.com/o/oauth2/token"
:redirect-uri (format "http://%s/auth/google_oauth2/callback" (env :hostname ))
:access-query-param :access_token
:scope ["https://www.googleapis.com/auth/userinfo.email"]
:grant-type "authorization_code"
:access-type "online"
:approval_prompt ""}))
(def auth-req
(oauth2/make-auth-request google-com-oauth2))
(defn- google-access-token [request]
(oauth2/get-access-token google-com-oauth2 (:params request) auth-req))
(defn- google-user-email [access-token]
(let [response (oauth2/get "https://www.googleapis.com/oauth2/v1/userinfo" {:oauth2 access-token})]
(get (parse-string (:body response)) "email")))
(defroutes auth-controller
(context "/auth" []
(GET "/" [] (redirect (:uri auth-req)))
(GET "/google_oauth2/callback" []
(assoc
(redirect "/dashboard")
:session {:user-email {:value (google-user-email (google-access-token *request*))}}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment