2012-06-07 00:25:44 +00:00
|
|
|
# Copyright 2012 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
require 'google/api_client'
|
|
|
|
|
2012-11-02 20:56:53 +00:00
|
|
|
fixtures_path = File.expand_path('../../../fixtures', __FILE__)
|
|
|
|
|
2014-09-17 15:03:33 +00:00
|
|
|
RSpec.describe Google::APIClient::KeyUtils do
|
2012-11-02 20:56:53 +00:00
|
|
|
it 'should read PKCS12 files from the filesystem' do
|
2014-08-14 16:37:47 +00:00
|
|
|
if RUBY_PLATFORM == 'java' && RUBY_VERSION.start_with?('1.8')
|
|
|
|
pending "Reading from PKCS12 not supported on jruby 1.8.x"
|
|
|
|
end
|
2012-11-02 20:56:53 +00:00
|
|
|
path = File.expand_path('files/privatekey.p12', fixtures_path)
|
|
|
|
key = Google::APIClient::KeyUtils.load_from_pkcs12(path, 'notasecret')
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(key).not_to eq(nil)
|
2012-11-02 20:56:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should read PKCS12 files from loaded files' do
|
2014-08-14 16:37:47 +00:00
|
|
|
if RUBY_PLATFORM == 'java' && RUBY_VERSION.start_with?('1.8')
|
|
|
|
pending "Reading from PKCS12 not supported on jruby 1.8.x"
|
|
|
|
end
|
2012-11-02 20:56:53 +00:00
|
|
|
path = File.expand_path('files/privatekey.p12', fixtures_path)
|
|
|
|
content = File.read(path)
|
|
|
|
key = Google::APIClient::KeyUtils.load_from_pkcs12(content, 'notasecret')
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(key).not_to eq(nil)
|
2012-11-02 20:56:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should read PEM files from the filesystem' do
|
|
|
|
path = File.expand_path('files/secret.pem', fixtures_path)
|
|
|
|
key = Google::APIClient::KeyUtils.load_from_pem(path, 'notasecret')
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(key).not_to eq(nil)
|
2012-11-02 20:56:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should read PEM files from loaded files' do
|
|
|
|
path = File.expand_path('files/secret.pem', fixtures_path)
|
|
|
|
content = File.read(path)
|
|
|
|
key = Google::APIClient::KeyUtils.load_from_pem(content, 'notasecret')
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(key).not_to eq(nil)
|
2012-11-02 20:56:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-09-17 15:03:33 +00:00
|
|
|
RSpec.describe Google::APIClient::JWTAsserter do
|
2012-09-24 23:09:17 +00:00
|
|
|
include ConnectionHelpers
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-07 00:25:44 +00:00
|
|
|
before do
|
|
|
|
@key = OpenSSL::PKey::RSA.new 2048
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-07 00:25:44 +00:00
|
|
|
it 'should generate valid JWTs' do
|
|
|
|
asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
|
2013-01-02 19:50:45 +00:00
|
|
|
jwt = asserter.to_authorization.to_jwt
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(jwt).not_to eq(nil)
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-07 00:25:44 +00:00
|
|
|
claim = JWT.decode(jwt, @key.public_key, true)
|
2014-08-14 16:06:44 +00:00
|
|
|
claim = claim[0] if claim[0]
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(claim["iss"]).to eq('client1')
|
|
|
|
expect(claim["scope"]).to eq('scope1 scope2')
|
2012-06-07 00:25:44 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2013-01-12 15:29:47 +00:00
|
|
|
it 'should allow impersonation' do
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/o/oauth2/token') do |env|
|
|
|
|
params = Addressable::URI.form_unencode(env[:body])
|
|
|
|
JWT.decode(params.assoc("assertion").last, @key.public_key)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(params.assoc("grant_type")).to eq(['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer'])
|
2014-12-15 19:44:51 +00:00
|
|
|
[200, {'content-type' => 'application/json'}, '{
|
2013-01-12 15:29:47 +00:00
|
|
|
"access_token" : "1/abcdef1234567890",
|
|
|
|
"token_type" : "Bearer",
|
|
|
|
"expires_in" : 3600
|
|
|
|
}']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
|
|
|
|
auth = asserter.authorize('user1@email.com', { :connection => conn })
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(auth).not_to eq(nil?)
|
|
|
|
expect(auth.person).to eq('user1@email.com')
|
2013-01-12 15:29:47 +00:00
|
|
|
conn.verify
|
|
|
|
end
|
|
|
|
|
2012-06-07 00:25:44 +00:00
|
|
|
it 'should send valid access token request' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
2012-06-07 00:25:44 +00:00
|
|
|
stub.post('/o/oauth2/token') do |env|
|
|
|
|
params = Addressable::URI.form_unencode(env[:body])
|
|
|
|
JWT.decode(params.assoc("assertion").last, @key.public_key)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(params.assoc("grant_type")).to eq(['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer'])
|
2014-12-15 19:44:51 +00:00
|
|
|
[200, {'content-type' => 'application/json'}, '{
|
2012-06-07 00:25:44 +00:00
|
|
|
"access_token" : "1/abcdef1234567890",
|
|
|
|
"token_type" : "Bearer",
|
|
|
|
"expires_in" : 3600
|
|
|
|
}']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
|
2012-09-24 23:09:17 +00:00
|
|
|
auth = asserter.authorize(nil, { :connection => conn })
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(auth).not_to eq(nil?)
|
|
|
|
expect(auth.access_token).to eq("1/abcdef1234567890")
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2012-06-07 00:25:44 +00:00
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
|
2012-10-30 20:18:12 +00:00
|
|
|
it 'should be refreshable' do
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/o/oauth2/token') do |env|
|
|
|
|
params = Addressable::URI.form_unencode(env[:body])
|
|
|
|
JWT.decode(params.assoc("assertion").last, @key.public_key)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(params.assoc("grant_type")).to eq(['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer'])
|
2014-12-15 19:44:51 +00:00
|
|
|
[200, {'content-type' => 'application/json'}, '{
|
2012-10-30 20:18:12 +00:00
|
|
|
"access_token" : "1/abcdef1234567890",
|
|
|
|
"token_type" : "Bearer",
|
|
|
|
"expires_in" : 3600
|
|
|
|
}']
|
|
|
|
end
|
|
|
|
stub.post('/o/oauth2/token') do |env|
|
|
|
|
params = Addressable::URI.form_unencode(env[:body])
|
|
|
|
JWT.decode(params.assoc("assertion").last, @key.public_key)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(params.assoc("grant_type")).to eq(['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer'])
|
2014-12-15 19:44:51 +00:00
|
|
|
[200, {'content-type' => 'application/json'}, '{
|
2012-10-30 20:18:12 +00:00
|
|
|
"access_token" : "1/0987654321fedcba",
|
|
|
|
"token_type" : "Bearer",
|
|
|
|
"expires_in" : 3600
|
|
|
|
}']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
|
|
|
|
auth = asserter.authorize(nil, { :connection => conn })
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(auth).not_to eq(nil?)
|
|
|
|
expect(auth.access_token).to eq("1/abcdef1234567890")
|
|
|
|
|
2012-10-30 20:18:12 +00:00
|
|
|
auth.fetch_access_token!(:connection => conn)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(auth.access_token).to eq("1/0987654321fedcba")
|
|
|
|
|
2012-10-30 20:18:12 +00:00
|
|
|
conn.verify
|
2014-08-14 15:54:16 +00:00
|
|
|
end
|
2012-06-07 00:25:44 +00:00
|
|
|
end
|
|
|
|
|
2014-09-17 15:03:33 +00:00
|
|
|
RSpec.describe Google::APIClient::ComputeServiceAccount do
|
2013-08-14 23:29:16 +00:00
|
|
|
include ConnectionHelpers
|
|
|
|
|
|
|
|
it 'should query metadata server' do
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/computeMetadata/v1beta1/instance/service-accounts/default/token') do |env|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env.url.host).to eq('metadata')
|
2014-12-15 19:44:51 +00:00
|
|
|
[200, {'content-type' => 'application/json'}, '{
|
2013-08-14 23:29:16 +00:00
|
|
|
"access_token" : "1/abcdef1234567890",
|
|
|
|
"token_type" : "Bearer",
|
|
|
|
"expires_in" : 3600
|
|
|
|
}']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
service_account = Google::APIClient::ComputeServiceAccount.new
|
|
|
|
auth = service_account.fetch_access_token!({ :connection => conn })
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(auth).not_to eq(nil?)
|
|
|
|
expect(auth["access_token"]).to eq("1/abcdef1234567890")
|
2013-08-14 23:29:16 +00:00
|
|
|
conn.verify
|
|
|
|
end
|
|
|
|
end
|