OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // 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 "base/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/browser/password_manager/encryptor.h" | 10 #include "chrome/browser/password_manager/encryptor.h" |
11 #include "chrome/browser/sync/util/sqlite_utils.h" | 11 #include "chrome/browser/sync/util/sqlite_utils.h" |
12 | 12 |
13 namespace browser_sync { | 13 namespace browser_sync { |
14 | 14 |
15 void UserSettings::SetAuthTokenForService( | 15 void UserSettings::SetAuthTokenForService( |
16 const std::string& email, | 16 const std::string& email, |
17 const std::string& service_name, | 17 const std::string& service_name, |
18 const std::string& long_lived_service_token) { | 18 const std::string& long_lived_service_token) { |
19 | 19 |
20 VLOG(1) << "Saving auth token " << long_lived_service_token | 20 VLOG(1) << "Saving auth token " << long_lived_service_token |
21 << " for " << email << "for service " << service_name; | 21 << " for " << email << "for service " << service_name; |
22 | 22 |
23 std::string encrypted_service_token; | 23 std::string encrypted_service_token; |
24 if (!Encryptor::EncryptString(long_lived_service_token, | 24 if (!Encryptor::EncryptString(long_lived_service_token, |
25 &encrypted_service_token)) { | 25 &encrypted_service_token)) { |
26 LOG(ERROR) << "Encrytion failed: " << long_lived_service_token; | 26 LOG(ERROR) << "Encrytion failed: " << long_lived_service_token; |
27 return; | 27 return; |
28 } | 28 } |
29 ScopedDBHandle dbhandle(this); | 29 ScopedDBHandle dbhandle(this); |
30 SQLStatement statement; | 30 sqlite_utils::SQLStatement statement; |
31 statement.prepare(dbhandle.get(), | 31 statement.prepare(dbhandle.get(), |
32 "INSERT INTO cookies " | 32 "INSERT INTO cookies " |
33 "(email, service_name, service_token) " | 33 "(email, service_name, service_token) " |
34 "values (?, ?, ?)"); | 34 "values (?, ?, ?)"); |
35 statement.bind_string(0, email); | 35 statement.bind_string(0, email); |
36 statement.bind_string(1, service_name); | 36 statement.bind_string(1, service_name); |
37 statement.bind_blob(2, encrypted_service_token.data(), | 37 statement.bind_blob(2, encrypted_service_token.data(), |
38 encrypted_service_token.size()); | 38 encrypted_service_token.size()); |
39 if (SQLITE_DONE != statement.step()) { | 39 if (SQLITE_DONE != statement.step()) { |
40 LOG(FATAL) << sqlite3_errmsg(dbhandle.get()); | 40 LOG(FATAL) << sqlite3_errmsg(dbhandle.get()); |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
44 bool UserSettings::GetLastUserAndServiceToken(const std::string& service_name, | 44 bool UserSettings::GetLastUserAndServiceToken(const std::string& service_name, |
45 std::string* username, | 45 std::string* username, |
46 std::string* service_token) { | 46 std::string* service_token) { |
47 ScopedDBHandle dbhandle(this); | 47 ScopedDBHandle dbhandle(this); |
48 SQLStatement query; | 48 sqlite_utils::SQLStatement query; |
49 query.prepare(dbhandle.get(), | 49 query.prepare(dbhandle.get(), |
50 "SELECT email, service_token FROM cookies" | 50 "SELECT email, service_token FROM cookies" |
51 " WHERE service_name = ?"); | 51 " WHERE service_name = ?"); |
52 query.bind_string(0, service_name.c_str()); | 52 query.bind_string(0, service_name.c_str()); |
53 | 53 |
54 if (SQLITE_ROW == query.step()) { | 54 if (SQLITE_ROW == query.step()) { |
55 std::string encrypted_service_token; | 55 std::string encrypted_service_token; |
56 query.column_blob_as_string(1, &encrypted_service_token); | 56 query.column_blob_as_string(1, &encrypted_service_token); |
57 if (!Encryptor::DecryptString(encrypted_service_token, service_token)) { | 57 if (!Encryptor::DecryptString(encrypted_service_token, service_token)) { |
58 LOG(ERROR) << "Decryption failed: " << encrypted_service_token; | 58 LOG(ERROR) << "Decryption failed: " << encrypted_service_token; |
59 return false; | 59 return false; |
60 } | 60 } |
61 *username = query.column_string(0); | 61 *username = query.column_string(0); |
62 | 62 |
63 VLOG(1) << "Found service token for:" << *username << " @ " << service_name | 63 VLOG(1) << "Found service token for:" << *username << " @ " << service_name |
64 << " returning: " << *service_token; | 64 << " returning: " << *service_token; |
65 | 65 |
66 return true; | 66 return true; |
67 } | 67 } |
68 | 68 |
69 VLOG(1) << "Couldn't find service token for " << service_name; | 69 VLOG(1) << "Couldn't find service token for " << service_name; |
70 | 70 |
71 return false; | 71 return false; |
72 } | 72 } |
73 | 73 |
74 } // namespace browser_sync | 74 } // namespace browser_sync |
OLD | NEW |