File: discovery_controller_spec.rb

package info (click to toggle)
ruby-doorkeeper-openid-connect 1.8.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: ruby: 3,120; makefile: 7; sh: 4
file content (316 lines) | stat: -rw-r--r-- 10,261 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# frozen_string_literal: true

require 'rails_helper'

describe Doorkeeper::OpenidConnect::DiscoveryController, type: :controller do
  describe '#provider' do
    it 'returns the provider configuration' do
      get :provider
      data = JSON.parse(response.body)

      expect(data.sort).to match({
        'issuer' => 'dummy',
        'authorization_endpoint' => 'http://test.host/oauth/authorize',
        'token_endpoint' => 'http://test.host/oauth/token',
        'revocation_endpoint' => 'http://test.host/oauth/revoke',
        'introspection_endpoint' => 'http://test.host/oauth/introspect',
        'userinfo_endpoint' => 'http://test.host/oauth/userinfo',
        'jwks_uri' => 'http://test.host/oauth/discovery/keys',

        'scopes_supported' => ['openid'],
        'response_types_supported' => ['code', 'token', 'id_token', 'id_token token'],
        'response_modes_supported' => %w[query fragment form_post],
        'grant_types_supported' => %w[authorization_code client_credentials implicit_oidc],

        'token_endpoint_auth_methods_supported' => %w[client_secret_basic client_secret_post],

        'subject_types_supported' => [
          'public',
        ],

        'id_token_signing_alg_values_supported' => [
          'RS256',
        ],

        'claim_types_supported' => [
          'normal',
        ],

        'claims_supported' => %w[
          iss
          sub
          aud
          exp
          iat
          name
          variable_name
          created_at
          updated_at
          token_id
          both_responses
          id_token_response
          user_info_response
        ],

        'code_challenge_methods_supported' => %w[
          plain
          S256
        ],
      }.sort)
    end

    context 'when refresh_token grant type is enabled' do
      before { Doorkeeper.configure { use_refresh_token } }

      it 'add refresh_token to grant_types_supported' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['grant_types_supported']).to eq %w[authorization_code client_credentials refresh_token]
      end
    end

    context 'when issuer block' do
      before { Doorkeeper::OpenidConnect.configure { issuer do |r, a| "test-issuer" end } }

      it 'return blocks result' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['issuer']).to eq "test-issuer"
      end
    end

    context 'when grant_flows is configed with only client_credentials' do
      before { Doorkeeper.configure { grant_flows %w[client_credentials] } }

      it 'return empty response_modes_supported' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['response_modes_supported']).to eq []
      end
    end

    context 'when grant_flows is configed only implicit flow' do
      before { Doorkeeper.configure { grant_flows %w[implicit_oidc] } }

      it 'return fragment and form_post as response_modes_supported' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['response_modes_supported']).to eq %w[fragment form_post]
      end
    end

    context 'when grant_flows is configed with authorization_code and implicit flow' do
      before { Doorkeeper.configure { grant_flows %w[authorization_code implicit_oidc] } }

      it 'return query, fragment and form_post as response_modes_supported' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['response_modes_supported']).to eq %w[query fragment form_post]
      end
    end

    it 'uses the protocol option for generating URLs' do
      Doorkeeper::OpenidConnect.configure do
        protocol { :testing }
      end

      get :provider
      data = JSON.parse(response.body)

      expect(data['authorization_endpoint']).to eq 'testing://test.host/oauth/authorize'
    end

    context 'when the discovery_url_options option is set for all endpoints' do
      before do
        Doorkeeper::OpenidConnect.configure do
          discovery_url_options do |request|
            {
              authorization: { host: 'alternate-authorization.host' },
              token: { host: 'alternate-token.host' },
              revocation: { host: 'alternate-revocation.host' },
              introspection: { host: 'alternate-introspection.host' },
              userinfo: { host: 'alternate-userinfo.host' },
              jwks: { host: 'alternate-jwks.host' }
            }
          end
        end
      end

      it 'uses the discovery_url_options option when generating the endpoint urls' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['authorization_endpoint']).to eq 'http://alternate-authorization.host/oauth/authorize'
        expect(data['token_endpoint']).to eq 'http://alternate-token.host/oauth/token'
        expect(data['revocation_endpoint']).to eq 'http://alternate-revocation.host/oauth/revoke'
        expect(data['introspection_endpoint']).to eq 'http://alternate-introspection.host/oauth/introspect'
        expect(data['userinfo_endpoint']).to eq 'http://alternate-userinfo.host/oauth/userinfo'
        expect(data['jwks_uri']).to eq 'http://alternate-jwks.host/oauth/discovery/keys'
      end
    end

    context 'when the discovery_url_options option is only set for some endpoints' do
      before do
        Doorkeeper::OpenidConnect.configure do
          discovery_url_options do |request|
            { authorization: { host: 'alternate-authorization.host' } }
          end
        end
      end

      it 'does not use the discovery_url_options option when generating other URLs' do
        get :provider
        data = JSON.parse(response.body)

        {
          'token_endpoint' => 'http://test.host/oauth/token',
          'revocation_endpoint' => 'http://test.host/oauth/revoke',
          'introspection_endpoint' => 'http://test.host/oauth/introspect',
          'userinfo_endpoint' => 'http://test.host/oauth/userinfo',
          'jwks_uri' => 'http://test.host/oauth/discovery/keys',
        }.each do |endpoint, expected_url|
          expect(data[endpoint]).to eq expected_url
        end
      end
    end

    it 'does not return an end session endpoint if none is configured' do
      get :provider
      data = JSON.parse(response.body)

      expect(data.key?('end_session_endpoint')).to be(false)
    end

    it 'uses the configured end session endpoint with self as context' do
      Doorkeeper::OpenidConnect.configure do
        end_session_endpoint -> { logout_url }
      end

      def controller.logout_url
        'http://test.host/logout'
      end

      get :provider
      data = JSON.parse(response.body)

      expect(data['end_session_endpoint']).to eq 'http://test.host/logout'
    end

    context 'when token inspection is disallowed' do
      let(:doorkeeper_config) { Doorkeeper.config }
      let!(:allow_token_introspection) { doorkeeper_config.allow_token_introspection }

      before do
        allow(doorkeeper_config).to receive(:allow_token_introspection).and_return(false)
        Rails.application.reload_routes!
      end

      after do
        allow(doorkeeper_config).to receive(:allow_token_introspection).and_return(allow_token_introspection)
        Rails.application.reload_routes!
      end

      it 'does not return introspection_endpoint' do
        get :provider
        data = JSON.parse(response.body)

        expect(data.key?('introspection_endpoint')).to be(false)
      end
    end
  end

  describe '#webfinger' do
    it 'requires the resource parameter' do
      expect do
        get :webfinger
      end.to raise_error ActionController::ParameterMissing
    end

    it 'returns the OpenID Connect relation' do
      get :webfinger, params: { resource: 'user@example.com' }
      data = JSON.parse(response.body)

      expect(data.sort).to eq({
        'subject' => 'user@example.com',
        'links' => [
          'rel' => 'http://openid.net/specs/connect/1.0/issuer',
          'href' => 'http://test.host/',
        ],
      }.sort)
    end

    context 'when the discovery_url_options option is set for webfinger endpoint' do
      before do
        Doorkeeper::OpenidConnect.configure do
          discovery_url_options do |request|
            { webfinger: { host: 'alternate-webfinger.host' } }
          end
        end
      end

      it 'uses the discovery_url_options option when generating the webfinger endpoint url' do
        get :webfinger, params: { resource: 'user@example.com' }
        data = JSON.parse(response.body)

        expect(data['links'].first['href']).to eq 'http://alternate-webfinger.host/'
      end
    end

    context 'when the discovery_url_options option uses the request for an endpoint' do
      before do
        Doorkeeper::OpenidConnect.configure do
          discovery_url_options do |request|
            {
              authorization: { host: 'alternate-authorization.host',
                               protocol: request.ssl? ? :https : :testing }
            }
          end
        end
      end

      it 'uses the discovery_url_options option when generating the webfinger endpoint url' do
        get :provider
        data = JSON.parse(response.body)

        expect(data['authorization_endpoint']).to eq 'testing://alternate-authorization.host/oauth/authorize'
      end
    end
  end

  describe '#keys' do
    subject { get :keys }

    shared_examples 'a key response' do |options|
      expected_parameters = options[:expected_parameters]

      it "includes only #{expected_parameters.join(', ')} parameters" do
        subject
        data = JSON.parse(response.body)
        key = data['keys'].first

        expect(key.keys.map(&:to_sym)).to match_array(expected_parameters)
      end
    end

    context 'when using an RSA key' do
      it_behaves_like 'a key response', expected_parameters: %i[kty kid e n use alg]
    end

    context 'when using an EC key' do
      before { configure_ec }

      it_behaves_like 'a key response', expected_parameters: %i[kty kid crv x y use alg]
    end

    context 'when using an HMAC key' do
      before { configure_hmac }

      it_behaves_like 'a key response', expected_parameters: %i[kty kid use alg]
    end
  end
end