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

auth_hash_spec.rb « saml « auth « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5286e22abc9972355a3a800dab40a48d98d3f29d (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
139
140
141
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Auth::Saml::AuthHash do
  include LoginHelpers

  let(:raw_info_attr) { { 'groups' => %w(Developers Freelancers) } }
  subject(:saml_auth_hash) { described_class.new(omniauth_auth_hash) }

  let(:info_hash) do
    {
      name: 'John',
      email: 'john@mail.com'
    }
  end

  let(:omniauth_auth_hash) do
    OmniAuth::AuthHash.new(uid: 'my-uid',
                           provider: 'saml',
                           info: info_hash,
                           extra: { raw_info: OneLogin::RubySaml::Attributes.new(raw_info_attr) } )
  end

  before do
    stub_saml_group_config(%w(Developers Freelancers Designers))
  end

  describe '#groups' do
    it 'returns array of groups' do
      expect(saml_auth_hash.groups).to eq(%w(Developers Freelancers))
    end

    context 'raw info hash attributes empty' do
      let(:raw_info_attr) { {} }

      it 'returns an empty array' do
        expect(saml_auth_hash.groups).to be_a(Array)
      end
    end
  end

  describe '#azure_group_overage_claim?' do
    context 'when the claim is not present' do
      let(:raw_info_attr) { {} }

      it 'is false' do
        expect(saml_auth_hash.azure_group_overage_claim?).to eq(false)
      end
    end

    context 'when the claim is present' do
      # The value of the claim is irrelevant, but it's still included
      # in the test response to keep tests as real-world as possible.
      # https://learn.microsoft.com/en-us/security/zero-trust/develop/configure-tokens-group-claims-app-roles#group-overages
      let(:raw_info_attr) do
        {
          'http://schemas.microsoft.com/claims/groups.link' =>
            ['https://graph.windows.net/8c750e43/users/e631c82c/getMemberObjects']
        }
      end

      it 'is true' do
        expect(saml_auth_hash.azure_group_overage_claim?).to eq(true)
      end
    end
  end

  describe '#authn_context' do
    let(:auth_hash_data) do
      {
        provider: 'saml',
        uid: 'some_uid',
        info:
          {
            name: 'mockuser',
            email: 'mock@email.ch',
            image: 'mock_user_thumbnail_url'
          },
        credentials:
          {
            token: 'mock_token',
            secret: 'mock_secret'
          },
        extra:
          {
            raw_info:
              {
                info:
                  {
                    name: 'mockuser',
                    email: 'mock@email.ch',
                    image: 'mock_user_thumbnail_url'
                  }
              }
          }
      }
    end

    subject(:saml_auth_hash) { described_class.new(OmniAuth::AuthHash.new(auth_hash_data)) }

    context 'with response_object' do
      before do
        auth_hash_data[:extra][:response_object] = { document:
                                                         saml_xml(File.read('spec/fixtures/authentication/saml_response.xml')) }
      end

      it 'can extract authn_context' do
        expect(saml_auth_hash.authn_context).to eq 'urn:oasis:names:tc:SAML:2.0:ac:classes:Password'
      end
    end

    context 'with SAML 2.0 response_object' do
      before do
        auth_hash_data[:extra][:response_object] = { document:
                                                         saml_xml(File.read('spec/fixtures/authentication/saml2_response.xml')) }
      end

      it 'can extract authn_context' do
        expect(saml_auth_hash.authn_context).to eq 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport'
      end
    end

    context 'with ADFS SAML response_object' do
      before do
        auth_hash_data[:extra][:response_object] = { document:
                                                         saml_xml(File.read('spec/fixtures/authentication/adfs_saml_response.xml')) }
      end

      it 'can extract authn_context' do
        expect(saml_auth_hash.authn_context).to eq 'urn:federation:authentication:windows'
      end
    end

    context 'without response_object' do
      it 'returns an empty string' do
        expect(saml_auth_hash.authn_context).to be_nil
      end
    end
  end
end