Skip to content

Commit fd098bb

Browse files
committed
Merge branch 'feat/silence_no_tokens_warning' into 'main'
✨ silence_no_tokens_warning See merge request oauth-xx/oauth2!646
2 parents d1bc745 + 5e5afbe commit fd098bb

File tree

7 files changed

+284
-127
lines changed

7 files changed

+284
-127
lines changed

.rubocop_gradual.lock

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
[66, 5, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2485198147],
44
[78, 5, 74, "Style/InvertibleUnlessCondition: Prefer `if Gem.rubygems_version >= Gem::Version.new(\"2.7.0\")` over `unless Gem.rubygems_version < Gem::Version.new(\"2.7.0\")`.", 2453573257]
55
],
6-
"lib/oauth2.rb:3563577000": [
7-
[34, 5, 21, "ThreadSafety/ClassAndModuleAttributes: Avoid mutating class and module attributes.", 622027168],
8-
[37, 11, 7, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 651502127]
6+
"lib/oauth2.rb:1956148869": [
7+
[35, 5, 21, "ThreadSafety/ClassAndModuleAttributes: Avoid mutating class and module attributes.", 622027168],
8+
[38, 11, 7, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 651502127]
9+
],
10+
"lib/oauth2/access_token.rb:2233632404": [
11+
[49, 13, 5, "Style/IdenticalConditionalBranches: Move `t_key` out of the conditional.", 183811513],
12+
[55, 13, 5, "Style/IdenticalConditionalBranches: Move `t_key` out of the conditional.", 183811513]
913
],
1014
"lib/oauth2/authenticator.rb:3711266135": [
1115
[42, 5, 113, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 734523108]
@@ -28,11 +32,11 @@
2832
[130, 3, 52, "Gemspec/DependencyVersion: Dependency version specification is required.", 3163430777],
2933
[131, 3, 48, "Gemspec/DependencyVersion: Dependency version specification is required.", 425065368]
3034
],
31-
"spec/oauth2/access_token_spec.rb:2406469319": [
35+
"spec/oauth2/access_token_spec.rb:3473606468": [
3236
[3, 1, 34, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/access_token*_spec.rb`.", 1972107547],
33-
[612, 13, 25, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 770233088],
34-
[682, 9, 101, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3022740639],
35-
[686, 9, 79, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2507338967]
37+
[780, 13, 25, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 770233088],
38+
[850, 9, 101, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3022740639],
39+
[854, 9, 79, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2507338967]
3640
],
3741
"spec/oauth2/authenticator_spec.rb:853320290": [
3842
[3, 1, 36, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/authenticator*_spec.rb`.", 819808017],
@@ -41,26 +45,26 @@
4145
[69, 15, 38, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1480816240],
4246
[79, 13, 23, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2314399065]
4347
],
44-
"spec/oauth2/client_spec.rb:623759711": [
48+
"spec/oauth2/client_spec.rb:2085440011": [
4549
[6, 1, 29, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/client*_spec.rb`.", 439549885],
4650
[174, 7, 492, "RSpec/NoExpectationExample: No expectation found in this example.", 1272021224],
4751
[193, 7, 592, "RSpec/NoExpectationExample: No expectation found in this example.", 3428877205],
4852
[206, 15, 20, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2320605227],
4953
[221, 15, 20, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1276531672],
5054
[236, 15, 43, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1383956904],
5155
[251, 15, 43, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 3376202107],
52-
[865, 5, 360, "RSpec/NoExpectationExample: No expectation found in this example.", 536201463],
53-
[874, 5, 461, "RSpec/NoExpectationExample: No expectation found in this example.", 3392600621],
54-
[885, 5, 340, "RSpec/NoExpectationExample: No expectation found in this example.", 244592251],
55-
[930, 63, 2, "RSpec/BeEq: Prefer `be` over `eq`.", 5860785],
56-
[975, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
57-
[979, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
58-
[987, 7, 89, "RSpec/NoExpectationExample: No expectation found in this example.", 4609419],
59-
[1075, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
60-
[1079, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
61-
[1159, 17, 12, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 664794325],
62-
[1184, 5, 459, "RSpec/NoExpectationExample: No expectation found in this example.", 2216851076],
63-
[1194, 7, 450, "RSpec/NoExpectationExample: No expectation found in this example.", 2619808549]
56+
[829, 5, 360, "RSpec/NoExpectationExample: No expectation found in this example.", 536201463],
57+
[838, 5, 461, "RSpec/NoExpectationExample: No expectation found in this example.", 3392600621],
58+
[849, 5, 340, "RSpec/NoExpectationExample: No expectation found in this example.", 244592251],
59+
[894, 63, 2, "RSpec/BeEq: Prefer `be` over `eq`.", 5860785],
60+
[939, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
61+
[943, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
62+
[951, 7, 89, "RSpec/NoExpectationExample: No expectation found in this example.", 4609419],
63+
[1039, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
64+
[1043, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
65+
[1123, 17, 12, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 664794325],
66+
[1148, 5, 459, "RSpec/NoExpectationExample: No expectation found in this example.", 2216851076],
67+
[1158, 7, 450, "RSpec/NoExpectationExample: No expectation found in this example.", 2619808549]
6468
],
6569
"spec/oauth2/error_spec.rb:1209122273": [
6670
[23, 1, 28, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/error*_spec.rb`.", 3385870076],
@@ -97,7 +101,7 @@
97101
"spec/oauth2/version_spec.rb:1001406821": [
98102
[3, 1, 30, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/version*_spec.rb`.", 1099517182]
99103
],
100-
"spec/oauth2_spec.rb:2292862590": [
104+
"spec/oauth2_spec.rb:4211477230": [
101105
[3, 1, 21, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2*_spec.rb`.", 3359091140]
102106
]
103107
}

CHANGELOG.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ and this project adheres to [Semantic Versioning v2](https://semver.org/spec/v2.
1010
### Fixed
1111
### Removed
1212

13-
## [2.0.10] - 2025-05-12 ([tag][2.0.10t])
13+
## [2.0.10] - 2025-05-XX ([tag][2.0.10t])
1414
### Added
1515
- [!635](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/635) - `.gitlab-ci.yml` file (@jessieay)
16-
- [!643](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/643) - Add token_name option (@pboling)
17-
- Specify the parameter name that identifies the access token
18-
- [!642](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/642) - 20 year certificate for signing gem releases, expires 2045-04-29 (@pboling)
19-
- Gemspec metadata (@pboling)
20-
- funding_uri
21-
- news_uri
22-
- mailing_list_uri
23-
- SHA256 and SHA512 Checksums for release (@pboling)
2416
- [#638](https://gitlab.com/oauth-xx/oauth2/-/issues/638) - Documentation of support for ILO Fundamental Principles of Rights at Work
17+
- [!642](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/642) - 20 year certificate for signing gem releases, expires 2045-04-29 (@pboling)
18+
- Gemspec metadata (@pboling)
19+
- funding_uri
20+
- news_uri
21+
- mailing_list_uri
22+
- SHA256 and SHA512 Checksums for release (@pboling)
23+
- [!643](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/643) - Add `token_name` option (@pboling)
24+
- Specify the parameter name that identifies the access token
25+
- [!645](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/645) - Add `OAuth2::OAUTH_DEBUG` constant, based on `ENV["OAUTH_DEBUG"] (@pboling)
26+
- [!646](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/646) - Add `OAuth2.config.silence_extra_tokens_warning`, default: false (@pboling)
2527
### Changed
28+
- Default value of `OAuth2.config.silence_extra_tokens_warning` was `false`, now `true`
2629
- Gem releases are now cryptographically signed, with a 20-year cert (@pboling)
2730
- Allow linux distros to build release without signing, as their package managers sign independently
2831
### Fixed

lib/oauth2.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module OAuth2
2828
OAUTH_DEBUG = ENV.fetch("OAUTH_DEBUG", "false").casecmp("true").zero?
2929
DEFAULT_CONFIG = SnakyHash::SymbolKeyed.new(
3030
silence_extra_tokens_warning: true,
31+
silence_no_tokens_warning: true,
3132
)
3233
@config = DEFAULT_CONFIG.dup
3334
class << self

lib/oauth2/access_token.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class << self
3131
# 'access_token', 'id_token', 'token' (or their symbolic versions)
3232
# @note If multiple token keys are present, a warning will be issued unless
3333
# OAuth2.config.silence_extra_tokens_warning is true
34+
# @note If no token keys are present, a warning will be issued unless
35+
# OAuth2.config.silence_no_tokens_warning is true
3436
# @note For "soon-to-expire"/"clock-skew" functionality see the `:expires_latency` option.
3537
# @mote If snaky key conversion is being used, token_name needs to match the converted key.
3638
#
@@ -40,21 +42,18 @@ class << self
4042
def from_hash(client, hash)
4143
fresh = hash.dup
4244
# If token_name is present, then use that key name
43-
if fresh.key?(:token_name)
44-
key = fresh[:token_name]
45-
if key.nil? || !fresh.key?(key)
46-
warn(%[
47-
OAuth2::AccessToken#from_hash key mismatch.
48-
Custom token_name (#{key}) does match any keys (#{fresh.keys})
49-
You may need to set `snaky: false`. See inline documentation for more info.
50-
])
45+
key =
46+
if fresh.key?(:token_name)
47+
t_key = fresh[:token_name]
48+
no_tokens_warning(fresh, t_key)
49+
t_key
50+
else
51+
# Otherwise, if one of the supported default keys is present, use whichever has precedence
52+
supported_keys = TOKEN_KEY_LOOKUP & fresh.keys
53+
t_key = supported_keys[0]
54+
extra_tokens_warning(supported_keys, t_key)
55+
t_key
5156
end
52-
else
53-
# Otherwise, if one of the supported default keys is present, use whichever has precedence
54-
supported_keys = TOKEN_KEY_LOOKUP & fresh.keys
55-
key = supported_keys[0]
56-
extra_tokens_warning(supported_keys, key)
57-
end
5857
token = fresh.delete(key) || ""
5958
new(client, token, fresh)
6059
end
@@ -77,6 +76,17 @@ def extra_tokens_warning(supported_keys, key)
7776

7877
warn("OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key (#{supported_keys}); using #{key.inspect}.")
7978
end
79+
80+
def no_tokens_warning(hash, key)
81+
return if OAuth2.config.silence_no_tokens_warning
82+
return if key && hash.key?(key)
83+
84+
warn(%[
85+
OAuth2::AccessToken#from_hash key mismatch.
86+
Custom token_name (#{key}) is not found in (#{hash.keys})
87+
You may need to set `snaky: false`. See inline documentation for more info.
88+
])
89+
end
8090
end
8191

8292
# Initialize an AccessToken
@@ -117,7 +127,7 @@ def initialize(client, token, opts = {})
117127
if @client.options[:raise_errors]
118128
error = Error.new(opts)
119129
raise(error)
120-
else
130+
elsif !OAuth2.config.silence_no_tokens_warning
121131
warn("OAuth2::AccessToken has no token")
122132
end
123133
end

0 commit comments

Comments
 (0)