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 "base/memory/ptr_util.h" |
| 8 |
| 9 namespace blimp { |
| 10 namespace client { |
| 11 |
| 12 namespace { |
| 13 // OAuth2 token scope. |
| 14 const char kOAuth2TokenScope[] = |
| 15 "https://www.googleapis.com/auth/userinfo.email"; |
| 16 } // namespace |
| 17 |
| 18 IdentitySource::IdentitySource(BlimpClientContextDelegate* delegate, |
| 19 const TokenCallback& callback) |
| 20 : OAuth2TokenService::Consumer("blimp_client"), |
| 21 token_callback_(callback), |
| 22 is_fetching_token_(false), |
| 23 delegate_(delegate) { |
| 24 DCHECK(delegate_); |
| 25 |
| 26 // Create identity provider. |
| 27 identity_provider_ = delegate_->CreateIdentityProvider(); |
| 28 DCHECK(identity_provider_.get()); |
| 29 identity_provider_->AddObserver(this); |
| 30 } |
| 31 |
| 32 IdentitySource::~IdentitySource() { |
| 33 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
| 34 identity_provider_->RemoveObserver(this); |
| 35 } |
| 36 |
| 37 void IdentitySource::Connect() { |
| 38 if (is_fetching_token_) { |
| 39 return; |
| 40 } |
| 41 |
| 42 const std::string& account_id = identity_provider_->GetActiveAccountId(); |
| 43 |
| 44 // User must sign in first. |
| 45 if (account_id.empty()) { |
| 46 delegate_->OnAuthenticationError( |
| 47 BlimpClientContextDelegate::AuthError::NOT_SIGNED_IN); |
| 48 return; |
| 49 } |
| 50 |
| 51 account_id_ = account_id; |
| 52 is_fetching_token_ = true; |
| 53 FetchAuthToken(); |
| 54 } |
| 55 |
| 56 void IdentitySource::OnGetTokenSuccess( |
| 57 const OAuth2TokenService::Request* request, |
| 58 const std::string& access_token, |
| 59 const base::Time& expiration_time) { |
| 60 token_request_.reset(); |
| 61 is_fetching_token_ = false; |
| 62 |
| 63 if (token_callback_) { |
| 64 token_callback_.Run(access_token); |
| 65 } |
| 66 } |
| 67 |
| 68 // Fail to get the token after retries attempts in native layer and Java layer. |
| 69 void IdentitySource::OnGetTokenFailure( |
| 70 const OAuth2TokenService::Request* request, |
| 71 const GoogleServiceAuthError& error) { |
| 72 token_request_.reset(); |
| 73 is_fetching_token_ = false; |
| 74 |
| 75 DCHECK(delegate_); |
| 76 delegate_->OnAuthenticationError( |
| 77 BlimpClientContextDelegate::AuthError::OAUTH_TOKEN_FAIL); |
| 78 } |
| 79 |
| 80 void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { |
| 81 if (account_id != account_id_) { |
| 82 return; |
| 83 } |
| 84 |
| 85 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
| 86 FetchAuthToken(); |
| 87 } |
| 88 |
| 89 void IdentitySource::FetchAuthToken() { |
| 90 OAuth2TokenService* token_service = identity_provider_->GetTokenService(); |
| 91 DCHECK(token_service); |
| 92 |
| 93 if (token_service->RefreshTokenIsAvailable(account_id_)) { |
| 94 OAuth2TokenService::ScopeSet scopes; |
| 95 scopes.insert(kOAuth2TokenScope); |
| 96 token_request_ = token_service->StartRequest(account_id_, scopes, this); |
| 97 } else { |
| 98 identity_provider_->AddActiveAccountRefreshTokenObserver(this); |
| 99 } |
| 100 } |
| 101 |
| 102 } // namespace client |
| 103 } // namespace blimp |
OLD | NEW |