fake_profile_oauth2_token_service.cc revision f2477e01787aa58f445919b809d89e252beef54f
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) 73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { 83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { 113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// static 143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build( 153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) content::BrowserContext* profile) { 1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService(); 1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) service->Initialize(reinterpret_cast<Profile*>(profile)); 1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return service; 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() { 223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { 253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable( 2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& account_id) { 2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return !GetRefreshToken(account_id).empty(); 3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)} 3168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::UpdateCredentials( 33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& account_id, 34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& refresh_token) { 35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) IssueRefreshTokenForUser(account_id, refresh_token); 36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueRefreshToken( 393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& token) { 408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) IssueRefreshTokenForUser("account_id", token); 418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser( 448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const std::string& account_id, 458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const std::string& token) { 463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) refresh_token_ = token; 473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (refresh_token_.empty()) 488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) FireRefreshTokenRevoked(account_id); 493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) else 508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) FireRefreshTokenAvailable(account_id); 513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here? 523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueTokenForScope( 553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scope, 563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& access_token, 573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::Time& expiration) { 583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) CompleteRequests(false, 593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scope, 603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GoogleServiceAuthError::AuthErrorNone(), 613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) access_token, 623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) expiration); 633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueErrorForScope( 663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scope, 673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GoogleServiceAuthError& error) { 683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) CompleteRequests(false, scope, error, std::string(), base::Time()); 693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests( 723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GoogleServiceAuthError& error) { 733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) CompleteRequests(true, ScopeSet(), error, std::string(), base::Time()); 743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests( 773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& access_token, 783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::Time& expiration) { 793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) CompleteRequests(true, 803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ScopeSet(), 813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GoogleServiceAuthError::AuthErrorNone(), 823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) access_token, 833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) expiration); 843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::CompleteRequests( 873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) bool all_scopes, 883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scope, 893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const GoogleServiceAuthError& error, 903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& access_token, 913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::Time& expiration) { 923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests = 933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) GetPendingRequests(); 948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Walk the requests and notify the callbacks. 963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) it != pending_requests_.end(); ++it) { 983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (it->request && (all_scopes || it->scopes == scope)) 993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) it->request->InformConsumer(error, access_token, expiration); 1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) } 1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 10368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::string FakeProfileOAuth2TokenService::GetRefreshToken( 10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& account_id) { 1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return refresh_token_; 1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)net::URLRequestContextGetter* 1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::GetRequestContext() { 1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return NULL; 1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::RevokeCredentialsOnServer( 114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& refresh_token) { 115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Don't try to contact server in tests. 116f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)std::vector<FakeProfileOAuth2TokenService::PendingRequest> 1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::GetPendingRequests() { 1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::vector<PendingRequest> valid_requests; 1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); 1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) it != pending_requests_.end(); ++it) { 1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (it->request) 1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) valid_requests.push_back(*it); 1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) } 1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return valid_requests; 1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::FetchOAuth2Token( 1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) RequestImpl* request, 13168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& account_id, 1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) net::URLRequestContextGetter* getter, 1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& client_id, 1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const std::string& client_secret, 1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const ScopeSet& scopes) { 1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) PendingRequest pending_request; 13768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) pending_request.account_id = account_id; 1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.client_id = client_id; 1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.client_secret = client_secret; 1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.scopes = scopes; 1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_request.request = request->AsWeakPtr(); 1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) pending_requests_.push_back(pending_request); 1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::InvalidateOAuth2Token( 146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& account_id, 147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& client_id, 148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const ScopeSet& scopes, 149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::string& access_token) { 150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Do nothing, as we don't have a cache from which to remove the token. 151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 152