blob: 2bda76459d301da6565f98000a62457e8ca79fd1 [file] [log] [blame]
[email protected]a263ff52014-04-23 19:46:531// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Colin Blundell3e0efae2018-06-05 07:11:495#include "components/invalidation/impl/profile_identity_provider.h"
[email protected]a263ff52014-04-23 19:46:536
Sebastien Marchand53801a32019-01-25 16:26:117#include "base/bind.h"
Colin Blundell7b50d332018-06-28 14:42:528#include "components/invalidation/public/active_account_access_token_fetcher_impl.h"
[email protected]a263ff52014-04-23 19:46:539
Colin Blundell3e0efae2018-06-05 07:11:4910namespace invalidation {
11
Colin Blundell2a96a6912018-07-13 08:57:5112namespace {
13
14// ActiveAccountAccessTokenFetcher implementation that is backed by
15// IdentityManager and wraps an AccessTokenFetcher internally.
16class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher {
17 public:
18 AccessTokenFetcherAdaptor(const std::string& active_account_id,
19 const std::string& oauth_consumer_name,
20 identity::IdentityManager* identity_manager,
Mario Sanchez Pradac56d53c2018-10-29 12:31:1621 const identity::ScopeSet& scopes,
Colin Blundell2a96a6912018-07-13 08:57:5122 ActiveAccountAccessTokenCallback callback);
23 ~AccessTokenFetcherAdaptor() override = default;
24
25 private:
Colin Blundell80bede82018-07-16 11:18:2726 // Invokes |callback_| with (|error|, |access_token_info.token|).
27 void HandleTokenRequestCompletion(
28 GoogleServiceAuthError error,
29 identity::AccessTokenInfo access_token_info);
Colin Blundell2a96a6912018-07-13 08:57:5130
31 ActiveAccountAccessTokenCallback callback_;
32 std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_;
33
34 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor);
35};
36
37AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor(
38 const std::string& active_account_id,
39 const std::string& oauth_consumer_name,
40 identity::IdentityManager* identity_manager,
Mario Sanchez Pradac56d53c2018-10-29 12:31:1641 const identity::ScopeSet& scopes,
Colin Blundell2a96a6912018-07-13 08:57:5142 ActiveAccountAccessTokenCallback callback)
43 : callback_(std::move(callback)) {
44 access_token_fetcher_ = identity_manager->CreateAccessTokenFetcherForAccount(
45 active_account_id, oauth_consumer_name, scopes,
46 base::BindOnce(&AccessTokenFetcherAdaptor::HandleTokenRequestCompletion,
Marc Treib46f3a042018-08-13 09:12:4047 base::Unretained(this)),
48 identity::AccessTokenFetcher::Mode::kImmediate);
Colin Blundell2a96a6912018-07-13 08:57:5149}
50
51void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
52 GoogleServiceAuthError error,
Colin Blundell80bede82018-07-16 11:18:2753 identity::AccessTokenInfo access_token_info) {
Colin Blundell2a96a6912018-07-13 08:57:5154 access_token_fetcher_.reset();
55
Colin Blundell80bede82018-07-16 11:18:2756 std::move(callback_).Run(error, access_token_info.token);
Colin Blundell2a96a6912018-07-13 08:57:5157}
58
59} // namespace
60
[email protected]a263ff52014-04-23 19:46:5361ProfileIdentityProvider::ProfileIdentityProvider(
Colin Blundell2a96a6912018-07-13 08:57:5162 identity::IdentityManager* identity_manager)
63 : identity_manager_(identity_manager) {
64 identity_manager_->AddObserver(this);
[email protected]a263ff52014-04-23 19:46:5365}
66
67ProfileIdentityProvider::~ProfileIdentityProvider() {
Colin Blundell2a96a6912018-07-13 08:57:5168 identity_manager_->RemoveObserver(this);
[email protected]a263ff52014-04-23 19:46:5369}
70
[email protected]a263ff52014-04-23 19:46:5371std::string ProfileIdentityProvider::GetActiveAccountId() {
Tanja Gornak32e7d7f42018-08-31 10:33:2672 return active_account_id_;
[email protected]a263ff52014-04-23 19:46:5373}
74
Tanja Gornak16e197e2019-02-25 12:50:5675bool ProfileIdentityProvider::IsActiveAccountWithRefreshToken() {
Colin Blundell2a96a6912018-07-13 08:57:5176 if (GetActiveAccountId().empty() || !identity_manager_ ||
77 !identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId()))
Colin Blundella212f4f2018-06-05 14:13:1178 return false;
79
80 return true;
81}
82
Tanja Gornak32e7d7f42018-08-31 10:33:2683void ProfileIdentityProvider::SetActiveAccountId(
84 const std::string& account_id) {
85 if (account_id == active_account_id_)
86 return;
87
88 if (!active_account_id_.empty())
89 FireOnActiveAccountLogout();
90
91 active_account_id_ = account_id;
92 if (!active_account_id_.empty())
93 FireOnActiveAccountLogin();
94}
95
Colin Blundell7b50d332018-06-28 14:42:5296std::unique_ptr<ActiveAccountAccessTokenFetcher>
97ProfileIdentityProvider::FetchAccessToken(
98 const std::string& oauth_consumer_name,
Mario Sanchez Pradac56d53c2018-10-29 12:31:1699 const identity::ScopeSet& scopes,
Colin Blundell7b50d332018-06-28 14:42:52100 ActiveAccountAccessTokenCallback callback) {
Colin Blundell2a96a6912018-07-13 08:57:51101 return std::make_unique<AccessTokenFetcherAdaptor>(
102 GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes,
Colin Blundell7b50d332018-06-28 14:42:52103 std::move(callback));
104}
105
106void ProfileIdentityProvider::InvalidateAccessToken(
Mario Sanchez Pradac56d53c2018-10-29 12:31:16107 const identity::ScopeSet& scopes,
Colin Blundell7b50d332018-06-28 14:42:52108 const std::string& access_token) {
Colin Blundell2a96a6912018-07-13 08:57:51109 identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
110 access_token);
Colin Blundell7b50d332018-06-28 14:42:52111}
112
Colin Blundell2a96a6912018-07-13 08:57:51113void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
Gyuyoung Kimb70d3c72019-02-12 01:45:43114 const CoreAccountInfo& account_info) {
Colin Blundell2a96a6912018-07-13 08:57:51115 ProcessRefreshTokenUpdateForAccount(account_info.account_id);
Colin Blundelld0b89cd82018-06-29 13:39:00116}
117
Colin Blundell2a96a6912018-07-13 08:57:51118void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount(
Colin Blundell63d23fa2018-09-20 09:01:55119 const std::string& account_id) {
120 ProcessRefreshTokenRemovalForAccount(account_id);
Colin Blundelld0b89cd82018-06-29 13:39:00121}
122
Colin Blundell3e0efae2018-06-05 07:11:49123} // namespace invalidation