Skip to content

Commit

Permalink
renamed enabled_crits to allowed_crits
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Oct 16, 2024
1 parent faa6f85 commit abd5476
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions lib/jwt/encoded_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module JWT
# encoded_token = JWT::EncodedToken.new(token.jwt)
# encoded_token.verify_signature!algorithm: 'HS256', key: 'secret')
# encoded_token.payload # => {'pay' => 'load'}
#
class EncodedToken
include Claims::VerificationMethods

Expand All @@ -22,17 +21,15 @@ class EncodedToken
# Initializes a new EncodedToken instance.
#
# @param jwt [String] the encoded JWT token.
# @param enabled_crits [Array<String>] the list of enabled critical headers.
# @param allow_unverified [Boolean] whether to allow access to payload for unverified tokens.
# @param allowed_crits [Array<String>] the list of allowed critical headers.
# @raise [ArgumentError] if the provided JWT is not a String.
# @raise [ArgumentError] if enabled_crits is not an Array.
def initialize(jwt, enabled_crits: [])
# @raise [ArgumentError] if allowed_crits is not an Array.
def initialize(jwt, allowed_crits: [])
raise ArgumentError, 'Provided JWT must be a String' unless jwt.is_a?(String)
raise ArgumentError, 'enabled_crits must be an Array' unless enabled_crits.is_a?(Array)
raise ArgumentError, 'allowed_crits must be an Array' unless allowed_crits.is_a?(Array)

@enabled_crits = enabled_crits
@allowed_crits = allowed_crits
@jwt = jwt
@signature_verified = false
@encoded_header, @encoded_payload, @encoded_signature = jwt.split('.')
end

Expand Down Expand Up @@ -62,7 +59,6 @@ def header

# Returns the payload of the JWT token.
#
# @param allow_unverified [Boolean] whether to allow payloads to be accessed for unverified tokens.
# @return [Hash] the payload.
def payload
@payload ||= decode_payload
Expand Down Expand Up @@ -122,7 +118,7 @@ def verify_crit!(crit)
raise InvalidCritError, "'#{crit}' missing from crit header"
end

return if Array(enabled_crits).include?(crit)
return if Array(allowed_crits).include?(crit)

raise InvalidCritError, "'#{crit}' not enabled for token instance"
end
Expand All @@ -131,7 +127,7 @@ def verify_crit!(crit)

private

attr_reader :enabled_crits
attr_reader :allowed_crits

def decode_payload
raise JWT::DecodeError, 'Encoded payload is empty' if encoded_payload == ''
Expand Down
2 changes: 1 addition & 1 deletion spec/jwt/encoded_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
end

context 'when payload is not encoded and the b64 crit is enabled' do
subject(:token) { described_class.new(encoded_token, enabled_crits: ['b64']) }
subject(:token) { described_class.new(encoded_token, allowed_crits: ['b64']) }
let(:encoded_token) { 'eyJhbGciOiJIUzI1NiIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..signature' }
before { token.encoded_payload = '{"foo": "bar"}' }

Expand Down

0 comments on commit abd5476

Please sign in to comment.