Much progress

This commit is contained in:
Simon Brooke 2018-06-29 18:39:08 +01:00
parent 4e296537c4
commit 635d1830d3
42 changed files with 105 additions and 95 deletions

View file

@ -49,12 +49,11 @@
:hmac-sha1)))
(db/list-authorities db/*db* {}))))
(def authority!
;; Closure to allow authorities to be created once when the function is first
;; Closure to allow authorities map to be created once when the function is first
;; called. The argument `id` should be a string, the id of some authority
;; known to the database. As side-effect, the key `:authority` is bound in the
;; session to the selected authority.
;; known to the database. As side-effect, the authorities map is bound in the
;; closure.
(let [authorities (atom nil)]
(fn [id]
(if
@ -69,8 +68,7 @@
(if authority
(do
(log/debug (str "Selected authority " id))
(session/put! :authority authority)))
authority))))
authority))))))
(defn oauth-callback-uri
"Generates the oauth request callback URI."
@ -79,23 +77,18 @@
(defn fetch-request-token
"Fetches a request token from the authority implied by this `request`."
[request]
(let [callback-uri (oauth-callback-uri request)
auth-id (:authority (:params request))
auth (authority! auth-id)]
(log/info "Attempting to authorise with authority " auth-id)
(if
auth
(do
(log/info "Fetching request token using callback-uri" callback-uri)
(oauth/request-token auth (oauth-callback-uri request)))
(throw (Exception. (str "No such authority: " auth-id))))))
([request auth]
(let [callback-uri (oauth-callback-uri request)]
(log/info "Fetching request token using callback-uri" callback-uri)
(oauth/request-token auth (oauth-callback-uri request))))
([request]
(fetch-request-token request (:authority (:session request)))))
(defn fetch-access-token
[request_token]
(oauth/access-token (session/get :authority) request_token (:oauth_verifier request_token)))
[request_token authority]
(oauth/access-token authority request_token (:oauth_verifier request_token)))
(defn auth-redirect-uri
"Gets the URI the user should be redirected to when authenticating."
[request-token]
(str (oauth/user-approval-uri (session/get :authority) request-token)))
[request-token authority]
(str (oauth/user-approval-uri authority request-token)))

View file

@ -523,7 +523,7 @@
(defroutes
auto-selmer-routes
(GET
"/index"
"/admin"
request
(route/restricted (apply (resolve-handler "index") (list request))))
(GET

View file

@ -1,6 +1,6 @@
(ns
youyesyet.routes.auto-json
"JSON routes for youyesyet auto-generated by [Application Description Language framework](https://github.com/simon-brooke/adl) at 20180629T141537.425Z"
"JSON routes for youyesyet auto-generated by [Application Description Language framework](https://github.com/simon-brooke/adl) at 20180629T163618.749Z"
(:require
[adl-support.core :as support]
[clojure.java.io :as io]

View file

@ -1,7 +1,8 @@
(ns ^{:doc "Routes/pages available to unauthenticated users."
:author "Simon Brooke"} youyesyet.routes.home
(:require [clojure.walk :refer [keywordize-keys]]
[clojure.java.io :refer [input-stream]]
(:require [clojure.java.io :as io]
[clojure.tools.logging :as log]
[clojure.walk :refer [keywordize-keys]]
[noir.response :as nresponse]
[noir.util.route :as route]
[ring.util.http-response :refer [content-type ok]]
@ -10,7 +11,7 @@
[youyesyet.oauth :as oauth]
[compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :as response]
[clojure.java.io :as io]))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
@ -86,27 +87,41 @@
redirect-to (or (:redirect-to params) "roles")]
(cond
(:authority params)
(oauth/fetch-request-token request)
(let [auth (oauth/authority! (:authority params))]
(if auth
(do
(log/info "Attempting to authorise with authority " (:authority params))
(oauth/fetch-request-token
(assoc request :session (assoc session :authority auth))
auth))
(throw (Exception. (str "No such authority: " (:authority params))))))
;; this is obviously, ABSURDLY, insecure. I don't want to put just-about-good-enough,
;; it-will-do-for-now security in place; instead, I want this to be test code only
;; until we have o-auth properly working.
(and user (= username password))
(assoc (response/found redirect-to) :session (assoc session :user username))
user
(layout/render "login.html" {:title (str "User " username " is unknown") :redirect-to redirect-to})
(layout/render
"login.html"
{:title (str "User " username " is unknown") :redirect-to redirect-to})
true
(layout/render "login.html" {:title "Please log in" :redirect-to redirect-to :authorities (db-core/list-authorities db-core/*db*)}))))
(layout/render
"login.html"
{:title "Please log in"
:redirect-to redirect-to
:authorities (db-core/list-authorities db-core/*db*)}))))
(defroutes home-routes
(GET "/" [] (home-page))
(GET "/js/:file" [file]
(-> (input-stream (str "resources/public/js/" file))
ok
(content-type "text/javascript;charset=UTF-8")))
;; (GET "/js/:file" [file]
;; (-> (io/input-stream (str "resources/public/js/" file))
;; ok
;; (content-type "text/javascript;charset=UTF-8")))
(GET "/home" [] (home-page))
(GET "/about" [] (about-page))
(GET "/roles" request (route/restricted (roles-page request)))
(GET "/canvassers" [] (route/restricted (app-page)))
(GET "/app" [] (route/restricted (app-page)))
(GET "/call-me" [] (call-me-page nil))
(POST "/call-me" request (call-me-page request))

View file

@ -1,18 +1,22 @@
(ns ^{:doc "OAuth authentication routes - not finished, does not work yet."
:author "Simon Brooke"} youyesyet.routes.oauth
(:require [compojure.core :refer [defroutes GET]]
(:require [clojure.tools.logging :as log]
[compojure.core :refer [defroutes GET]]
[ring.util.http-response :refer [ok found]]
[clojure.java.io :as io]
[youyesyet.oauth :as oauth]
[clojure.tools.logging :as log]))
[youyesyet.oauth :as oauth]))
(defn oauth-init
"Initiates the OAuth with the authority implied by this `request`"
[request]
(-> (oauth/fetch-request-token request)
:oauth_token
oauth/auth-redirect-uri
found))
;; (-> (oauth/fetch-request-token request)
;; :oauth_token
;; oauth/auth-redirect-uri
;; found))
(found
(oauth/auth-redirect-uri
(:oauth_token (oauth/fetch-request-token request))
(:authority (:session request)))))
(defn oauth-callback
"Handles the callback from the authority."
@ -22,7 +26,8 @@
(-> (found "/login")
(assoc :flash {:denied true}))
; fetch the request token and do anything else you wanna do if not denied.
(let [{:keys [user_id screen_name]} (oauth/fetch-access-token request_token)]
(let [{:keys [user_id screen_name]}
(oauth/fetch-access-token request_token (:authority session))]
(log/info "successfully authenticated as" user_id screen_name)
(-> (found "/")
(assoc :session