Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/credential_cache_win.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/browser/sync/glue/chrome_encryptor.h" | |
| 14 #include "chrome/common/chrome_paths_internal.h" | |
| 15 #include "chrome/common/json_pref_store.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // File in which sync credentials are cached. | |
| 21 const FilePath::CharType kSyncCredentialsFilename[] = | |
| 22 FILE_PATH_LITERAL("Sync Credentials"); | |
| 23 | |
| 24 // Names of fields in the the sync credential cache. | |
| 25 const char kAuthenticatedUsername[] = "authenticated_username"; | |
| 26 const char kSid[] = "sid"; | |
| 27 const char kLsid[] = "lsid"; | |
| 28 const char kEncryptionBootstrapToken[] = "encryption_bootstrap_token"; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 namespace csync { | |
| 33 | |
| 34 CredentialCache::CredentialCache(const std::string& authenticated_username, | |
| 35 const std::string& sid, | |
| 36 const std::string& lsid, | |
| 37 const std::string& encryption_bootstrap_token, | |
| 38 const FilePath& profile_dir) | |
| 39 : authenticated_username_(authenticated_username), | |
| 40 sid_(sid), | |
| 41 lsid_(lsid), | |
| 42 encryption_bootstrap_token_(encryption_bootstrap_token), | |
| 43 profile_dir_(profile_dir) { | |
| 44 DCHECK(RunningInCorrectProfileDir()); | |
| 45 } | |
| 46 | |
| 47 CredentialCache::CredentialCache(const FilePath& profile_dir) | |
| 48 : profile_dir_(profile_dir) {} | |
| 49 | |
| 50 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.
| |
| 51 | |
| 52 bool CredentialCache::RunningOnCorrectThread() const { | |
| 53 return content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE); | |
| 54 } | |
| 55 | |
| 56 bool CredentialCache::RunningInCorrectProfileDir() const { | |
| 57 return IsDefaultProfileDir(profile_dir_); | |
| 58 } | |
| 59 | |
| 60 FilePath CredentialCache::GetCredentialPathInAlternateProfile() const { | |
| 61 FilePath alternate_user_data_dir; | |
| 62 chrome::GetAlternateUserDataDirectory(&alternate_user_data_dir); | |
| 63 FilePath alternate_default_profile_dir = | |
| 64 ProfileManager::GetDefaultProfileDir(alternate_user_data_dir); | |
| 65 return alternate_default_profile_dir.Append(kSyncCredentialsFilename); | |
| 66 } | |
| 67 | |
| 68 // static | |
| 69 bool CredentialCache::IsDefaultProfileDir(const FilePath& profile_dir) { | |
| 70 FilePath default_user_data_dir; | |
| 71 chrome::GetDefaultUserDataDirectory(&default_user_data_dir); | |
| 72 return profile_dir == | |
| 73 ProfileManager::GetDefaultProfileDir(default_user_data_dir); | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 base::StringValue* CredentialCache::PackCredential( | |
| 78 const std::string& clear_text) { | |
| 79 browser_sync::ChromeEncryptor encryptor; | |
| 80 std::string encrypted_text; | |
| 81 if (!encryptor.EncryptString(clear_text, &encrypted_text)) { | |
| 82 NOTREACHED(); | |
| 83 return NULL; | |
| 84 } | |
| 85 std::string encoded_text; | |
| 86 if (!base::Base64Encode(encrypted_text, &encoded_text)) { | |
| 87 NOTREACHED(); | |
| 88 return NULL; | |
| 89 } | |
| 90 return base::Value::CreateStringValue(encoded_text); | |
| 91 } | |
| 92 | |
| 93 // static | |
| 94 void CredentialCache::UnpackCredential(const base::Value* packed, | |
| 95 std::string* unpacked) { | |
| 96 std::string encoded; | |
| 97 if (!packed->GetAsString(&encoded)) { | |
| 98 NOTREACHED(); | |
| 99 return; | |
| 100 } | |
| 101 std::string encrypted; | |
| 102 if (!base::Base64Decode(encoded, &encrypted)) { | |
| 103 NOTREACHED(); | |
| 104 return; | |
| 105 } | |
| 106 browser_sync::ChromeEncryptor encryptor; | |
| 107 if (!encryptor.DecryptString(encrypted, unpacked)) { | |
| 108 NOTREACHED(); | |
| 109 return; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 // static | |
| 114 void CredentialCache::PersistAuthTokens( | |
| 115 scoped_refptr<CredentialCache> credentials) { | |
| 116 DCHECK(credentials->RunningOnCorrectThread()); | |
| 117 DCHECK(!credentials->authenticated_username_.empty() && | |
| 118 !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.
| |
| 119 !credentials->lsid_.empty()); | |
| 120 | |
| 121 scoped_refptr<JsonPrefStore> store = new JsonPrefStore( | |
| 122 credentials->profile_dir_.Append(kSyncCredentialsFilename), | |
| 123 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 124 content::BrowserThread::FILE)); | |
| 125 store->ReadPrefs(); | |
| 126 store->SetValue(kAuthenticatedUsername, | |
| 127 PackCredential(credentials->authenticated_username_)); | |
| 128 store->SetValue(kSid, PackCredential(credentials->sid_)); | |
| 129 store->SetValue(kLsid, PackCredential(credentials->lsid_)); | |
| 130 store->CommitPendingWrite(); | |
| 131 credentials.release(); | |
| 132 } | |
| 133 | |
| 134 // static | |
| 135 void CredentialCache::PersistEncryptionBootstrapToken( | |
| 136 scoped_refptr<CredentialCache> credentials) { | |
| 137 DCHECK(credentials->RunningOnCorrectThread()); | |
| 138 DCHECK(!credentials->encryption_bootstrap_token_.empty()); | |
| 139 | |
| 140 scoped_refptr<JsonPrefStore> store = new JsonPrefStore( | |
| 141 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.
| |
| 142 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 143 content::BrowserThread::FILE)); | |
| 144 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.
| |
| 145 store->SetValue(kEncryptionBootstrapToken, | |
| 146 PackCredential(credentials->encryption_bootstrap_token_)); | |
| 147 store->CommitPendingWrite(); | |
| 148 credentials.release(); | |
| 149 } | |
| 150 | |
| 151 // static | |
| 152 void CredentialCache::LoadCredentialsFromAlternateProfile( | |
| 153 scoped_refptr<CredentialCache> credentials) { | |
| 154 DCHECK(credentials->RunningOnCorrectThread()); | |
| 155 | |
| 156 FilePath credential_path = credentials->GetCredentialPathInAlternateProfile(); | |
| 157 if (!file_util::PathExists(credential_path)) | |
| 158 return; | |
| 159 | |
| 160 scoped_refptr<JsonPrefStore> store = new JsonPrefStore( | |
| 161 credential_path, | |
| 162 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 163 content::BrowserThread::FILE)); | |
| 164 store->ReadPrefs(); | |
| 165 | |
| 166 const base::Value* authenticated_username = NULL; | |
| 167 const base::Value* sid = NULL; | |
| 168 const base::Value* lsid = NULL; | |
| 169 const base::Value* encryption_bootstrap_token = NULL; | |
| 170 | |
| 171 if ((store->GetValue(kAuthenticatedUsername, | |
| 172 &authenticated_username) != PrefStore::READ_OK) || | |
| 173 (store->GetValue(kSid, &sid) != PrefStore::READ_OK) || | |
| 174 (store->GetValue(kLsid, &lsid) != PrefStore::READ_OK) || | |
| 175 (store->GetValue(kEncryptionBootstrapToken, | |
| 176 &encryption_bootstrap_token) != PrefStore::READ_OK)) { | |
| 177 return; | |
| 178 } | |
| 179 | |
| 180 UnpackCredential(authenticated_username, | |
| 181 &credentials->authenticated_username_); | |
| 182 UnpackCredential(sid, &credentials->sid_); | |
| 183 UnpackCredential(lsid, &credentials->lsid_); | |
| 184 UnpackCredential(encryption_bootstrap_token, | |
| 185 &credentials->encryption_bootstrap_token_); | |
| 186 } | |
| 187 | |
| 188 } // namespace csync | |
| OLD | NEW |