[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 | |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 7 | #include "components/invalidation/public/active_account_access_token_fetcher_impl.h" |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 8 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 9 | namespace invalidation { |
| 10 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 11 | namespace { |
| 12 | |
| 13 | // ActiveAccountAccessTokenFetcher implementation that is backed by |
| 14 | // IdentityManager and wraps an AccessTokenFetcher internally. |
| 15 | class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher { |
| 16 | public: |
| 17 | AccessTokenFetcherAdaptor(const std::string& active_account_id, |
| 18 | const std::string& oauth_consumer_name, |
| 19 | identity::IdentityManager* identity_manager, |
| 20 | const OAuth2TokenService::ScopeSet& scopes, |
| 21 | ActiveAccountAccessTokenCallback callback); |
| 22 | ~AccessTokenFetcherAdaptor() override = default; |
| 23 | |
| 24 | private: |
Colin Blundell | 80bede8 | 2018-07-16 11:18:27 | [diff] [blame] | 25 | // Invokes |callback_| with (|error|, |access_token_info.token|). |
| 26 | void HandleTokenRequestCompletion( |
| 27 | GoogleServiceAuthError error, |
| 28 | identity::AccessTokenInfo access_token_info); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 29 | |
| 30 | ActiveAccountAccessTokenCallback callback_; |
| 31 | std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_; |
| 32 | |
| 33 | DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor); |
| 34 | }; |
| 35 | |
| 36 | AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor( |
| 37 | const std::string& active_account_id, |
| 38 | const std::string& oauth_consumer_name, |
| 39 | identity::IdentityManager* identity_manager, |
| 40 | const OAuth2TokenService::ScopeSet& scopes, |
| 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)), |
| 47 | identity::AccessTokenFetcher::Mode::kImmediate); |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion( |
| 51 | GoogleServiceAuthError error, |
Colin Blundell | 80bede8 | 2018-07-16 11:18:27 | [diff] [blame] | 52 | identity::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( |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 61 | identity::IdentityManager* identity_manager) |
| 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 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 70 | std::string ProfileIdentityProvider::GetActiveAccountId() { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 71 | return identity_manager_->GetPrimaryAccountInfo().account_id; |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 72 | } |
| 73 | |
Colin Blundell | a212f4f | 2018-06-05 14:13:11 | [diff] [blame] | 74 | bool ProfileIdentityProvider::IsActiveAccountAvailable() { |
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 | |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 82 | std::unique_ptr<ActiveAccountAccessTokenFetcher> |
| 83 | ProfileIdentityProvider::FetchAccessToken( |
| 84 | const std::string& oauth_consumer_name, |
| 85 | const OAuth2TokenService::ScopeSet& scopes, |
| 86 | ActiveAccountAccessTokenCallback callback) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 87 | return std::make_unique<AccessTokenFetcherAdaptor>( |
| 88 | GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes, |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 89 | std::move(callback)); |
| 90 | } |
| 91 | |
| 92 | void ProfileIdentityProvider::InvalidateAccessToken( |
| 93 | const OAuth2TokenService::ScopeSet& scopes, |
| 94 | const std::string& access_token) { |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 95 | identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes, |
| 96 | access_token); |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 97 | } |
| 98 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 99 | void ProfileIdentityProvider::OnPrimaryAccountSet( |
| 100 | const AccountInfo& primary_account_info) { |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 101 | FireOnActiveAccountLogin(); |
| 102 | } |
| 103 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 104 | void ProfileIdentityProvider::OnPrimaryAccountCleared( |
| 105 | const AccountInfo& previous_primary_account_info) { |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 106 | FireOnActiveAccountLogout(); |
| 107 | } |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 108 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 109 | void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount( |
| 110 | const AccountInfo& account_info, |
| 111 | bool is_valid) { |
| 112 | ProcessRefreshTokenUpdateForAccount(account_info.account_id); |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame] | 113 | } |
| 114 | |
Colin Blundell | 2a96a691 | 2018-07-13 08:57:51 | [diff] [blame] | 115 | void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount( |
| 116 | const AccountInfo& account_info) { |
| 117 | ProcessRefreshTokenRemovalForAccount(account_info.account_id); |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame] | 118 | } |
| 119 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 120 | } // namespace invalidation |