google auth backend

This commit is contained in:
Bart Akeley 2018-05-13 10:46:30 -05:00
parent c5abfd1203
commit e1e98a297a
3 changed files with 33 additions and 24 deletions

View file

@ -5,7 +5,8 @@
add-food-item-handler
add-image-handler
get-images-handler]]
[aretherecookies.auth :refer [auth-backend]]
[aretherecookies.auth :refer [facebook-auth-backend
google-auth-backend]]
[environ.core :refer [env]]
[compojure.handler :refer [api]]
[compojure.core :refer :all]
@ -35,8 +36,10 @@
(let [port (Integer. (or port (env :port) 3000))]
(->
(api #'app)
(wrap-authorization auth-backend)
(wrap-authentication auth-backend)
(wrap-authorization facebook-auth-backend)
(wrap-authorization google-auth-backend)
(wrap-authentication facebook-auth-backend)
(wrap-authentication google-auth-backend)
wrap-multipart-params
(wrap-json-body {:keywords? true})
(jetty/run-jetty {:port port :join? false}))))

View file

@ -4,16 +4,33 @@
(def tokens (atom {}))
(defn facebook-me [token]
(client/get (str "https://graph.facebook.com/me?access_token=" token) {:accept :json}))
(def facebook-me-url "https://graph.facebook.com/me?access_token=")
(defn facebook-me-ok? [token]
(= 200 (:status (facebook-me token))) (swap! tokens assoc (keyword token) true))
(def google-userinfo-url "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=")
(defn facebook-token-auth [_ token]
(cond
(get @tokens (keyword token)) token
(facebook-me-ok? token) token))
(defn verify-token
"perform rest requesting using token and return boolean if successful"
[token url]
(= 200 (:status (client/get (str url token) {:accept :json}))))
(def auth-backend
(backends/token {:token-name "facebook-token" :authfn facebook-token-auth}))
(defn cache-token
"swap the new token into our cache map atom and return the token"
[token]
(swap! tokens assoc (keyword token) true))
(defn verify-and-cache-token
"if a REST request is successful against url using token, cache the token and return it"
[token url]
(if (verify-token token url) (cache-token token)))
(defn url-backend
"return a buddy auth backend that validates tokens agianst given url"
[url]
(fn [_ token] (get @tokens (keyword token) (verify-and-cache-token token url))))
(def facebook-auth-backend
(backends/token {:token-name "facebook-token" :authfn (url-backend facebook-me-url)}))
(def google-auth-backend
(backends/token {:token-name "google-token" :authfn (url-backend google-userinfo-url)}))

View file

@ -35,17 +35,6 @@
([key map]
(update map key build-s3-url)))
;(defn update-image-url-to-s3
; "convert image filename key to url key"
; [image]
; (update image :url build-s3-url))
;
;
;(defn update-thumbimage-to-s3
; "convert all the images of a food item to fully qualified s3 URLs"
; [food-item]
; (update food-item :thumbimage build-s3-url))
(defn food-items-handler
"return a list of food items for a given filter and location"