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/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "components/user_manager/user_id.h" | 11 #include "components/user_manager/user_id.h" |
12 #include "components/user_manager/user_manager.h" | 12 #include "components/user_manager/user_manager.h" |
13 #include "google_apis/gaia/gaia_oauth_client.h" | 13 #include "google_apis/gaia/gaia_oauth_client.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const char kTokenHandlePref[] = "PasswordTokenHandle"; | 17 const char kTokenHandlePref[] = "PasswordTokenHandle"; |
| 18 const char kTokenHandleStatusPref[] = "TokenHandleStatus"; |
| 19 |
| 20 const char kHandleStatusValid[] = "valid"; |
| 21 const char kHandleStatusInvalid[] = "invalid"; |
| 22 const char* kDefaultHandleStatus = kHandleStatusValid; |
| 23 |
18 static const int kMaxRetries = 3; | 24 static const int kMaxRetries = 3; |
19 } | 25 |
| 26 } // namespace |
20 | 27 |
21 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) | 28 TokenHandleUtil::TokenHandleUtil(user_manager::UserManager* user_manager) |
22 : user_manager_(user_manager), weak_factory_(this) { | 29 : user_manager_(user_manager), weak_factory_(this) { |
23 } | 30 } |
24 | 31 |
25 TokenHandleUtil::~TokenHandleUtil() { | 32 TokenHandleUtil::~TokenHandleUtil() { |
26 weak_factory_.InvalidateWeakPtrs(); | 33 weak_factory_.InvalidateWeakPtrs(); |
27 gaia_client_.reset(); | 34 gaia_client_.reset(); |
28 } | 35 } |
29 | 36 |
30 bool TokenHandleUtil::HasToken(const user_manager::UserID& user_id) { | 37 bool TokenHandleUtil::HasToken(const user_manager::UserID& user_id) { |
31 const base::DictionaryValue* dict = nullptr; | 38 const base::DictionaryValue* dict = nullptr; |
32 std::string token; | 39 std::string token; |
33 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | 40 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) |
34 return false; | 41 return false; |
35 if (!dict->GetString(kTokenHandlePref, &token)) | 42 if (!dict->GetString(kTokenHandlePref, &token)) |
36 return false; | 43 return false; |
37 return !token.empty(); | 44 return !token.empty(); |
38 } | 45 } |
39 | 46 |
40 void TokenHandleUtil::DeleteToken(const user_manager::UserID& user_id) { | 47 bool TokenHandleUtil::ShouldObtainHandle(const user_manager::UserID& user_id) { |
| 48 const base::DictionaryValue* dict = nullptr; |
| 49 std::string token; |
| 50 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) |
| 51 return true; |
| 52 if (!dict->GetString(kTokenHandlePref, &token)) |
| 53 return true; |
| 54 if (token.empty()) |
| 55 return true; |
| 56 std::string status(kDefaultHandleStatus); |
| 57 dict->GetString(kTokenHandleStatusPref, &status); |
| 58 return kHandleStatusInvalid == status; |
| 59 } |
| 60 |
| 61 void TokenHandleUtil::DeleteHandle(const user_manager::UserID& user_id) { |
41 const base::DictionaryValue* dict = nullptr; | 62 const base::DictionaryValue* dict = nullptr; |
42 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) | 63 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) |
43 return; | 64 return; |
44 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); | 65 scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy()); |
45 if (!dict_copy->Remove(kTokenHandlePref, nullptr)) | 66 dict_copy->Remove(kTokenHandlePref, nullptr); |
46 return; | 67 dict_copy->Remove(kTokenHandleStatusPref, nullptr); |
47 user_manager_->UpdateKnownUserPrefs(user_id, *dict_copy.get(), | 68 user_manager_->UpdateKnownUserPrefs(user_id, *dict_copy.get(), |
48 /* replace values */ true); | 69 /* replace values */ true); |
49 } | 70 } |
50 | 71 |
| 72 void TokenHandleUtil::MarkHandleInvalid(const user_manager::UserID& user_id) { |
| 73 user_manager_->SetKnownUserStringPref(user_id, kTokenHandleStatusPref, |
| 74 kHandleStatusInvalid); |
| 75 } |
| 76 |
51 void TokenHandleUtil::CheckToken(const user_manager::UserID& user_id, | 77 void TokenHandleUtil::CheckToken(const user_manager::UserID& user_id, |
52 const TokenValidationCallback& callback) { | 78 const TokenValidationCallback& callback) { |
53 const base::DictionaryValue* dict = nullptr; | 79 const base::DictionaryValue* dict = nullptr; |
54 std::string token; | 80 std::string token; |
55 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) { | 81 if (!user_manager_->FindKnownUserPrefs(user_id, &dict)) { |
56 callback.Run(user_id, UNKNOWN); | 82 callback.Run(user_id, UNKNOWN); |
57 return; | 83 return; |
58 } | 84 } |
59 if (!dict->GetString(kTokenHandlePref, &token)) { | 85 if (!dict->GetString(kTokenHandlePref, &token)) { |
60 callback.Run(user_id, UNKNOWN); | 86 callback.Run(user_id, UNKNOWN); |
(...skipping 10 matching lines...) Expand all Loading... |
71 token, scoped_ptr<TokenDelegate>(new TokenDelegate( | 97 token, scoped_ptr<TokenDelegate>(new TokenDelegate( |
72 weak_factory_.GetWeakPtr(), false /* validate */, user_id, | 98 weak_factory_.GetWeakPtr(), false /* validate */, user_id, |
73 token, callback))); | 99 token, callback))); |
74 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, | 100 gaia_client_->GetTokenHandleInfo(token, kMaxRetries, |
75 validation_delegates_.get(token)); | 101 validation_delegates_.get(token)); |
76 } | 102 } |
77 | 103 |
78 void TokenHandleUtil::StoreTokenHandle(const user_manager::UserID& user_id, | 104 void TokenHandleUtil::StoreTokenHandle(const user_manager::UserID& user_id, |
79 const std::string& handle) { | 105 const std::string& handle) { |
80 user_manager_->SetKnownUserStringPref(user_id, kTokenHandlePref, handle); | 106 user_manager_->SetKnownUserStringPref(user_id, kTokenHandlePref, handle); |
| 107 user_manager_->SetKnownUserStringPref(user_id, kTokenHandleStatusPref, |
| 108 kHandleStatusValid); |
81 } | 109 } |
82 | 110 |
83 void TokenHandleUtil::OnValidationComplete(const std::string& token) { | 111 void TokenHandleUtil::OnValidationComplete(const std::string& token) { |
84 validation_delegates_.erase(token); | 112 validation_delegates_.erase(token); |
85 } | 113 } |
86 | 114 |
87 void TokenHandleUtil::OnObtainTokenComplete( | 115 void TokenHandleUtil::OnObtainTokenComplete( |
88 const user_manager::UserID& user_id) { | 116 const user_manager::UserID& user_id) { |
89 obtain_delegates_.erase(user_id); | 117 obtain_delegates_.erase(user_id); |
90 } | 118 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } else { | 184 } else { |
157 if (!token_info->HasKey("error")) { | 185 if (!token_info->HasKey("error")) { |
158 int expires_in = 0; | 186 int expires_in = 0; |
159 if (token_info->GetInteger("expires_in", &expires_in)) | 187 if (token_info->GetInteger("expires_in", &expires_in)) |
160 outcome = (expires_in < 0) ? INVALID : VALID; | 188 outcome = (expires_in < 0) ? INVALID : VALID; |
161 } | 189 } |
162 } | 190 } |
163 callback_.Run(user_id_, outcome); | 191 callback_.Run(user_id_, outcome); |
164 NotifyDone(); | 192 NotifyDone(); |
165 } | 193 } |
OLD | NEW |