Index: blimp/client/core/session/identity_source.cc |
diff --git a/blimp/client/core/session/identity_source.cc b/blimp/client/core/session/identity_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..36fa0f7563366f1cbc3ea7fe9d6bd36d228027ba |
--- /dev/null |
+++ b/blimp/client/core/session/identity_source.cc |
@@ -0,0 +1,93 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "blimp/client/core/session/identity_source.h" |
+ |
+namespace blimp { |
+namespace client { |
+ |
+namespace { |
+// OAuth2 token scope. |
+const char kOAuth2TokenScope[] = |
+ "https://www.googleapis.com/auth/userinfo.email"; |
+} // namespace |
+ |
+IdentitySource::IdentitySource( |
+ std::unique_ptr<IdentityProvider> identity_provider) |
+ : OAuth2TokenService::Consumer("blimp_client"), |
+ identity_provider_(std::move(identity_provider)) { |
+ DCHECK(identity_provider_); |
+ |
+ identity_provider_->AddObserver(this); |
+} |
+ |
+IdentitySource::~IdentitySource() { |
+ identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
+ identity_provider_->RemoveObserver(this); |
+} |
+ |
+void IdentitySource::Connect(const TokenCallback& token_callback) { |
+ const std::string& account_id = identity_provider_->GetActiveAccountId(); |
+ |
+ // User must sign in first. |
+ if (account_id.empty()) { |
+ delegate_->OnAuthenticationError( |
+ BlimpClientContextDelegate::AuthError::NOT_SIGNED_IN); |
+ return; |
+ } |
+ |
+ account_id_ = account_id; |
+ token_callback_ = token_callback; |
+ FetchAuthToken(); |
+} |
+ |
+void IdentitySource::SetDelegate(BlimpClientContextDelegate* delegate) { |
+ delegate_ = delegate; |
+} |
+ |
+void IdentitySource::OnGetTokenSuccess( |
+ const OAuth2TokenService::Request* request, |
+ const std::string& access_token, |
+ const base::Time& expiration_time) { |
+ token_request_.reset(); |
+ |
+ if (token_callback_) { |
+ token_callback_.Run(access_token); |
nyquist
2016/08/16 18:38:05
Should this reset the callback?
xingliu
2016/08/17 00:32:24
Removed token callback as parameter here, instead
|
+ } |
+} |
+ |
+// Fail to get the token after retries attempts in native layer and Java layer. |
+void IdentitySource::OnGetTokenFailure( |
nyquist
2016/08/16 18:38:05
Should this also reset the callback?
xingliu
2016/08/17 00:32:24
Same as above.
|
+ const OAuth2TokenService::Request* request, |
+ const GoogleServiceAuthError& error) { |
+ token_request_.reset(); |
+ DCHECK(delegate_); |
+ delegate_->OnAuthenticationError( |
+ BlimpClientContextDelegate::AuthError::OAUTH_TOKEN_FAIL); |
+} |
+ |
+void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { |
+ if (account_id != account_id_) { |
+ return; |
+ } |
+ |
+ identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
+ FetchAuthToken(); |
+} |
+ |
+void IdentitySource::FetchAuthToken() { |
+ OAuth2TokenService* token_service = identity_provider_->GetTokenService(); |
+ DCHECK(token_service); |
+ |
+ if (token_service->RefreshTokenIsAvailable(account_id_)) { |
+ OAuth2TokenService::ScopeSet scopes; |
+ scopes.insert(kOAuth2TokenScope); |
+ token_request_ = token_service->StartRequest(account_id_, scopes, this); |
+ } else { |
+ identity_provider_->AddActiveAccountRefreshTokenObserver(this); |
+ } |
+} |
+ |
+} // namespace client |
+} // namespace blimp |