Skip to content

Commit 55aed08

Browse files
committed
Fix Rubocop errors
1 parent 36ca8e8 commit 55aed08

File tree

6 files changed

+67
-57
lines changed

6 files changed

+67
-57
lines changed

Gemfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source 'https://rubygems.org'
33
gemspec
44

55
gem 'gem-release'
6-
gem 'rake'
76
gem 'jwt'
7+
gem 'rake'
88

99
group :development do
1010
gem 'dotenv'
@@ -19,8 +19,8 @@ group :test do
1919
gem 'listen', '~> 3.1.5'
2020
gem 'rack-test'
2121
gem 'rspec', '~> 3.5'
22-
gem 'rubocop', '>= 0.30', platforms: [
23-
:ruby_19, :ruby_20, :ruby_21, :ruby_22
22+
gem 'rubocop', '>= 0.30', platforms: %i[
23+
ruby_19 ruby_20 ruby_21 ruby_22
2424
]
2525
gem 'simplecov'
2626
gem 'webmock'

Rakefile

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ begin
1010
RuboCop::RakeTask.new
1111
rescue LoadError
1212
task :rubocop do
13-
$stderr.puts 'Rubocop is disabled'
13+
warn 'Rubocop is disabled'
1414
end
1515
end
1616

@@ -23,7 +23,7 @@ namespace :sinatra do
2323
end
2424

2525
desc 'Run specs'
26-
task default: [:spec, :rubocop]
26+
task default: %i[spec rubocop]
2727
task test: :spec
2828
task :guard do
2929
system 'bundle exec guard'

lib/omniauth/auth0/jwt_validator.rb

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
module OmniAuth
77
module Auth0
8+
# JWT Validator class
89
class JWTValidator
9-
1010
attr_accessor :issuer
1111

1212
# Initializer
@@ -17,7 +17,7 @@ class JWTValidator
1717
def initialize(options)
1818
temp_domain = URI(options.domain)
1919
temp_domain = URI("https://#{options.domain}") unless temp_domain.scheme
20-
@issuer = "#{temp_domain.to_s}/"
20+
@issuer = "#{temp_domain}/"
2121

2222
@client_id = options.client_id
2323
@client_secret = options.client_secret
@@ -31,37 +31,23 @@ def decode(jwt)
3131
head = token_head(jwt)
3232

3333
# Make sure the algorithm is supported and get the decode key.
34+
decode_key = @client_secret
3435
if head[:alg] == 'RS256'
35-
jwks_x5c = jwks_key(:x5c, head[:kid])
36-
raise JWT::VerificationError, :jwks_missing_x5c if jwks_x5c.nil?
37-
decode_key = jwks_public_cert(jwks_x5c.first)
38-
elsif head[:alg] == 'HS256'
39-
decode_key = @client_secret
40-
else
36+
decode_key = rs256_decode_key(head[:kid])
37+
elsif head[:alg] != 'HS256'
4138
raise JWT::VerificationError, :id_token_alg_unsupported
4239
end
4340

44-
# Docs: https://github.com/jwt/ruby-jwt#add-custom-header-fields
45-
decode_options = {
46-
algorithm: head[:alg],
47-
leeway: 30,
48-
verify_expiration: true,
49-
verify_iss: true,
50-
iss: @issuer,
51-
verify_aud: true,
52-
aud: @client_id,
53-
verify_not_before: true
54-
}
55-
5641
# Docs: https://github.com/jwt/ruby-jwt#algorithms-and-usage
57-
JWT.decode(jwt, decode_key, true, decode_options)
42+
JWT.decode(jwt, decode_key, true, decode_opts(head[:alg]))
5843
end
5944

6045
# Get the decoded head segment from a JWT.
6146
# @return hash - The parsed head of the JWT passed, empty hash if not.
6247
def token_head(jwt)
6348
jwt_parts = jwt.split('.')
6449
return {} if blank?(jwt_parts) || blank?(jwt_parts[0])
50+
6551
json_parse(Base64.decode64(jwt_parts[0]))
6652
end
6753

@@ -81,12 +67,36 @@ def jwks_public_cert(x5c)
8167
# @return nil|string
8268
def jwks_key(key, kid)
8369
return nil if blank?(jwks[:keys])
70+
8471
matching_jwk = jwks[:keys].find { |jwk| jwk[:kid] == kid }
8572
matching_jwk[key] if matching_jwk
8673
end
8774

8875
private
8976

77+
# Get the JWT decode options
78+
# Docs: https://github.com/jwt/ruby-jwt#add-custom-header-fields
79+
# @return hash
80+
def decode_opts(alg)
81+
{
82+
algorithm: alg,
83+
leeway: 30,
84+
verify_expiration: true,
85+
verify_iss: true,
86+
iss: @issuer,
87+
verify_aud: true,
88+
aud: @client_id,
89+
verify_not_before: true
90+
}
91+
end
92+
93+
def rs256_decode_key(kid)
94+
jwks_x5c = jwks_key(:x5c, kid)
95+
raise JWT::VerificationError, :jwks_missing_x5c if jwks_x5c.nil?
96+
97+
jwks_public_cert(jwks_x5c.first)
98+
end
99+
90100
# Get a JWKS from the issuer
91101
# @return void
92102
def jwks
@@ -105,7 +115,7 @@ def blank?(obj)
105115
# @param json string - JSON to parse.
106116
# @return hash
107117
def json_parse(json)
108-
JSON.parse(json, {:symbolize_names => true})
118+
JSON.parse(json, symbolize_names: true)
109119
end
110120
end
111121
end

lib/omniauth/strategies/auth0.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module Strategies
99
class Auth0 < OmniAuth::Strategies::OAuth2
1010
option :name, 'auth0'
1111

12-
args [
13-
:client_id,
14-
:client_secret,
15-
:domain
16-
]
12+
args %i[
13+
client_id
14+
client_secret
15+
domain
16+
]
1717

1818
# Setup client URLs used during authentication
1919
def client
@@ -36,16 +36,17 @@ def client
3636
}
3737

3838
if access_token.params
39-
credentials.merge!({
39+
credentials.merge!(
4040
'id_token' => access_token.params['id_token'],
4141
'token_type' => access_token.params['token_type'],
42-
'refresh_token' => access_token.refresh_token,
43-
})
42+
'refresh_token' => access_token.refresh_token
43+
)
4444
end
4545

4646
# Make sure the ID token can be verified and decoded.
4747
auth0_jwt = OmniAuth::Auth0::JWTValidator.new(options)
48-
fail!(:invalid_id_token) unless auth0_jwt.decode(credentials['id_token']).length
48+
jwt_decoded = auth0_jwt.decode(credentials['id_token'])
49+
fail!(:invalid_id_token) unless jwt_decoded.length
4950

5051
credentials
5152
end

spec/omniauth/auth0/jwt_validator_spec.rb

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
require 'jwt'
44

55
describe OmniAuth::Auth0::JWTValidator do
6-
76
#
87
# Reused data
98
#
109

1110
let(:client_id) { 'CLIENT_ID' }
1211
let(:client_secret) { 'CLIENT_SECRET' }
1312
let(:domain) { 'samples.auth0.com' }
14-
let(:future_timecode) { 32503680000 }
15-
let(:past_timecode) { 303912000 }
13+
let(:future_timecode) { 32_503_680_000 }
14+
let(:past_timecode) { 303_912_000 }
1615
let(:jwks_kid) { 'NkJCQzIyQzRBMEU4NjhGNUU4MzU4RkY0M0ZDQzkwOUQ0Q0VGNUMwQg' }
1716

1817
let(:rsa_private_key) do
@@ -24,7 +23,7 @@
2423
keys: [
2524
{
2625
kid: jwks_kid,
27-
x5c: [ Base64.encode64(make_cert(rsa_private_key).to_der) ]
26+
x5c: [Base64.encode64(make_cert(rsa_private_key).to_der)]
2827
}
2928
]
3029
}.to_json
@@ -33,7 +32,7 @@
3332
let(:jwks) do
3433
current_dir = File.dirname(__FILE__)
3534
jwks_file = File.read("#{current_dir}/../../resources/jwks.json")
36-
JSON.parse(jwks_file, {:symbolize_names => true})
35+
JSON.parse(jwks_file, symbolize_names: true)
3736
end
3837

3938
#
@@ -122,7 +121,7 @@
122121
let(:jwt_validator) do
123122
make_jwt_validator
124123
end
125-
124+
126125
before do
127126
stub_jwks
128127
stub_dummy_jwks
@@ -146,7 +145,7 @@
146145

147146
it 'should fail with invalid issuer' do
148147
payload = {
149-
iss: "https://auth0.com/"
148+
iss: 'https://auth0.com/'
150149
}
151150
token = make_hs256_token(payload)
152151
expect do
@@ -178,7 +177,7 @@
178177
it 'should fail with invalid audience' do
179178
payload = {
180179
iss: "https://#{domain}/",
181-
aud: "Auth0"
180+
aud: 'Auth0'
182181
}
183182
token = make_hs256_token(payload)
184183
expect do
@@ -234,10 +233,10 @@
234233
def make_jwt_validator(opt_domain = domain)
235234
options = Struct.new(:domain, :client_id, :client_secret)
236235
OmniAuth::Auth0::JWTValidator.new(options.new(
237-
opt_domain,
238-
client_id,
239-
client_secret
240-
))
236+
opt_domain,
237+
client_id,
238+
client_secret
239+
))
241240
end
242241

243242
def make_hs256_token(payload = nil)
@@ -247,12 +246,12 @@ def make_hs256_token(payload = nil)
247246

248247
def make_rs256_token(payload = nil)
249248
payload = { sub: 'abc123' } if payload.nil?
250-
JWT.encode payload, rsa_private_key, 'RS256', { kid: jwks_kid }
249+
JWT.encode payload, rsa_private_key, 'RS256', kid: jwks_kid
251250
end
252251

253252
def make_cert(private_key)
254253
cert = OpenSSL::X509::Certificate.new
255-
cert.issuer = OpenSSL::X509::Name.parse("/C=BE/O=Auth0/OU=Auth0/CN=Auth0")
254+
cert.issuer = OpenSSL::X509::Name.parse('/C=BE/O=Auth0/OU=Auth0/CN=Auth0')
256255
cert.subject = cert.issuer
257256
cert.not_before = Time.now
258257
cert.not_after = Time.now + 365 * 24 * 60 * 60
@@ -264,12 +263,12 @@ def make_cert(private_key)
264263
ef.subject_certificate = cert
265264
ef.issuer_certificate = cert
266265
cert.extensions = [
267-
ef.create_extension("basicConstraints","CA:TRUE", true),
268-
ef.create_extension("subjectKeyIdentifier", "hash")
266+
ef.create_extension('basicConstraints', 'CA:TRUE', true),
267+
ef.create_extension('subjectKeyIdentifier', 'hash')
269268
]
270269
cert.add_extension ef.create_extension(
271-
"authorityKeyIdentifier",
272-
"keyid:always,issuer:always"
270+
'authorityKeyIdentifier',
271+
'keyid:always,issuer:always'
273272
)
274273

275274
cert.sign private_key, OpenSSL::Digest::SHA1.new
@@ -299,4 +298,4 @@ def stub_dummy_jwks
299298
status: 200
300299
)
301300
end
302-
end
301+
end

spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
2-
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
1+
$LOAD_PATH.unshift File.expand_path(__dir__)
2+
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
33

44
require 'simplecov'
55
if ENV['COVERAGE']

0 commit comments

Comments
 (0)