fake_profile_oauth2_token_service.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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"
8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/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()
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : auto_post_fetch_response_on_message_loop_(false),
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      weak_ptr_factory_(this) {
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SigninAccountIdHelper::SetDisableForTest(true);
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SigninAccountIdHelper::SetDisableForTest(false);
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& account_id) const {
2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return !GetRefreshToken(account_id).empty();
2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FakeProfileOAuth2TokenService::LoadCredentials(
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& primary_account_id) {
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Empty implementation as FakeProfileOAuth2TokenService does not have any
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // credentials to load.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() {
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<std::string> account_ids;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (std::map<std::string, std::string>::const_iterator iter =
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    account_ids.push_back(iter->first);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return account_ids;
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::UpdateCredentials(
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::string& account_id,
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::string& refresh_token) {
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  IssueRefreshTokenForUser(account_id, refresh_token);
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueRefreshToken(
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& token) {
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  IssueRefreshTokenForUser("account_id", token);
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const std::string& account_id,
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const std::string& token) {
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (token.empty()) {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    refresh_tokens_.erase(account_id);
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    FireRefreshTokenRevoked(account_id);
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    refresh_tokens_[account_id] = token;
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    FireRefreshTokenAvailable(account_id);
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& access_token,
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Time& expiration) {
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompleteRequests(account_id,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   true,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   ScopeSet(),
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   GoogleServiceAuthError::AuthErrorNone(),
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   access_token,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   expiration);
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueTokenForScope(
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const ScopeSet& scope,
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& access_token,
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const base::Time& expiration) {
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompleteRequests("",
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   false,
883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   scope,
893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   GoogleServiceAuthError::AuthErrorNone(),
903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   access_token,
913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   expiration);
923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueErrorForScope(
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const ScopeSet& scope,
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const GoogleServiceAuthError& error) {
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompleteRequests("", false, scope, error, std::string(), base::Time());
983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const GoogleServiceAuthError& error) {
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& access_token,
1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const base::Time& expiration) {
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompleteRequests("",
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   true,
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   ScopeSet(),
1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   GoogleServiceAuthError::AuthErrorNone(),
1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   access_token,
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                   expiration);
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::CompleteRequests(
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& account_id,
1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    bool all_scopes,
1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const ScopeSet& scope,
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const GoogleServiceAuthError& error,
1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& access_token,
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const base::Time& expiration) {
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      GetPendingRequests();
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Walk the requests and notify the callbacks.
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)       it != pending_requests_.end(); ++it) {
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!it->request)
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      continue;
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool scope_matches = all_scopes || it->scopes == scope;
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool account_matches = account_id.empty() || account_id == it->account_id;
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (account_matches && scope_matches)
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      it->request->InformConsumer(error, access_token, expiration);
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
13968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::string FakeProfileOAuth2TokenService::GetRefreshToken(
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& account_id) const {
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::map<std::string, std::string>::const_iterator it =
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      refresh_tokens_.find(account_id);
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (it != refresh_tokens_.end())
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return it->second;
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return std::string();
1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)net::URLRequestContextGetter*
1493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::GetRequestContext() {
1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return NULL;
1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)std::vector<FakeProfileOAuth2TokenService::PendingRequest>
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)FakeProfileOAuth2TokenService::GetPendingRequests() {
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  std::vector<PendingRequest> valid_requests;
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)       it != pending_requests_.end(); ++it) {
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (it->request)
1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      valid_requests.push_back(*it);
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return valid_requests;
1623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void FakeProfileOAuth2TokenService::FetchOAuth2Token(
1653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    RequestImpl* request,
16668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    const std::string& account_id,
1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    net::URLRequestContextGetter* getter,
1683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& client_id,
1693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const std::string& client_secret,
1703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const ScopeSet& scopes) {
1713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  PendingRequest pending_request;
17268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  pending_request.account_id = account_id;
1733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  pending_request.client_id = client_id;
1743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  pending_request.client_secret = client_secret;
1753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  pending_request.scopes = scopes;
1763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  pending_request.request = request->AsWeakPtr();
1773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  pending_requests_.push_back(pending_request);
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (auto_post_fetch_response_on_message_loop_) {
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        &FakeProfileOAuth2TokenService::IssueAllTokensForAccount,
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        weak_ptr_factory_.GetWeakPtr(),
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        account_id,
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        "access_token",
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::Time::Max()));
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
188f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)OAuth2AccessTokenFetcher*
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)FakeProfileOAuth2TokenService::CreateAccessTokenFetcher(
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& account_id,
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    net::URLRequestContextGetter* getter,
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    OAuth2AccessTokenConsumer* consumer) {
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NOTREACHED();
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return NULL;
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
198f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
199f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::string& account_id,
200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::string& client_id,
201f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ScopeSet& scopes,
202f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const std::string& access_token) {
203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Do nothing, as we don't have a cache from which to remove the token.
204f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
205