OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" | 5 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" |
6 | 6 |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 10 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "components/user_manager/user_id.h" | |
13 #include "components/user_manager/user_manager.h" | 12 #include "components/user_manager/user_manager.h" |
14 #include "google_apis/gaia/gaia_oauth_client.h" | 13 #include "google_apis/gaia/gaia_oauth_client.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 const char kTokenHandlePref[] = "PasswordTokenHandle"; | 17 const char kTokenHandlePref[] = "PasswordTokenHandle"; |
19 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; | 18 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; |
20 | 19 |
21 const char kHandleStatusValid[] = "valid"; | 20 const char kHandleStatusValid[] = "valid"; |
22 const char kHandleStatusInvalid[] = "invalid"; | 21 const char kHandleStatusInvalid[] = "invalid"; |
23 const char* kDefaultHandleStatus = kHandleStatusValid; | 22 const char* kDefaultHandleStatus = kHandleStatusValid; |
24 | 23 |
25 static const int kMaxRetries = 3; | 24 static const int kMaxRetries = 3; |
26 | 25 |
27 } // namespace | 26 } // namespace |
28 | 27 |
29 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) | 28 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) |
30 : user_manager_(user_manager), weak_factory_(this) { | 29 : user_manager_(user_manager), weak_factory_(this) { |
31 } | 30 } |
32 | 31 |
33 TokenHandleUtil::~TokenHandleUtil() { | 32 TokenHandleUtil::~TokenHandleUtil() { |
34 weak_factory_.InvalidateWeakPtrs(); | 33 weak_factory_.InvalidateWeakPtrs(); |
35 gaia_client_.reset(); | 34 gaia_client_.reset(); |
36 } | 35 } |
37 | 36 |
38 bool TokenHandleUtil::HasToken(const user_manager::UserID& user_id) { | 37 bool TokenHandleUtil::HasToken(const AccountId& account_id) { |
39 const base::DictionaryValue* dict = nullptr; | 38 const base::DictionaryValue* dict = nullptr; |
40 std::string token; | 39 std::string token; |
41 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | 40 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) |
42 return false; | 41 return false; |
43 if (!dict->GetString(kTokenHandlePref, &token)) | 42 if (!dict->GetString(kTokenHandlePref, &token)) |
44 return false; | 43 return false; |
45 return !token.empty(); | 44 return !token.empty(); |
46 } | 45 } |
47 | 46 |
48 bool TokenHandleUtil::ShouldObtainHandle(const user_manager::UserID& user_id) { | 47 bool TokenHandleUtil::ShouldObtainHandle(const AccountId& account_id) { |
49 const base::DictionaryValue* dict = nullptr; | 48 const base::DictionaryValue* dict = nullptr; |
50 std::string token; | 49 std::string token; |
51 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | 50 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) |
52 return true; | 51 return true; |
53 if (!dict->GetString(kTokenHandlePref, &token)) | 52 if (!dict->GetString(kTokenHandlePref, &token)) |
54 return true; | 53 return true; |
55 if (token.empty()) | 54 if (token.empty()) |
56 return true; | 55 return true; |
57 std::string status(kDefaultHandleStatus); | 56 std::string status(kDefaultHandleStatus); |
58 dict->GetString(kTokenHandleStatusPref, &status); | 57 dict->GetString(kTokenHandleStatusPref, &status); |
59 return kHandleStatusInvalid == status; | 58 return kHandleStatusInvalid == status; |
60 } | 59 } |
61 | 60 |
62 void TokenHandleUtil::DeleteHandle(const user_manager::UserID& user_id) { | 61 void TokenHandleUtil::DeleteHandle(const AccountId& account_id) { |
63 const base::DictionaryValue* dict = nullptr; | 62 const base::DictionaryValue* dict = nullptr; |
64 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | 63 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) |
65 return; | 64 return; |
66 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); | 65 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); |
67 dict_copy->Remove(kTokenHandlePref, nullptr); | 66 dict_copy->Remove(kTokenHandlePref, nullptr); |
68 dict_copy->Remove(kTokenHandleStatusPref, nullptr); | 67 dict_copy->Remove(kTokenHandleStatusPref, nullptr); |
69 user_manager_->UpdateKnownUserPrefs(user_id, *dict_copy.get(), | 68 user_manager_->UpdateKnownUserPrefs(account_id, *dict_copy.get(), |
70 /* replace values */ true); | 69 /* replace values */ true); |
71 } | 70 } |
72 | 71 |
73 void TokenHandleUtil::MarkHandleInvalid(const user_manager::UserID& user_id) { | 72 void TokenHandleUtil::MarkHandleInvalid(const AccountId& account_id) { |
74 user_manager_->SetKnownUserStringPref(user_id, kTokenHandleStatusPref, | 73 user_manager_->SetKnownUserStringPref(account_id, kTokenHandleStatusPref, |
75 kHandleStatusInvalid); | 74 kHandleStatusInvalid); |
76 } | 75 } |
77 | 76 |
78 void TokenHandleUtil::CheckToken(const user_manager::UserID& user_id, | 77 void TokenHandleUtil::CheckToken(const AccountId& account_id, |
79 const TokenValidationCallback& callback) { | 78 const TokenValidationCallback& callback) { |
80 const base::DictionaryValue* dict = nullptr; | 79 const base::DictionaryValue* dict = nullptr; |
81 std::string token; | 80 std::string token; |
82 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) { | 81 if (!user_manager_->FindKnownUserPrefs(account_id, &dict)) { |
83 callback.Run(user_id, UNKNOWN); | 82 callback.Run(account_id, UNKNOWN); |
84 return; | 83 return; |
85 } | 84 } |
86 if (!dict->GetString(kTokenHandlePref, &token)) { | 85 if (!dict->GetString(kTokenHandlePref, &token)) { |
87 callback.Run(user_id, UNKNOWN); | 86 callback.Run(account_id, UNKNOWN); |
88 return; | 87 return; |
89 } | 88 } |
90 | 89 |
91 if (!gaia_client_.get()) { | 90 if (!gaia_client_.get()) { |
92 auto request_context = | 91 auto request_context = |
93 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); | 92 chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext(); |
94 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); | 93 gaia_client_.reset(new gaia::GaiaOAuthClient(request_context)); |
95 } | 94 } |
96 | 95 |
97 validation_delegates_.set( | 96 validation_delegates_.set( |
98 token, scoped_ptr<TokenDelegate>(new TokenDelegate( | 97 token, scoped_ptr<TokenDelegate>(new TokenDelegate( |
99 weak_factory_.GetWeakPtr(), user_id, token, callback))); | 98 weak_factory_.GetWeakPtr(), account_id, token, callback))); |
100 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, | 99 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, |
101 validation_delegates_.get(token)); | 100 validation_delegates_.get(token)); |
102 } | 101 } |
103 | 102 |
104 void TokenHandleUtil::StoreTokenHandle(const user_manager::UserID& user_id, | 103 void TokenHandleUtil::StoreTokenHandle(const AccountId& account_id, |
105 const std::string& handle) { | 104 const std::string& handle) { |
106 user_manager_->SetKnownUserStringPref(user_id, kTokenHandlePref, handle); | 105 user_manager_->SetKnownUserStringPref(account_id, kTokenHandlePref, handle); |
107 user_manager_->SetKnownUserStringPref(user_id, kTokenHandleStatusPref, | 106 user_manager_->SetKnownUserStringPref(account_id, kTokenHandleStatusPref, |
108 kHandleStatusValid); | 107 kHandleStatusValid); |
109 } | 108 } |
110 | 109 |
111 void TokenHandleUtil::OnValidationComplete(const std::string& token) { | 110 void TokenHandleUtil::OnValidationComplete(const std::string& token) { |
112 validation_delegates_.erase(token); | 111 validation_delegates_.erase(token); |
113 } | 112 } |
114 | 113 |
115 void TokenHandleUtil::OnObtainTokenComplete( | 114 void TokenHandleUtil::OnObtainTokenComplete(const AccountId& account_id) { |
116 const user_manager::UserID& user_id) { | 115 obtain_delegates_.erase(account_id); |
117 obtain_delegates_.erase(user_id); | |
118 } | 116 } |
119 | 117 |
120 TokenHandleUtil::TokenDelegate::TokenDelegate( | 118 TokenHandleUtil::TokenDelegate::TokenDelegate( |
121 const base::WeakPtr<TokenHandleUtil>& owner, | 119 const base::WeakPtr<TokenHandleUtil>& owner, |
122 const user_manager::UserID& user_id, | 120 const AccountId& account_id, |
123 const std::string& token, | 121 const std::string& token, |
124 const TokenValidationCallback& callback) | 122 const TokenValidationCallback& callback) |
125 : owner_(owner), | 123 : owner_(owner), |
126 user_id_(user_id), | 124 account_id_(account_id), |
127 token_(token), | 125 token_(token), |
128 tokeninfo_response_start_time_(base::TimeTicks::Now()), | 126 tokeninfo_response_start_time_(base::TimeTicks::Now()), |
129 callback_(callback) { | 127 callback_(callback) {} |
130 } | |
131 | 128 |
132 TokenHandleUtil::TokenDelegate::~TokenDelegate() { | 129 TokenHandleUtil::TokenDelegate::~TokenDelegate() { |
133 } | 130 } |
134 | 131 |
135 void TokenHandleUtil::TokenDelegate::OnOAuthError() { | 132 void TokenHandleUtil::TokenDelegate::OnOAuthError() { |
136 callback_.Run(user_id_, INVALID); | 133 callback_.Run(account_id_, INVALID); |
137 NotifyDone(); | 134 NotifyDone(); |
138 } | 135 } |
139 | 136 |
140 // Warning: NotifyDone() deletes |this| | 137 // Warning: NotifyDone() deletes |this| |
141 void TokenHandleUtil::TokenDelegate::NotifyDone() { | 138 void TokenHandleUtil::TokenDelegate::NotifyDone() { |
142 if (owner_) | 139 if (owner_) |
143 owner_->OnValidationComplete(token_); | 140 owner_->OnValidationComplete(token_); |
144 } | 141 } |
145 | 142 |
146 void TokenHandleUtil::TokenDelegate::OnNetworkError(int response_code) { | 143 void TokenHandleUtil::TokenDelegate::OnNetworkError(int response_code) { |
147 callback_.Run(user_id_, UNKNOWN); | 144 callback_.Run(account_id_, UNKNOWN); |
148 NotifyDone(); | 145 NotifyDone(); |
149 } | 146 } |
150 | 147 |
151 void TokenHandleUtil::TokenDelegate::OnGetTokenInfoResponse( | 148 void TokenHandleUtil::TokenDelegate::OnGetTokenInfoResponse( |
152 scoped_ptr<base::DictionaryValue> token_info) { | 149 scoped_ptr<base::DictionaryValue> token_info) { |
153 TokenHandleStatus outcome = UNKNOWN; | 150 TokenHandleStatus outcome = UNKNOWN; |
154 if (!token_info->HasKey("error")) { | 151 if (!token_info->HasKey("error")) { |
155 int expires_in = 0; | 152 int expires_in = 0; |
156 if (token_info->GetInteger("expires_in", &expires_in)) | 153 if (token_info->GetInteger("expires_in", &expires_in)) |
157 outcome = (expires_in < 0) ? INVALID : VALID; | 154 outcome = (expires_in < 0) ? INVALID : VALID; |
158 } | 155 } |
159 | 156 |
160 const base::TimeDelta duration = | 157 const base::TimeDelta duration = |
161 base::TimeTicks::Now() - tokeninfo_response_start_time_; | 158 base::TimeTicks::Now() - tokeninfo_response_start_time_; |
162 UMA_HISTOGRAM_TIMES("Login.TokenCheckResponseTime", duration); | 159 UMA_HISTOGRAM_TIMES("Login.TokenCheckResponseTime", duration); |
163 callback_.Run(user_id_, outcome); | 160 callback_.Run(account_id_, outcome); |
164 NotifyDone(); | 161 NotifyDone(); |
165 } | 162 } |
OLD | NEW |