Skip to content

Commit

Permalink
use environment instead of setup page - much simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
kornysietsma committed Jun 4, 2011
1 parent 263f64d commit 18e0ba6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 80 deletions.
14 changes: 0 additions & 14 deletions resources/public/index.html
Expand Up @@ -34,20 +34,6 @@ <h1>Example Clojure Twitter Application</h1>
</section>
</section>

<script type="text/html" class="view-template" data-name="noinit">
<p>Hi folks - if you can see this message, it means that the Heroku application has been restarted
after being idle for too long. Sadly, as this application stores state in memory,
the application can't be used after re-starting unless I re-enter Twitter credentials.</p>
<p>If you <b>really</b> want to see it in action, contact me (<a href="http://korny.info">Korny</a>) and ask me to re-enter the credentials. Though there's not a lot to see, the <a href="https://github.com/kornysietsma/twitter-example">code</a> is more interesting than the app.</p>
<hr/>
<p>Not initialized! Please enter key and secret for a twitter application valid for this domain:</p>
<form id="initialize">
<p><label for="key">Key:</label><input id="key" type="text"/></p>
<p><label for="secret">Secret:</label><input id="secret" type="text"/></p>
<input type="submit" />
</form>
</script>

<script type="text/html" class="view-template" data-name="noauth">
<p>Not authorized - please <a href="{{authUrl}}" class="auth">Authorize in Twitter</a></p>
</script>
Expand Down
22 changes: 2 additions & 20 deletions resources/public/javascript/twitter-example.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 27 additions & 35 deletions src/twitter_example/core.clj
Expand Up @@ -4,7 +4,8 @@
ring.middleware.json-params
ring.middleware.session
sandbar.stateful-session
[ring.adapter.jetty :only [run-jetty]])
[ring.adapter.jetty :only [run-jetty]]
clojure.contrib.except)
(:require [compojure.route :as route]
[compojure.handler :as handler]
[clj-json.core :as json]
Expand All @@ -13,23 +14,6 @@
[ring.util.response :as response]
twitter))

; config is constructed once, via client posting to /initialize.json
; - client detects the need for config from a failed (401) auth request with :initialized = false
(def config (atom nil))

(defn consumer
"twitter consumer from global config"
{:pre [@config]}
[] (:consumer @config))

(def oauth-response-path "/twitter_oauth_response")

(defn oauth-response-callback
"the url on our site that Twitter should redirect users back to"
{:pre [@config]}
[]
(str "http://" (:host @config) ":" (:port @config) oauth-response-path))

(defn make-consumer
"construct a Twitter consumer"
[key secret]
Expand All @@ -39,6 +23,28 @@
"https://api.twitter.com/oauth/authorize"
:hmac-sha1))

(def oauth-response-path "/twitter_oauth_response")

(def config
(let [env (System/getenv)
key (get env "TWITTER_KEY")
secret (get env "TWITTER_SECRET")
host (get env "CALLBACK_HOST")
port (get env "CALLBACK_PORT")
callback (str "http://" host ":" port oauth-response-path)]
(println key secret host port)
(throw-if-not (and key secret host port) "Not all required environment variables set")
{:consumer (make-consumer key secret)
:callback callback}))

(defn consumer
"twitter consumer from global config"
[] (:consumer config))

(defn oauth-response-callback
"the url on our site that Twitter should redirect users back to"
[] (:callback config))

(defn twitter-request-token
"fetch request token from twitter to start the oauth authorization dance"
[]
Expand Down Expand Up @@ -71,13 +77,6 @@

(defroutes main-routes
(GET "/" [] (resource "public/index.html"))
(POST "/initialize.json" [key secret :as request]
(if @config
(-> (json-response {:message "already initialized!"}) (response/status 500))
(let [{:keys [server-name server-port]} request
consumer (make-consumer key secret)]
(reset! config {:host server-name :port server-port :consumer consumer})
(json-response {:ok true}))))
(GET oauth-response-path [oauth_token oauth_verifier]
(let [request-token (session-get :request-token)
resp (access-token-response request-token oauth_verifier)]
Expand All @@ -102,19 +101,12 @@
[handler]
(fn [request]
(if (re-matches #"/auth/.*" (:uri request))
(cond
(nil? @config)
(->
(json-response {:initialized false, :authorized false})
(response/status 401))
(session-get :twitter-oauth)
(let [oauth (session-get :twitter-oauth)]
(handler (assoc request :twitter-oauth oauth)))
:else
(if-let [oauth (session-get :twitter-oauth)]
(handler (assoc request :twitter-oauth oauth))
(let [request-token (twitter-request-token)
auth-url (callback-uri request-token)]
(session-put! :request-token request-token)
(-> (json-response {:initialized true, :authorized false, :authUrl auth-url})
(-> (json-response {:authUrl auth-url})
(response/status 401))))
(handler request))))

Expand Down
13 changes: 2 additions & 11 deletions views/coffee/twitter-example.coffee
Expand Up @@ -9,12 +9,6 @@ class TwitterExample
setup_bindings: ->
$("#initialize").live("submit", (event) => @initialize(event))

initialize: (event) ->
event.preventDefault()
data = { key: $("#key").val(), secret: $("#secret").val() }
result = $.post "/initialize.json", data, => @check_status()
result.fail => @show_error("unexpected error", result)

check_status: ->
result = $.getJSON "/auth/status.json"
result.done (data) =>
Expand All @@ -33,13 +27,10 @@ class TwitterExample
auth_error: (data) ->
if data.status == 401
payload = JSON.parse(data.responseText)
unless payload? and payload.initialized? and payload.authorized? # note checking existence of flags, not value!
unless payload? and payload.authUrl?
@show_error("Unexpected error payload:",{payload: payload, response: data})
else
if payload.initialized
@render_view(@outputElement,"noauth", payload)
else
@render_view(@outputElement,"noinit")
@render_view(@outputElement,"noauth", payload)
else
@show_error("Unexpected error:", data)

Expand Down

0 comments on commit 18e0ba6

Please sign in to comment.