Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(566)

Side by Side Diff: blimp/client/core/session/identity_source.cc

Issue 2204223005: Blimp OAuth2 token retreival on application start up. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor fix. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 : OAuth2TokenService::Consumer("blimp_client"),
20 is_fetching_token_(false),
21 delegate_(delegate) {
22 DCHECK(delegate_);
23
24 // Create identity provider.
25 identity_provider_ = delegate_->CreateIdentityProvider();
26 DCHECK(identity_provider_.get());
27 identity_provider_->AddObserver(this);
28 }
29
30 IdentitySource::~IdentitySource() {
31 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this);
32 identity_provider_->RemoveObserver(this);
33 }
34
35 void IdentitySource::Connect() {
36 if (is_fetching_token_) {
37 return;
38 }
39
40 const std::string& account_id = identity_provider_->GetActiveAccountId();
41
42 // User must sign in first.
43 if (account_id.empty()) {
44 delegate_->OnAuthenticationError(
45 BlimpClientContextDelegate::AuthError::NOT_SIGNED_IN);
46 return;
47 }
48
49 account_id_ = account_id;
50 is_fetching_token_ = true;
51 FetchAuthToken();
52 }
53
54 void IdentitySource::SetTokenCallback(const TokenCallback& callback) {
55 token_callback_ = callback;
56 }
57
58 void IdentitySource::OnGetTokenSuccess(
59 const OAuth2TokenService::Request* request,
60 const std::string& access_token,
61 const base::Time& expiration_time) {
62 token_request_.reset();
63 is_fetching_token_ = false;
64
65 if (token_callback_) {
66 token_callback_.Run(access_token);
67 }
68 }
69
70 // Fail to get the token after retries attempts in native layer and Java layer.
71 void IdentitySource::OnGetTokenFailure(
72 const OAuth2TokenService::Request* request,
73 const GoogleServiceAuthError& error) {
74 token_request_.reset();
75 is_fetching_token_ = false;
76
77 DCHECK(delegate_);
78 delegate_->OnAuthenticationError(
79 BlimpClientContextDelegate::AuthError::OAUTH_TOKEN_FAIL);
80 }
81
82 void IdentitySource::OnRefreshTokenAvailable(const std::string& account_id) {
83 if (account_id != account_id_) {
84 return;
85 }
86
87 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this);
88 FetchAuthToken();
89 }
90
91 void IdentitySource::FetchAuthToken() {
92 OAuth2TokenService* token_service = identity_provider_->GetTokenService();
93 DCHECK(token_service);
94
95 if (token_service->RefreshTokenIsAvailable(account_id_)) {
96 OAuth2TokenService::ScopeSet scopes;
97 scopes.insert(kOAuth2TokenScope);
98 token_request_ = token_service->StartRequest(account_id_, scopes, this);
99 } else {
100 identity_provider_->AddActiveAccountRefreshTokenObserver(this);
101 }
102 }
103
104 } // namespace client
105 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698