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 #include "chrome/browser/sync/util/user_settings.h"
      6 
      7 #include <string>
      8 
      9 #include "base/logging.h"
     10 #include "chrome/browser/sync/util/crypto_helpers.h"
     11 #include "chrome/browser/sync/util/data_encryption.h"
     12 #include "chrome/common/sqlite_utils.h"
     13 
     14 using std::string;
     15 
     16 namespace browser_sync {
     17 
     18 void UserSettings::SetAuthTokenForService(const string& email,
     19     const string& service_name, const string& long_lived_service_token) {
     20   ScopedDBHandle dbhandle(this);
     21   SQLStatement statement;
     22   statement.prepare(dbhandle.get(),
     23                     "INSERT INTO cookies "
     24                     "(email, service_name, service_token) "
     25                     "values (?, ?, ?)");
     26   statement.bind_string(0, email);
     27   statement.bind_string(1, service_name);
     28   statement.bind_blob(2, &EncryptData(long_lived_service_token));
     29   if (SQLITE_DONE != statement.step()) {
     30     LOG(FATAL) << sqlite3_errmsg(dbhandle.get());
     31   }
     32 }
     33 
     34 // Returns the username whose credentials have been persisted as well as
     35 // a service token for the named service.
     36 bool UserSettings::GetLastUserAndServiceToken(const string& service_name,
     37                                               string* username,
     38                                               string* service_token) {
     39   ScopedDBHandle dbhandle(this);
     40   SQLStatement query;
     41   query.prepare(dbhandle.get(),
     42                 "SELECT email, service_token FROM cookies"
     43                 " WHERE service_name = ?");
     44   query.bind_string(0, service_name.c_str());
     45 
     46   if (SQLITE_ROW == query.step()) {
     47     *username = query.column_string(0);
     48 
     49     std::vector<uint8> encrypted_service_token;
     50     query.column_blob_as_vector(1, &encrypted_service_token);
     51     DecryptData(encrypted_service_token, service_token);
     52     return true;
     53   }
     54 
     55   return false;
     56 }
     57 
     58 }  // namespace browser_sync
     59