Monkey patching OAuth2 gem to include Stripe disconnection method

This commit is contained in:
Steve Pettitt
2016-09-29 21:32:27 +01:00
committed by Rob Harrington
parent 7fd8c5956d
commit eed11faa62
3 changed files with 58 additions and 0 deletions

View File

@@ -1,13 +1,19 @@
require File.join(Rails.root, '/lib/oauth2/strategy/deauthorize')
require File.join(Rails.root, '/lib/oauth2/client')
require 'oauth2'
module Admin
module StripeHelper
class << self
attr_accessor :client, :options
end
@options = {
:site => 'https://connect.stripe.com',
:authorize_url => '/oauth/authorize',
:deauthorize_url => '/oauth/deauthorize',
:token_url => '/oauth/token'
}
@client = OAuth2::Client.new(
ENV['STRIPE_CLIENT_ID'],
ENV['STRIPE_INSTANCE_SECRET_KEY'],
@@ -23,5 +29,12 @@ module Admin
# State param will be passed back after auth
StripeHelper.client.auth_code.authorize_url(state: options)
end
def deauthorize_stripe(account_id)
stripe_account = StripeAccount.find(account_id)
if stripe_account
response = StripeHelper.client.deauthorize(stripe_account.stripe_user_id).deauthorize_request
end
end
end
end

21
lib/oauth2/client.rb Normal file
View File

@@ -0,0 +1,21 @@
require 'oauth2'
OAuth2::Client.class_eval do
def deauthorize_url(params = nil)
connection.build_url(options[:deauthorize_url]).to_s
end
def deauthorize(account)
client_object = self.dup
client_object.options[:stripe_user_id] = account
@deauthorize ||= OAuth2::Strategy::Deauthorize.new(client_object)
end
def deauthorize_request(params)
headers = params.delete(:headers)
opts = {}
opts[:body] = params
opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'}
opts[:headers].merge!(headers) if headers
request(:post, deauthorize_url, opts)
end
end

View File

@@ -0,0 +1,24 @@
module OAuth2
module Strategy
# Deauthorization Strategy -- for Stripe
class Deauthorize < Base
# The required query parameters for the authorize URL
#
def deauthorize_params(params = {})
params.merge({ 'client_id' => @client.id,
'stripe_user_id' => @client.options[:stripe_user_id]
})
end
def deauthorize_url(params = {})
@client.deauthorize_url(deauthorize_params.merge(params))
end
def deauthorize_request(params = {})
params = params.merge(deauthorize_params).merge(client_params)
@client.deauthorize_request(params)
end
end
end
end