-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move nbf validator into it's own class
- Loading branch information
Showing
8 changed files
with
72 additions
and
34 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
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module Validators | ||
class NotBeforeClaimValidator | ||
def initialize(leeway:) | ||
@leeway = leeway | ||
end | ||
|
||
def validate!(payload:, **_args) | ||
return unless payload.is_a?(Hash) | ||
return unless payload.key?('nbf') | ||
|
||
raise JWT::ImmatureSignature, 'Signature nbf has not been reached' if payload['nbf'].to_i > (Time.now.to_i + leeway) | ||
end | ||
|
||
private | ||
|
||
attr_reader :leeway | ||
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ::JWT::Validators::NotBeforeClaimValidator do | ||
let(:payload) { { 'nbf' => (Time.now.to_i + 5) } } | ||
|
||
describe '#validate!' do | ||
context 'when nbf is in the future' do | ||
it 'raises JWT::ImmatureSignature' do | ||
expect { described_class.new(leeway: 0).validate!(payload: payload) }.to raise_error JWT::ImmatureSignature | ||
end | ||
end | ||
|
||
context 'when nbf is in the past' do | ||
let(:payload) { { 'nbf' => (Time.now.to_i - 5) } } | ||
|
||
it 'does not raise error' do | ||
expect { described_class.new(leeway: 0).validate!(payload: payload) }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'when leeway is given' do | ||
it 'does not raise error' do | ||
expect { described_class.new(leeway: 10).validate!(payload: payload) }.not_to raise_error | ||
end | ||
end | ||
end | ||
end |