1 # Copyright 2012 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 require 'google/api_client'
19 fixtures_path = File.expand_path('../../../fixtures', __FILE__)
21 describe Google::APIClient::KeyUtils do
22 it 'should read PKCS12 files from the filesystem' do
23 pending "Reading from PKCS12 not supported on jruby" if RUBY_PLATFORM == 'java'
24 path = File.expand_path('files/privatekey.p12', fixtures_path)
25 key = Google::APIClient::KeyUtils.load_from_pkcs12(path, 'notasecret')
29 it 'should read PKCS12 files from loaded files' do
30 pending "Reading from PKCS12 not supported on jruby" if RUBY_PLATFORM == 'java'
31 path = File.expand_path('files/privatekey.p12', fixtures_path)
32 content = File.read(path)
33 key = Google::APIClient::KeyUtils.load_from_pkcs12(content, 'notasecret')
37 it 'should read PEM files from the filesystem' do
38 path = File.expand_path('files/secret.pem', fixtures_path)
39 key = Google::APIClient::KeyUtils.load_from_pem(path, 'notasecret')
43 it 'should read PEM files from loaded files' do
44 path = File.expand_path('files/secret.pem', fixtures_path)
45 content = File.read(path)
46 key = Google::APIClient::KeyUtils.load_from_pem(content, 'notasecret')
52 describe Google::APIClient::JWTAsserter do
53 include ConnectionHelpers
56 @key = OpenSSL::PKey::RSA.new 2048
59 it 'should generate valid JWTs' do
60 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
61 jwt = asserter.to_authorization.to_jwt
64 claim = JWT.decode(jwt, @key.public_key, true)
65 claim["iss"].should == 'client1'
66 claim["scope"].should == 'scope1 scope2'
69 it 'should allow impersonation' do
70 conn = stub_connection do |stub|
71 stub.post('/o/oauth2/token') do |env|
72 params = Addressable::URI.form_unencode(env[:body])
73 JWT.decode(params.assoc("assertion").last, @key.public_key)
74 params.assoc("grant_type").should == ['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer']
76 "access_token" : "1/abcdef1234567890",
77 "token_type" : "Bearer",
82 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
83 auth = asserter.authorize('user1@email.com', { :connection => conn })
84 auth.should_not == nil?
85 auth.person.should == 'user1@email.com'
89 it 'should send valid access token request' do
90 conn = stub_connection do |stub|
91 stub.post('/o/oauth2/token') do |env|
92 params = Addressable::URI.form_unencode(env[:body])
93 JWT.decode(params.assoc("assertion").last, @key.public_key)
94 params.assoc("grant_type").should == ['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer']
96 "access_token" : "1/abcdef1234567890",
97 "token_type" : "Bearer",
102 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
103 auth = asserter.authorize(nil, { :connection => conn })
104 auth.should_not == nil?
105 auth.access_token.should == "1/abcdef1234567890"
109 it 'should be refreshable' do
110 conn = stub_connection do |stub|
111 stub.post('/o/oauth2/token') do |env|
112 params = Addressable::URI.form_unencode(env[:body])
113 JWT.decode(params.assoc("assertion").last, @key.public_key)
114 params.assoc("grant_type").should == ['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer']
116 "access_token" : "1/abcdef1234567890",
117 "token_type" : "Bearer",
121 stub.post('/o/oauth2/token') do |env|
122 params = Addressable::URI.form_unencode(env[:body])
123 JWT.decode(params.assoc("assertion").last, @key.public_key)
124 params.assoc("grant_type").should == ['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer']
126 "access_token" : "1/0987654321fedcba",
127 "token_type" : "Bearer",
132 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
133 auth = asserter.authorize(nil, { :connection => conn })
134 auth.should_not == nil?
135 auth.access_token.should == "1/abcdef1234567890"
137 auth.fetch_access_token!(:connection => conn)
138 auth.access_token.should == "1/0987654321fedcba"