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