Skip to content

Instantly share code, notes, and snippets.

@sausman

sausman/1__.env Secret

Last active May 30, 2019 14:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sausman/df58a196b3bc0381b0e8 to your computer and use it in GitHub Desktop.
Save sausman/df58a196b3bc0381b0e8 to your computer and use it in GitHub Desktop.
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
gem "attr_encrypted", "~> 3.0.0"
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
rails generate model TokenData \
encrypted_access_token \
encrypted_access_token_iv \
encrypted_refresh_token \
encrypted_refresh_token_iv \
expires_in:integer \
scope \
account_id
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
$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
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
class Foo
include TokenConcern
def bar
app_token.get "events"
end
def baz
account_token.get "customers"
end
end
@alexey
Copy link

alexey commented Sep 21, 2017

Thank you for great gist !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment