google-auth-library-ruby/spec/googleauth/user_authorizer_spec.rb

324 lines
10 KiB
Ruby
Raw Normal View History

2015-10-14 20:53:37 +00:00
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-03-15 19:34:54 +00:00
spec_dir = File.expand_path File.join(File.dirname(__FILE__))
$LOAD_PATH.unshift spec_dir
2015-10-14 20:53:37 +00:00
$LOAD_PATH.uniq!
2019-03-15 19:34:54 +00:00
require "googleauth"
require "googleauth/user_authorizer"
require "uri"
require "multi_json"
require "spec_helper"
2015-10-14 20:53:37 +00:00
describe Google::Auth::UserAuthorizer do
include TestHelpers
2019-03-15 19:34:54 +00:00
let(:client_id) { Google::Auth::ClientId.new "testclient", "notasecret" }
let(:scope) { %w[email profile] }
2015-10-14 20:53:37 +00:00
let(:token_store) { DummyTokenStore.new }
2019-03-15 19:34:54 +00:00
let(:callback_uri) { "https://www.example.com/oauth/callback" }
let :authorizer do
2015-10-14 20:53:37 +00:00
Google::Auth::UserAuthorizer.new(client_id,
scope,
token_store,
callback_uri)
end
2019-03-15 19:34:54 +00:00
shared_examples "valid authorization url" do
it "should have a valid base URI" do
2015-10-14 20:53:37 +00:00
expect(uri).to match %r{https://accounts.google.com/o/oauth2/auth}
end
2019-03-15 19:34:54 +00:00
it "should request offline access" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/access_type=offline/)
end
2019-03-15 19:34:54 +00:00
it "should request response type code" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/response_type=code/)
end
2019-03-15 19:34:54 +00:00
it "should force approval" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/approval_prompt=force/)
end
2019-03-15 19:34:54 +00:00
it "should include granted scopes" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/include_granted_scopes=true/)
end
2019-03-15 19:34:54 +00:00
it "should include the correct client id" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/client_id=testclient/)
end
2019-03-15 19:34:54 +00:00
it "should not include a client secret" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to_not match(/client_secret/)
end
2019-03-15 19:34:54 +00:00
it "should include the callback uri" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(
%r{redirect_uri=https://www.example.com/oauth/callback}
)
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should include the scope" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/scope=email%20profile/)
end
end
2019-03-15 19:34:54 +00:00
context "when generating authorization URLs with user ID & state" do
let :uri do
authorizer.get_authorization_url login_hint: "user1", state: "mystate"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it_behaves_like "valid authorization url"
2015-10-14 20:53:37 +00:00
2019-03-15 19:34:54 +00:00
it "includes a login hint" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/login_hint=user1/)
end
2019-03-15 19:34:54 +00:00
it "includes the app state" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/state=mystate/)
end
end
2019-03-15 19:34:54 +00:00
context "when generating authorization URLs with user ID and no state" do
let(:uri) { authorizer.get_authorization_url login_hint: "user1" }
2015-10-14 20:53:37 +00:00
2019-03-15 19:34:54 +00:00
it_behaves_like "valid authorization url"
2015-10-14 20:53:37 +00:00
2019-03-15 19:34:54 +00:00
it "includes a login hint" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to match(/login_hint=user1/)
end
2019-03-15 19:34:54 +00:00
it "does not include the state parameter" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to_not match(/state/)
end
end
2019-03-15 19:34:54 +00:00
context "when generating authorization URLs with no user ID and no state" do
2015-10-14 20:53:37 +00:00
let(:uri) { authorizer.get_authorization_url }
2019-03-15 19:34:54 +00:00
it_behaves_like "valid authorization url"
2015-10-14 20:53:37 +00:00
2019-03-15 19:34:54 +00:00
it "does not include the login hint parameter" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to_not match(/login_hint/)
end
2019-03-15 19:34:54 +00:00
it "does not include the state parameter" do
2015-10-14 20:53:37 +00:00
expect(URI(uri).query).to_not match(/state/)
end
end
2019-03-15 19:34:54 +00:00
context "when retrieving tokens" do
let :token_json do
2015-10-14 20:53:37 +00:00
MultiJson.dump(
2019-03-15 19:34:54 +00:00
access_token: "accesstoken",
refresh_token: "refreshtoken",
expiration_time_millis: 1_441_234_742_000
)
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
context "with a valid user id" do
let :credentials do
token_store.store "user1", token_json
authorizer.get_credentials "user1"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return an instance of UserRefreshCredentials" do
2015-10-14 20:53:37 +00:00
expect(credentials).to be_instance_of(
Google::Auth::UserRefreshCredentials
)
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return credentials with a valid refresh token" do
expect(credentials.refresh_token).to eq "refreshtoken"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return credentials with a valid access token" do
expect(credentials.access_token).to eq "accesstoken"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return credentials with a valid client ID" do
expect(credentials.client_id).to eq "testclient"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return credentials with a valid client secret" do
expect(credentials.client_secret).to eq "notasecret"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return credentials with a valid scope" do
expect(credentials.scope).to eq %w[email profile]
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should return credentials with a valid expiration time" do
2015-10-14 20:53:37 +00:00
expect(credentials.expires_at).to eq Time.at(1_441_234_742)
end
end
2019-03-15 19:34:54 +00:00
context "with an invalid user id" do
it "should return nil" do
expect(authorizer.get_credentials("notauser")).to be_nil
2015-10-14 20:53:37 +00:00
end
end
end
2019-03-15 19:34:54 +00:00
context "when saving tokens" do
2015-10-14 20:53:37 +00:00
let(:expiry) { Time.now.to_i }
2019-03-15 19:34:54 +00:00
let :credentials do
2015-10-14 20:53:37 +00:00
Google::Auth::UserRefreshCredentials.new(
2019-03-15 19:34:54 +00:00
client_id: client_id.id,
2015-10-14 20:53:37 +00:00
client_secret: client_id.secret,
2019-03-15 19:34:54 +00:00
scope: scope,
refresh_token: "refreshtoken",
access_token: "accesstoken",
expires_at: expiry
2015-10-14 20:53:37 +00:00
)
end
2019-03-15 19:34:54 +00:00
let :token_json do
authorizer.store_credentials "user1", credentials
token_store.load "user1"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should persist in the token store" do
2015-10-14 20:53:37 +00:00
expect(token_json).to_not be_nil
end
2019-03-15 19:34:54 +00:00
it "should persist the refresh token" do
expect(MultiJson.load(token_json)["refresh_token"]).to eq "refreshtoken"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should persist the access token" do
expect(MultiJson.load(token_json)["access_token"]).to eq "accesstoken"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should persist the client id" do
expect(MultiJson.load(token_json)["client_id"]).to eq "testclient"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should persist the scope" do
expect(MultiJson.load(token_json)["scope"]).to include("email", "profile")
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should persist the expiry as milliseconds" do
2015-10-14 20:53:37 +00:00
expected_expiry = expiry * 1000
2019-03-15 19:34:54 +00:00
expect(MultiJson.load(token_json)["expiration_time_millis"]).to eql(
expected_expiry
)
2015-10-14 20:53:37 +00:00
end
end
2019-03-15 19:34:54 +00:00
context "with valid authorization code" do
let :token_json do
MultiJson.dump("access_token" => "1/abc123",
"token_type" => "Bearer",
"expires_in" => 3600)
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
before :example do
stub_request(:post, "https://oauth2.googleapis.com/token")
2015-10-14 20:53:37 +00:00
.to_return(body: token_json, status: 200, headers: {
2019-03-15 19:34:54 +00:00
"Content-Type" => "application/json"
})
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should exchange a code for credentials" do
2015-10-14 20:53:37 +00:00
credentials = authorizer.get_credentials_from_code(
2019-03-15 19:34:54 +00:00
user_id: "user1", code: "code"
)
2019-03-15 19:34:54 +00:00
expect(credentials.access_token).to eq "1/abc123"
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should not store credentials when get only requested" do
authorizer.get_credentials_from_code user_id: "user1", code: "code"
expect(token_store.load("user1")).to be_nil
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
it "should store credentials when requested" do
2015-10-14 20:53:37 +00:00
authorizer.get_and_store_credentials_from_code(
2019-03-15 19:34:54 +00:00
user_id: "user1", code: "code"
)
2019-03-15 19:34:54 +00:00
expect(token_store.load("user1")).to_not be_nil
2015-10-14 20:53:37 +00:00
end
end
2019-03-15 19:34:54 +00:00
context "with invalid authorization code" do
before :example do
stub_request(:post, "https://oauth2.googleapis.com/token")
2015-10-14 20:53:37 +00:00
.to_return(status: 400)
end
2019-03-15 19:34:54 +00:00
it "should raise an authorization error" do
2015-10-14 20:53:37 +00:00
expect do
2019-03-15 19:34:54 +00:00
authorizer.get_credentials_from_code user_id: "user1", code: "badcode"
2015-10-14 20:53:37 +00:00
end.to raise_error Signet::AuthorizationError
end
2019-03-15 19:34:54 +00:00
it "should not store credentials when exchange fails" do
2015-10-14 20:53:37 +00:00
expect do
2019-03-15 19:34:54 +00:00
authorizer.get_credentials_from_code user_id: "user1", code: "badcode"
2015-10-14 20:53:37 +00:00
end.to raise_error Signet::AuthorizationError
2019-03-15 19:34:54 +00:00
expect(token_store.load("user1")).to be_nil
2015-10-14 20:53:37 +00:00
end
end
2019-03-15 19:34:54 +00:00
context "when reovking authorization" do
let :token_json do
2015-10-14 20:53:37 +00:00
MultiJson.dump(
2019-03-15 19:34:54 +00:00
access_token: "accesstoken",
refresh_token: "refreshtoken",
expiration_time_millis: 1_441_234_742_000
)
2015-10-14 20:53:37 +00:00
end
2019-03-15 19:34:54 +00:00
before :example do
token_store.store "user1", token_json
stub_request(:post, "https://oauth2.googleapis.com/revoke")
.with(body: hash_including("token" => "refreshtoken"))
2015-10-14 20:53:37 +00:00
.to_return(status: 200)
end
2019-03-15 19:34:54 +00:00
it "should revoke the grant" do
authorizer.revoke_authorization "user1"
2015-10-14 20:53:37 +00:00
expect(a_request(
2019-03-15 19:34:54 +00:00
:post, "https://oauth2.googleapis.com/revoke"
).with(body: hash_including("token" => "refreshtoken")))
2015-10-14 20:53:37 +00:00
.to have_been_made
end
2019-03-15 19:34:54 +00:00
it "should remove the token from storage" do
authorizer.revoke_authorization "user1"
expect(token_store.load("user1")).to be_nil
2015-10-14 20:53:37 +00:00
end
end
# TODO: - Test that tokens are monitored
# TODO - Test scope enforcement (auth if upgrade required)
end