-
-
Save telekid/3eff1030756f67237ae1932540f44ef5 to your computer and use it in GitHub Desktop.
token-exchange.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(e/defn ^:private ExchangeAndSaveTokens | |
[emissary-config session-id old-refresh-token] | |
(e/server | |
(let [{:keys [refresh_token id_token refresh_expires_in] :as tokens} | |
(emissary/exchange-tokens emissary-config old-refresh-token)] | |
(when tokens | |
(db/save-session! globals/!db-conn session-id id_token refresh_token refresh_expires_in) | |
(db/get-tokens! globals/db session-id))))) | |
(e/defn GetSessionTokens | |
"Exchange current access and refresh token for new access and refresh token. | |
Returns map containing tokens on success or nil on failure." | |
[emissary-config] | |
(e/server | |
(when-let [session-id (get-in e/*http-request* [:session :emissary/session-id])] | |
(when-let [{:emissary.session/keys [refresh-token expires-at] :as tokens} | |
(db/get-tokens! globals/db session-id)] | |
(let [exp-seconds (int (quot (.getTime expires-at) 1000)) | |
padding 5 ;; Seconds before expiration to refresh | |
next-refresh (- exp-seconds padding) | |
server-time (int (quot e/system-time-ms 1000))] | |
(if (> server-time next-refresh) | |
(ExchangeAndSaveTokens. emissary-config session-id refresh-token) | |
tokens)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment