diff --git a/lib/stripe/oauth.rb b/lib/stripe/oauth.rb index 105e8cdd3d..37ca203cdc 100644 --- a/lib/stripe/oauth.rb +++ b/lib/stripe/oauth.rb @@ -1,9 +1,5 @@ module Stripe class OAuth - class << self - attr_accessor :client, :options - end - @options = { :site => 'https://connect.stripe.com', :authorize_url => '/oauth/authorize', @@ -14,38 +10,42 @@ module Stripe @client = OAuth2::Client.new( ENV['STRIPE_CLIENT_ID'], ENV['STRIPE_INSTANCE_SECRET_KEY'], - options + @options ) - def self.authorize_url(enterprise_id, options = {}) - options[:enterprise_id] = enterprise_id - jwt = jwt_encode(options) - # State param will be passed back after auth - client.auth_code.authorize_url(state: jwt, scope: 'read_write') - end + class << self + attr_accessor :client - def self.request_access_token(auth_code) - # Fetch and return the account details from Stripe - client.auth_code.get_token(auth_code).params - end + def authorize_url(enterprise_id, options = {}) + options[:enterprise_id] = enterprise_id + jwt = jwt_encode(options) + # State param will be passed back after auth + client.auth_code.authorize_url(state: jwt, scope: 'read_write') + end - def self.deauthorize(stripe_user_id) - client.deauthorize(stripe_user_id).deauthorize_request - end + def request_access_token(auth_code) + # Fetch and return the account details from Stripe + client.auth_code.get_token(auth_code).params + end - private + def deauthorize(stripe_user_id) + client.deauthorize(stripe_user_id).deauthorize_request + end - def self.secret_token - Openfoodnetwork::Application.config.secret_token - end + private - def self.jwt_encode(payload) - JWT.encode(payload, secret_token, 'HS256') - end + def secret_token + Openfoodnetwork::Application.config.secret_token + end - def self.jwt_decode(token) - # Returns the original payload - JWT.decode(token, secret_token, true, algorithm: 'HS256')[0] + def jwt_encode(payload) + JWT.encode(payload, secret_token, 'HS256') + end + + def jwt_decode(token) + # Returns the original payload + JWT.decode(token, secret_token, true, algorithm: 'HS256')[0] + end end end end diff --git a/spec/lib/stripe/oauth_spec.rb b/spec/lib/stripe/oauth_spec.rb index c7b16ca257..526c1d1167 100644 --- a/spec/lib/stripe/oauth_spec.rb +++ b/spec/lib/stripe/oauth_spec.rb @@ -15,7 +15,7 @@ module Stripe uri = URI.parse(url) params = CGI.parse(uri.query) expect(params.keys).to include 'client_id', 'response_type', 'state', 'scope' - expect(params["state"]).to eq [OAuth.jwt_encode(enterprise_id: enterprise_id)] + expect(params["state"]).to eq [OAuth.send(:jwt_encode, enterprise_id: enterprise_id)] expect(uri.scheme).to eq 'https' expect(uri.host).to eq 'connect.stripe.com' expect(uri.path).to eq '/oauth/authorize'