[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" |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 8 | #include "components/invalidation/public/active_account_access_token_fetcher_impl.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: |
| 18 | AccessTokenFetcherAdaptor(const std::string& active_account_id, |
| 19 | const std::string& oauth_consumer_name, |
| 20 | identity::IdentityManager* identity_manager, |
Mario Sanchez Prada | c56d53c | 2018-10-29 12:31:16 | [diff] [blame] | 21 | const identity::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|). |
| 27 | void HandleTokenRequestCompletion( |
| 28 | GoogleServiceAuthError error, |
| 29 | identity::AccessTokenInfo access_token_info); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 30 | |
| 31 | ActiveAccountAccessTokenCallback callback_; |
| 32 | std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_; |
| 33 | |
| 34 | DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor); |
| 35 | }; |
| 36 | |
| 37 | AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor( |
| 38 | const std::string& active_account_id, |
| 39 | const std::string& oauth_consumer_name, |
| 40 | identity::IdentityManager* identity_manager, |
Mario Sanchez Prada | c56d53c | 2018-10-29 12:31:16 | [diff] [blame] | 41 | const identity::ScopeSet& scopes, |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 42 | 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 Treib | 46f3a04 | 2018-08-13 09:12:40 | [diff] [blame] | 47 | base::Unretained(this)), |
| 48 | identity::AccessTokenFetcher::Mode::kImmediate); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion( |
| 52 | GoogleServiceAuthError error, |
Colin Blundell | 80bede8 | 2018-07-16 11:18:27 | [diff] [blame] | 53 | identity::AccessTokenInfo access_token_info) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 54 | access_token_fetcher_.reset(); |
| 55 | |
Colin Blundell | 80bede8 | 2018-07-16 11:18:27 | [diff] [blame] | 56 | std::move(callback_).Run(error, access_token_info.token); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 61 | ProfileIdentityProvider::ProfileIdentityProvider( |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 62 | identity::IdentityManager* identity_manager) |
| 63 | : identity_manager_(identity_manager) { |
| 64 | identity_manager_->AddObserver(this); |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ProfileIdentityProvider::~ProfileIdentityProvider() { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 68 | identity_manager_->RemoveObserver(this); |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 71 | std::string ProfileIdentityProvider::GetActiveAccountId() { |
Tanja Gornak | 32e7d7f4 | 2018-08-31 10:33:26 | [diff] [blame] | 72 | return active_account_id_; |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 73 | } |
| 74 | |
Tanja Gornak | 16e197e | 2019-02-25 12:50:56 | [diff] [blame^] | 75 | bool ProfileIdentityProvider::IsActiveAccountWithRefreshToken() { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 76 | if (GetActiveAccountId().empty() || !identity_manager_ || |
| 77 | !identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId())) |
Colin Blundell | a212f4f | 2018-06-05 14:13:11 | [diff] [blame] | 78 | return false; |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
Tanja Gornak | 32e7d7f4 | 2018-08-31 10:33:26 | [diff] [blame] | 83 | void 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 Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 96 | std::unique_ptr<ActiveAccountAccessTokenFetcher> |
| 97 | ProfileIdentityProvider::FetchAccessToken( |
| 98 | const std::string& oauth_consumer_name, |
Mario Sanchez Prada | c56d53c | 2018-10-29 12:31:16 | [diff] [blame] | 99 | const identity::ScopeSet& scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 100 | ActiveAccountAccessTokenCallback callback) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 101 | return std::make_unique<AccessTokenFetcherAdaptor>( |
| 102 | GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 103 | std::move(callback)); |
| 104 | } |
| 105 | |
| 106 | void ProfileIdentityProvider::InvalidateAccessToken( |
Mario Sanchez Prada | c56d53c | 2018-10-29 12:31:16 | [diff] [blame] | 107 | const identity::ScopeSet& scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 108 | const std::string& access_token) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 109 | identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes, |
| 110 | access_token); |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 111 | } |
| 112 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 113 | void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount( |
Gyuyoung Kim | b70d3c7 | 2019-02-12 01:45:43 | [diff] [blame] | 114 | const CoreAccountInfo& account_info) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 115 | ProcessRefreshTokenUpdateForAccount(account_info.account_id); |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame] | 116 | } |
| 117 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 118 | void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount( |
Colin Blundell | 63d23fa | 2018-09-20 09:01:55 | [diff] [blame] | 119 | const std::string& account_id) { |
| 120 | ProcessRefreshTokenRemovalForAccount(account_id); |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame] | 121 | } |
| 122 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 123 | } // namespace invalidation |