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..69732d6fe6fda0d23e41e1ff171323359d3df63c |
--- /dev/null |
+++ b/blimp/client/core/session/identity_source.cc |
@@ -0,0 +1,87 @@ |
+// 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 std::string& account_id = identity_provider_->GetActiveAccountId(); |
+ |
+ // User must sign in first. |
+ if (account_id.empty()) { |
+ delegate_->OnError(BlimpClientContextDelegate::NOT_SIGNED_IN); |
+ return; |
+ } |
+ |
+ account_id_ = account_id; |
+ 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(); |
+ // TODO(xingliu): Use the token to connect. |
+} |
+ |
+// Fail to get the token after retries attempts in native layer and Java layer. |
+void IdentitySource::OnGetTokenFailure( |
+ const OAuth2TokenService::Request* request, |
+ const GoogleServiceAuthError& error) { |
+ token_request_.reset(); |
+ DCHECK(delegate_); |
+ delegate_->OnError(BlimpClientContextDelegate::OAUTH_TOKEN_FAIL); |
+} |
+ |
+void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { |
nyquist
2016/08/12 05:47:04
Should this also reset |token_request_|?
xingliu
2016/08/12 17:58:27
Investigate the code a little bit, I think we shou
|
+ 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 |