-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bing back compatibility with old apis
- Loading branch information
Showing
24 changed files
with
609 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ coverage/ | |
*gemfile.lock | ||
.byebug_history | ||
*.gem | ||
doc/ |
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'error' | ||
|
||
module JWT | ||
class ClaimsValidator | ||
def initialize(payload) | ||
Deprecations.warning('The ::JWT::ClaimsValidator class is deprecated and will be removed in the next major version of ruby-jwt') | ||
@payload = payload | ||
end | ||
|
||
def validate! | ||
Claims::Numeric.verify!(payload: @payload) | ||
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module JWA | ||
module Compat | ||
module ClassMethods | ||
def from_algorithm(algorithm) | ||
new(algorithm) | ||
end | ||
|
||
def sign(algorithm, msg, key) | ||
Deprecations.warning('Support for calling sign with positional arguments will be removed in future ruby-jwt versions') | ||
|
||
from_algorithm(algorithm).sign(data: msg, signing_key: key) | ||
end | ||
|
||
def verify(algorithm, key, signing_input, signature) | ||
Deprecations.warning('Support for calling verify with positional arguments will be removed in future ruby-jwt versions') | ||
|
||
from_algorithm(algorithm).verify(data: signing_input, signature: signature, verification_key: key) | ||
end | ||
end | ||
|
||
def self.included(klass) | ||
klass.extend(ClassMethods) | ||
end | ||
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
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 | ||
|
||
require_relative 'error' | ||
|
||
module JWT | ||
class Verify | ||
DEFAULTS = { leeway: 0 }.freeze | ||
METHODS = %w[verify_aud verify_expiration verify_iat verify_iss verify_jti verify_not_before verify_sub verify_required_claims].freeze | ||
|
||
class << self | ||
METHODS.each do |method_name| | ||
define_method(method_name) do |payload, options| | ||
new(payload, options).send(method_name) | ||
end | ||
end | ||
|
||
def verify_claims(payload, options) | ||
Deprecations.warning('The ::JWT::Verify.verify_claims method is deprecated and will be removed in the next major version of ruby-jwt') | ||
::JWT::Claims.verify!(payload, options) | ||
end | ||
end | ||
|
||
def initialize(payload, options) | ||
Deprecations.warning('The ::JWT::Verify class is deprecated and will be removed in the next major version of ruby-jwt') | ||
@payload = payload | ||
@options = DEFAULTS.merge(options) | ||
end | ||
|
||
METHODS.each do |method_name| | ||
define_method(method_name) do | ||
::JWT::Claims.verify!(@payload, @options.merge(method_name => true)) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe JWT::ClaimsValidator do | ||
let(:validator) { described_class.new(claims) } | ||
|
||
describe '#validate!' do | ||
subject { validator.validate! } | ||
|
||
shared_examples_for 'a NumericDate claim' do |claim| | ||
context "when #{claim} payload is an integer" do | ||
let(:claims) { { claim => 12_345 } } | ||
|
||
it 'does not raise error' do | ||
expect { subject }.not_to raise_error | ||
end | ||
|
||
context 'and key is a string' do | ||
let(:claims) { { claim.to_s => 43.32 } } | ||
|
||
it 'does not raise error' do | ||
expect { subject }.not_to raise_error | ||
end | ||
end | ||
end | ||
|
||
context "when #{claim} payload is a float" do | ||
let(:claims) { { claim => 43.32 } } | ||
|
||
it 'does not raise error' do | ||
expect { subject }.not_to raise_error | ||
end | ||
end | ||
|
||
context "when #{claim} payload is a string" do | ||
let(:claims) { { claim => '1' } } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error JWT::InvalidPayload | ||
end | ||
|
||
context 'and key is a string' do | ||
let(:claims) { { claim.to_s => '1' } } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error JWT::InvalidPayload | ||
end | ||
end | ||
end | ||
|
||
context "when #{claim} payload is a Time object" do | ||
let(:claims) { { claim => Time.now } } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error JWT::InvalidPayload | ||
end | ||
end | ||
|
||
context "when #{claim} payload is a string" do | ||
let(:claims) { { claim => '1' } } | ||
|
||
it 'raises error' do | ||
expect { subject }.to raise_error JWT::InvalidPayload | ||
end | ||
end | ||
end | ||
|
||
context 'exp claim' do | ||
it_should_behave_like 'a NumericDate claim', :exp | ||
end | ||
|
||
context 'iat claim' do | ||
it_should_behave_like 'a NumericDate claim', :iat | ||
end | ||
|
||
context 'nbf claim' do | ||
it_should_behave_like 'a NumericDate claim', :nbf | ||
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'JWT::JWA::Eddsa' do | ||
let(:key) { RbNaCl::Signatures::Ed25519::SigningKey.generate } | ||
|
||
before do | ||
skip('Requires the rbnacl gem') unless JWT.rbnacl? | ||
end | ||
|
||
context 'backwards compatibility' do | ||
it 'signs and verifies' do | ||
signature = JWT::JWA::Eddsa.sign('RS256', 'data', key) | ||
expect(JWT::JWA::Eddsa.verify('RS256', key.verify_key, 'data', signature)).to be(true) | ||
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'JWT::JWA::HmacRbNaCl' do | ||
before do | ||
skip('Requires the rbnacl gem') unless JWT.rbnacl_6_or_greater? | ||
end | ||
context 'backwards compatibility' do | ||
it 'signs and verifies' do | ||
signature = JWT::JWA::HmacRbNaCl.sign('HS512256', 'data', 'key') | ||
expect(JWT::JWA::HmacRbNaCl.verify('HS512256', 'key', 'data', signature)).to be(true) | ||
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe JWT::JWA do | ||
describe '.create' do | ||
describe 'Backwards compatibility' do | ||
describe 'create, sign and verify' do | ||
it 'finds an algorithm with old api' do | ||
alg = described_class.create('HS256') | ||
signature = alg.sign(data: 'data', signing_key: 'key') | ||
expect(signature).to be_a(String) | ||
expect(alg.verify(data: 'data', signature: signature, verification_key: 'key')).to be(true) | ||
end | ||
end | ||
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
Oops, something went wrong.