Skip to content

Commit

Permalink
Added some basic authentication stuff with BrowserID. Not very pretty…
Browse files Browse the repository at this point in the history
… yet.
  • Loading branch information
Raynes committed Dec 28, 2011
1 parent b82c493 commit 0683f5d
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 19 deletions.
3 changes: 2 additions & 1 deletion config.clj
@@ -1,3 +1,4 @@
{:db-name "refheap"
:db-port 27017
:db-host "localhost"}
:db-host "localhost"
:host "localhost"}
3 changes: 2 additions & 1 deletion project.clj
Expand Up @@ -3,6 +3,7 @@
:dependencies [[org.clojure/clojure "1.3.0"]
[noir "1.3.0-alpha2"]
[congomongo "0.1.7"]
[clj-config "0.2.0"]]
[clj-config "0.2.0"]
[clj-http "0.2.6"]]
:main refheap.server)

10 changes: 9 additions & 1 deletion resources/public/js/refheap.js
Expand Up @@ -2,6 +2,14 @@

$(document).ready(function(){
$("#signin").click(function(event) {
alert("SIGN THE FUCK IN.")
navigator.id.getVerifiedEmail(function(assertion) {
if (assertion) {
$.post('/user/verify',
{ assertion: assertion },
function(data) { $("body").html(data)})
} else {
alert("Login failure!")
}
})
})
});
6 changes: 6 additions & 0 deletions src/refheap/config.clj
@@ -0,0 +1,6 @@
(ns refheap.config
(:require [clj-config.core :as cfg]))

(def config
"Some external configuration."
(cfg/safely cfg/read-config "config.clj"))
34 changes: 34 additions & 0 deletions src/refheap/models/login.clj
@@ -0,0 +1,34 @@
(ns refheap.models.login
(:use [refheap.config :only [config]])
(:require [somnium.congomongo :as mongo]
[clj-http.client :as http]
[noir.session :as session]
[cheshire.core :as json]))

(defn create-user [email name]
(let [qmap {:email email
:username name}]
(when-not (mongo/fetch-one :users :where {:email email})
(mongo/insert!
:users
qmap)
(session/put! :user qmap))))

(defn user-exists [email]
(when-let [user (:username
(mongo/fetch-one
:users
:where {:email email}))]
(session/put! :user {:email email
:username user})
user))

(defn verify-assertion [assertion]
(let [verified (json/parse-string
(:body
(http/post "https://browserid.org/verify"
{:query-params {:assertion assertion
:audience (:host config)}}))
true)]
(when (= "okay" (:status verified))
verified)))
8 changes: 2 additions & 6 deletions src/refheap/server.clj
@@ -1,11 +1,7 @@
(ns refheap.server
(:use [refheap.config :only [config]])
(:require [noir.server :as server]
[somnium.congomongo :as mongo]
[clj-config.core :as cfg]))

(def config
"Some external configuration."
(cfg/safely cfg/read-config "config.clj"))
[somnium.congomongo :as mongo]))

(mongo/set-connection!
(mongo/make-connection (config :db-name)
Expand Down
4 changes: 2 additions & 2 deletions src/refheap/views/about.clj
Expand Up @@ -3,7 +3,7 @@
[refheap.views.common :only [layout]]
[hiccup.page-helpers :only [link-to]]))

(def about-page
(defn about-page []
(layout
[:div.written
[:p
Expand All @@ -27,4 +27,4 @@
"If you'd like to take a look at the code and/or contribute, fork it "
(link-to "https://github.com/Raynes/refheap" "on Github!")]]))

(defpage "/about" [] about-page)
(defpage "/about" [] (about-page))
12 changes: 8 additions & 4 deletions src/refheap/views/common.clj
@@ -1,24 +1,28 @@
(ns refheap.views.common
(:use [noir.core :only [defpartial]]
[hiccup.page-helpers :only [include-css include-js link-to]]))
[hiccup.page-helpers :only [include-css include-js link-to]])
(:require [noir.session :as session]))

(defpartial layout [& content]
[:head
[:title "The Refusal Heap"]
(include-css "/css/refheap.css")
(include-css "/css/native.css")
(include-js "/js/jquery-1.7.1.min.js")
(include-js "https://browserid.org/include.js")
(include-js "/js/refheap.js")]
[:body
[:div#header
[:a#site {:href "/paste"} "The Refusal Heap"]
[:div#headerlinks
(link-to "/pastes" "All Pastes")
(link-to "/about" "About")
[:img#signin {:src "/img/browserid.png"}]]]
(if-let [user (and (bound? #'session/*noir-session*)
(session/get :user))]
nil
[:img#signin {:src "/img/browserid.png"}])]]
[:div#content
[:div#container
content]
[:div#container content]
[:div#footer
[:p.centered
"Powered by " (link-to "http://clojure.org" "Clojure") ", "
Expand Down
4 changes: 2 additions & 2 deletions src/refheap/views/home.clj
Expand Up @@ -4,10 +4,10 @@
[noir.statuses :only [set-page!]]
[noir.core :only [defpartial defpage]]))

(def not-found
(defn not-found []
(layout [:p.centered "Just no."]))

(set-page! 404 not-found)
(set-page! 404 (not-found))

(defpage "/" []
(redirect "/paste"))
39 changes: 39 additions & 0 deletions src/refheap/views/login.clj
@@ -0,0 +1,39 @@
(ns refheap.views.login
(:use [hiccup.form-helpers :only [text-field submit-button form-to]]
[refheap.views.common :only [layout]]
[noir.core :only [defpage]]
[noir.response :only [redirect]])
(:require [refheap.models.login :as login]
[noir.session :as session]))

(defn create-user-page [email]
(session/flash-put! :email email)
(layout
(when-let [error (session/flash-get :error)]
[:p.error error])
(form-to
[:post "/user/create"]
[:p "You're almost there! Just enter a username and you'll be on your way."]
(text-field :name)
(submit-button "submit"))))

(defpage [:post "/user/create"] {:keys [name]}
(let [email (session/flash-get :email)]
(if (login/create-user email name)
(redirect "/paste")
(do (session/flash-put! :error "Username already exists.")
(create-user-page email)))))

(defpage [:post "/user/login"] {:keys [email]}
(if-let [username (login/user-exists email)]
(redirect "/paste")
(do (session/flash-put! :email email)
(redirect "/user/create"))))

(defpage [:post "/user/verify"] {:keys [assertion]}
(when-let [{:keys [email]} (login/verify-assertion assertion)]
(prn email)
(if (login/user-exists email)
(do (prn (session/get :user))
(redirect "/paste"))
(create-user-page email))))
3 changes: 1 addition & 2 deletions src/refheap/views/paste.clj
Expand Up @@ -39,8 +39,7 @@
[:br.clear]))))

(defpage "/paste" []
(binding [session/*noir-session* (atom {:user 1})]
(create-paste-page)))
(create-paste-page))

(defpage [:post "/paste/create"] {:keys [paste language private]}
(redirect
Expand Down

0 comments on commit 0683f5d

Please sign in to comment.