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(const TokenCallback& token_callback) { | |
31 const std::string& account_id = identity_provider_->GetActiveAccountId(); | |
32 | |
33 // User must sign in first. | |
34 if (account_id.empty()) { | |
35 delegate_->OnAuthenticationError( | |
36 BlimpClientContextDelegate::AuthError::NOT_SIGNED_IN); | |
37 return; | |
38 } | |
39 | |
40 account_id_ = account_id; | |
41 token_callback_ = token_callback; | |
42 FetchAuthToken(); | |
43 } | |
44 | |
45 void IdentitySource::SetDelegate(BlimpClientContextDelegate* delegate) { | |
46 delegate_ = delegate; | |
47 } | |
48 | |
49 void IdentitySource::OnGetTokenSuccess( | |
50 const OAuth2TokenService::Request* request, | |
51 const std::string& access_token, | |
52 const base::Time& expiration_time) { | |
53 token_request_.reset(); | |
54 | |
55 if (token_callback_) { | |
56 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
| |
57 } | |
58 } | |
59 | |
60 // Fail to get the token after retries attempts in native layer and Java layer. | |
61 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.
| |
62 const OAuth2TokenService::Request* request, | |
63 const GoogleServiceAuthError& error) { | |
64 token_request_.reset(); | |
65 DCHECK(delegate_); | |
66 delegate_->OnAuthenticationError( | |
67 BlimpClientContextDelegate::AuthError::OAUTH_TOKEN_FAIL); | |
68 } | |
69 | |
70 void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) { | |
71 if (account_id != account_id_) { | |
72 return; | |
73 } | |
74 | |
75 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); | |
76 FetchAuthToken(); | |
77 } | |
78 | |
79 void IdentitySource::FetchAuthToken() { | |
80 OAuth2TokenService* token_service = identity_provider_->GetTokenService(); | |
81 DCHECK(token_service); | |
82 | |
83 if (token_service->RefreshTokenIsAvailable(account_id_)) { | |
84 OAuth2TokenService::ScopeSet scopes; | |
85 scopes.insert(kOAuth2TokenScope); | |
86 token_request_ = token_service->StartRequest(account_id_, scopes, this); | |
87 } else { | |
88 identity_provider_->AddActiveAccountRefreshTokenObserver(this); | |
89 } | |
90 } | |
91 | |
92 } // namespace client | |
93 } // namespace blimp | |
OLD | NEW |