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
|
require 'spec_helper.rb'
require 'base64'
describe Rack::OAuth2::Server::Token do
let(:request) { Rack::MockRequest.new app }
let(:app) do
Rack::OAuth2::Server::Token.new do |request, response|
response.access_token = Rack::OAuth2::AccessToken::Bearer.new(access_token: 'access_token')
end
end
let(:params) do
{
grant_type: 'authorization_code',
client_id: 'client_id',
code: 'authorization_code',
redirect_uri: 'http://client.example.com/callback'
}
end
subject { request.post('/token', params: params) }
context 'when multiple client credentials are given' do
context 'when different credentials are given' do
let(:env) do
Rack::MockRequest.env_for(
'/token',
'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('client_id2:client_secret')}",
params: params
)
end
it 'should fail with unsupported_grant_type' do
status, headers, response = app.call(env)
status.should == 400
response.first.should include '"error":"invalid_request"'
end
end
context 'when same credentials are given' do
let(:env) do
Rack::MockRequest.env_for(
'/token',
'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('client_id:client_secret')}",
params: params
)
end
it 'should ignore duplicates' do
status, headers, response = app.call(env)
status.should == 200
end
end
end
context 'when unsupported grant_type is given' do
before do
params.merge!(grant_type: 'unknown')
end
its(:status) { should == 400 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"error":"unsupported_grant_type"' }
end
[:client_id, :grant_type].each do |required|
context "when #{required} is missing" do
before do
params.delete_if do |key, value|
key == required
end
end
its(:status) { should == 400 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"error":"invalid_request"' }
end
end
context 'when client_id is given via JWT client assertion' do
before do
require 'json/jwt'
params[:client_assertion] = JSON::JWT.new(
sub: params[:client_id]
# NOTE: actual client_assertion should have more claims.
).sign('client_secret').to_s
params[:client_assertion_type] = Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER
params.delete(:client_id)
end
context 'when client_assertion is invalid JWT' do
before do
params[:client_assertion] = 'invalid-jwt'
end
its(:status) { should == 400 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"error":"invalid_request"' }
end
context 'when client_assertion_type is missing' do
before do
params.delete(:client_assertion_type)
end
its(:status) { should == 400 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"error":"invalid_request"' }
end
context 'when client_assertion_type is unknown' do
before do
params[:client_assertion_type] = 'unknown'
end
its(:status) { should == 400 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"error":"invalid_request"' }
end
context 'when client_assertion issuer is different from client_id' do
before do
params[:client_id] = 'another_client_id'
end
its(:status) { should == 400 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"error":"invalid_request"' }
end
context 'otherwise' do
its(:status) { should == 200 }
its(:content_type) { should == 'application/json' }
its(:body) { should include '"access_token":"access_token"' }
end
end
Rack::OAuth2::Server::Token::ErrorMethods::DEFAULT_DESCRIPTION.each do |error, default_message|
status = if error == :invalid_client
401
else
400
end
context "when #{error}" do
let(:app) do
Rack::OAuth2::Server::Token.new do |request, response|
request.send "#{error}!"
end
end
its(:status) { should == status }
its(:content_type) { should == 'application/json' }
its(:body) { should include "\"error\":\"#{error}\"" }
its(:body) { should include "\"error_description\":\"#{default_message}\"" }
if error == :invalid_client
its(:headers) { should include 'WWW-Authenticate' }
end
end
end
context 'when skip_www_authenticate option is specified on invalid_client' do
let(:app) do
Rack::OAuth2::Server::Token.new do |request, response|
request.invalid_client!(
Rack::OAuth2::Server::Token::ErrorMethods::DEFAULT_DESCRIPTION[:invalid_client],
skip_www_authenticate: true
)
end
end
its(:headers) { should_not include 'WWW-Authenticate' }
end
context 'when responding' do
context 'when access_token is missing' do
let(:app) do
Rack::OAuth2::Server::Token.new
end
it do
expect { request.post('/', params: params) }.to raise_error AttrRequired::AttrMissing
end
end
end
describe 'extensibility' do
before do
require 'rack/oauth2/server/token/extension/example'
end
subject { app }
let(:env) do
Rack::MockRequest.env_for(
'/token',
params: params
)
end
let(:request) { Rack::OAuth2::Server::Token::Request.new env }
its(:extensions) { should == [Rack::OAuth2::Server::Token::Extension::Example] }
describe 'JWT assertion' do
let(:params) do
{
grant_type: 'urn:ietf:params:oauth:grant-type:example',
assertion: 'header.payload.signature'
}
end
it do
app.send(
:grant_type_for, request
).should == Rack::OAuth2::Server::Token::Extension::Example
end
end
end
end
|