Skip to content

Commit

Permalink
Support refreshing and validate expiry time
Browse files Browse the repository at this point in the history
Implement refresh method, support expiry time checking in is-valid
  • Loading branch information
Ian Barber committed Sep 16, 2013
1 parent 7f3e7f2 commit ff61382
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/gapi/auth.clj
Expand Up @@ -39,16 +39,18 @@
params)

(defn is-valid
"Returns true if the authentication is valid"
"Returns true if the authentication is valid, and in date."
[state]
(if (state :authtoken)
true ;; TODO: check expiry
(if (@state :token)
(< (System/currentTimeMillis) (@state :expires))
false))

(defn generate-auth-url
"Retrieve a URL suitable for redirecting the user to for auth permissions.
Scopes should be supplied as a vector of required scopes."
[state scopes]
Scopes should be supplied as a vector of required scopes. An optional third
param is a map with access_type and approval_prompt keys."
([state scopes] (generate-auth-url state scopes {:access_type "offline" :approval_prompt "auto"}))
([state scopes opts]
(let [
oauth2-state (generate-state)
params [
Expand All @@ -57,11 +59,11 @@
(encode "scope" (string/join " " scopes))
(encode "state" oauth2-state)
"response_type=code"
"access_type=offline"
"approval_promp=auto"
(encode "access_type" (opts :access_type))
(encode "approval_prompt" (opts :approval_prompt))
]]
(swap! state assoc :state oauth2-state)
(str auth_url "?" (string/join "&" params))))
(str auth_url "?" (string/join "&" params)))))

(defn exchange-token
"Handle the user response from the oauth flow and retrieve a valid
Expand All @@ -87,7 +89,21 @@

(defn refresh-token
"Generate a new authentication token using the refresh token"
[state] true)
[state]
(if (@state :refresh)
(let [params [
(encode "client_id" (@state :client_id))
(encode "client_secret" (@state :client_secret))
(encode "refresh_token" (@state :refresh))
"grant_type=refresh_token"
]
http_resp (http/post token_url {:body (string/join "&" params)
:content-type "application/x-www-form-urlencoded"})
resp (json/read-json (http_resp :body))]
(swap! state assoc :token (resp :access_token)
:expires (+ (System/currentTimeMillis) (* (resp :expires_in) 1000)))
true)
false))

(defn- encode
"Combine the key and value with an = and URL encode each part"
Expand All @@ -98,8 +114,8 @@
"Generate a random string for the state"
[]
(let [buff (make-array Byte/TYPE 10)]
(-> (java.security.SecureRandom.)
(.nextBytes buff))
(-> (org.apache.commons.codec.binary.Base64.)
(.encode buff)
(String.))))
(-> (java.security.SecureRandom.)
(.nextBytes buff))
(-> (org.apache.commons.codec.binary.Base64.)
(.encode buff)
(String.))))

0 comments on commit ff61382

Please sign in to comment.