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