Skip to content

Commit

Permalink
Merge branch '628/silence_extra_tokens_warning' into 'main'
Browse files Browse the repository at this point in the history
🐛 New global configuration option OAuth2.config.silence_extra_tokens_warning (default: false)

Closes #628

See merge request oauth-xx/oauth2!631
  • Loading branch information
pboling committed Sep 1, 2022
2 parents dc717f6 + 62616ce commit 598ccfc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 8 deletions.
15 changes: 11 additions & 4 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-07-13 09:52:51 +0700 using RuboCop version 0.68.1.
# on 2022-09-01 09:04:26 +0700 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -16,14 +16,14 @@ Metrics/AbcSize:
Metrics/BlockLength:
Max: 35

# Offense count: 5
# Offense count: 4
Metrics/CyclomaticComplexity:
Max: 12

# Offense count: 10
# Offense count: 11
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 34
Max: 28

# Offense count: 3
Metrics/PerceivedComplexity:
Expand All @@ -37,3 +37,10 @@ RSpec/ContextWording:
- 'spec/oauth2/access_token_spec.rb'
- 'spec/oauth2/authenticator_spec.rb'
- 'spec/oauth2/client_spec.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle.
# SupportedStyles: inline, group
Style/AccessModifierDeclarations:
Exclude:
- 'lib/oauth2.rb'
7 changes: 5 additions & 2 deletions CHANGELOG.md
Expand Up @@ -10,9 +10,12 @@ and this project adheres to [Semantic Versioning v2](https://semver.org/spec/v2.
### Fixed
### Removed

## [2.0.8] - 2022-08-30
## [2.0.8] - 2022-09-01
### Changed
- Extract snaky_hash to external dependency (@pboling)
- [!630](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/630) - Extract snaky_hash to external dependency (@pboling)

### Added
- [!631](https://gitlab.com/oauth-xx/oauth2/-/merge_requests/631) - New global configuration option OAuth2.config.silence_extra_tokens_warning (default: false) fixes [#628](https://gitlab.com/oauth-xx/oauth2/-/issues/628)

## [2.0.7] - 2022-08-22
### Added
Expand Down
9 changes: 9 additions & 0 deletions lib/oauth2.rb
Expand Up @@ -24,6 +24,15 @@

# The namespace of this library
module OAuth2
DEFAULT_CONFIG = SnakyHash::SymbolKeyed.new(silence_extra_tokens_warning: false)
@config = DEFAULT_CONFIG.dup
class << self
attr_accessor :config
end
def configure
yield @config
end
module_function :configure
end

OAuth2::Version.class_eval do
Expand Down
13 changes: 11 additions & 2 deletions lib/oauth2/access_token.rb
Expand Up @@ -20,8 +20,7 @@ def from_hash(client, hash)
fresh = hash.dup
supported_keys = TOKEN_KEY_LOOKUP & fresh.keys
key = supported_keys[0]
# Having too many is sus, and may lead to bugs. Having none is fine (e.g. refresh flow doesn't need a token).
warn("OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key (#{supported_keys}); using #{key.inspect}.") if supported_keys.length > 1
extra_tokens_warning(supported_keys, key)
token = fresh.delete(key)
new(client, token, fresh)
end
Expand All @@ -34,6 +33,16 @@ def from_hash(client, hash)
def from_kvform(client, kvform)
from_hash(client, Rack::Utils.parse_query(kvform))
end

private

# Having too many is sus, and may lead to bugs. Having none is fine (e.g. refresh flow doesn't need a token).
def extra_tokens_warning(supported_keys, key)
return if OAuth2.config.silence_extra_tokens_warning
return if supported_keys.length <= 1

warn("OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key (#{supported_keys}); using #{key.inspect}.")
end
end

# Initialize an AccessToken
Expand Down
24 changes: 24 additions & 0 deletions spec/oauth2/access_token_spec.rb
Expand Up @@ -52,6 +52,30 @@
MSG
expect(printed).to eq(msg)
end

context 'when silenced' do
subject(:printed) do
capture(:stderr) do
target
end
end

before do
OAuth2.configure do |config|
config.silence_extra_tokens_warning = true
end
end

after do
OAuth2.configure do |config|
config.silence_extra_tokens_warning = false
end
end

it 'does not warn on STDERR' do
expect(printed).to eq('')
end
end
end

context 'with keys in a different order to the lookup' do
Expand Down
31 changes: 31 additions & 0 deletions spec/oauth2_spec.rb
@@ -0,0 +1,31 @@
# frozen_string_literal: true

RSpec.describe OAuth2 do
it 'has a default config for silence_extra_tokens_warning' do
expect(described_class.config.silence_extra_tokens_warning).to eq(false)
end

describe '.configure' do
subject(:configure) do
described_class.configure do |config|
config.silence_extra_tokens_warning = true
end
end

before do
described_class.configure do |config|
config.silence_extra_tokens_warning = false
end
end

after do
described_class.configure do |config|
config.silence_extra_tokens_warning = false
end
end

it 'can change setting of silence_extra_tokens_warning' do
block_is_expected.to change(described_class.config, :silence_extra_tokens_warning).from(false).to(true)
end
end
end

0 comments on commit 598ccfc

Please sign in to comment.