Skip to content

Commit 3f1a899

Browse files
committed
Unify code style and simplify rubocop configuration
1 parent 1d2b671 commit 3f1a899

File tree

16 files changed

+84
-120
lines changed

16 files changed

+84
-120
lines changed

.rubocop.yml

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,17 @@ AllCops:
99
Style/Documentation:
1010
Enabled: false
1111

12-
Style/BlockDelimiters:
13-
Exclude:
14-
- spec/**/*_spec.rb
15-
16-
Style/GuardClause:
17-
Enabled: false
18-
1912
Style/IfUnlessModifier:
2013
Enabled: false
2114

22-
Style/Lambda:
23-
Enabled: false
24-
25-
Style/RaiseArgs:
26-
Enabled: false
27-
2815
Metrics/AbcSize:
2916
Max: 25
3017

3118
Metrics/ClassLength:
32-
Max: 121
33-
34-
Metrics/ModuleLength:
35-
Max: 100
19+
Max: 112
3620

3721
Metrics/MethodLength:
38-
Max: 20
22+
Max: 18
3923

4024
Metrics/BlockLength:
4125
Exclude:
@@ -45,24 +29,5 @@ Metrics/BlockLength:
4529
Layout/LineLength:
4630
Enabled: false
4731

48-
Layout/EndAlignment:
49-
EnforcedStyleAlignWith: variable
50-
51-
Layout/EmptyLineBetweenDefs:
52-
Enabled: true
53-
AllowAdjacentOneLineDefs: true
54-
55-
Style/FormatString:
56-
Enabled: false
57-
58-
Layout/MultilineMethodCallIndentation:
59-
EnforcedStyle: indented
60-
61-
Layout/MultilineOperationIndentation:
62-
EnforcedStyle: indented
63-
64-
Style/WordArray:
65-
Enabled: false
66-
6732
Gemspec/DevelopmentDependencies:
6833
EnforcedStyle: gemspec

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**Fixes and enhancements:**
1212

1313
- Print deprecation warnings only on when token decoding succeeds [#600](https://github.com/jwt/ruby-jwt/pull/600) ([@anakinj](https://github.com/anakinj))
14+
- Unify code style [#602](https://github.com/jwt/ruby-jwt/pull/602) ([@anakinj](https://github.com/anakinj))
1415
- Your contribution here
1516

1617
## [v2.8.1](https://github.com/jwt/ruby-jwt/tree/v2.8.1) (2024-02-29)

lib/jwt/configuration/jwk_configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def kid_generator_type=(value)
1818
JWT::JWK::Thumbprint
1919
else
2020
raise ArgumentError, "#{value} is not a valid kid generator type."
21-
end
21+
end
2222
end
2323

2424
attr_accessor :kid_generator

lib/jwt/decode.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module JWT
1010
# Decoding logic for JWT
1111
class Decode
1212
def initialize(jwt, key, verify, options, &keyfinder)
13-
raise(JWT::DecodeError, 'Nil JSON web token') unless jwt
13+
raise JWT::DecodeError, 'Nil JSON web token' unless jwt
1414

1515
@jwt = jwt
1616
@key = key
@@ -30,7 +30,7 @@ def decode_segments
3030
verify_signature
3131
verify_claims
3232
end
33-
raise(JWT::DecodeError, 'Not enough or too many segments') unless header && payload
33+
raise JWT::DecodeError, 'Not enough or too many segments' unless header && payload
3434

3535
[payload, header]
3636
end
@@ -46,21 +46,21 @@ def verify_signature
4646

4747
return if Array(@key).any? { |key| verify_signature_for?(key) }
4848

49-
raise(JWT::VerificationError, 'Signature verification failed')
49+
raise JWT::VerificationError, 'Signature verification failed'
5050
end
5151

5252
def verify_algo
53-
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
54-
raise(JWT::IncorrectAlgorithm, 'Token is missing alg header') unless alg_in_header
55-
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') if allowed_and_valid_algorithms.empty?
53+
raise JWT::IncorrectAlgorithm, 'An algorithm must be specified' if allowed_algorithms.empty?
54+
raise JWT::IncorrectAlgorithm, 'Token is missing alg header' unless alg_in_header
55+
raise JWT::IncorrectAlgorithm, 'Expected a different algorithm' if allowed_and_valid_algorithms.empty?
5656
end
5757

5858
def set_key
5959
@key = find_key(&@keyfinder) if @keyfinder
6060
@key = ::JWT::JWK::KeyFinder.new(jwks: @options[:jwks], allow_nil_kid: @options[:allow_nil_kid]).key_for(header['kid']) if @options[:jwks]
61-
if (x5c_options = @options[:x5c])
62-
@key = X5cKeyFinder.new(x5c_options[:root_certificates], x5c_options[:crls]).from(header['x5c'])
63-
end
61+
return unless (x5c_options = @options[:x5c])
62+
63+
@key = X5cKeyFinder.new(x5c_options[:root_certificates], x5c_options[:crls]).from(header['x5c'])
6464
end
6565

6666
def verify_signature_for?(key)
@@ -122,7 +122,7 @@ def validate_segment_count!
122122
return if !@verify && segment_length == 2 # If no verifying required, the signature is not needed
123123
return if segment_length == 2 && none_algorithm?
124124

125-
raise(JWT::DecodeError, 'Not enough or too many segments')
125+
raise JWT::DecodeError, 'Not enough or too many segments'
126126
end
127127

128128
def segment_length

lib/jwt/jwk/ec.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,26 @@ def create_ec_key(jwk_crv, jwk_x, jwk_y, jwk_d) # rubocop:disable Metrics/Method
153153
)
154154

155155
sequence = if jwk_d
156-
# https://datatracker.ietf.org/doc/html/rfc5915.html
157-
# ECPrivateKey ::= SEQUENCE {
158-
# version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
159-
# privateKey OCTET STRING,
160-
# parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
161-
# publicKey [1] BIT STRING OPTIONAL
162-
# }
163-
164-
OpenSSL::ASN1::Sequence([
165-
OpenSSL::ASN1::Integer(1),
166-
OpenSSL::ASN1::OctetString(OpenSSL::BN.new(decode_octets(jwk_d), 2).to_s(2)),
167-
OpenSSL::ASN1::ObjectId(curve, 0, :EXPLICIT),
168-
OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed), 1, :EXPLICIT)
169-
])
170-
else
171-
OpenSSL::ASN1::Sequence([
172-
OpenSSL::ASN1::Sequence([OpenSSL::ASN1::ObjectId('id-ecPublicKey'), OpenSSL::ASN1::ObjectId(curve)]),
173-
OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed))
174-
])
175-
end
156+
# https://datatracker.ietf.org/doc/html/rfc5915.html
157+
# ECPrivateKey ::= SEQUENCE {
158+
# version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
159+
# privateKey OCTET STRING,
160+
# parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
161+
# publicKey [1] BIT STRING OPTIONAL
162+
# }
163+
164+
OpenSSL::ASN1::Sequence([
165+
OpenSSL::ASN1::Integer(1),
166+
OpenSSL::ASN1::OctetString(OpenSSL::BN.new(decode_octets(jwk_d), 2).to_s(2)),
167+
OpenSSL::ASN1::ObjectId(curve, 0, :EXPLICIT),
168+
OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed), 1, :EXPLICIT)
169+
])
170+
else
171+
OpenSSL::ASN1::Sequence([
172+
OpenSSL::ASN1::Sequence([OpenSSL::ASN1::ObjectId('id-ecPublicKey'), OpenSSL::ASN1::ObjectId(curve)]),
173+
OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed))
174+
])
175+
end
176176

177177
OpenSSL::PKey::EC.new(sequence.to_der)
178178
end

lib/jwt/jwk/key_finder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def initialize(options)
88
jwks_or_loader = options[:jwks]
99

1010
@jwks_loader = if jwks_or_loader.respond_to?(:call)
11-
jwks_or_loader
12-
else
13-
->(_options) { jwks_or_loader }
14-
end
11+
jwks_or_loader
12+
else
13+
->(_options) { jwks_or_loader }
14+
end
1515
end
1616

1717
def key_for(kid)

lib/jwt/jwk/set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(jwks = nil, options = {}) # rubocop:disable Metrics/CyclomaticCom
2525
jwks.map { |k| JWT::JWK.new(k, nil, options) }
2626
else
2727
raise ArgumentError, 'Can only create new JWKS from Hash, Array and JWK'
28-
end
28+
end
2929
end
3030

3131
def export(options = {})

lib/jwt/verify.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ def verify_aud
3434
return unless (options_aud = @options[:aud])
3535

3636
aud = @payload['aud']
37-
raise(JWT::InvalidAudError, "Invalid audience. Expected #{options_aud}, received #{aud || '<none>'}") if ([*aud] & [*options_aud]).empty?
37+
raise JWT::InvalidAudError, "Invalid audience. Expected #{options_aud}, received #{aud || '<none>'}" if ([*aud] & [*options_aud]).empty?
3838
end
3939

4040
def verify_expiration
4141
return unless contains_key?(@payload, 'exp')
42-
raise(JWT::ExpiredSignature, 'Signature has expired') if @payload['exp'].to_i <= (Time.now.to_i - exp_leeway)
42+
raise JWT::ExpiredSignature, 'Signature has expired' if @payload['exp'].to_i <= (Time.now.to_i - exp_leeway)
4343
end
4444

4545
def verify_iat
4646
return unless contains_key?(@payload, 'iat')
4747

4848
iat = @payload['iat']
49-
raise(JWT::InvalidIatError, 'Invalid iat') if !iat.is_a?(Numeric) || iat.to_f > Time.now.to_f
49+
raise JWT::InvalidIatError, 'Invalid iat' if !iat.is_a?(Numeric) || iat.to_f > Time.now.to_f
5050
end
5151

5252
def verify_iss
@@ -60,7 +60,7 @@ def verify_iss
6060
when *options_iss
6161
nil
6262
else
63-
raise(JWT::InvalidIssuerError, "Invalid issuer. Expected #{options_iss}, received #{iss || '<none>'}")
63+
raise JWT::InvalidIssuerError, "Invalid issuer. Expected #{options_iss}, received #{iss || '<none>'}"
6464
end
6565
end
6666

@@ -70,29 +70,29 @@ def verify_jti
7070

7171
if options_verify_jti.respond_to?(:call)
7272
verified = options_verify_jti.arity == 2 ? options_verify_jti.call(jti, @payload) : options_verify_jti.call(jti)
73-
raise(JWT::InvalidJtiError, 'Invalid jti') unless verified
73+
raise JWT::InvalidJtiError, 'Invalid jti' unless verified
7474
elsif jti.to_s.strip.empty?
75-
raise(JWT::InvalidJtiError, 'Missing jti')
75+
raise JWT::InvalidJtiError, 'Missing jti'
7676
end
7777
end
7878

7979
def verify_not_before
8080
return unless contains_key?(@payload, 'nbf')
81-
raise(JWT::ImmatureSignature, 'Signature nbf has not been reached') if @payload['nbf'].to_i > (Time.now.to_i + nbf_leeway)
81+
raise JWT::ImmatureSignature, 'Signature nbf has not been reached' if @payload['nbf'].to_i > (Time.now.to_i + nbf_leeway)
8282
end
8383

8484
def verify_sub
8585
return unless (options_sub = @options[:sub])
8686

8787
sub = @payload['sub']
88-
raise(JWT::InvalidSubError, "Invalid subject. Expected #{options_sub}, received #{sub || '<none>'}") unless sub.to_s == options_sub.to_s
88+
raise JWT::InvalidSubError, "Invalid subject. Expected #{options_sub}, received #{sub || '<none>'}" unless sub.to_s == options_sub.to_s
8989
end
9090

9191
def verify_required_claims
9292
return unless (options_required_claims = @options[:required_claims])
9393

9494
options_required_claims.each do |required_claim|
95-
raise(JWT::MissingRequiredClaim, "Missing required claim #{required_claim}") unless contains_key?(@payload, required_claim)
95+
raise JWT::MissingRequiredClaim, "Missing required claim #{required_claim}" unless contains_key?(@payload, required_claim)
9696
end
9797
end
9898

lib/jwt/x5c_key_finder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module JWT
77
# See https://tools.ietf.org/html/rfc7515#section-4.1.6
88
class X5cKeyFinder
99
def initialize(root_certificates, crls = nil)
10-
raise(ArgumentError, 'Root certificates must be specified') unless root_certificates
10+
raise ArgumentError, 'Root certificates must be specified' unless root_certificates
1111

1212
@store = build_store(root_certificates, crls)
1313
end
@@ -24,7 +24,7 @@ def from(x5c_header_or_certificates)
2424
error = "#{error} Certificate subject: #{current_cert.subject}."
2525
end
2626

27-
raise(JWT::VerificationError, error)
27+
raise JWT::VerificationError, error
2828
end
2929
end
3030

spec/integration/readme_examples_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308
# The jwk loader would fetch the set of JWKs from a trusted source,
309309
# to avoid malicious invalidations some kind of protection needs to be implemented.
310310
# This example only allows cache invalidations every 5 minutes.
311-
jwk_loader = ->(options) do
311+
jwk_loader = lambda do |options|
312312
if options[:kid_not_found] && @cache_last_update < Time.now.to_i - 300
313313
logger.info("Invalidating JWK cache. #{options[:kid]} not found from previous cache")
314314
@cached_keys = nil
@@ -352,7 +352,7 @@
352352

353353
token = JWT.encode(payload, jwk.signing_key, 'RS512', headers)
354354

355-
jwks_loader = ->(options) do
355+
jwks_loader = lambda do |options|
356356
# The jwk loader would fetch the set of JWKs from a trusted source.
357357
# To avoid malicious requests triggering cache invalidations there needs to be
358358
# some kind of grace time or other logic for determining the validity of the invalidation.

0 commit comments

Comments
 (0)