Welcome to mirror list, hosted at ThFree Co, Russian Federation.

asymmetric_spec.rb « jwt « jira_connect « atlassian « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 86d672067a394a90f7d698d6a7efd5acf59b6f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Atlassian::JiraConnect::Jwt::Asymmetric do
  describe '#valid?' do
    let_it_be(:private_key) { OpenSSL::PKey::RSA.generate 3072 }

    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(:jwt) { JWT.encode(jwt_claims, private_key, 'RS256', jwt_headers) }
    let(:public_key) { private_key.public_key }
    let(:stub_asymmetric_jwt_cdn) { 'https://connect-install-keys.atlassian.com' }
    let(:install_keys_url) { "#{stub_asymmetric_jwt_cdn}/#{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

    context 'with jira_connect_proxy_url setting' do
      let(:stub_asymmetric_jwt_cdn) { 'https://example.com/-/jira_connect/public_keys' }

      before do
        stub_application_setting(jira_connect_proxy_url: 'https://example.com')
      end

      it 'requests the settings CDN' do
        expect(JWT).to receive(:decode).twice.and_call_original

        expect(asymmetric_jwt).to be_valid

        expect(WebMock).to have_requested(:get, "https://example.com/-/jira_connect/public_keys/#{public_key_id}")
      end

      context 'when jira_connect_oauth_self_managed disabled' do
        let(:stub_asymmetric_jwt_cdn) { 'https://connect-install-keys.atlassian.com' }

        before do
          stub_feature_flags(jira_connect_oauth_self_managed: false)
        end

        it 'requests the default 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
      end
    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