oauth2 1.4.11 → 2.0.25
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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +956 -85
- data/CITATION.cff +20 -0
- data/CODE_OF_CONDUCT.md +24 -23
- data/CONTRIBUTING.md +265 -34
- data/FUNDING.md +70 -0
- data/LICENSE.md +111 -0
- data/{LICENSE → MIT.md} +6 -7
- data/README.md +1013 -327
- data/RUBOCOP.md +71 -0
- data/SECURITY.md +11 -16
- data/certs/pboling.pem +27 -0
- data/lib/oauth2/access_token.rb +286 -46
- data/lib/oauth2/auth_sanitizer.rb +20 -0
- data/lib/oauth2/authenticator.rb +48 -9
- data/lib/oauth2/client.rb +466 -132
- data/lib/oauth2/error.rb +63 -24
- data/lib/oauth2/filtered_attributes.rb +10 -0
- data/lib/oauth2/response.rb +140 -43
- data/lib/oauth2/strategy/assertion.rb +68 -40
- data/lib/oauth2/strategy/auth_code.rb +25 -4
- data/lib/oauth2/strategy/client_credentials.rb +3 -3
- data/lib/oauth2/strategy/implicit.rb +17 -2
- data/lib/oauth2/strategy/password.rb +14 -4
- data/lib/oauth2/version.rb +2 -59
- data/lib/oauth2.rb +101 -12
- data/sig/oauth2/access_token.rbs +25 -0
- data/sig/oauth2/authenticator.rbs +22 -0
- data/sig/oauth2/client.rbs +52 -0
- data/sig/oauth2/error.rbs +8 -0
- data/sig/oauth2/filtered_attributes.rbs +11 -0
- data/sig/oauth2/response.rbs +18 -0
- data/sig/oauth2/sanitized_logger.rbs +32 -0
- data/sig/oauth2/strategy.rbs +34 -0
- data/sig/oauth2/thing_filter.rbs +10 -0
- data/sig/oauth2/version.rbs +6 -0
- data/sig/oauth2.rbs +9 -0
- data.tar.gz.sig +0 -0
- metadata +409 -83
- metadata.gz.sig +0 -0
- data/lib/oauth2/mac_token.rb +0 -130
data/lib/oauth2/client.rb
CHANGED
|
@@ -1,36 +1,51 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require "faraday"
|
|
4
|
+
require "logger"
|
|
5
|
+
|
|
6
|
+
# simplecov:disable since coverage tracking only runs on the builds with Faraday v2
|
|
7
|
+
# We do run builds on Faraday v0 (and v1!), so this code is actually covered!
|
|
8
|
+
# This is the only nocov in the whole project!
|
|
9
|
+
if Faraday::Utils.respond_to?(:default_space_encoding)
|
|
10
|
+
# This setting doesn't exist in faraday 0.x
|
|
11
|
+
Faraday::Utils.default_space_encoding = "%20"
|
|
12
|
+
end
|
|
13
|
+
# simplecov:enable
|
|
5
14
|
|
|
6
15
|
module OAuth2
|
|
7
16
|
ConnectionError = Class.new(Faraday::ConnectionFailed)
|
|
17
|
+
TimeoutError = Class.new(Faraday::TimeoutError)
|
|
18
|
+
|
|
8
19
|
# The OAuth2::Client class
|
|
9
20
|
class Client # rubocop:disable Metrics/ClassLength
|
|
10
|
-
|
|
21
|
+
RESERVED_REQ_KEYS = %w[body headers params redirect_count].freeze
|
|
22
|
+
RESERVED_PARAM_KEYS = (RESERVED_REQ_KEYS + %w[parse snaky snaky_hash_klass token_method]).freeze
|
|
23
|
+
|
|
24
|
+
include FilteredAttributes
|
|
11
25
|
|
|
12
26
|
attr_reader :id, :secret, :site
|
|
13
27
|
attr_accessor :options
|
|
14
28
|
attr_writer :connection
|
|
29
|
+
filtered_attributes :secret
|
|
15
30
|
|
|
16
|
-
#
|
|
17
|
-
# Client ID and Client Secret registered to your
|
|
18
|
-
# application.
|
|
31
|
+
# Initializes a new OAuth2::Client instance using the Client ID and Client Secret registered to your application.
|
|
19
32
|
#
|
|
20
33
|
# @param [String] client_id the client_id value
|
|
21
34
|
# @param [String] client_secret the client_secret value
|
|
22
|
-
# @param [Hash] options the options to
|
|
35
|
+
# @param [Hash] options the options to configure the client
|
|
23
36
|
# @option options [String] :site the OAuth2 provider site host
|
|
24
|
-
# @option options [String] :
|
|
25
|
-
# @option options [String] :
|
|
26
|
-
# @option options [String] :token_url ('oauth/token') absolute or relative URL path to the Token endpoint
|
|
27
|
-
# @option options [Symbol] :token_method (:post) HTTP method to use to request token (:get
|
|
28
|
-
# @option options [Symbol] :auth_scheme (:basic_auth)
|
|
29
|
-
# @option options [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday
|
|
30
|
-
# @option options [
|
|
31
|
-
# @option options [
|
|
32
|
-
# @option options [Logger] :logger (::Logger.new($stdout))
|
|
33
|
-
# @option options [
|
|
37
|
+
# @option options [String] :authorize_url ('/oauth/authorize') absolute or relative URL path to the Authorization endpoint
|
|
38
|
+
# @option options [String] :revoke_url ('/oauth/revoke') absolute or relative URL path to the Revoke endpoint
|
|
39
|
+
# @option options [String] :token_url ('/oauth/token') absolute or relative URL path to the Token endpoint
|
|
40
|
+
# @option options [Symbol] :token_method (:post) HTTP method to use to request token (:get, :post, :post_with_query_string)
|
|
41
|
+
# @option options [Symbol] :auth_scheme (:basic_auth) the authentication scheme (:basic_auth, :request_body, :tls_client_auth, :private_key_jwt)
|
|
42
|
+
# @option options [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday
|
|
43
|
+
# @option options [Boolean] :raise_errors (true) whether to raise an OAuth2::Error on responses with 400+ status codes
|
|
44
|
+
# @option options [Integer] :max_redirects (5) maximum number of redirects to follow
|
|
45
|
+
# @option options [Logger] :logger (::Logger.new($stdout)) Logger instance for HTTP request/response output; requires OAUTH_DEBUG to be true. When debug logging is enabled, sensitive values are filtered using {OAuth2::AUTH_SANITIZER::SanitizedLogger} initialized from `OAuth2.config[:filtered_label]` and the key names in `OAuth2.config[:filtered_debug_keys]`.
|
|
46
|
+
# @option options [Class] :access_token_class (AccessToken) class to use for access tokens; you can subclass OAuth2::AccessToken, @version 2.0+
|
|
47
|
+
# @option options [Hash] :ssl SSL options for Faraday
|
|
48
|
+
#
|
|
34
49
|
# @yield [builder] The Faraday connection builder
|
|
35
50
|
def initialize(client_id, client_secret, options = {}, &block)
|
|
36
51
|
opts = options.dup
|
|
@@ -38,37 +53,45 @@ module OAuth2
|
|
|
38
53
|
@secret = client_secret
|
|
39
54
|
@site = opts.delete(:site)
|
|
40
55
|
ssl = opts.delete(:ssl)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
warn("OAuth2::Client#initialize argument `extract_access_token` will be removed in oauth2 v3. Refactor to use `access_token_class`.") if opts[:extract_access_token]
|
|
57
|
+
@options = {
|
|
58
|
+
authorize_url: "oauth/authorize",
|
|
59
|
+
revoke_url: "oauth/revoke",
|
|
60
|
+
token_url: "oauth/token",
|
|
61
|
+
token_method: :post,
|
|
62
|
+
auth_scheme: :basic_auth,
|
|
63
|
+
connection_opts: {},
|
|
64
|
+
connection_build: block,
|
|
65
|
+
max_redirects: 5,
|
|
66
|
+
raise_errors: true,
|
|
67
|
+
logger: ::Logger.new($stdout),
|
|
68
|
+
access_token_class: AccessToken,
|
|
69
|
+
}.merge(opts)
|
|
51
70
|
@options[:connection_opts][:ssl] = ssl if ssl
|
|
52
71
|
end
|
|
53
72
|
|
|
54
73
|
# Set the site host
|
|
55
74
|
#
|
|
56
|
-
# @param
|
|
75
|
+
# @param [String] value the OAuth2 provider site host
|
|
76
|
+
# @return [String] the site host value
|
|
57
77
|
def site=(value)
|
|
58
78
|
@connection = nil
|
|
59
79
|
@site = value
|
|
60
80
|
end
|
|
61
81
|
|
|
62
82
|
# The Faraday connection object
|
|
83
|
+
#
|
|
84
|
+
# @return [Faraday::Connection] the initialized Faraday connection
|
|
63
85
|
def connection
|
|
64
86
|
@connection ||=
|
|
65
87
|
Faraday.new(site, options[:connection_opts]) do |builder|
|
|
66
88
|
oauth_debug_logging(builder)
|
|
67
|
-
|
|
68
|
-
|
|
89
|
+
connection_build = options[:connection_build]
|
|
90
|
+
if connection_build
|
|
91
|
+
connection_build.call(builder)
|
|
69
92
|
else
|
|
70
|
-
builder.request
|
|
71
|
-
builder.adapter
|
|
93
|
+
builder.request(:url_encoded) # form-encode POST params
|
|
94
|
+
builder.adapter(Faraday.default_adapter) # make requests with Net::HTTP
|
|
72
95
|
end
|
|
73
96
|
end
|
|
74
97
|
end
|
|
@@ -76,6 +99,7 @@ module OAuth2
|
|
|
76
99
|
# The authorize endpoint URL of the OAuth2 provider
|
|
77
100
|
#
|
|
78
101
|
# @param [Hash] params additional query parameters
|
|
102
|
+
# @return [String] the constructed authorize URL
|
|
79
103
|
def authorize_url(params = {})
|
|
80
104
|
params = (params || {}).merge(redirection_params)
|
|
81
105
|
connection.build_url(options[:authorize_url], params).to_s
|
|
@@ -83,118 +107,173 @@ module OAuth2
|
|
|
83
107
|
|
|
84
108
|
# The token endpoint URL of the OAuth2 provider
|
|
85
109
|
#
|
|
86
|
-
# @param [Hash] params additional query parameters
|
|
110
|
+
# @param [Hash, nil] params additional query parameters
|
|
111
|
+
# @return [String] the constructed token URL
|
|
87
112
|
def token_url(params = nil)
|
|
88
113
|
connection.build_url(options[:token_url], params).to_s
|
|
89
114
|
end
|
|
90
115
|
|
|
116
|
+
# The revoke endpoint URL of the OAuth2 provider
|
|
117
|
+
#
|
|
118
|
+
# @param [Hash, nil] params additional query parameters
|
|
119
|
+
# @return [String] the constructed revoke URL
|
|
120
|
+
def revoke_url(params = nil)
|
|
121
|
+
connection.build_url(options[:revoke_url], params).to_s
|
|
122
|
+
end
|
|
123
|
+
|
|
91
124
|
# Makes a request relative to the specified site root.
|
|
92
125
|
#
|
|
93
|
-
#
|
|
126
|
+
# Updated HTTP 1.1 specification (IETF RFC 7231) relaxed the original constraint (IETF RFC 2616),
|
|
127
|
+
# allowing the use of relative URLs in Location headers.
|
|
128
|
+
#
|
|
129
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.2
|
|
130
|
+
#
|
|
131
|
+
# @param [Symbol] verb one of [:get, :post, :put, :delete]
|
|
94
132
|
# @param [String] url URL path of request
|
|
95
|
-
# @param [Hash]
|
|
96
|
-
# @option
|
|
97
|
-
# @option
|
|
98
|
-
# @option
|
|
99
|
-
# @option
|
|
100
|
-
# code response for this request.
|
|
101
|
-
# @option
|
|
102
|
-
# @
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
raise ConnectionError, e
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
response = Response.new(response, :parse => opts[:parse])
|
|
133
|
+
# @param [Hash] req_opts the options to make the request with
|
|
134
|
+
# @option req_opts [Hash] :params additional query parameters for the URL of the request
|
|
135
|
+
# @option req_opts [Hash, String] :body the body of the request
|
|
136
|
+
# @option req_opts [Hash] :headers http request headers
|
|
137
|
+
# @option req_opts [Boolean] :raise_errors whether to raise an OAuth2::Error on 400+ status
|
|
138
|
+
# code response for this request. Overrides the client instance setting.
|
|
139
|
+
# @option req_opts [Symbol] :parse @see Response::initialize
|
|
140
|
+
# @option req_opts [Boolean] :snaky (true) @see Response::initialize
|
|
141
|
+
#
|
|
142
|
+
# @yield [req] The block is passed the request being made, allowing customization
|
|
143
|
+
# @yieldparam [Faraday::Request] req The request object that can be modified
|
|
144
|
+
# @see Faraday::Connection#run_request
|
|
145
|
+
#
|
|
146
|
+
# @return [OAuth2::Response] the response from the request
|
|
147
|
+
def request(verb, url, req_opts = {}, &block)
|
|
148
|
+
response = execute_request(verb, url, req_opts, &block)
|
|
149
|
+
status = response.status
|
|
116
150
|
|
|
117
|
-
case
|
|
151
|
+
case status
|
|
118
152
|
when 301, 302, 303, 307
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return response if
|
|
153
|
+
redirect_count = (req_opts[:redirect_count] || 0).to_i + 1
|
|
154
|
+
req_opts[:redirect_count] = redirect_count
|
|
155
|
+
return response if redirect_count > options[:max_redirects]
|
|
122
156
|
|
|
123
|
-
if
|
|
157
|
+
if status == 303
|
|
124
158
|
verb = :get
|
|
125
|
-
|
|
159
|
+
req_opts.delete(:body)
|
|
126
160
|
end
|
|
127
|
-
location = response.headers[
|
|
161
|
+
location = response.headers["location"]
|
|
128
162
|
if location
|
|
129
|
-
|
|
163
|
+
current_location = response.response.env.url
|
|
164
|
+
full_location = resolve_redirect_location(current_location, location)
|
|
165
|
+
request(verb, full_location, sanitize_redirect_options(req_opts, current_location, full_location))
|
|
130
166
|
else
|
|
131
167
|
error = Error.new(response)
|
|
132
|
-
raise(error, "Got #{
|
|
168
|
+
raise(error, "Got #{status} status code, but no Location header was present")
|
|
133
169
|
end
|
|
134
170
|
when 200..299, 300..399
|
|
135
|
-
# on non-redirecting 3xx statuses,
|
|
171
|
+
# on non-redirecting 3xx statuses, return the response
|
|
136
172
|
response
|
|
137
173
|
when 400..599
|
|
138
|
-
|
|
139
|
-
|
|
174
|
+
if req_opts.fetch(:raise_errors, options[:raise_errors])
|
|
175
|
+
error = Error.new(response)
|
|
176
|
+
raise(error)
|
|
177
|
+
end
|
|
140
178
|
|
|
141
|
-
response.error = error
|
|
142
179
|
response
|
|
143
180
|
else
|
|
144
181
|
error = Error.new(response)
|
|
145
|
-
raise(error, "Unhandled status code value of #{
|
|
182
|
+
raise(error, "Unhandled status code value of #{status}")
|
|
146
183
|
end
|
|
147
184
|
end
|
|
148
185
|
|
|
149
|
-
#
|
|
186
|
+
# Retrieves an access token from the token endpoint using the specified parameters
|
|
150
187
|
#
|
|
151
|
-
# @param
|
|
152
|
-
#
|
|
153
|
-
#
|
|
154
|
-
#
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
188
|
+
# @param [Hash] params a Hash of params for the token endpoint
|
|
189
|
+
# * params can include a 'headers' key with a Hash of request headers
|
|
190
|
+
# * params can include a 'parse' key with the Symbol name of response parsing strategy (default: :automatic)
|
|
191
|
+
# * params can include a 'snaky' key to control snake_case conversion (default: false)
|
|
192
|
+
# @param [Hash] access_token_opts options that will be passed to the AccessToken initialization
|
|
193
|
+
# @param [Proc] extract_access_token (deprecated) a proc that can extract the access token from the response
|
|
194
|
+
#
|
|
195
|
+
# @yield [opts] The block is passed the options being used to make the request
|
|
196
|
+
# @yieldparam [Hash] opts options being passed to the http library
|
|
197
|
+
#
|
|
198
|
+
# @return [AccessToken, nil] the initialized AccessToken instance, or nil if token extraction fails
|
|
199
|
+
# and raise_errors is false
|
|
200
|
+
#
|
|
201
|
+
# @note The extract_access_token parameter is deprecated and will be removed in oauth2 v3.
|
|
202
|
+
# Use access_token_class on initialization instead.
|
|
203
|
+
#
|
|
204
|
+
# @example
|
|
205
|
+
# client.get_token(
|
|
206
|
+
# 'grant_type' => 'authorization_code',
|
|
207
|
+
# 'code' => 'auth_code_value',
|
|
208
|
+
# 'headers' => {'Authorization' => 'Basic ...'}
|
|
209
|
+
# )
|
|
210
|
+
def get_token(params, access_token_opts = {}, extract_access_token = nil, &block)
|
|
211
|
+
warn("OAuth2::Client#get_token argument `extract_access_token` will be removed in oauth2 v3. Refactor to use `access_token_class` on #initialize.") if extract_access_token
|
|
212
|
+
extract_access_token ||= options[:extract_access_token]
|
|
213
|
+
req_opts = params_to_req_opts(params)
|
|
214
|
+
response = request(http_method, token_url, req_opts, &block)
|
|
215
|
+
|
|
216
|
+
# In v1.4.x, the deprecated extract_access_token option retrieves the token from the response.
|
|
217
|
+
# We preserve this behavior here, but a custom access_token_class that implements #from_hash
|
|
218
|
+
# should be used instead.
|
|
219
|
+
if extract_access_token
|
|
220
|
+
parse_response_legacy(response, access_token_opts, extract_access_token)
|
|
171
221
|
else
|
|
172
|
-
|
|
173
|
-
opts[:headers] = {}
|
|
174
|
-
end
|
|
175
|
-
opts[:headers] = opts[:headers].merge(headers)
|
|
176
|
-
http_method = options[:token_method]
|
|
177
|
-
response = request(http_method, token_url, opts)
|
|
178
|
-
|
|
179
|
-
access_token = begin
|
|
180
|
-
build_access_token(response, access_token_opts, extract_access_token)
|
|
181
|
-
rescue StandardError
|
|
182
|
-
nil
|
|
222
|
+
parse_response(response, access_token_opts)
|
|
183
223
|
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Makes a request to revoke a token at the authorization server
|
|
227
|
+
#
|
|
228
|
+
# @param [String] token The token to be revoked
|
|
229
|
+
# @param [String, nil] token_type_hint A hint about the type of the token being revoked (e.g., 'access_token' or 'refresh_token')
|
|
230
|
+
# @param [Hash] params additional parameters for the token revocation
|
|
231
|
+
# @option params [Symbol] :parse (:automatic) parsing strategy for the response
|
|
232
|
+
# @option params [Boolean] :snaky (true) whether to convert response keys to snake_case
|
|
233
|
+
# @option params [Symbol] :token_method (:post_with_query_string) overrides OAuth2::Client#options[:token_method]
|
|
234
|
+
# @option params [Hash] :headers Additional request headers
|
|
235
|
+
#
|
|
236
|
+
# @yield [req] The block is passed the request being made, allowing customization
|
|
237
|
+
# @yieldparam [Faraday::Request] req The request object that can be modified
|
|
238
|
+
#
|
|
239
|
+
# @return [OAuth2::Response] OAuth2::Response instance
|
|
240
|
+
#
|
|
241
|
+
# @api public
|
|
242
|
+
#
|
|
243
|
+
# @note If the token passed to the request
|
|
244
|
+
# is an access token, the server MAY revoke the respective refresh
|
|
245
|
+
# token as well.
|
|
246
|
+
# @note If the token passed to the request
|
|
247
|
+
# is a refresh token and the authorization server supports the
|
|
248
|
+
# revocation of access tokens, then the authorization server SHOULD
|
|
249
|
+
# also invalidate all access tokens based on the same authorization
|
|
250
|
+
# grant
|
|
251
|
+
# @note If the server responds with HTTP status code 503, your code must
|
|
252
|
+
# assume the token still exists and may retry after a reasonable delay.
|
|
253
|
+
# The server may include a "Retry-After" header in the response to
|
|
254
|
+
# indicate how long the service is expected to be unavailable to the
|
|
255
|
+
# requesting client.
|
|
256
|
+
#
|
|
257
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7009
|
|
258
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
259
|
+
def revoke_token(token, token_type_hint = nil, params = {}, &block)
|
|
260
|
+
params[:token_method] ||= :post_with_query_string
|
|
261
|
+
params[:token] = token
|
|
262
|
+
params[:token_type_hint] = token_type_hint if token_type_hint
|
|
184
263
|
|
|
185
|
-
|
|
186
|
-
response.parsed.is_a?(Hash) &&
|
|
187
|
-
(response.parsed['access_token'] || response.parsed['id_token'])
|
|
188
|
-
)
|
|
264
|
+
req_opts = params_to_req_opts(params)
|
|
189
265
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
raise(error)
|
|
193
|
-
elsif !response_contains_token
|
|
194
|
-
return nil
|
|
195
|
-
end
|
|
266
|
+
request(http_method, revoke_url, req_opts, &block)
|
|
267
|
+
end
|
|
196
268
|
|
|
197
|
-
|
|
269
|
+
# The HTTP Method of the request
|
|
270
|
+
#
|
|
271
|
+
# @return [Symbol] HTTP verb, one of [:get, :post, :put, :delete]
|
|
272
|
+
def http_method
|
|
273
|
+
http_meth = options[:token_method].to_sym
|
|
274
|
+
return :post if http_meth == :post_with_query_string
|
|
275
|
+
|
|
276
|
+
http_meth
|
|
198
277
|
end
|
|
199
278
|
|
|
200
279
|
# The Authorization Code strategy
|
|
@@ -225,6 +304,15 @@ module OAuth2
|
|
|
225
304
|
@client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self)
|
|
226
305
|
end
|
|
227
306
|
|
|
307
|
+
# The Assertion strategy
|
|
308
|
+
#
|
|
309
|
+
# This allows for assertion-based authentication where an identity provider
|
|
310
|
+
# asserts the identity of the user or client application seeking access.
|
|
311
|
+
#
|
|
312
|
+
# @see http://datatracker.ietf.org/doc/html/rfc7521
|
|
313
|
+
# @see http://datatracker.ietf.org/doc/html/draft-ietf-oauth-assertions-01#section-4.1
|
|
314
|
+
#
|
|
315
|
+
# @return [OAuth2::Strategy::Assertion] the initialized Assertion strategy
|
|
228
316
|
def assertion
|
|
229
317
|
@assertion ||= OAuth2::Strategy::Assertion.new(self)
|
|
230
318
|
end
|
|
@@ -235,7 +323,10 @@ module OAuth2
|
|
|
235
323
|
# requesting authorization. If it is provided at authorization time it MUST
|
|
236
324
|
# also be provided with the token exchange request.
|
|
237
325
|
#
|
|
238
|
-
#
|
|
326
|
+
# OAuth 2.1 note: Authorization Servers must compare redirect URIs using exact string matching.
|
|
327
|
+
# This client simply forwards the configured redirect_uri; the exact-match validation happens server-side.
|
|
328
|
+
#
|
|
329
|
+
# Providing :redirect_uri to the OAuth2::Client instantiation will take
|
|
239
330
|
# care of managing this.
|
|
240
331
|
#
|
|
241
332
|
# @api semipublic
|
|
@@ -244,21 +335,181 @@ module OAuth2
|
|
|
244
335
|
# @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3
|
|
245
336
|
# @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.2.1
|
|
246
337
|
# @see https://datatracker.ietf.org/doc/html/rfc6749#section-10.6
|
|
338
|
+
# @see https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-13
|
|
339
|
+
#
|
|
247
340
|
# @return [Hash] the params to add to a request or URL
|
|
248
341
|
def redirection_params
|
|
249
|
-
|
|
250
|
-
|
|
342
|
+
redirect_uri = options[:redirect_uri]
|
|
343
|
+
if redirect_uri
|
|
344
|
+
{"redirect_uri" => redirect_uri}
|
|
251
345
|
else
|
|
252
346
|
{}
|
|
253
347
|
end
|
|
254
348
|
end
|
|
255
349
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
350
|
+
private
|
|
351
|
+
|
|
352
|
+
# Processes request parameters and transforms them into request options
|
|
353
|
+
#
|
|
354
|
+
# @param [Hash] params the request parameters to process
|
|
355
|
+
# @option params [Symbol] :parse (:automatic) parsing strategy for the response
|
|
356
|
+
# @option params [Boolean] :snaky (true) whether to convert response keys to snake_case
|
|
357
|
+
# @option params [Class] :snaky_hash_klass (SnakyHash::StringKeyed) class to use for snake_case hash conversion
|
|
358
|
+
# @option params [Symbol] :token_method (:post) HTTP method to use for token request
|
|
359
|
+
# @option params [Hash] :headers Additional HTTP headers for the request
|
|
360
|
+
#
|
|
361
|
+
# @return [Hash] the processed request options
|
|
362
|
+
#
|
|
363
|
+
# @api private
|
|
364
|
+
def params_to_req_opts(params)
|
|
365
|
+
parse, snaky, snaky_hash_klass, token_method, params, headers = parse_snaky_params_headers(params)
|
|
366
|
+
req_opts = {
|
|
367
|
+
raise_errors: options[:raise_errors],
|
|
368
|
+
token_method: token_method || options[:token_method],
|
|
369
|
+
parse: parse,
|
|
370
|
+
snaky: snaky,
|
|
371
|
+
snaky_hash_klass: snaky_hash_klass,
|
|
372
|
+
}
|
|
373
|
+
if req_opts[:token_method] == :post
|
|
374
|
+
# NOTE: If proliferation of request types continues, we should implement a parser solution for Request,
|
|
375
|
+
# just like we have with Response.
|
|
376
|
+
req_opts[:body] = if headers["Content-Type"] == "application/json"
|
|
377
|
+
params.to_json
|
|
378
|
+
else
|
|
379
|
+
params
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
req_opts[:headers] = {"Content-Type" => "application/x-www-form-urlencoded"}
|
|
383
|
+
else
|
|
384
|
+
req_opts[:params] = params
|
|
385
|
+
req_opts[:headers] = {}
|
|
386
|
+
end
|
|
387
|
+
req_opts[:headers].merge!(headers)
|
|
388
|
+
req_opts
|
|
259
389
|
end
|
|
260
390
|
|
|
261
|
-
|
|
391
|
+
# Processes and transforms parameters for OAuth requests
|
|
392
|
+
#
|
|
393
|
+
# @param [Hash] params the input parameters to process
|
|
394
|
+
# @option params [Symbol] :parse (:automatic) parsing strategy for the response
|
|
395
|
+
# @option params [Boolean] :snaky (true) whether to convert response keys to snake_case
|
|
396
|
+
# @option params [Class] :snaky_hash_klass (SnakyHash::StringKeyed) class to use for snake_case hash conversion
|
|
397
|
+
# @option params [Symbol] :token_method overrides the default token method for this request
|
|
398
|
+
# @option params [Hash] :headers HTTP headers for the request
|
|
399
|
+
#
|
|
400
|
+
# @return [Array<(Symbol, Boolean, Class, Symbol, Hash, Hash)>] Returns an array containing:
|
|
401
|
+
# - parse strategy (Symbol)
|
|
402
|
+
# - snaky flag for response key transformation (Boolean)
|
|
403
|
+
# - hash class for snake_case conversion (Class)
|
|
404
|
+
# - token method override (Symbol, nil)
|
|
405
|
+
# - processed parameters (Hash)
|
|
406
|
+
# - HTTP headers (Hash)
|
|
407
|
+
#
|
|
408
|
+
# @api private
|
|
409
|
+
def parse_snaky_params_headers(params)
|
|
410
|
+
params = params.map do |key, value|
|
|
411
|
+
if RESERVED_PARAM_KEYS.include?(key)
|
|
412
|
+
[key.to_sym, value]
|
|
413
|
+
else
|
|
414
|
+
[key, value]
|
|
415
|
+
end
|
|
416
|
+
end.to_h
|
|
417
|
+
parse = params.key?(:parse) ? params.delete(:parse) : Response::DEFAULT_OPTIONS[:parse]
|
|
418
|
+
snaky = params.key?(:snaky) ? params.delete(:snaky) : Response::DEFAULT_OPTIONS[:snaky]
|
|
419
|
+
snaky_hash_klass = params.key?(:snaky_hash_klass) ? params.delete(:snaky_hash_klass) : Response::DEFAULT_OPTIONS[:snaky_hash_klass]
|
|
420
|
+
token_method = params.delete(:token_method) if params.key?(:token_method)
|
|
421
|
+
params = authenticator.apply(params)
|
|
422
|
+
# authenticator may add :headers, and we separate them from params here
|
|
423
|
+
headers = params.delete(:headers) || {}
|
|
424
|
+
[parse, snaky, snaky_hash_klass, token_method, params, headers]
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# Executes an HTTP request with error handling and response processing
|
|
428
|
+
#
|
|
429
|
+
# @param [Symbol] verb the HTTP method to use (:get, :post, :put, :delete)
|
|
430
|
+
# @param [String] url the URL for the request
|
|
431
|
+
# @param [Hash] opts the request options
|
|
432
|
+
# @option opts [Hash] :body the request body
|
|
433
|
+
# @option opts [Hash] :headers the request headers
|
|
434
|
+
# @option opts [Hash] :params the query parameters to append to the URL
|
|
435
|
+
# @option opts [Symbol, nil] :parse (:automatic) parsing strategy for the response
|
|
436
|
+
# @option opts [Boolean] :snaky (true) whether to convert response keys to snake_case
|
|
437
|
+
#
|
|
438
|
+
# @yield [req] gives access to the request object before sending
|
|
439
|
+
# @yieldparam [Faraday::Request] req the request object that can be modified
|
|
440
|
+
#
|
|
441
|
+
# @return [OAuth2::Response] the response wrapped in an OAuth2::Response object
|
|
442
|
+
#
|
|
443
|
+
# @raise [OAuth2::ConnectionError] when there's a network error
|
|
444
|
+
# @raise [OAuth2::TimeoutError] when the request times out
|
|
445
|
+
#
|
|
446
|
+
# @api private
|
|
447
|
+
def execute_request(verb, url, opts = {})
|
|
448
|
+
url = connection.build_url(url).to_s
|
|
449
|
+
# See: Hash#partition https://bugs.ruby-lang.org/issues/16252
|
|
450
|
+
req_opts, oauth_opts = opts.
|
|
451
|
+
partition { |key, _value| RESERVED_REQ_KEYS.include?(key.to_s) }.
|
|
452
|
+
map(&:to_h)
|
|
453
|
+
|
|
454
|
+
begin
|
|
455
|
+
response = connection.run_request(verb, url, req_opts[:body], req_opts[:headers]) do |req|
|
|
456
|
+
req.params.update(req_opts[:params]) if req_opts[:params]
|
|
457
|
+
yield(req) if block_given?
|
|
458
|
+
end
|
|
459
|
+
rescue Faraday::ConnectionFailed => exception
|
|
460
|
+
raise ConnectionError, exception
|
|
461
|
+
rescue Faraday::TimeoutError => exception
|
|
462
|
+
raise TimeoutError, exception
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
parse = oauth_opts.key?(:parse) ? oauth_opts.delete(:parse) : Response::DEFAULT_OPTIONS[:parse]
|
|
466
|
+
snaky = oauth_opts.key?(:snaky) ? oauth_opts.delete(:snaky) : Response::DEFAULT_OPTIONS[:snaky]
|
|
467
|
+
|
|
468
|
+
Response.new(response, parse: parse, snaky: snaky)
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
def resolve_redirect_location(current_location, location)
|
|
472
|
+
return protocol_relative_redirect_location(current_location, location) if location.respond_to?(:start_with?) && location.start_with?("//")
|
|
473
|
+
|
|
474
|
+
current_location.merge(location)
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
def protocol_relative_redirect_location(current_location, location)
|
|
478
|
+
protocol_relative_location = URI.parse(location)
|
|
479
|
+
authority = +""
|
|
480
|
+
authority << "#{protocol_relative_location.userinfo}@" if protocol_relative_location.userinfo
|
|
481
|
+
authority << protocol_relative_location.host.to_s
|
|
482
|
+
authority << ":#{protocol_relative_location.port}" if protocol_relative_location.port
|
|
483
|
+
|
|
484
|
+
current_location.dup.tap do |safe_location|
|
|
485
|
+
safe_location.path = "///#{authority}#{protocol_relative_location.path}"
|
|
486
|
+
safe_location.query = protocol_relative_location.query if safe_location.respond_to?(:query=)
|
|
487
|
+
safe_location.fragment = protocol_relative_location.fragment if safe_location.respond_to?(:fragment=)
|
|
488
|
+
end
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
def sanitize_redirect_options(req_opts, current_location, next_location)
|
|
492
|
+
return req_opts unless cross_origin_redirect?(current_location, next_location)
|
|
493
|
+
|
|
494
|
+
headers = req_opts[:headers]
|
|
495
|
+
return req_opts unless headers && headers.any? { |key, _value| authorization_header?(key) }
|
|
496
|
+
|
|
497
|
+
safe_opts = req_opts.dup
|
|
498
|
+
safe_headers = headers.dup
|
|
499
|
+
safe_headers.delete_if { |key, _value| authorization_header?(key) }
|
|
500
|
+
safe_opts[:headers] = safe_headers
|
|
501
|
+
safe_opts
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
def authorization_header?(key)
|
|
505
|
+
key.to_s.casecmp("Authorization").zero?
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
def cross_origin_redirect?(current_location, next_location)
|
|
509
|
+
current_location.scheme != next_location.scheme ||
|
|
510
|
+
current_location.host != next_location.host ||
|
|
511
|
+
current_location.port != next_location.port
|
|
512
|
+
end
|
|
262
513
|
|
|
263
514
|
# Returns the authenticator object
|
|
264
515
|
#
|
|
@@ -267,26 +518,109 @@ module OAuth2
|
|
|
267
518
|
Authenticator.new(id, secret, options[:auth_scheme])
|
|
268
519
|
end
|
|
269
520
|
|
|
270
|
-
#
|
|
521
|
+
# Parses the OAuth response and builds an access token using legacy extraction method
|
|
522
|
+
#
|
|
523
|
+
# @deprecated Use {#parse_response} instead
|
|
524
|
+
#
|
|
525
|
+
# @param [OAuth2::Response] response the OAuth2::Response from the token endpoint
|
|
526
|
+
# @param [Hash] access_token_opts options to pass to the AccessToken initialization
|
|
527
|
+
# @param [Proc] extract_access_token proc to extract the access token from response
|
|
528
|
+
#
|
|
529
|
+
# @return [AccessToken, nil] the initialized AccessToken if successful, nil if extraction fails
|
|
530
|
+
# and raise_errors option is false
|
|
531
|
+
#
|
|
532
|
+
# @raise [OAuth2::Error] if response indicates an error and raise_errors option is true
|
|
271
533
|
#
|
|
272
|
-
# @
|
|
273
|
-
def
|
|
274
|
-
|
|
275
|
-
return unless parsed_response.is_a?(Hash)
|
|
534
|
+
# @api private
|
|
535
|
+
def parse_response_legacy(response, access_token_opts, extract_access_token)
|
|
536
|
+
access_token = build_access_token_legacy(response, access_token_opts, extract_access_token)
|
|
276
537
|
|
|
277
|
-
|
|
538
|
+
return access_token if access_token
|
|
278
539
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
extract_access_token.from_hash(self, hash)
|
|
283
|
-
else
|
|
284
|
-
extract_access_token.call(self, hash)
|
|
540
|
+
if options[:raise_errors]
|
|
541
|
+
error = Error.new(response)
|
|
542
|
+
raise(error)
|
|
285
543
|
end
|
|
544
|
+
|
|
545
|
+
nil
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
# Parses the OAuth response and builds an access token using the configured access token class
|
|
549
|
+
#
|
|
550
|
+
# @param [OAuth2::Response] response the OAuth2::Response from the token endpoint
|
|
551
|
+
# @param [Hash] access_token_opts options to pass to the AccessToken initialization
|
|
552
|
+
#
|
|
553
|
+
# @return [AccessToken] the initialized AccessToken instance
|
|
554
|
+
#
|
|
555
|
+
# @raise [OAuth2::Error] if the response is empty/invalid and the raise_errors option is true
|
|
556
|
+
#
|
|
557
|
+
# @api private
|
|
558
|
+
def parse_response(response, access_token_opts)
|
|
559
|
+
access_token_class = options[:access_token_class]
|
|
560
|
+
data = response.parsed
|
|
561
|
+
|
|
562
|
+
unless data.is_a?(Hash) && !data.empty?
|
|
563
|
+
return unless options[:raise_errors]
|
|
564
|
+
|
|
565
|
+
error = Error.new(response)
|
|
566
|
+
raise(error)
|
|
567
|
+
end
|
|
568
|
+
|
|
569
|
+
build_access_token(response, access_token_opts, access_token_class)
|
|
570
|
+
end
|
|
571
|
+
|
|
572
|
+
# Creates an access token instance from response data using the specified token class
|
|
573
|
+
#
|
|
574
|
+
# @param [OAuth2::Response] response the OAuth2::Response from the token endpoint
|
|
575
|
+
# @param [Hash] access_token_opts additional options to pass to the AccessToken initialization
|
|
576
|
+
# @param [Class] access_token_class the class that should be used to create access token instances
|
|
577
|
+
#
|
|
578
|
+
# @return [AccessToken] an initialized AccessToken instance with response data
|
|
579
|
+
#
|
|
580
|
+
# @note If the access token class responds to response=, the full response object will be set
|
|
581
|
+
#
|
|
582
|
+
# @api private
|
|
583
|
+
def build_access_token(response, access_token_opts, access_token_class)
|
|
584
|
+
access_token_class.from_hash(self, response.parsed.merge(access_token_opts)).tap do |access_token|
|
|
585
|
+
access_token.response = response if access_token.respond_to?(:response=)
|
|
586
|
+
end
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
# Builds an access token using a legacy extraction proc
|
|
590
|
+
#
|
|
591
|
+
# @deprecated Use {#build_access_token} instead
|
|
592
|
+
#
|
|
593
|
+
# @param [OAuth2::Response] response the OAuth2::Response from the token endpoint
|
|
594
|
+
# @param [Hash] access_token_opts additional options to pass to the access token extraction
|
|
595
|
+
# @param [Proc] extract_access_token a proc that takes client and token hash as arguments
|
|
596
|
+
# and returns an access token instance
|
|
597
|
+
#
|
|
598
|
+
# @return [AccessToken, nil] the access token instance if extraction succeeds,
|
|
599
|
+
# nil if any error occurs during extraction
|
|
600
|
+
#
|
|
601
|
+
# @api private
|
|
602
|
+
def build_access_token_legacy(response, access_token_opts, extract_access_token)
|
|
603
|
+
extract_access_token.call(self, response.parsed.merge(access_token_opts))
|
|
604
|
+
rescue
|
|
605
|
+
# An error will be raised by the called if nil is returned and options[:raise_errors] is truthy, so this rescue is but temporary.
|
|
606
|
+
# Unfortunately, it does hide the real error, but this is deprecated legacy code,
|
|
607
|
+
# and this was effectively the long-standing pre-existing behavior, so there is little point in changing it.
|
|
608
|
+
nil
|
|
286
609
|
end
|
|
287
610
|
|
|
288
611
|
def oauth_debug_logging(builder)
|
|
289
|
-
|
|
612
|
+
if OAuth2::OAUTH_DEBUG
|
|
613
|
+
config = OAuth2.config
|
|
614
|
+
builder.response(
|
|
615
|
+
:logger,
|
|
616
|
+
OAuth2::AUTH_SANITIZER::SanitizedLogger.new(
|
|
617
|
+
options[:logger],
|
|
618
|
+
filtered_keys: config[:filtered_debug_keys],
|
|
619
|
+
label: config[:filtered_label]
|
|
620
|
+
),
|
|
621
|
+
bodies: true
|
|
622
|
+
)
|
|
623
|
+
end
|
|
290
624
|
end
|
|
291
625
|
end
|
|
292
626
|
end
|