OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "chrome/browser/chromeos/login/signin/token_handler_util.h" | |
6 | |
7 #include "base/memory/weak_ptr.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "components/user_manager/user_id.h" | |
12 #include "components/user_manager/user_manager.h" | |
13 #include "google_apis/gaia/gaia_oauth_client.h" | |
14 | |
15 namespace { | |
16 | |
17 const char kTokenHandlePref[] = "PasswordTokenHandle"; | |
18 static const int kMaxRetries = 3; | |
19 } | |
20 | |
21 TokenHandlerUtil::TokenHandlerUtil(user_manager::UserManager* user_manager) | |
22 : user_manager_(user_manager), weak_factory_(this) { | |
23 } | |
24 | |
25 TokenHandlerUtil::~TokenHandlerUtil() { | |
26 weak_factory_.InvalidateWeakPtrs(); | |
27 gaia_client_.reset(); | |
28 } | |
29 | |
30 bool TokenHandlerUtil::HasToken(const user_manager::UserID& user_id) { | |
31 const base::DictionaryValue* dict = nullptr; | |
32 std::string token; | |
33 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | |
34 return false; | |
35 if (!dict->GetString(kTokenHandlePref, &token)) | |
36 return false; | |
37 return !token.empty(); | |
38 } | |
39 | |
40 void TokenHandlerUtil::DeleteToken(const user_manager::UserID& user_id) { | |
41 const base::DictionaryValue* dict = nullptr; | |
42 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | |
Nikita (slow)
2015/03/25 17:08:27
Lines 42-47 should be moved to UserManagerBase i.e
| |
43 return; | |
44 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); | |
45 if (!dict_copy->Remove(kTokenHandlePref, nullptr)) | |
46 return; | |
47 user_manager_->UpdateKnownUserPrefs(user_id, *dict_copy.get(), | |
48 /* replace values */ true); | |
49 } | |
50 | |
51 void TokenHandlerUtil::CheckToken(const user_manager::UserID& user_id, | |
52 const TokenValidationCallback& callback) { | |
53 const base::DictionaryValue* dict = nullptr; | |
54 std::string token; | |
55 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) { | |
56 callback.Run(user_id, UNKNOWN); | |
57 return; | |
58 } | |
59 if (!dict->GetString(kTokenHandlePref, &token)) { | |
60 callback.Run(user_id, UNKNOWN); | |
61 return; | |
62 } | |
63 | |
64 if (!gaia_client_.get()) { | |
65 auto request_context = | |
66 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); | |
67 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); | |
68 } | |
69 | |
70 validation_delegates_.set( | |
71 token, scoped_ptr<TokenValidationDelegate>(new TokenValidationDelegate( | |
72 weak_factory_.GetWeakPtr(), user_id, token, callback))); | |
73 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, | |
74 validation_delegates_.get(token)); | |
75 } | |
76 | |
77 void TokenHandlerUtil::OnValidationComplete(const std::string& token) { | |
78 validation_delegates_.erase(token); | |
79 } | |
80 | |
81 TokenHandlerUtil::TokenValidationDelegate::TokenValidationDelegate( | |
82 const base::WeakPtr<TokenHandlerUtil>& owner, | |
83 const user_manager::UserID& user_id, | |
84 const std::string& token, | |
85 const TokenValidationCallback& callback) | |
86 : owner_(owner), user_id_(user_id), token_(token), callback_(callback) { | |
87 } | |
88 | |
89 TokenHandlerUtil::TokenValidationDelegate::~TokenValidationDelegate() { | |
90 } | |
91 | |
92 void TokenHandlerUtil::TokenValidationDelegate::OnOAuthError() { | |
93 callback_.Run(user_id_, INVALID); | |
94 if (owner_) | |
95 owner_->OnValidationComplete(token_); | |
96 } | |
97 | |
98 void TokenHandlerUtil::TokenValidationDelegate::OnNetworkError( | |
99 int response_code) { | |
100 callback_.Run(user_id_, UNKNOWN); | |
101 if (owner_) | |
102 owner_->OnValidationComplete(token_); | |
103 } | |
104 | |
105 void TokenHandlerUtil::TokenValidationDelegate::OnGetTokenInfoResponse( | |
106 scoped_ptr<base::DictionaryValue> token_info) { | |
107 TokenHandleStatus outcome = UNKNOWN; | |
108 if (!token_info->HasKey("error")) { | |
109 int expires_in = 0; | |
110 if (token_info->GetInteger("expires_in", &expires_in)) | |
111 outcome = (expires_in < 0) ? INVALID : VALID; | |
112 } | |
113 callback_.Run(user_id_, outcome); | |
114 if (owner_) | |
115 owner_->OnValidationComplete(token_); | |
116 } | |
OLD | NEW |