OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 entry. | 3 // found in the LICENSE entry. |
4 // | 4 // |
5 // Implement the storage of service tokens in memory. | 5 // Implement the storage of service tokens in memory. |
6 | 6 |
7 #include "chrome/browser/sync/util/user_settings.h" | 7 #include "chrome/browser/sync/util/user_settings.h" |
8 | 8 |
| 9 #include "chrome/browser/password_manager/encryptor.h" |
| 10 #include "chrome/browser/sync/util/query_helpers.h" |
| 11 |
9 namespace browser_sync { | 12 namespace browser_sync { |
10 | 13 |
11 void UserSettings::ClearAllServiceTokens() { | |
12 service_tokens_.clear(); | |
13 } | |
14 | |
15 void UserSettings::SetAuthTokenForService( | 14 void UserSettings::SetAuthTokenForService( |
16 const std::string& email, | 15 const std::string& email, |
17 const std::string& service_name, | 16 const std::string& service_name, |
18 const std::string& long_lived_service_token) { | 17 const std::string& long_lived_service_token) { |
19 service_tokens_[service_name] = long_lived_service_token; | 18 std::string encrypted_service_token; |
| 19 if (!Encryptor::EncryptString(long_lived_service_token, |
| 20 &encrypted_service_token)) { |
| 21 LOG(ERROR) << "Encrytion failed: " << long_lived_service_token; |
| 22 return; |
| 23 } |
| 24 ScopedDBHandle dbhandle(this); |
| 25 ExecOrDie(dbhandle.get(), "INSERT INTO cookies " |
| 26 "(email, service_name, service_token) " |
| 27 "values (?, ?, ?)", email, service_name, encrypted_service_token); |
| 28 } |
| 29 |
| 30 void UserSettings::ClearAllServiceTokens() { |
| 31 ScopedDBHandle dbhandle(this); |
| 32 ExecOrDie(dbhandle.get(), "DELETE FROM cookies"); |
| 33 } |
| 34 |
| 35 bool UserSettings::GetLastUser(std::string* username) { |
| 36 ScopedDBHandle dbhandle(this); |
| 37 ScopedStatement query(PrepareQuery(dbhandle.get(), |
| 38 "SELECT email FROM cookies")); |
| 39 if (SQLITE_ROW == sqlite3_step(query.get())) { |
| 40 GetColumn(query.get(), 0, username); |
| 41 return true; |
| 42 } else { |
| 43 return false; |
| 44 } |
20 } | 45 } |
21 | 46 |
22 bool UserSettings::GetLastUserAndServiceToken(const std::string& service_name, | 47 bool UserSettings::GetLastUserAndServiceToken(const std::string& service_name, |
23 std::string* username, | 48 std::string* username, |
24 std::string* service_token) { | 49 std::string* service_token) { |
25 ServiceTokenMap::const_iterator iter = service_tokens_.find(service_name); | 50 ScopedDBHandle dbhandle(this); |
| 51 ScopedStatement query(PrepareQuery( |
| 52 dbhandle.get(), |
| 53 "SELECT email, service_token FROM cookies WHERE service_name = ?", |
| 54 service_name)); |
26 | 55 |
27 if (iter != service_tokens_.end()) { | 56 if (SQLITE_ROW == sqlite3_step(query.get())) { |
28 *username = email_; | 57 std::string encrypted_service_token; |
29 *service_token = iter->second; | 58 GetColumn(query.get(), 1, &encrypted_service_token); |
| 59 if (!Encryptor::DecryptString(encrypted_service_token, service_token)) { |
| 60 LOG(ERROR) << "Decryption failed: " << encrypted_service_token; |
| 61 return false; |
| 62 } |
| 63 GetColumn(query.get(), 0, username); |
30 return true; | 64 return true; |
31 } | 65 } |
32 | 66 |
33 return false; | 67 return false; |
34 } | 68 } |
35 | 69 |
36 } // namespace browser_sync | 70 } // namespace browser_sync |
OLD | NEW |