OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/chromeos/policy/device_local_account.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/chromeos/settings/cros_settings_names.h" |
| 13 #include "crypto/sha2.h" |
| 14 |
| 15 namespace policy { |
| 16 |
| 17 namespace { |
| 18 |
| 19 static const char kPublicAccountDomainPrefix[] = "public-accounts"; |
| 20 |
| 21 static const char kKioskAppAccountDomainPrefix[] = "kiosk-apps"; |
| 22 |
| 23 static const char kDeviceLocalAccountDomainSuffix[] = ".device-local.localhost"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 DeviceLocalAccount::DeviceLocalAccount(Type type, |
| 28 const std::string& account_id, |
| 29 const std::string& kiosk_app_id, |
| 30 const std::string& kiosk_app_update_url) |
| 31 : type(type), |
| 32 account_id(account_id), |
| 33 user_id(GenerateDeviceLocalAccountUserId(account_id, type)), |
| 34 kiosk_app_id(kiosk_app_id), |
| 35 kiosk_app_update_url(kiosk_app_update_url) { |
| 36 } |
| 37 |
| 38 std::string GenerateDeviceLocalAccountUserId(const std::string& account_id, |
| 39 DeviceLocalAccount::Type type) { |
| 40 std::string domain_prefix; |
| 41 switch (type) { |
| 42 case DeviceLocalAccount::TYPE_PUBLIC_SESSION: |
| 43 domain_prefix = kPublicAccountDomainPrefix; |
| 44 break; |
| 45 case DeviceLocalAccount::TYPE_KIOSK_APP: |
| 46 domain_prefix = kKioskAppAccountDomainPrefix; |
| 47 break; |
| 48 case DeviceLocalAccount::TYPE_COUNT: |
| 49 NOTREACHED(); |
| 50 break; |
| 51 } |
| 52 char hash[crypto::kSHA256Length]; |
| 53 crypto::SHA256HashString(account_id, &hash, sizeof(hash)); |
| 54 return base::HexEncode(&hash, sizeof(hash)) + "@" + |
| 55 domain_prefix + kDeviceLocalAccountDomainSuffix; |
| 56 } |
| 57 |
| 58 std::vector<DeviceLocalAccount> DecodeDeviceLocalAccountsList( |
| 59 const base::ListValue* list) { |
| 60 std::vector<DeviceLocalAccount> accounts; |
| 61 if (!list) |
| 62 return accounts; |
| 63 |
| 64 std::set<std::string> account_ids; |
| 65 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 66 const base::DictionaryValue* entry = NULL; |
| 67 std::string account_id; |
| 68 int type; |
| 69 std::string kiosk_app_id; |
| 70 if (!list->GetDictionary(i, &entry) || |
| 71 !entry->GetStringWithoutPathExpansion( |
| 72 chromeos::kAccountsPrefDeviceLocalAccountsKeyId, &account_id) || |
| 73 account_id.empty() || |
| 74 !entry->GetIntegerWithoutPathExpansion( |
| 75 chromeos::kAccountsPrefDeviceLocalAccountsKeyType, &type) || |
| 76 type < 0 || type >= DeviceLocalAccount::TYPE_COUNT || |
| 77 (type == DeviceLocalAccount::TYPE_KIOSK_APP && |
| 78 !entry->GetStringWithoutPathExpansion( |
| 79 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId, |
| 80 &kiosk_app_id))) { |
| 81 LOG(ERROR) << "Corrupt entry in device-local account list at index " << i |
| 82 << "."; |
| 83 continue; |
| 84 } |
| 85 std::string kiosk_app_update_url; |
| 86 if (type == DeviceLocalAccount::TYPE_KIOSK_APP) { |
| 87 entry->GetStringWithoutPathExpansion( |
| 88 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL, |
| 89 &kiosk_app_update_url); |
| 90 } |
| 91 if (!account_ids.insert(account_id).second) { |
| 92 LOG(ERROR) << "Duplicate entry in device-local account list at index " |
| 93 << i << ": " << account_id << "."; |
| 94 continue; |
| 95 } |
| 96 accounts.push_back(DeviceLocalAccount( |
| 97 static_cast<DeviceLocalAccount::Type>(type), |
| 98 account_id, kiosk_app_id, kiosk_app_update_url)); |
| 99 } |
| 100 return accounts; |
| 101 } |
| 102 |
| 103 } // namespace policy |
OLD | NEW |