fake_profile_oauth2_token_service.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file. 43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h" 63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/message_loop/message_loop.h" 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/signin_account_id_helper.h" 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { 113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { 143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) : auto_post_fetch_response_on_message_loop_(false) { 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SigninAccountIdHelper::SetDisableForTest(true); 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SigninAccountIdHelper::SetDisableForTest(false); 233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable( 2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& account_id) { 2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return !GetRefreshToken(account_id).empty(); 2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)} 2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FakeProfileOAuth2TokenService::LoadCredentials( 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& primary_account_id) { 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Empty implementation as FakeProfileOAuth2TokenService does not have any 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // credentials to load. 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() { 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string> account_ids; 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (std::map<std::string, std::string>::const_iterator iter = 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) { 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) account_ids.push_back(iter->first); 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return account_ids; 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::UpdateCredentials( 46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& account_id, 47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& refresh_token) { 48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) IssueRefreshTokenForUser(account_id, refresh_token); 49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueRefreshToken( 523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& token) { 538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) IssueRefreshTokenForUser("account_id", token); 548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( 578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const std::string& account_id, 588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const std::string& token) { 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (token.empty()) { 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) refresh_tokens_.erase(account_id); 618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) FireRefreshTokenRevoked(account_id); 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } else { 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) refresh_tokens_[account_id] = token; 648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) FireRefreshTokenAvailable(account_id); 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& account_id, 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& access_token, 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::Time& expiration) { 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CompleteRequests(account_id, 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) true, 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ScopeSet(), 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GoogleServiceAuthError::AuthErrorNone(), 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) access_token, 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) expiration); 793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueTokenForScope( 823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scope, 833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& access_token, 843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::Time& expiration) { 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CompleteRequests("", 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) false, 873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scope, 883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GoogleServiceAuthError::AuthErrorNone(), 893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) access_token, 903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) expiration); 913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueErrorForScope( 943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scope, 953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GoogleServiceAuthError& error) { 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CompleteRequests("", false, scope, error, std::string(), base::Time()); 973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests( 1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GoogleServiceAuthError& error) { 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time()); 1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests( 1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& access_token, 1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::Time& expiration) { 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CompleteRequests("", 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) true, 1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ScopeSet(), 1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GoogleServiceAuthError::AuthErrorNone(), 1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) access_token, 1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) expiration); 1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::CompleteRequests( 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& account_id, 1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) bool all_scopes, 1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scope, 1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GoogleServiceAuthError& error, 1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& access_token, 1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::Time& expiration) { 1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests = 1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GetPendingRequests(); 1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Walk the requests and notify the callbacks. 1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) it != pending_requests_.end(); ++it) { 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (!it->request) 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) continue; 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool scope_matches = all_scopes || it->scopes == scope; 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool account_matches = account_id.empty() || account_id == it->account_id; 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (account_matches && scope_matches) 1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) it->request->InformConsumer(error, access_token, expiration); 1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) } 1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 13868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::string FakeProfileOAuth2TokenService::GetRefreshToken( 13968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& account_id) { 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return refresh_tokens_.count(account_id) > 0 ? refresh_tokens_[account_id] : 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string(); 1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)net::URLRequestContextGetter* 1453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::GetRequestContext() { 1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return NULL; 1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)std::vector<FakeProfileOAuth2TokenService::PendingRequest> 1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::GetPendingRequests() { 1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::vector<PendingRequest> valid_requests; 1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) it != pending_requests_.end(); ++it) { 1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (it->request) 1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) valid_requests.push_back(*it); 1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) } 1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return valid_requests; 1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::FetchOAuth2Token( 1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) RequestImpl* request, 16268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& account_id, 1633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) net::URLRequestContextGetter* getter, 1643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& client_id, 1653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& client_secret, 1663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scopes) { 1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) PendingRequest pending_request; 16868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) pending_request.account_id = account_id; 1693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.client_id = client_id; 1703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.client_secret = client_secret; 1713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.scopes = scopes; 1723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.request = request->AsWeakPtr(); 1733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_requests_.push_back(pending_request); 1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (auto_post_fetch_response_on_message_loop_) { 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) &FakeProfileOAuth2TokenService::IssueAllTokensForAccount, 1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::Unretained(this), 1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) account_id, 1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) "access_token", 1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::Time::Max())); 1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 184f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 185f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( 186f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& account_id, 187f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& client_id, 188f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const ScopeSet& scopes, 189f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& access_token) { 190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Do nothing, as we don't have a cache from which to remove the token. 191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 192