-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not decode payload when b64 header is false
- Loading branch information
Showing
8 changed files
with
165 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module Claims | ||
# Responsible of validation the crit header | ||
class Crit | ||
# Initializes a new Crit instance. | ||
# | ||
# @param expected_crits [String] the expected crit header values for the JWT token. | ||
def initialize(expected_crits:) | ||
@expected_crits = Array(expected_crits) | ||
end | ||
|
||
# Verifies the critical claim ('crit') in the JWT token header. | ||
# | ||
# @param context [Object] the context containing the JWT payload and header. | ||
# @param _args [Hash] additional arguments (not used). | ||
# @raise [JWT::InvalidCritError] if the crit claim is invalid. | ||
# @return [nil] | ||
def verify!(context:, **_args) | ||
raise(JWT::InvalidCritError, 'Crit header missing') unless context.header['crit'] | ||
raise(JWT::InvalidCritError, 'Crit header should be an array') unless context.header['crit'].is_a?(Array) | ||
|
||
missing = (expected_crits - context.header['crit']) | ||
raise(JWT::InvalidCritError, "Crit header missing expected values: #{missing.join(', ')}") if missing.any? | ||
|
||
nil | ||
end | ||
|
||
private | ||
|
||
attr_reader :expected_crits | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe JWT::Claims::Crit do | ||
subject(:verify!) { described_class.new(expected_crits: expected_crits).verify!(context: SpecSupport::Token.new(header: header)) } | ||
let(:expected_crits) { [] } | ||
let(:header) { {} } | ||
|
||
context 'when header is missing' do | ||
it 'raises JWT::InvalidCritError' do | ||
expect { verify! }.to raise_error(JWT::InvalidCritError, 'Crit header missing') | ||
end | ||
end | ||
|
||
context 'when header is not an array' do | ||
let(:header) { { 'crit' => 'not_an_array' } } | ||
|
||
it 'raises JWT::InvalidCritError' do | ||
expect { verify! }.to raise_error(JWT::InvalidCritError, 'Crit header should be an array') | ||
end | ||
end | ||
|
||
context 'when header is an array and not containing the expected value' do | ||
let(:header) { { 'crit' => %w[crit1] } } | ||
let(:expected_crits) { %w[crit2] } | ||
it 'raises an InvalidCritError' do | ||
expect { verify! }.to raise_error(JWT::InvalidCritError, 'Crit header missing expected values: crit2') | ||
end | ||
end | ||
|
||
context 'when header is an array containing exactly the expected values' do | ||
let(:header) { { 'crit' => %w[crit1 crit2] } } | ||
let(:expected_crits) { %w[crit1 crit2] } | ||
it 'does not raise an error' do | ||
expect(verify!).to eq(nil) | ||
end | ||
end | ||
|
||
context 'when header is an array containing at least the expected values' do | ||
let(:header) { { 'crit' => %w[crit1 crit2 crit3] } } | ||
let(:expected_crits) { %w[crit1 crit2] } | ||
it 'does not raise an error' do | ||
expect(verify!).to eq(nil) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module SpecSupport | ||
Token = Struct.new(:payload, keyword_init: true) | ||
Token = Struct.new(:payload, :header, keyword_init: true) | ||
end |