diff options
Diffstat (limited to 'spec/lib/atlassian/jira_connect/jwt')
-rw-r--r-- | spec/lib/atlassian/jira_connect/jwt/asymmetric_spec.rb | 104 | ||||
-rw-r--r-- | spec/lib/atlassian/jira_connect/jwt/symmetric_spec.rb | 97 |
2 files changed, 201 insertions, 0 deletions
diff --git a/spec/lib/atlassian/jira_connect/jwt/asymmetric_spec.rb b/spec/lib/atlassian/jira_connect/jwt/asymmetric_spec.rb new file mode 100644 index 00000000000..12ed47a1025 --- /dev/null +++ b/spec/lib/atlassian/jira_connect/jwt/asymmetric_spec.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Atlassian::JiraConnect::Jwt::Asymmetric do + describe '#valid?' do + subject(:asymmetric_jwt) { described_class.new(jwt, verification_claims) } + + let(:verification_claims) { jwt_claims } + let(:jwt_claims) { { aud: aud, iss: client_key, qsh: qsh } } + let(:aud) { 'https://test.host/-/jira_connect' } + let(:client_key) { '1234' } + let(:public_key_id) { '123e4567-e89b-12d3-a456-426614174000' } + let(:jwt_headers) { { kid: public_key_id } } + let(:private_key) { OpenSSL::PKey::RSA.generate 2048 } + let(:jwt) { JWT.encode(jwt_claims, private_key, 'RS256', jwt_headers) } + let(:public_key) { private_key.public_key } + let(:install_keys_url) { "https://connect-install-keys.atlassian.com/#{public_key_id}" } + let(:qsh) do + Atlassian::Jwt.create_query_string_hash('https://gitlab.test/events/installed', 'POST', 'https://gitlab.test') + end + + before do + stub_request(:get, install_keys_url) + .to_return(body: public_key.to_s, status: 200) + end + + it 'returns true when verified with public key from CDN' do + expect(JWT).to receive(:decode).twice.and_call_original + + expect(asymmetric_jwt).to be_valid + + expect(WebMock).to have_requested(:get, install_keys_url) + end + + context 'JWT does not contain a key ID' do + let(:public_key_id) { nil } + + it { is_expected.not_to be_valid } + end + + context 'JWT contains a key ID that is not a valid UUID4' do + let(:public_key_id) { '123' } + + it { is_expected.not_to be_valid } + end + + context 'public key can not be retrieved' do + before do + stub_request(:get, install_keys_url).to_return(body: '', status: 404) + end + + it { is_expected.not_to be_valid } + end + + context 'retrieving the public raises an error' do + before do + allow(Gitlab::HTTP).to receive(:get).and_raise(SocketError) + end + + it { is_expected.not_to be_valid } + end + + context 'token decoding raises an error' do + before do + allow(JWT).to receive(:decode).and_call_original + allow(JWT).to receive(:decode).with( + jwt, anything, true, + { aud: anything, verify_aud: true, iss: client_key, verify_iss: true, algorithm: 'RS256' } + ).and_raise(JWT::DecodeError) + end + + it { is_expected.not_to be_valid } + end + + context 'when iss could not be verified' do + let(:verification_claims) { { aud: jwt_claims[:aud], iss: 'some other iss', qsh: jwt_claims[:qsh] } } + + it { is_expected.not_to be_valid } + end + + context 'when qsh could not be verified' do + let(:verification_claims) { { aud: jwt_claims[:aud], iss: client_key, qsh: 'some other qsh' } } + + it { is_expected.not_to be_valid } + end + end + + describe '#iss_claim' do + subject { asymmetric_jwt.iss_claim } + + let(:asymmetric_jwt) { described_class.new('123', anything) } + + it { is_expected.to eq(nil) } + + context 'when jwt is verified' do + before do + asymmetric_jwt.instance_variable_set(:@claims, { 'iss' => 'client_key' }) + end + + it { is_expected.to eq('client_key') } + end + end +end diff --git a/spec/lib/atlassian/jira_connect/jwt/symmetric_spec.rb b/spec/lib/atlassian/jira_connect/jwt/symmetric_spec.rb new file mode 100644 index 00000000000..61adff7e221 --- /dev/null +++ b/spec/lib/atlassian/jira_connect/jwt/symmetric_spec.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Atlassian::JiraConnect::Jwt::Symmetric do + let(:shared_secret) { 'secret' } + + describe '#iss_claim' do + let(:jwt) { Atlassian::Jwt.encode({ iss: '123' }, shared_secret) } + + subject { described_class.new(jwt).iss_claim } + + it { is_expected.to eq('123') } + + context 'invalid JWT' do + let(:jwt) { '123' } + + it { is_expected.to eq(nil) } + end + end + + describe '#sub_claim' do + let(:jwt) { Atlassian::Jwt.encode({ sub: '123' }, shared_secret) } + + subject { described_class.new(jwt).sub_claim } + + it { is_expected.to eq('123') } + + context 'invalid JWT' do + let(:jwt) { '123' } + + it { is_expected.to eq(nil) } + end + end + + describe '#valid?' do + subject { described_class.new(jwt).valid?(shared_secret) } + + context 'invalid JWT' do + let(:jwt) { '123' } + + it { is_expected.to eq(false) } + end + + context 'valid JWT' do + let(:jwt) { Atlassian::Jwt.encode({}, shared_secret) } + + it { is_expected.to eq(true) } + end + end + + describe '#verify_qsh_claim' do + let(:jwt) { Atlassian::Jwt.encode({ qsh: qsh_claim }, shared_secret) } + let(:qsh_claim) do + Atlassian::Jwt.create_query_string_hash('https://gitlab.test/subscriptions', 'GET', 'https://gitlab.test') + end + + subject(:verify_qsh_claim) do + described_class.new(jwt).verify_qsh_claim('https://gitlab.test/subscriptions', 'GET', 'https://gitlab.test') + end + + it { is_expected.to eq(true) } + + context 'qsh does not match' do + let(:qsh_claim) do + Atlassian::Jwt.create_query_string_hash('https://example.com/foo', 'POST', 'https://example.com') + end + + it { is_expected.to eq(false) } + end + + context 'creating query string hash raises an error' do + let(:qsh_claim) { '123' } + + specify do + expect(Atlassian::Jwt).to receive(:create_query_string_hash).and_raise(StandardError) + + expect(verify_qsh_claim).to eq(false) + end + end + end + + describe '#verify_context_qsh_claim' do + let(:jwt) { Atlassian::Jwt.encode({ qsh: qsh_claim }, shared_secret) } + let(:qsh_claim) { 'context-qsh' } + + subject(:verify_context_qsh_claim) { described_class.new(jwt).verify_context_qsh_claim } + + it { is_expected.to eq(true) } + + context 'jwt does not contain a context qsh' do + let(:qsh_claim) { '123' } + + it { is_expected.to eq(false) } + end + end +end |