Skip to content

Commit 5231b9e

Browse files
committed
✨ silence_no_tokens_warning
1 parent d1bc745 commit 5231b9e

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

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: 24 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,17 @@ 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+
no_tokens_warning(fresh, key)
48+
fresh[:token_name]
49+
else
50+
# Otherwise, if one of the supported default keys is present, use whichever has precedence
51+
supported_keys = TOKEN_KEY_LOOKUP & fresh.keys
52+
t_key = supported_keys[0]
53+
extra_tokens_warning(supported_keys, t_key)
54+
t_key
5155
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
5856
token = fresh.delete(key) || ""
5957
new(client, token, fresh)
6058
end
@@ -77,6 +75,17 @@ def extra_tokens_warning(supported_keys, key)
7775

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

8291
# Initialize an AccessToken
@@ -117,7 +126,7 @@ def initialize(client, token, opts = {})
117126
if @client.options[:raise_errors]
118127
error = Error.new(opts)
119128
raise(error)
120-
else
129+
elsif !OAuth2.config.silence_no_tokens_warning
121130
warn("OAuth2::AccessToken has no token")
122131
end
123132
end

spec/oauth2/access_token_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@
121121
expect(printed).to eq(msg)
122122
end
123123
end
124+
125+
context "with no token keys" do
126+
subject(:printed) do
127+
capture(:stderr) do
128+
target
129+
end
130+
end
131+
132+
before do
133+
@original_sntw = OAuth2.config.silence_no_tokens_warning
134+
OAuth2.config.silence_no_tokens_warning = false
135+
end
136+
137+
after do
138+
OAuth2.config.silence_no_tokens_warning = @original_sntw
139+
end
140+
141+
let(:hash) do
142+
{
143+
blather: "confusing bug here",
144+
rather: token,
145+
}
146+
end
147+
148+
it "warns on STDERR and selects the correct key" do
149+
msg = <<-MSG.lstrip
150+
OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key ([:access_token, :id_token]); using :access_token.
151+
MSG
152+
block_is_expected.to raise_error(OAuth2::Error)
153+
end
154+
end
124155
end
125156

126157
describe "#initialize" do
@@ -314,6 +345,15 @@ def assert_initialized_token(target)
314345
end
315346
end
316347

348+
before do
349+
@original_sntw = OAuth2.config.silence_no_tokens_warning
350+
OAuth2.config.silence_no_tokens_warning = false
351+
end
352+
353+
after do
354+
OAuth2.config.silence_no_tokens_warning = @original_sntw
355+
end
356+
317357
it "warns on STDERR" do
318358
msg = <<-MSG.lstrip
319359
OAuth2::AccessToken has no token
@@ -343,6 +383,15 @@ def assert_initialized_token(target)
343383
context "when there is nil token" do
344384
let(:token) { nil }
345385

386+
before do
387+
@original_sntw = OAuth2.config.silence_no_tokens_warning
388+
OAuth2.config.silence_no_tokens_warning = false
389+
end
390+
391+
after do
392+
OAuth2.config.silence_no_tokens_warning = @original_sntw
393+
end
394+
346395
context "when there is no refresh_token" do
347396
it "does not raise on initialize" do
348397
block_is_expected.not_to raise_error

spec/oauth2_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@
99
subject(:configure) do
1010
described_class.configure do |config|
1111
config.silence_extra_tokens_warning = true
12+
config.silence_no_tokens_warning = true
1213
end
1314
end
1415

1516
before do
1617
described_class.configure do |config|
1718
config.silence_extra_tokens_warning = false
19+
config.silence_no_tokens_warning = false
1820
end
1921
end
2022

2123
after do
2224
described_class.configure do |config|
2325
config.silence_extra_tokens_warning = false
26+
config.silence_no_tokens_warning = false
2427
end
2528
end
2629

2730
it "can change setting of silence_extra_tokens_warning" do
2831
block_is_expected.to change(described_class.config, :silence_extra_tokens_warning).from(false).to(true)
2932
end
33+
34+
it "can change setting of silence_no_tokens_warning" do
35+
block_is_expected.to change(described_class.config, :silence_no_tokens_warning).from(false).to(true)
36+
end
3037
end
3138
end

0 commit comments

Comments
 (0)