Chromium Code Reviews| Index: chrome/browser/sync/credential_cache_win.cc |
| diff --git a/chrome/browser/sync/credential_cache_win.cc b/chrome/browser/sync/credential_cache_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..02829f1b6e223259445de4e475e0c36f8399bbbf |
| --- /dev/null |
| +++ b/chrome/browser/sync/credential_cache_win.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/sync/credential_cache_win.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/file_util.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/sync/glue/chrome_encryptor.h" |
| +#include "chrome/common/chrome_paths_internal.h" |
| +#include "chrome/common/json_pref_store.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace { |
| + |
| +// File in which sync credentials are cached. |
| +const FilePath::CharType kSyncCredentialsFilename[] = |
| + FILE_PATH_LITERAL("Sync Credentials"); |
| + |
| +// Names of fields in the the sync credential cache. |
| +const char kAuthenticatedUsername[] = "authenticated_username"; |
| +const char kSid[] = "sid"; |
| +const char kLsid[] = "lsid"; |
| +const char kEncryptionBootstrapToken[] = "encryption_bootstrap_token"; |
| + |
| +} // namespace |
| + |
| +namespace csync { |
| + |
| +CredentialCache::CredentialCache(const std::string& authenticated_username, |
| + const std::string& sid, |
| + const std::string& lsid, |
| + const std::string& encryption_bootstrap_token, |
| + const FilePath& profile_dir) |
| + : authenticated_username_(authenticated_username), |
| + sid_(sid), |
| + lsid_(lsid), |
| + encryption_bootstrap_token_(encryption_bootstrap_token), |
| + profile_dir_(profile_dir) { |
| + DCHECK(RunningInCorrectProfileDir()); |
| +} |
| + |
| +CredentialCache::CredentialCache(const FilePath& profile_dir) |
| + : profile_dir_(profile_dir) {} |
| + |
| +CredentialCache::~CredentialCache() {} |
|
Roger Tawa OOO till Jul 10th
2012/06/27 21:23:28
i think the } should go on the next line.
Raghu Simha
2012/07/19 06:57:07
Done.
|
| + |
| +bool CredentialCache::RunningOnCorrectThread() const { |
| + return content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE); |
| +} |
| + |
| +bool CredentialCache::RunningInCorrectProfileDir() const { |
| + return IsDefaultProfileDir(profile_dir_); |
| +} |
| + |
| +FilePath CredentialCache::GetCredentialPathInAlternateProfile() const { |
| + FilePath alternate_user_data_dir; |
| + chrome::GetAlternateUserDataDirectory(&alternate_user_data_dir); |
| + FilePath alternate_default_profile_dir = |
| + ProfileManager::GetDefaultProfileDir(alternate_user_data_dir); |
| + return alternate_default_profile_dir.Append(kSyncCredentialsFilename); |
| +} |
| + |
| +// static |
| +bool CredentialCache::IsDefaultProfileDir(const FilePath& profile_dir) { |
| + FilePath default_user_data_dir; |
| + chrome::GetDefaultUserDataDirectory(&default_user_data_dir); |
| + return profile_dir == |
| + ProfileManager::GetDefaultProfileDir(default_user_data_dir); |
| +} |
| + |
| +// static |
| +base::StringValue* CredentialCache::PackCredential( |
| + const std::string& clear_text) { |
| + browser_sync::ChromeEncryptor encryptor; |
| + std::string encrypted_text; |
| + if (!encryptor.EncryptString(clear_text, &encrypted_text)) { |
| + NOTREACHED(); |
| + return NULL; |
| + } |
| + std::string encoded_text; |
| + if (!base::Base64Encode(encrypted_text, &encoded_text)) { |
| + NOTREACHED(); |
| + return NULL; |
| + } |
| + return base::Value::CreateStringValue(encoded_text); |
| +} |
| + |
| +// static |
| +void CredentialCache::UnpackCredential(const base::Value* packed, |
| + std::string* unpacked) { |
| + std::string encoded; |
| + if (!packed->GetAsString(&encoded)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + std::string encrypted; |
| + if (!base::Base64Decode(encoded, &encrypted)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + browser_sync::ChromeEncryptor encryptor; |
| + if (!encryptor.DecryptString(encrypted, unpacked)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| +} |
| + |
| +// static |
| +void CredentialCache::PersistAuthTokens( |
| + scoped_refptr<CredentialCache> credentials) { |
| + DCHECK(credentials->RunningOnCorrectThread()); |
| + DCHECK(!credentials->authenticated_username_.empty() && |
| + !credentials->sid_.empty() && |
|
Roger Tawa OOO till Jul 10th
2012/06/27 21:23:28
should this be indented only 4 spaces?
Raghu Simha
2012/07/19 06:57:07
This method has been removed.
|
| + !credentials->lsid_.empty()); |
| + |
| + scoped_refptr<JsonPrefStore> store = new JsonPrefStore( |
| + credentials->profile_dir_.Append(kSyncCredentialsFilename), |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::FILE)); |
| + store->ReadPrefs(); |
| + store->SetValue(kAuthenticatedUsername, |
| + PackCredential(credentials->authenticated_username_)); |
| + store->SetValue(kSid, PackCredential(credentials->sid_)); |
| + store->SetValue(kLsid, PackCredential(credentials->lsid_)); |
| + store->CommitPendingWrite(); |
| + credentials.release(); |
| +} |
| + |
| +// static |
| +void CredentialCache::PersistEncryptionBootstrapToken( |
| + scoped_refptr<CredentialCache> credentials) { |
| + DCHECK(credentials->RunningOnCorrectThread()); |
| + DCHECK(!credentials->encryption_bootstrap_token_.empty()); |
| + |
| + scoped_refptr<JsonPrefStore> store = new JsonPrefStore( |
| + credentials->profile_dir_.Append(kSyncCredentialsFilename), |
|
Roger Tawa OOO till Jul 10th
2012/06/27 21:23:28
I think it would be better to write a function tha
Raghu Simha
2012/07/19 06:57:07
Agree. Done.
|
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::FILE)); |
| + store->ReadPrefs(); |
|
Andrew T Wilson (Slow)
2012/06/26 23:26:13
So, if we ever don't have sid/lsid here, that's an
Raghu Simha
2012/07/19 06:57:07
This method has been removed.
|
| + store->SetValue(kEncryptionBootstrapToken, |
| + PackCredential(credentials->encryption_bootstrap_token_)); |
| + store->CommitPendingWrite(); |
| + credentials.release(); |
| +} |
| + |
| +// static |
| +void CredentialCache::LoadCredentialsFromAlternateProfile( |
| + scoped_refptr<CredentialCache> credentials) { |
| + DCHECK(credentials->RunningOnCorrectThread()); |
| + |
| + FilePath credential_path = credentials->GetCredentialPathInAlternateProfile(); |
| + if (!file_util::PathExists(credential_path)) |
| + return; |
| + |
| + scoped_refptr<JsonPrefStore> store = new JsonPrefStore( |
| + credential_path, |
| + content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::FILE)); |
| + store->ReadPrefs(); |
| + |
| + const base::Value* authenticated_username = NULL; |
| + const base::Value* sid = NULL; |
| + const base::Value* lsid = NULL; |
| + const base::Value* encryption_bootstrap_token = NULL; |
| + |
| + if ((store->GetValue(kAuthenticatedUsername, |
| + &authenticated_username) != PrefStore::READ_OK) || |
| + (store->GetValue(kSid, &sid) != PrefStore::READ_OK) || |
| + (store->GetValue(kLsid, &lsid) != PrefStore::READ_OK) || |
| + (store->GetValue(kEncryptionBootstrapToken, |
| + &encryption_bootstrap_token) != PrefStore::READ_OK)) { |
| + return; |
| + } |
| + |
| + UnpackCredential(authenticated_username, |
| + &credentials->authenticated_username_); |
| + UnpackCredential(sid, &credentials->sid_); |
| + UnpackCredential(lsid, &credentials->lsid_); |
| + UnpackCredential(encryption_bootstrap_token, |
| + &credentials->encryption_bootstrap_token_); |
| +} |
| + |
| +} // namespace csync |