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

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: Fixes for nits. 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 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
OLDNEW
« no previous file with comments | « blimp/client/core/session/identity_source.h ('k') | blimp/client/core/session/identity_source_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698