-
-
Save sausman/df58a196b3bc0381b0e8 to your computer and use it in GitHub Desktop.
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
DWOLLA_ACCOUNT_ID= # your Dwolla account id, e.g. "875ab9e7-e211-472c-a7b5-af6c798b041d" | |
DWOLLA_CLIENT_ID= # your App's consumer key | |
DWOLLA_CLIENT_SECRET= # your App's consumer secret | |
DWOLLA_ACCOUNT_REFRESH_TOKEN= # refresh token generated on dwolla.com | |
SECRET_KEY= # a secret used for encrypting oauth tokens |
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
gem "attr_encrypted", "~> 3.0.0" |
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
ActiveSupport::Inflector.inflections(:en) do |inflect| | |
# If you don't add this, Rails will name your model TokenDatum | |
# when you run the command in generate_model.sh | |
inflect.irregular "data", "data" | |
end |
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
rails generate model TokenData \ | |
encrypted_access_token \ | |
encrypted_access_token_iv \ | |
encrypted_refresh_token \ | |
encrypted_refresh_token_iv \ | |
expires_in:integer \ | |
scope \ | |
account_id |
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
class TokenData < ActiveRecord::Base | |
DESIRED_FRESHNESS = 1.minute | |
SECRET_KEY = ENV["SECRET_KEY"] | |
attr_encrypted :access_token, key: SECRET_KEY | |
attr_encrypted :refresh_token, key: SECRET_KEY | |
# look in the token_data table for the most recent token matching the given criteria | |
# if one does not exist throw an `ActiveRecord::RecordNotFound` error | |
# if one does exist convert the `TokenData` to a fresh `DwollaV2::Token` (see `#to_fresh_token`) | |
def self.fresh_token_by! criteria | |
where(criteria) | |
.order(created_at: :desc) | |
.first! | |
.to_fresh_token | |
end | |
def to_fresh_token | |
if expired? | |
# if the token data is expired either refresh the token (account token) or get a new token (app token) | |
account_id? \ | |
? $dwolla.auths.refresh(self) \ | |
: $dwolla.auths.client | |
else | |
# if the token is not expired just convert it to a DwollaV2::Token | |
$dwolla.tokens.new(self) | |
end | |
end | |
private | |
def expired? | |
created_at < Time.now.utc - expires_in.seconds + DESIRED_FRESHNESS | |
end | |
end |
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
$dwolla = DwollaV2::Client.new(id: ENV["DWOLLA_CLIENT_ID"], secret: ENV["DWOLLA_CLIENT_SECRET"]) do |config| | |
# whenever a token is granted, save it to ActiveRecord | |
config.on_grant do |token| | |
TokenData.create! token | |
end | |
end | |
# create an application token if one doesn't already exist | |
begin | |
TokenData.fresh_token_by! account_id: nil | |
rescue ActiveRecord::RecordNotFound => e | |
$dwolla.auths.client # this gets saved in our on_grant callback | |
end | |
# create an account token if one doesn't already exist | |
begin | |
TokenData.fresh_token_by! account_id: ENV["DWOLLA_ACCOUNT_ID"] | |
rescue ActiveRecord::RecordNotFound => e | |
TokenData.create! account_id: ENV["DWOLLA_ACCOUNT_ID"], | |
refresh_token: ENV["DWOLLA_ACCOUNT_REFRESH_TOKEN"], | |
expires_in: -1 | |
end |
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
module TokenConcern | |
extend ActiveSupport::Concern | |
private | |
def account_token | |
@account_token ||= TokenData.fresh_token_by! account_id: ENV["DWOLLA_ACCOUNT_ID"] | |
end | |
def app_token | |
@app_token ||= TokenData.fresh_token_by! account_id: nil | |
end | |
end |
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
class Foo | |
include TokenConcern | |
def bar | |
app_token.get "events" | |
end | |
def baz | |
account_token.get "customers" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for great gist !