| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 | |
| 5 #include "blimp/client/core/session/identity_source.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "blimp/client/core/switches/blimp_client_switches.h" | |
| 12 | |
| 13 namespace blimp { | |
| 14 namespace client { | |
| 15 | |
| 16 namespace { | |
| 17 // OAuth2 token scope. | |
| 18 const char kOAuth2TokenScope[] = | |
| 19 "https://www.googleapis.com/auth/userinfo.email"; | |
| 20 | |
| 21 // Max retry times when OAuth2 token request is canceled. | |
| 22 const int kTokenRequestCancelMaxRetry = 3; | |
| 23 } // namespace | |
| 24 | |
| 25 IdentitySource::IdentitySource( | |
| 26 std::unique_ptr<IdentityProvider> identity_provider, | |
| 27 const base::Callback<void(const GoogleServiceAuthError&)>& error_callback, | |
| 28 const TokenCallback& callback) | |
| 29 : OAuth2TokenService::Consumer("blimp_client"), | |
| 30 identity_provider_(std::move(identity_provider)), | |
| 31 error_callback_(error_callback), | |
| 32 token_callback_(callback), | |
| 33 is_fetching_token_(false), | |
| 34 retry_times_(0) { | |
| 35 DCHECK(identity_provider_.get()); | |
| 36 identity_provider_->AddObserver(this); | |
| 37 } | |
| 38 | |
| 39 IdentitySource::~IdentitySource() { | |
| 40 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); | |
| 41 identity_provider_->RemoveObserver(this); | |
| 42 } | |
| 43 | |
| 44 void IdentitySource::Connect() { | |
| 45 if (is_fetching_token_) { | |
| 46 return; | |
| 47 } | |
| 48 | |
| 49 // Pass empty token to assignment source if we have command line switches. | |
| 50 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kEngineIP)) { | |
| 51 if (token_callback_) { | |
| 52 token_callback_.Run(std::string()); | |
| 53 } | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 // User must sign in first to get an OAuth2 token. | |
| 58 const std::string& account_id = identity_provider_->GetActiveAccountId(); | |
| 59 if (account_id.empty()) { | |
| 60 VLOG(1) << "User is not signed in before connection to Blimp engine."; | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 account_id_ = account_id; | |
| 65 is_fetching_token_ = true; | |
| 66 FetchAuthToken(); | |
| 67 } | |
| 68 | |
| 69 std::string IdentitySource::GetActiveUsername() { | |
| 70 return identity_provider_->GetActiveUsername(); | |
| 71 } | |
| 72 | |
| 73 // Add sign in state observer. | |
| 74 void IdentitySource::AddObserver(IdentityProvider::Observer* observer) { | |
| 75 DCHECK(identity_provider_); | |
| 76 identity_provider_->AddObserver(observer); | |
| 77 } | |
| 78 | |
| 79 // Remove sign in state observer. | |
| 80 void IdentitySource::RemoveObserver(IdentityProvider::Observer* observer) { | |
| 81 DCHECK(identity_provider_); | |
| 82 identity_provider_->RemoveObserver(observer); | |
| 83 } | |
| 84 | |
| 85 void IdentitySource::OnGetTokenSuccess( | |
| 86 const OAuth2TokenService::Request* request, | |
| 87 const std::string& access_token, | |
| 88 const base::Time& expiration_time) { | |
| 89 token_request_.reset(); | |
| 90 is_fetching_token_ = false; | |
| 91 retry_times_ = 0; | |
| 92 | |
| 93 if (token_callback_) { | |
| 94 token_callback_.Run(access_token); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 // Fail to get the token after retries attempts in native layer and Java layer. | |
| 99 void IdentitySource::OnGetTokenFailure( | |
| 100 const OAuth2TokenService::Request* request, | |
| 101 const GoogleServiceAuthError& error) { | |
| 102 token_request_.reset(); | |
| 103 | |
| 104 // Retry the request. | |
| 105 // The embedder can invalidate the refresh token at any time, this happens | |
| 106 // during application start up or when user switches account. | |
| 107 // OnGetTokenFailure will be called and the error code is REQUEST_CANCELED. | |
| 108 if (error.state() == GoogleServiceAuthError::State::REQUEST_CANCELED && | |
| 109 retry_times_ < kTokenRequestCancelMaxRetry) { | |
| 110 retry_times_++; | |
| 111 VLOG(1) << "Retrying to get OAuth2 token due to request cancellation. " | |
| 112 "retry time = " | |
| 113 << retry_times_; | |
| 114 FetchAuthToken(); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 // If request failure was not caused by cancellation, or reached max retry | |
| 119 // times on request cancellation, propagate the error to embedder. | |
| 120 is_fetching_token_ = false; | |
| 121 retry_times_ = 0; | |
| 122 VLOG(1) << "OAuth2 token error: " << error.state(); | |
| 123 error_callback_.Run(error); | |
| 124 } | |
| 125 | |
| 126 void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { | |
| 127 if (account_id != account_id_) { | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); | |
| 132 FetchAuthToken(); | |
| 133 } | |
| 134 | |
| 135 void IdentitySource::FetchAuthToken() { | |
| 136 OAuth2TokenService* token_service = identity_provider_->GetTokenService(); | |
| 137 DCHECK(token_service); | |
| 138 | |
| 139 if (token_service->RefreshTokenIsAvailable(account_id_)) { | |
| 140 OAuth2TokenService::ScopeSet scopes; | |
| 141 scopes.insert(kOAuth2TokenScope); | |
| 142 token_request_ = token_service->StartRequest(account_id_, scopes, this); | |
| 143 } else { | |
| 144 identity_provider_->AddActiveAccountRefreshTokenObserver(this); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 } // namespace client | |
| 149 } // namespace blimp | |
| OLD | NEW |