-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State is now localized just call(wake-token-retreiver) Added a new config param for refresh percentage
- Loading branch information
Showing
5 changed files
with
63 additions
and
32 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,30 @@ A Clojure library designed to grab tokens from OAuth 2.0 | |
## Usage | ||
|
||
|
||
```clojure | ||
(:require [funcade/core :as f]) | ||
|
||
... | ||
(def token-store (f/wake-wake-token-retreiver "my-token-key" config)) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tolitius
Contributor
|
||
|
||
... | ||
(def token ((:next-token token-store))) | ||
|
||
... | ||
((:stop token-store)) | ||
|
||
``` | ||
|
||
where config is: | ||
```clojure | ||
{:token-url OAuth 2.0 server url | ||
:grant-type OAuth 2.0 grant type (client_crdentials, implicit, etc) | ||
:client-id OAuth 2.0 client id | ||
:client-secret OAuth 2.0 secret (a hex string) | ||
:scope OAuth 2.0 Scope | ||
:token-headers a map of headers {"Cookie" "foo=bar"} | ||
:refresh-percent (when less than x% of time between expires-at and issued-at remains, refresh the token)} | ||
``` | ||
## License | ||
|
||
Copyright © 2018 shvetsm | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,42 @@ | ||
(ns funcade.core | ||
(:require [clojure.core.async :as a] | ||
[clojure.tools.logging :as log] | ||
[funcade.state :refer [state]] | ||
[funcade.tokens :as t])) | ||
|
||
(defn init-token-channel! | ||
"funcade-params are | ||
{:token-url OAuth 2.0 server url | ||
:grant-type OAuth 2.0 grant type (client_crdentials, implicit, etc) | ||
:client-id OAuth 2.0 client id | ||
:client-secret OAuth 2.0 secret (a hex string) | ||
:scope OAuth 2.0 Scope | ||
:token-headers a map of headers {\"Cookie\" \"foo=bar\"}" | ||
[token-key funcade-params] | ||
(defn- init-token-channel! | ||
[token-key funcade-params token-store] | ||
(let [stop-chan (a/chan 10) | ||
[token err] (a/<!! (t/new-token! funcade-params))] | ||
(if err | ||
(do | ||
(log/error "can't acquire token!" err) | ||
(throw err)) | ||
(do | ||
(swap! state (fn [s] (assoc s token-key token))) | ||
(swap! token-store (fn [s] (assoc s token-key token))) | ||
(t/schedule-token-renewal | ||
(name token-key) | ||
token-key | ||
(partial t/renew-token? 1/24) | ||
(partial t/renew-token? (/ (or (:refresh-percent funcade-params) 10) 100)) | ||
(fn [] (t/new-token! funcade-params)) | ||
stop-chan) | ||
{:token-details (:body token) :stop-chan stop-chan})))) | ||
stop-chan | ||
token-store) | ||
stop-chan)))) | ||
|
||
(defn- stop-token-channel! [stop-chan] | ||
(a/put! stop-chan ::stop)) | ||
|
||
(defn wake-token-retreiver [token-name config] | ||
"config is | ||
{:token-url OAuth 2.0 server url | ||
:grant-type OAuth 2.0 grant type (client_crdentials, implicit, etc) | ||
:client-id OAuth 2.0 client id | ||
:client-secret OAuth 2.0 secret (a hex string) | ||
:scope OAuth 2.0 Scope | ||
:token-headers a map of headers {\"Cookie\" \"foo=bar\"} | ||
:refresh-percent (when less than x% of time between expires-at and issued-at remains, refresh the token)}" | ||
|
||
(let [token-store (atom {}) | ||
stop-channel (init-token-channel! token-name config token-store)] | ||
{:next-token #(:access-token (get @token-store token-name) ) | ||
:stop (partial stop-token-channel! stop-channel)})) | ||
|
||
(defn stop-token-channel! [{:keys [stop-chan]}] | ||
(a/put! stop-chan ::stop)) |
This file was deleted.
Oops, something went wrong.
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
wake wake