/
oauth.rb
64 lines (54 loc) · 2.18 KB
/
oauth.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
module Stripe
module OAuth
module OAuthOperations
extend APIOperations::Request::ClassMethods
def self.execute_resource_request(method, url, params, opts)
opts = Util.normalize_opts(opts)
opts[:client] ||= StripeClient.active_client
opts[:api_base] ||= Stripe.connect_base
super(method, url, params, opts)
end
end
def self.get_client_id(params = {})
client_id = params[:client_id] || Stripe.client_id
unless client_id
raise AuthenticationError, "No client_id provided. " \
'Set your client_id using "Stripe.client_id = <CLIENT-ID>". ' \
"You can find your client_ids in your Stripe dashboard at " \
"https://dashboard.stripe.com/account/applications/settings, " \
"after registering your account as a platform. See " \
"https://stripe.com/docs/connect/standalone-accounts for details, " \
"or email support@stripe.com if you have any questions."
end
client_id
end
def self.authorize_url(params = {}, opts = {})
base = opts[:connect_base] || Stripe.connect_base
path = "/oauth/authorize"
path = "/express" + path if opts[:express]
params[:client_id] = get_client_id(params)
params[:response_type] ||= "code"
query = Util.encode_parameters(params)
"#{base}#{path}?#{query}"
end
def self.token(params = {}, opts = {})
opts = Util.normalize_opts(opts)
opts[:api_key] = params[:client_secret] if params[:client_secret]
resp, opts = OAuthOperations.execute_resource_request(
:post, "/oauth/token", params, opts
)
# This is just going to return a generic StripeObject, but that's okay
Util.convert_to_stripe_object(resp.data, opts)
end
def self.deauthorize(params = {}, opts = {})
opts = Util.normalize_opts(opts)
params[:client_id] = get_client_id(params)
resp, opts = OAuthOperations.execute_resource_request(
:post, "/oauth/deauthorize", params, opts
)
# This is just going to return a generic StripeObject, but that's okay
Util.convert_to_stripe_object(resp.data, opts)
end
end
end