diff --git a/src/NoteHub/handler.clj b/src/NoteHub/handler.clj index 894a779..56d8cb8 100644 --- a/src/NoteHub/handler.clj +++ b/src/NoteHub/handler.clj @@ -72,66 +72,14 @@ (submit-button {:class "button ui-elem" :id :publish-button} (get-message command))])]))) - -#_ ( - - ; ######## OLD CODE START - -(ns NoteHub.views.pages - (:require ) - (:use - - - [noir.response :only [redirect status content-type]] - [noir.core :only [defpage defpartial]] - [noir.statuses] - [noir.util.crypt :only [encrypt]])) - -; Sets a custom message for each needed HTTP status. -; The message to be assigned is extracted with a dynamically generated key -(doseq [code [400 403 404 500]] - (set-page! code - (let [message (get-message (keyword (str "status-" code)))] - (layout message - [:article [:h1 message]])))) - - +; TODO: make sure the status is really set to the response!!!! (defn- response - "shortcut for rendering an HTTP status" + "Sets a custom message for each needed HTTP status. + The message to be assigned is extracted with a dynamically generated key" [code] - (status code (get-page code))) - -; Routes -; ====== - -(defpage [:post "/post-note"] {:keys [session note signature password version]} - (if (= signature (api/get-signature session note)) - (let [pid "NoteHub" - psk (storage/get-psk pid)] - (if (storage/valid-publisher? pid) - (let [resp (api/post-note note pid (api/get-signature pid psk note) {:password password})] - (if (and - (storage/invalidate-session session) - (get-in resp [:status :success])) - (redirect (:longURL resp)) - (response 400))) - (response 500))) - (response 400))) - -(defpage [:post "/update-note"] {:keys [noteID note password version]} - (let [pid "NoteHub" - psk (storage/get-psk pid)] - (if (storage/valid-publisher? pid) - (let [resp (api/update-note noteID note pid - (api/get-signature pid psk noteID note password) - password)] - (if (get-in resp [:status :success]) - (redirect (:longURL resp)) - (response 403))) - (response 500)))) - -; ###### END OLD CODE -) + (let [message (get-message (keyword (str "status-" code)))] + (layout message + [:article [:h1 message]]))) (defn redirect [url] {:status 302 @@ -199,7 +147,7 @@ (GET "/new" [] (input-form "/post-note" :publish - (html (hidden-field :session (storage/create-session)) + (html (hidden-field :session (storage/sign (str (rand-int Integer/MAX_VALUE)))) (hidden-field {:id :signature} :signature)) (get-message :loading) :set-passwd)) @@ -231,6 +179,31 @@ long-url (if (empty? rest-params) core-url (util/url core-url rest-params))] (redirect long-url)))) + + (POST "/post-note" [session note signature password version] + (if (= signature (storage/sign session note)) + (let [pid "NoteHub" + psk (storage/get-psk pid)] + (if (storage/valid-publisher? pid) + (let [resp (api/post-note note pid (storage/sign pid psk note) {:password password})] + (if (get-in resp [:status :success]) + (redirect (:longURL resp)) + (response 400))) + (response 500))) + (response 400))) + + (POST "/update-note" [noteID note password version] + (let [pid "NoteHub" + psk (storage/get-psk pid)] + (if (storage/valid-publisher? pid) + (let [resp (api/update-note noteID note pid + (storage/sign pid psk noteID note password) + password)] + (if (get-in resp [:status :success]) + (redirect (:longURL resp)) + (response 403))) + (response 500)))) + (route/resources "/") (route/not-found "Not Found")) diff --git a/src/NoteHub/storage.clj b/src/NoteHub/storage.clj index 3bc1187..31720e8 100644 --- a/src/NoteHub/storage.clj +++ b/src/NoteHub/storage.clj @@ -38,16 +38,6 @@ (defn get-psk [pid] (redis :hget :publisher-key pid)) -(defn create-session [] - (let [token (sign (str (rand-int Integer/MAX_VALUE)))] - (redis :sadd :sessions token) - token)) - -(defn invalidate-session [token] - (let [was-valid (redis :sismember :sessions token)] - (redis :srem :sessions token) - (= 1 was-valid))) - (defn edit-note [noteID text] (redis :hset :edited noteID (get-current-date)) (redis :hset :note noteID text)) diff --git a/test/NoteHub/test/storage.clj b/test/NoteHub/test/storage.clj index 98f058e..139c10d 100644 --- a/test/NoteHub/test/storage.clj +++ b/test/NoteHub/test/storage.clj @@ -55,15 +55,6 @@ "update"))) (testing "of the note access" (is (not= (get-note (build-key date test-title)) "any text"))) - (testing "session management" - (let [s1 (create-session) - s2 (create-session) - s3 (create-session)] - (is (invalidate-session s1)) - (is (not (invalidate-session (str s1 s2)))) - (is (invalidate-session s2)) - (is (not (invalidate-session "wrongtoken"))) - (is (invalidate-session s3)))) (testing "of note existence" (is (note-exists? (build-key date test-title))) (is (short-url-exists? test-short-url))