2019-01-11 22:10:58 +00:00
|
|
|
(ns cat.routes.oauth
|
|
|
|
(:require [ring.util.http-response :refer [ok found]]
|
|
|
|
[compojure.core :refer [defroutes GET]]
|
2019-01-14 01:39:40 +00:00
|
|
|
[clojure.tools.logging :as log]
|
2019-01-14 22:41:10 +00:00
|
|
|
[cat.moauth :as mo]
|
|
|
|
[cat.db.core :refer [*db*] :as db]))
|
2019-01-14 01:39:40 +00:00
|
|
|
|
2019-02-15 23:49:06 +00:00
|
|
|
; This list contains application admins, they can add non-zeus people and can add relations
|
|
|
|
; More functionality is planned
|
|
|
|
(def admins [{:name "flynn" :zeusid 117}])
|
2019-01-14 02:24:16 +00:00
|
|
|
|
2019-01-14 01:39:40 +00:00
|
|
|
(defn set-user! [user session redirect-url]
|
2019-06-08 15:43:48 +00:00
|
|
|
(log/debug "Set user in session: " user)
|
2019-01-14 02:24:16 +00:00
|
|
|
(let [new-session (-> session
|
|
|
|
(assoc :user user)
|
2019-02-15 23:49:06 +00:00
|
|
|
(cond-> (some (partial = (select-keys user [:zeusid :name])) admins)
|
2019-06-08 15:43:48 +00:00
|
|
|
(->
|
|
|
|
(assoc-in [:user :admin] {:enabled false})
|
|
|
|
(assoc :identity "foo"))))]
|
2019-01-14 02:24:16 +00:00
|
|
|
(-> (found redirect-url)
|
|
|
|
(assoc :session new-session))))
|
2019-01-14 01:39:40 +00:00
|
|
|
|
|
|
|
(defn remove-user! [session redirect-url]
|
|
|
|
(-> (found redirect-url)
|
|
|
|
(assoc :session (dissoc session :user))))
|
|
|
|
|
|
|
|
(defn clear-session! [redirect-url]
|
|
|
|
(-> (found redirect-url)
|
2019-01-14 02:24:16 +00:00
|
|
|
(assoc :session nil)))
|
2019-01-11 22:10:58 +00:00
|
|
|
|
|
|
|
(defn oauth-init
|
2019-06-08 21:51:04 +00:00
|
|
|
"Initiates the OAuth"
|
2019-01-11 22:10:58 +00:00
|
|
|
[request]
|
2019-02-14 02:26:33 +00:00
|
|
|
(let [reee (mo/authorize-api-uri)]
|
2019-06-08 15:43:48 +00:00
|
|
|
(log/debug "authorize uri: " reee)
|
2019-02-14 02:26:33 +00:00
|
|
|
(-> reee
|
2019-02-14 03:44:26 +00:00
|
|
|
found)))
|
2019-01-11 22:10:58 +00:00
|
|
|
|
|
|
|
(defn oauth-callback
|
2019-01-14 22:41:10 +00:00
|
|
|
"Handles the callback from adams with the access_token
|
|
|
|
Fetches the user from the database, creating a new one if not found
|
|
|
|
Sets the user in the session and redirects back to origin \"/\" "
|
2019-06-08 21:51:04 +00:00
|
|
|
|
|
|
|
[{:keys [params session]}]
|
2019-01-11 22:10:58 +00:00
|
|
|
; oauth request was denied by user
|
|
|
|
(if (:denied params)
|
|
|
|
(-> (found "/")
|
|
|
|
(assoc :flash {:denied true}))
|
|
|
|
; fetch the request token and do anything else you wanna do if not denied.
|
2019-06-08 21:51:04 +00:00
|
|
|
(let [{:keys [access_token refresh_token]} (mo/get-authentication-response nil params)
|
2019-06-08 17:45:39 +00:00
|
|
|
fetched-user (mo/get-user-info access_token)
|
|
|
|
local-user (db/get-zeus-user {:zeusid (:id fetched-user)})]
|
|
|
|
(if local-user
|
|
|
|
(set-user! local-user session "/")
|
|
|
|
(try
|
|
|
|
(let [user-template {:name (:username fetched-user)
|
|
|
|
:gender nil
|
|
|
|
:zeusid (:id fetched-user)}
|
|
|
|
generated-key (-> user-template
|
|
|
|
(db/create-user!))]
|
|
|
|
(set-user! (assoc user-template :id (:generated_key generated-key)) session "/"))
|
|
|
|
(catch Exception e
|
|
|
|
(do
|
|
|
|
(log/warn "fetched user" fetched-user "already exists, but was not found")
|
|
|
|
(log/warn (:cause (Throwable->map e)))
|
|
|
|
(-> (found "/")
|
|
|
|
(assoc :flash {:error "An error occurred, please try again."})))))))))
|
2019-01-11 22:10:58 +00:00
|
|
|
|
2019-01-14 01:39:40 +00:00
|
|
|
;(catch [:status 401] _
|
|
|
|
; (error-page {:status 401
|
|
|
|
; :title "Error authenticating"
|
|
|
|
; :message "Please contact your system administrator to fix this issue"}))
|
2019-01-11 22:10:58 +00:00
|
|
|
|
2019-06-08 17:45:39 +00:00
|
|
|
; TODO catch using
|
|
|
|
;(defn multiple-status-endpoint [req]
|
|
|
|
; (let [resp (do-external-request req)]
|
|
|
|
; (condp = (:status resp)
|
|
|
|
; 201 (println ok)
|
|
|
|
; 401 (println error))))
|
|
|
|
|
2019-01-11 22:10:58 +00:00
|
|
|
|