[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 1 | // 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 Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 5 | #include "components/invalidation/impl/profile_identity_provider.h" |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 6 | |
Sebastien Marchand | 53801a3 | 2019-01-25 16:26:11 | [diff] [blame] | 7 | #include "base/bind.h" |
Henrique Ferreiro | 960e6f2e | 2019-07-04 12:33:15 | [diff] [blame] | 8 | #include "components/signin/public/identity_manager/access_token_info.h" |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 9 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 10 | namespace invalidation { |
| 11 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 12 | namespace { |
| 13 | |
| 14 | // ActiveAccountAccessTokenFetcher implementation that is backed by |
| 15 | // IdentityManager and wraps an AccessTokenFetcher internally. |
| 16 | class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher { |
| 17 | public: |
Tanmoy Mollik | f95f618 | 2019-06-24 17:40:34 | [diff] [blame] | 18 | AccessTokenFetcherAdaptor(const CoreAccountId& active_account_id, |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 19 | const std::string& oauth_consumer_name, |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 20 | signin::IdentityManager* identity_manager, |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 21 | const signin::ScopeSet& scopes, |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 22 | ActiveAccountAccessTokenCallback callback); |
| 23 | ~AccessTokenFetcherAdaptor() override = default; |
| 24 | |
| 25 | private: |
Colin Blundell | 80bede8 | 2018-07-16 11:18:27 | [diff] [blame] | 26 | // Invokes |callback_| with (|error|, |access_token_info.token|). |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 27 | void HandleTokenRequestCompletion(GoogleServiceAuthError error, |
| 28 | signin::AccessTokenInfo access_token_info); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 29 | |
| 30 | ActiveAccountAccessTokenCallback callback_; |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 31 | std::unique_ptr<signin::AccessTokenFetcher> access_token_fetcher_; |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 32 | |
| 33 | DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor); |
| 34 | }; |
| 35 | |
| 36 | AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor( |
Tanmoy Mollik | f95f618 | 2019-06-24 17:40:34 | [diff] [blame] | 37 | const CoreAccountId& active_account_id, |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 38 | const std::string& oauth_consumer_name, |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 39 | signin::IdentityManager* identity_manager, |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 40 | const signin::ScopeSet& scopes, |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 41 | ActiveAccountAccessTokenCallback callback) |
| 42 | : callback_(std::move(callback)) { |
| 43 | access_token_fetcher_ = identity_manager->CreateAccessTokenFetcherForAccount( |
| 44 | active_account_id, oauth_consumer_name, scopes, |
| 45 | base::BindOnce(&AccessTokenFetcherAdaptor::HandleTokenRequestCompletion, |
Marc Treib | 46f3a04 | 2018-08-13 09:12:40 | [diff] [blame] | 46 | base::Unretained(this)), |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 47 | signin::AccessTokenFetcher::Mode::kImmediate); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion( |
| 51 | GoogleServiceAuthError error, |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 52 | signin::AccessTokenInfo access_token_info) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 53 | access_token_fetcher_.reset(); |
| 54 | |
Colin Blundell | 80bede8 | 2018-07-16 11:18:27 | [diff] [blame] | 55 | std::move(callback_).Run(error, access_token_info.token); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 60 | ProfileIdentityProvider::ProfileIdentityProvider( |
Miyoung Shin | 23737f6 | 2019-07-23 15:43:31 | [diff] [blame] | 61 | signin::IdentityManager* identity_manager) |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 62 | : identity_manager_(identity_manager) { |
| 63 | identity_manager_->AddObserver(this); |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | ProfileIdentityProvider::~ProfileIdentityProvider() { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 67 | identity_manager_->RemoveObserver(this); |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 68 | } |
| 69 | |
Tanmoy Mollik | f95f618 | 2019-06-24 17:40:34 | [diff] [blame] | 70 | CoreAccountId ProfileIdentityProvider::GetActiveAccountId() { |
Tanja Gornak | 32e7d7f4 | 2018-08-31 10:33:26 | [diff] [blame] | 71 | return active_account_id_; |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 72 | } |
| 73 | |
Tanja Gornak | 16e197e | 2019-02-25 12:50:56 | [diff] [blame] | 74 | bool ProfileIdentityProvider::IsActiveAccountWithRefreshToken() { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 75 | if (GetActiveAccountId().empty() || !identity_manager_ || |
| 76 | !identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId())) |
Colin Blundell | a212f4f | 2018-06-05 14:13:11 | [diff] [blame] | 77 | return false; |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
Tanja Gornak | 32e7d7f4 | 2018-08-31 10:33:26 | [diff] [blame] | 82 | void ProfileIdentityProvider::SetActiveAccountId( |
Tanmoy Mollik | f95f618 | 2019-06-24 17:40:34 | [diff] [blame] | 83 | const CoreAccountId& account_id) { |
Tanja Gornak | 32e7d7f4 | 2018-08-31 10:33:26 | [diff] [blame] | 84 | if (account_id == active_account_id_) |
| 85 | return; |
| 86 | |
| 87 | if (!active_account_id_.empty()) |
| 88 | FireOnActiveAccountLogout(); |
| 89 | |
| 90 | active_account_id_ = account_id; |
| 91 | if (!active_account_id_.empty()) |
| 92 | FireOnActiveAccountLogin(); |
| 93 | } |
| 94 | |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 95 | std::unique_ptr<ActiveAccountAccessTokenFetcher> |
| 96 | ProfileIdentityProvider::FetchAccessToken( |
| 97 | const std::string& oauth_consumer_name, |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 98 | const signin::ScopeSet& scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 99 | ActiveAccountAccessTokenCallback callback) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 100 | return std::make_unique<AccessTokenFetcherAdaptor>( |
| 101 | GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 102 | std::move(callback)); |
| 103 | } |
| 104 | |
| 105 | void ProfileIdentityProvider::InvalidateAccessToken( |
James Cook | faf1c10 | 2020-02-27 15:23:20 | [diff] [blame] | 106 | const signin::ScopeSet& scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 107 | const std::string& access_token) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 108 | identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes, |
| 109 | access_token); |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 110 | } |
| 111 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 112 | void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount( |
Gyuyoung Kim | b70d3c7 | 2019-02-12 01:45:43 | [diff] [blame] | 113 | const CoreAccountInfo& account_info) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 114 | ProcessRefreshTokenUpdateForAccount(account_info.account_id); |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame] | 115 | } |
| 116 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 117 | void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount( |
Tanmoy Mollik | 66a47f78 | 2019-05-20 15:05:18 | [diff] [blame] | 118 | const CoreAccountId& account_id) { |
Colin Blundell | 63d23fa | 2018-09-20 09:01:55 | [diff] [blame] | 119 | ProcessRefreshTokenRemovalForAccount(account_id); |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame] | 120 | } |
| 121 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 122 | } // namespace invalidation |