Created
December 12, 2012 16:01
-
-
Save slagyr/4268974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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