any_auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE +21 -0
- data/README.md +518 -0
- data/lib/any_auth/auth.rb +21 -0
- data/lib/any_auth/configuration.rb +55 -0
- data/lib/any_auth/controller_helpers.rb +72 -0
- data/lib/any_auth/info.rb +9 -0
- data/lib/any_auth/pkce.rb +12 -0
- data/lib/any_auth/request.rb +17 -0
- data/lib/any_auth/strategies/adversus.rb +12 -0
- data/lib/any_auth/strategies/apple.rb +63 -0
- data/lib/any_auth/strategies/base.rb +90 -0
- data/lib/any_auth/strategies/developer.rb +30 -0
- data/lib/any_auth/strategies/economic.rb +12 -0
- data/lib/any_auth/strategies/google.rb +45 -0
- data/lib/any_auth/strategies/hubspot.rb +38 -0
- data/lib/any_auth/strategies/microsoft.rb +46 -0
- data/lib/any_auth/strategies/oauth1.rb +122 -0
- data/lib/any_auth/strategies/oauth2.rb +141 -0
- data/lib/any_auth/strategies/open_id_connect.rb +77 -0
- data/lib/any_auth/strategies/relatel.rb +28 -0
- data/lib/any_auth/strategies/saml.rb +118 -0
- data/lib/any_auth/strategies/shopify.rb +62 -0
- data/lib/any_auth/strategies/signicat.rb +73 -0
- data/lib/any_auth/strategies/tavle.rb +32 -0
- data/lib/any_auth/version.rb +5 -0
- data/lib/any_auth.rb +81 -0
- metadata +292 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module ControllerHelpers
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def redirect_to_provider(strategy, redirect_uri:, params: {})
|
|
8
|
+
auth_request = strategy.request_phase(redirect_uri:)
|
|
9
|
+
warn_about_plain_http if Rails.env.production? && !request.ssl?
|
|
10
|
+
stored = {
|
|
11
|
+
'provider' => strategy.provider,
|
|
12
|
+
'request' => auth_request.session,
|
|
13
|
+
'attributes' => strategy.attributes,
|
|
14
|
+
'params' => params,
|
|
15
|
+
'expires_at' => 10.minutes.from_now.to_i,
|
|
16
|
+
}
|
|
17
|
+
cookies.encrypted[any_auth_cookie_name] = {
|
|
18
|
+
value: stored, expires: 10.minutes, httponly: true, **any_auth_cookie_options
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
redirect_to auth_request.url, allow_other_host: true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def authenticate_with_provider(strategy)
|
|
25
|
+
stored = any_auth_cookie(strategy)
|
|
26
|
+
delete_any_auth_cookie
|
|
27
|
+
|
|
28
|
+
attributes = stored.fetch('attributes', {})
|
|
29
|
+
strategy.assign_attributes(attributes.slice(*strategy.class.attribute_names))
|
|
30
|
+
auth = strategy.callback_phase(callback_params, stored.fetch('request', {}))
|
|
31
|
+
auth.with(params: stored.fetch('params', {}))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def any_auth_params(strategy)
|
|
35
|
+
any_auth_cookie(strategy).fetch('params', {})
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def delete_any_auth_cookie
|
|
39
|
+
cookies.delete(any_auth_cookie_name, **any_auth_cookie_options)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def callback_params
|
|
43
|
+
request.query_parameters.merge(request.request_parameters)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def any_auth_cookie(strategy)
|
|
47
|
+
stored = cookies.encrypted[any_auth_cookie_name]
|
|
48
|
+
return {} unless stored.is_a?(Hash) && stored['provider'] == strategy.provider
|
|
49
|
+
return {} unless Time.at(stored['expires_at'].to_i).future?
|
|
50
|
+
|
|
51
|
+
stored
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def warn_about_plain_http
|
|
55
|
+
Rails.logger.warn 'AnyAuth: a sign-in started over plain HTTP, so its cookie is not ' \
|
|
56
|
+
'Secure and POST callbacks (Apple, SAML) cannot find it. Behind a TLS ' \
|
|
57
|
+
'proxy, set config.assume_ssl = true.'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def any_auth_cookie_name
|
|
61
|
+
return '__Host-any_auth' if request.ssl?
|
|
62
|
+
|
|
63
|
+
'any_auth'
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def any_auth_cookie_options
|
|
67
|
+
return { secure: true, same_site: :none } if request.ssl?
|
|
68
|
+
|
|
69
|
+
{ same_site: :lax }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
PKCE = Data.define(:verifier, :challenge) do
|
|
5
|
+
def self.pair
|
|
6
|
+
verifier = SecureRandom.urlsafe_base64(32)
|
|
7
|
+
challenge = Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)
|
|
8
|
+
|
|
9
|
+
new(verifier:, challenge:)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
Request = Data.define(:url, :session) do
|
|
5
|
+
def inspect
|
|
6
|
+
"#<data #{self.class.name} url=#{url.inspect}>"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def pretty_print(printer)
|
|
10
|
+
printer.text(inspect)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def as_json(*)
|
|
14
|
+
to_h.except(:session).as_json
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module Strategies
|
|
5
|
+
class Apple < OpenIDConnect
|
|
6
|
+
ISSUER = 'https://appleid.apple.com'
|
|
7
|
+
|
|
8
|
+
self.pkce = false
|
|
9
|
+
|
|
10
|
+
attr_accessor :team_id, :key_id, :private_key
|
|
11
|
+
|
|
12
|
+
def request_phase(redirect_uri:, scope: self.scope || 'email name', **params)
|
|
13
|
+
super(redirect_uri:, scope:, **params, response_mode: 'form_post')
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def callback_phase(params, session)
|
|
17
|
+
auth = super
|
|
18
|
+
|
|
19
|
+
auth.with(info: auth.info.with(name: full_name(params['user'])))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def client_secret
|
|
23
|
+
claims = {
|
|
24
|
+
iss: team_id,
|
|
25
|
+
sub: client_id,
|
|
26
|
+
aud: ISSUER,
|
|
27
|
+
iat: Time.current.to_i,
|
|
28
|
+
exp: 5.minutes.from_now.to_i,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
JWT.encode(claims, OpenSSL::PKey.read(private_key), 'ES256', { kid: key_id })
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def client_options
|
|
35
|
+
{
|
|
36
|
+
site: ISSUER,
|
|
37
|
+
authorize_url: '/auth/authorize',
|
|
38
|
+
token_url: '/auth/token',
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def valid_issuer?(claims)
|
|
45
|
+
claims['iss'] == ISSUER
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def info_from(claims)
|
|
49
|
+
Info.new(email: claims['email'], email_verified: claims['email_verified'].to_s == 'true')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def full_name(user)
|
|
53
|
+
user = JSON.parse(user.to_s)
|
|
54
|
+
name = user['name'] if user.is_a?(Hash)
|
|
55
|
+
return unless name.is_a?(Hash)
|
|
56
|
+
|
|
57
|
+
name.values_at('firstName', 'lastName').compact_blank.join(' ').presence
|
|
58
|
+
rescue JSON::ParserError
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module Strategies
|
|
5
|
+
class Base
|
|
6
|
+
include ActiveModel::Model
|
|
7
|
+
include ActiveModel::Attributes
|
|
8
|
+
|
|
9
|
+
attr_writer :provider
|
|
10
|
+
|
|
11
|
+
def self.provider
|
|
12
|
+
name.demodulize.underscore
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.fields?
|
|
16
|
+
attribute_names.any?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def provider
|
|
20
|
+
@provider || self.class.provider
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def expired?(_credentials)
|
|
24
|
+
false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def refresh(credentials)
|
|
28
|
+
credentials
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def inspect
|
|
32
|
+
"#<#{self.class.name} provider=#{provider.inspect}>"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def secure_compare(expected, given)
|
|
38
|
+
expected.present? && ActiveSupport::SecurityUtils.secure_compare(expected.to_s, given.to_s)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def error_from(status, body, headers = {})
|
|
42
|
+
code = error_code(body)
|
|
43
|
+
|
|
44
|
+
error_class_for(code, status).new(
|
|
45
|
+
"#{provider} returned #{code || status || 'an error'}",
|
|
46
|
+
code:,
|
|
47
|
+
description: error_description(body),
|
|
48
|
+
retry_after: retry_after(headers),
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def error_code(body)
|
|
53
|
+
body['error']
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def error_description(body)
|
|
57
|
+
body['error_description']
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def error_class_for(code, status)
|
|
61
|
+
case code
|
|
62
|
+
when 'invalid_client', 'invalid_request',
|
|
63
|
+
'unsupported_grant_type', 'unsupported_response_type'
|
|
64
|
+
ConfigurationError
|
|
65
|
+
when 'server_error', 'temporarily_unavailable'
|
|
66
|
+
Unavailable
|
|
67
|
+
else
|
|
68
|
+
error_class_for_status(status)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def error_class_for_status(status)
|
|
73
|
+
case status
|
|
74
|
+
when nil, 400, 401, 403 then AccessDenied
|
|
75
|
+
else Unavailable
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def retry_after(headers)
|
|
80
|
+
value = headers['retry-after'].to_s
|
|
81
|
+
return if value.blank?
|
|
82
|
+
return value.to_i if value.match?(/\A\d+\z/)
|
|
83
|
+
|
|
84
|
+
[(Time.httpdate(value) - Time.current).ceil, 0].max
|
|
85
|
+
rescue ArgumentError
|
|
86
|
+
nil
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module Strategies
|
|
5
|
+
class Developer < Base
|
|
6
|
+
attribute :email, :string
|
|
7
|
+
|
|
8
|
+
validates :email, presence: true
|
|
9
|
+
|
|
10
|
+
def request_phase(redirect_uri:)
|
|
11
|
+
session = { 'state' => SecureRandom.hex(24) }
|
|
12
|
+
home = URI(redirect_uri)
|
|
13
|
+
home.query = [home.query, { state: session['state'] }.to_query].compact.join('&')
|
|
14
|
+
|
|
15
|
+
Request.new(url: home.to_s, session:)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def callback_phase(params, session)
|
|
19
|
+
state = session['state']
|
|
20
|
+
raise InvalidCallback, 'Invalid state' unless secure_compare(state, params['state'])
|
|
21
|
+
raise InvalidCallback, 'Missing email' unless valid?
|
|
22
|
+
|
|
23
|
+
address = email.strip.downcase
|
|
24
|
+
info = Info.new(email: address, email_verified: true)
|
|
25
|
+
|
|
26
|
+
Auth.new(provider:, uid: address, credentials: {}, info:)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module Strategies
|
|
5
|
+
class Google < OpenIDConnect
|
|
6
|
+
authorize_option :access_type, :prompt, :hd, :login_hint
|
|
7
|
+
|
|
8
|
+
def client_options
|
|
9
|
+
{
|
|
10
|
+
site: 'https://www.googleapis.com',
|
|
11
|
+
authorize_url: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
12
|
+
token_url: 'https://oauth2.googleapis.com/token',
|
|
13
|
+
}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def info_from(claims)
|
|
19
|
+
Info.new(
|
|
20
|
+
name: claims['name'],
|
|
21
|
+
email: claims['email'],
|
|
22
|
+
email_verified: email_verified?(claims),
|
|
23
|
+
image: claims['picture'],
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def valid_claims?(claims, session)
|
|
28
|
+
super && (hd.blank? || claims['hd'] == hd)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def valid_issuer?(claims)
|
|
32
|
+
claims['iss'].in?(%w[https://accounts.google.com accounts.google.com])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def email_verified?(claims)
|
|
36
|
+
claims['email_verified'] == true && google_hosted?(claims)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def google_hosted?(claims)
|
|
40
|
+
claims['hd'].present? ||
|
|
41
|
+
claims['email'].to_s.downcase.end_with?('@gmail.com', '@googlemail.com')
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module Strategies
|
|
5
|
+
class Hubspot < OAuth2
|
|
6
|
+
def client_options
|
|
7
|
+
{
|
|
8
|
+
site: 'https://api.hubspot.com',
|
|
9
|
+
authorize_url: 'https://app.hubspot.com/oauth/authorize',
|
|
10
|
+
token_url: '/oauth/2026-09/token',
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def identity(access_token, _session)
|
|
17
|
+
token_info = introspect(access_token.token)
|
|
18
|
+
raise AccessDenied, "#{provider} returned no user" unless named_user?(token_info)
|
|
19
|
+
|
|
20
|
+
{
|
|
21
|
+
uid: token_info['user_id'].to_s,
|
|
22
|
+
info: Info.new(email: token_info['user']),
|
|
23
|
+
raw: token_info.except('token'),
|
|
24
|
+
}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def introspect(token)
|
|
28
|
+
body = { client_id:, client_secret:, token_type_hint: 'access_token', token: }
|
|
29
|
+
|
|
30
|
+
parsed { client.request(:post, '/oauth/2026-09/token/introspect', body:, snaky: false) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def named_user?(token_info)
|
|
34
|
+
token_info['active'] == true && token_info['user_id'].present?
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AnyAuth
|
|
4
|
+
module Strategies
|
|
5
|
+
class Microsoft < OpenIDConnect
|
|
6
|
+
authorize_option :prompt, :login_hint, :domain_hint
|
|
7
|
+
|
|
8
|
+
def client_options
|
|
9
|
+
{
|
|
10
|
+
site: 'https://login.microsoftonline.com',
|
|
11
|
+
authorize_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
|
12
|
+
token_url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
|
|
13
|
+
}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def identity_from(claims)
|
|
19
|
+
{ uid: "#{claims['tid']}:#{fetch_claim(claims, 'oid')}", info: info_from(claims) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def info_from(claims)
|
|
23
|
+
Info.new(
|
|
24
|
+
name: claims['name'],
|
|
25
|
+
email: claims['email'],
|
|
26
|
+
email_verified: email_verified?(claims),
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def valid_issuer?(claims)
|
|
31
|
+
tenant = claims['tid'].to_s
|
|
32
|
+
|
|
33
|
+
tenant.match?(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) &&
|
|
34
|
+
claims['iss'] == "https://login.microsoftonline.com/#{tenant}/v2.0"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def email_verified?(claims)
|
|
38
|
+
!personal_account?(claims) && claims['xms_edov'].to_s.in?(%w[true 1])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def personal_account?(claims)
|
|
42
|
+
claims['tid'] == '9188040d-6c67-4c5b-b112-36a304b66dad'
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
begin
|
|
4
|
+
gem 'oauth', '>= 1.1.6'
|
|
5
|
+
require 'oauth'
|
|
6
|
+
rescue LoadError => e
|
|
7
|
+
raise LoadError, "AnyAuth::Strategies::OAuth1 needs oauth 1.1.6 or later: #{e.message}"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
unless CGI.respond_to?(:parse)
|
|
11
|
+
raise LoadError, 'AnyAuth::Strategies::OAuth1 needs the cgi gem on Ruby 4.0, where CGI.parse ' \
|
|
12
|
+
'left Ruby itself and the oauth gem still calls it. ' \
|
|
13
|
+
'Add gem "cgi" to your Gemfile.'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module AnyAuth
|
|
17
|
+
module Strategies
|
|
18
|
+
class OAuth1 < Base
|
|
19
|
+
attr_accessor :consumer_key, :consumer_secret
|
|
20
|
+
|
|
21
|
+
def request_phase(redirect_uri:)
|
|
22
|
+
request_token = with_error_handling do
|
|
23
|
+
consumer.get_request_token(oauth_callback: redirect_uri)
|
|
24
|
+
end
|
|
25
|
+
raise ConfigurationError, "#{provider} did not confirm the callback" unless
|
|
26
|
+
request_token.callback_confirmed?
|
|
27
|
+
session = {
|
|
28
|
+
'request_token' => request_token.token,
|
|
29
|
+
'request_token_secret' => request_token.secret,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
Request.new(url: request_token.authorize_url, session:)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def callback_phase(params, session)
|
|
36
|
+
verify(params, session)
|
|
37
|
+
access_token = fetch_access_token(params['oauth_verifier'], session)
|
|
38
|
+
credentials = { 'token' => access_token.token, 'secret' => access_token.secret }
|
|
39
|
+
|
|
40
|
+
Auth.new(provider:, credentials:, **identity(access_token, session))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def verify(params, session)
|
|
46
|
+
request_token = session['request_token']
|
|
47
|
+
|
|
48
|
+
raise InvalidCallback, 'No sign-in in progress' if request_token.blank?
|
|
49
|
+
raise AccessDenied, 'Sign-in denied' if secure_compare(request_token, params['denied'])
|
|
50
|
+
raise InvalidCallback, 'Invalid oauth_token' unless valid_token?(request_token, params)
|
|
51
|
+
raise InvalidCallback, 'Missing oauth_verifier' if params['oauth_verifier'].blank?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def valid_token?(request_token, params)
|
|
55
|
+
secure_compare(request_token, params['oauth_token'])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def fetch_access_token(verifier, session)
|
|
59
|
+
request_token = ::OAuth::RequestToken.new(consumer, session['request_token'],
|
|
60
|
+
session['request_token_secret'])
|
|
61
|
+
|
|
62
|
+
with_error_handling { request_token.get_access_token(oauth_verifier: verifier) }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def identity(_access_token, _session)
|
|
66
|
+
{}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def consumer
|
|
70
|
+
::OAuth::Consumer.new(consumer_key, consumer_secret, client_options)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def get(access_token, path)
|
|
74
|
+
response = with_error_handling { access_token.get(path) }
|
|
75
|
+
raise vendor_error(response) unless response.is_a?(Net::HTTPSuccess)
|
|
76
|
+
|
|
77
|
+
body = JSON.parse(response.body)
|
|
78
|
+
raise Unavailable, "#{provider} returned an unreadable response" unless body.is_a?(Hash)
|
|
79
|
+
|
|
80
|
+
body
|
|
81
|
+
rescue JSON::ParserError
|
|
82
|
+
raise Unavailable, "#{provider} returned an unreadable response", cause: nil
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def with_error_handling
|
|
86
|
+
yield
|
|
87
|
+
rescue ::OAuth::Unauthorized => e
|
|
88
|
+
raise vendor_error(e.request), cause: nil
|
|
89
|
+
rescue Net::HTTPExceptions => e
|
|
90
|
+
raise vendor_error(e.response), cause: nil
|
|
91
|
+
rescue Timeout::Error, SystemCallError, SocketError, OpenSSL::SSL::SSLError => e
|
|
92
|
+
raise Unavailable, "#{provider} request failed (#{e.class.name.demodulize})", cause: nil
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def vendor_error(response)
|
|
96
|
+
body = CGI.parse(response.body.to_s).transform_values(&:first)
|
|
97
|
+
|
|
98
|
+
error_from(response.code.to_i, body, response)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def error_code(body)
|
|
102
|
+
body['oauth_problem']
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def error_description(body)
|
|
106
|
+
body['oauth_problem_advice']
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def error_class_for(code, status)
|
|
110
|
+
case code
|
|
111
|
+
when 'consumer_key_unknown', 'consumer_key_rejected',
|
|
112
|
+
'signature_invalid', 'signature_method_rejected'
|
|
113
|
+
ConfigurationError
|
|
114
|
+
when 'consumer_key_refused'
|
|
115
|
+
Unavailable
|
|
116
|
+
else
|
|
117
|
+
super
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|