Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d499c58

Browse files
authored
431/fix post json for token (#629)
* 📝 Document release 2.0.7 Signed-off-by: Peter Boling <peter.boling@gmail.com> * 🐛 Allow POST of JSON to get token - Fixes #431 - Thanks @terracatta Signed-off-by: Peter Boling <peter.boling@gmail.com> * ♻️ Refactor method names that are too long Signed-off-by: Peter Boling <peter.boling@gmail.com> * 🔖 Prepare release 2.0.7 Signed-off-by: Peter Boling <peter.boling@gmail.com> * ♻️ Small Client refactor Signed-off-by: Peter Boling <peter.boling@gmail.com> * 📝 Document impact of fix Signed-off-by: Peter Boling <peter.boling@gmail.com> Signed-off-by: Peter Boling <peter.boling@gmail.com>
1 parent 5e20fdd commit d499c58

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

‎CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format (since v2) is based on [Keep a Changelog v1](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning v2](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.0.7] - 2022年08月22日
8+
### Added
9+
- [#629](https://github.com/oauth-xx/oauth2/pull/629) - Allow POST of JSON to get token (@pboling, @terracatta)
10+
11+
### Fixed
12+
- [#626](https://github.com/oauth-xx/oauth2/pull/626) - Fixes a regression in 2.0.6. Will now prefer the key order from the lookup, not the hash keys (@rickselby)
13+
- Note: This fixes compatibility with `omniauth-oauth2` and AWS
14+
- [#625](https://github.com/oauth-xx/oauth2/pull/625) - Fixes the printed version in the post install message (@hasghari)
15+
716
## [2.0.6] - 2022年07月13日
817
### Fixed
918
- [#624](https://github.com/oauth-xx/oauth2/pull/624) - Fixes a [regression](https://github.com/oauth-xx/oauth2/pull/623) in v2.0.5, where an error would be raised in refresh_token flows due to (legitimate) lack of access_token (@pboling)

‎lib/oauth2/client.rb

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,46 +157,50 @@ def request(verb, url, opts = {}, &block)
157157
def get_token(params, access_token_opts = {}, extract_access_token = nil, &block)
158158
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
159159
extract_access_token ||= options[:extract_access_token]
160-
params = params.map do |key, value|
161-
if RESERVED_PARAM_KEYS.include?(key)
162-
[key.to_sym, value]
163-
else
164-
[key, value]
165-
end
166-
end.to_h
167-
168-
parse = params.key?(:parse) ? params.delete(:parse) : Response::DEFAULT_OPTIONS[:parse]
169-
snaky = params.key?(:snaky) ? params.delete(:snaky) : Response::DEFAULT_OPTIONS[:snaky]
160+
parse, snaky, params, headers = parse_snaky_params_headers(params)
170161

171162
request_opts = {
172163
raise_errors: options[:raise_errors],
173164
parse: parse,
174165
snaky: snaky,
175166
}
176-
params = authenticator.apply(params)
177-
headers = params.delete(:headers) || {}
178167
if options[:token_method] == :post
179-
request_opts[:body] = params
168+
169+
# NOTE: If proliferation of request types continues we should implement a parser solution for Request,
170+
# just like we have with Response.
171+
request_opts[:body] = if headers['Content-Type'] == 'application/json'
172+
params.to_json
173+
else
174+
params
175+
end
176+
180177
request_opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'}
181178
else
182179
request_opts[:params] = params
183180
request_opts[:headers] = {}
184181
end
185182
request_opts[:headers].merge!(headers)
186-
http_method = options[:token_method]
187-
http_method = :post if http_method == :post_with_query_string
188183
response = request(http_method, token_url, request_opts, &block)
189184

190185
# In v1.4.x, the deprecated extract_access_token option retrieves the token from the response.
191186
# We preserve this behavior here, but a custom access_token_class that implements #from_hash
192187
# should be used instead.
193188
if extract_access_token
194-
parse_response_with_legacy_extract(response, access_token_opts, extract_access_token)
189+
parse_response_legacy(response, access_token_opts, extract_access_token)
195190
else
196191
parse_response(response, access_token_opts)
197192
end
198193
end
199194

195+
# The HTTP Method of the request
196+
# @return [Symbol] HTTP verb, one of :get, :post, :put, :delete
197+
def http_method
198+
http_meth = options[:token_method].to_sym
199+
return :post if http_meth == :post_with_query_string
200+
201+
http_meth
202+
end
203+
200204
# The Authorization Code strategy
201205
#
202206
# @see http://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-15#section-4.1
@@ -255,6 +259,22 @@ def redirection_params
255259

256260
private
257261

262+
def parse_snaky_params_headers(params)
263+
params = params.map do |key, value|
264+
if RESERVED_PARAM_KEYS.include?(key)
265+
[key.to_sym, value]
266+
else
267+
[key, value]
268+
end
269+
end.to_h
270+
parse = params.key?(:parse) ? params.delete(:parse) : Response::DEFAULT_OPTIONS[:parse]
271+
snaky = params.key?(:snaky) ? params.delete(:snaky) : Response::DEFAULT_OPTIONS[:snaky]
272+
params = authenticator.apply(params)
273+
# authenticator may add :headers, and we remove them here
274+
headers = params.delete(:headers) || {}
275+
[parse, snaky, params, headers]
276+
end
277+
258278
def execute_request(verb, url, opts = {})
259279
url = connection.build_url(url).to_s
260280

@@ -282,8 +302,8 @@ def authenticator
282302
Authenticator.new(id, secret, options[:auth_scheme])
283303
end
284304

285-
def parse_response_with_legacy_extract(response, access_token_opts, extract_access_token)
286-
access_token = build_access_token_legacy_extract(response, access_token_opts, extract_access_token)
305+
def parse_response_legacy(response, access_token_opts, extract_access_token)
306+
access_token = build_access_token_legacy(response, access_token_opts, extract_access_token)
287307

288308
return access_token if access_token
289309

@@ -321,7 +341,7 @@ def build_access_token(response, access_token_opts, access_token_class)
321341
# Builds the access token from the response of the HTTP call with legacy extract_access_token
322342
#
323343
# @return [AccessToken] the initialized AccessToken
324-
def build_access_token_legacy_extract(response, access_token_opts, extract_access_token)
344+
def build_access_token_legacy(response, access_token_opts, extract_access_token)
325345
extract_access_token.call(self, response.parsed.merge(access_token_opts))
326346
rescue StandardError
327347
nil

‎lib/oauth2/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module OAuth2
44
module Version
5-
VERSION = '2.0.6'.freeze
5+
VERSION = '2.0.7'.freeze
66
end
77
end

‎spec/oauth2/client_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,15 @@
583583
client.get_token({})
584584
end
585585

586+
it 'authenticates with JSON' do
587+
client = stubbed_client(auth_scheme: :basic_auth) do |stub|
588+
stub.post('/oauth/token') do |env|
589+
[200, {'Content-Type' => 'application/json'}, JSON.dump('access_token' => 'the-token')]
590+
end
591+
end
592+
client.get_token(headers: {'Content-Type' => 'application/json'})
593+
end
594+
586595
it 'sets the response object on the access token' do
587596
client = stubbed_client do |stub|
588597
stub.post('/oauth/token') do
@@ -901,6 +910,10 @@ def stubbed_client(params = {}, &stubs)
901910
end
902911
end
903912

913+
it 'instantiates an HTTP Method with this client' do
914+
expect(subject.http_method).to be_kind_of(Symbol)
915+
end
916+
904917
it 'instantiates an AuthCode strategy with this client' do
905918
expect(subject.auth_code).to be_kind_of(OAuth2::Strategy::AuthCode)
906919
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /