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
|
require 'spec_helper.rb'
describe Rack::OAuth2::Server::Authorize::Code do
let(:request) { Rack::MockRequest.new app }
let(:redirect_uri) { 'http://client.example.com/callback' }
let(:code_verifier) { SecureRandom.hex(16) }
let(:code_challenge) { Base64.urlsafe_encode64(OpenSSL::Digest::SHA256.digest(code_verifier)).delete('=') }
let(:code_challenge_method) { :S256 }
subject { @request }
describe 'authorization request' do
let :app do
Rack::OAuth2::Server::Authorize.new do |request, response|
@request = request
end
end
context 'when code_challenge is given' do
context 'when code_challenge_method is given' do
before do
request.get "/?response_type=code&client_id=client&redirect_uri=#{redirect_uri}&state=state&code_challenge=#{code_challenge}&code_challenge_method=#{code_challenge_method}"
end
its(:code_challenge) { should == code_challenge }
its(:code_challenge_method) { should == code_challenge_method.to_s }
end
context 'when code_challenge_method is omitted' do
before do
request.get "/?response_type=code&client_id=client&redirect_uri=#{redirect_uri}&state=state&code_challenge=#{code_challenge}"
end
its(:code_challenge) { should == code_challenge }
its(:code_challenge_method) { should == nil }
end
end
context 'otherwise' do
before do
request.get "/?response_type=code&client_id=client&redirect_uri=#{redirect_uri}&state=state"
end
its(:code_challenge) { should == nil }
its(:code_challenge_method) { should == nil }
end
end
describe 'token request' do
let(:app) do
Rack::OAuth2::Server::Token.new do |request, response|
@request = request
response.access_token = Rack::OAuth2::AccessToken::Bearer.new(access_token: 'access_token')
end
end
let(:default_params) do
{
grant_type: 'authorization_code',
client_id: 'client_id',
client_secret: 'client_secret',
code: 'authorization_code',
redirect_uri: 'http://client.example.com/callback'
}
end
context 'when code_verifier is given' do
before do
request.post '/', params: default_params.merge(
code_verifier: code_verifier
)
end
its(:code_verifier) { should == code_verifier }
describe '#verify_code_verifier!' do
context 'when code_verifier is given with code_challenge_method=plain' do
it do
expect do
subject.verify_code_verifier! code_verifier, :plain
end.not_to raise_error
end
end
context 'when collect code_challenge is given' do
it do
expect do
subject.verify_code_verifier! code_challenge
end.not_to raise_error
end
end
context 'when wrong code_challenge is blank' do
it do
expect do
subject.verify_code_verifier! 'wrong'
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
context 'when code_challenge is nil' do
it do
expect do
subject.verify_code_verifier! nil
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
context 'when unknown code_challenge_method is given' do
it do
expect do
subject.verify_code_verifier! code_challenge, :unknown
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
end
end
context 'otherwise' do
before do
request.post '/', params: default_params
end
its(:code_verifier) { should == nil }
describe '#verify_code_verifier!' do
context 'when code_verifier is given with code_challenge_method=plain' do
it do
expect do
subject.verify_code_verifier! code_verifier, :plain
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
context 'when collect code_challenge is given' do
it do
expect do
subject.verify_code_verifier! code_challenge
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
context 'when wrong code_challenge is blank' do
it do
expect do
subject.verify_code_verifier! 'wrong'
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
context 'when code_challenge is nil' do
it do
expect do
subject.verify_code_verifier! nil
end.not_to raise_error
end
end
context 'when unknown code_challenge_method is given' do
it do
expect do
subject.verify_code_verifier! code_challenge, :unknown
end.to raise_error Rack::OAuth2::Server::Token::BadRequest, /invalid_grant/
end
end
end
end
end
end
|