File: bearer_spec.rb

package info (click to toggle)
ruby-rack-oauth2 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: ruby: 4,013; makefile: 4
file content (123 lines) | stat: -rw-r--r-- 4,007 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
require 'spec_helper.rb'

describe Rack::OAuth2::Server::Resource::Bearer do
  let(:app) do
    Rack::OAuth2::Server::Resource::Bearer.new(simple_app) do |request|
      case request.access_token
      when 'valid_token'
        bearer_token
      when 'insufficient_scope_token'
        request.insufficient_scope!
      else
        request.invalid_token!
      end
    end
  end
  let(:bearer_token) do
    Rack::OAuth2::AccessToken::Bearer.new(access_token: 'valid_token')
  end
  let(:access_token) { env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN] }
  let(:request) { app.call(env) }
  subject { app.call(env) }

  shared_examples_for :authenticated_bearer_request do
    it 'should be authenticated' do
      status, headers, response = request
      status.should == 200
      access_token.should == bearer_token
    end
  end
  shared_examples_for :unauthorized_bearer_request do
    it 'should be unauthorized' do
      status, headers, response = request
      status.should == 401
      headers['WWW-Authenticate'].should include 'Bearer'
      access_token.should be_nil
    end
  end
  shared_examples_for :bad_bearer_request do
    it 'should be bad_request' do
      status, headers, response = request
      status.should == 400
      access_token.should be_nil
    end
  end
  shared_examples_for :skipped_authentication_request do
    it 'should skip OAuth 2.0 authentication' do
      status, headers, response = request
      status.should == 200
      access_token.should be_nil
    end
  end

  context 'when no access token is given' do
    let(:env) { Rack::MockRequest.env_for('/protected_resource') }
    it_behaves_like :skipped_authentication_request
  end

  context 'when valid_token is given' do
    context 'when token is in Authorization header' do
      let(:env) { Rack::MockRequest.env_for('/protected_resource', 'HTTP_AUTHORIZATION' => 'Bearer valid_token') }
      it_behaves_like :authenticated_bearer_request
    end

    context 'when token is in params' do
      let(:env) { Rack::MockRequest.env_for('/protected_resource', params: {access_token: 'valid_token'}) }
      it_behaves_like :authenticated_bearer_request
    end
  end

  context 'when invalid authorization header is given' do
    let(:env) { Rack::MockRequest.env_for('/protected_resource', 'HTTP_AUTHORIZATION' => '') }
    it_behaves_like :skipped_authentication_request
  end

  context 'when invalid_token is given' do
    let(:env) { Rack::MockRequest.env_for('/protected_resource', 'HTTP_AUTHORIZATION' => 'Bearer invalid_token') }

    context 'when token is in Authorization header' do
      it_behaves_like :unauthorized_bearer_request
    end

    context 'when token is in params' do
      let(:env) { Rack::MockRequest.env_for('/protected_resource', params: {access_token: 'invalid_token'}) }
      it_behaves_like :unauthorized_bearer_request
    end

    describe 'realm' do

      context 'when specified' do
        let(:realm) { 'server.example.com' }
        let(:app) do
          Rack::OAuth2::Server::Resource::Bearer.new(simple_app, realm) do |request|
            request.unauthorized!
          end
        end
        it 'should use specified realm' do
          status, headers, response = request
          headers['WWW-Authenticate'].should include "Bearer realm=\"#{realm}\""
        end
      end

      context 'otherwize' do
        it 'should use default realm' do
          status, headers, response = request
          headers['WWW-Authenticate'].should include "Bearer realm=\"#{Rack::OAuth2::Server::Resource::Bearer::DEFAULT_REALM}\""
        end
      end
    end
  end

  context 'when multiple access_token is given' do
    context 'when token is in Authorization header and params' do
      let(:env) do
        Rack::MockRequest.env_for(
          '/protected_resource',
          'HTTP_AUTHORIZATION' => 'Bearer valid_token',
          params: {access_token: 'valid_token'}
        )
      end
      it_behaves_like :bad_bearer_request
    end
  end
end