Home | History | Annotate | Download | only in policy
      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/memory/scoped_ptr.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "base/strings/string_util.h"
     13 #include "base/values.h"
     14 #include "chrome/browser/chromeos/settings/cros_settings.h"
     15 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
     16 #include "google_apis/gaia/gaia_auth_util.h"
     17 
     18 namespace policy {
     19 
     20 namespace {
     21 
     22 const char kPublicAccountDomainPrefix[] = "public-accounts";
     23 const char kKioskAppAccountDomainPrefix[] = "kiosk-apps";
     24 const char kDeviceLocalAccountDomainSuffix[] = ".device-local.localhost";
     25 
     26 }  // namespace
     27 
     28 DeviceLocalAccount::DeviceLocalAccount(Type type,
     29                                        const std::string& account_id,
     30                                        const std::string& kiosk_app_id,
     31                                        const std::string& kiosk_app_update_url)
     32     : type(type),
     33       account_id(account_id),
     34       user_id(GenerateDeviceLocalAccountUserId(account_id, type)),
     35       kiosk_app_id(kiosk_app_id),
     36       kiosk_app_update_url(kiosk_app_update_url) {
     37 }
     38 
     39 DeviceLocalAccount::~DeviceLocalAccount() {
     40 }
     41 
     42 std::string GenerateDeviceLocalAccountUserId(const std::string& account_id,
     43                                              DeviceLocalAccount::Type type) {
     44   std::string domain_prefix;
     45   switch (type) {
     46     case DeviceLocalAccount::TYPE_PUBLIC_SESSION:
     47       domain_prefix = kPublicAccountDomainPrefix;
     48       break;
     49     case DeviceLocalAccount::TYPE_KIOSK_APP:
     50       domain_prefix = kKioskAppAccountDomainPrefix;
     51       break;
     52     case DeviceLocalAccount::TYPE_COUNT:
     53       NOTREACHED();
     54       break;
     55   }
     56   return gaia::CanonicalizeEmail(
     57       base::HexEncode(account_id.c_str(), account_id.size()) + "@" +
     58       domain_prefix + kDeviceLocalAccountDomainSuffix);
     59 }
     60 
     61 bool IsDeviceLocalAccountUser(const std::string& user_id) {
     62   return EndsWith(gaia::ExtractDomainName(user_id),
     63                   kDeviceLocalAccountDomainSuffix,
     64                   true);
     65 }
     66 
     67 bool IsKioskAppUser(const std::string& user_id) {
     68   return gaia::ExtractDomainName(user_id) ==
     69       std::string(kKioskAppAccountDomainPrefix) +
     70           kDeviceLocalAccountDomainSuffix;
     71 }
     72 
     73 void SetDeviceLocalAccounts(
     74     chromeos::CrosSettings* cros_settings,
     75     const std::vector<DeviceLocalAccount>& accounts) {
     76   base::ListValue list;
     77   for (std::vector<DeviceLocalAccount>::const_iterator it = accounts.begin();
     78        it != accounts.end(); ++it) {
     79     scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
     80     entry->SetStringWithoutPathExpansion(
     81         chromeos::kAccountsPrefDeviceLocalAccountsKeyId,
     82         it->account_id);
     83     entry->SetIntegerWithoutPathExpansion(
     84         chromeos::kAccountsPrefDeviceLocalAccountsKeyType,
     85         it->type);
     86     if (it->type == DeviceLocalAccount::TYPE_KIOSK_APP) {
     87       entry->SetStringWithoutPathExpansion(
     88           chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId,
     89           it->kiosk_app_id);
     90       if (!it->kiosk_app_update_url.empty()) {
     91         entry->SetStringWithoutPathExpansion(
     92             chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL,
     93             it->kiosk_app_update_url);
     94       }
     95     }
     96     list.Append(entry.release());
     97   }
     98 
     99   cros_settings->Set(chromeos::kAccountsPrefDeviceLocalAccounts, list);
    100 }
    101 
    102 std::vector<DeviceLocalAccount> GetDeviceLocalAccounts(
    103     chromeos::CrosSettings* cros_settings) {
    104   std::vector<DeviceLocalAccount> accounts;
    105 
    106   const base::ListValue* list = NULL;
    107   cros_settings->GetList(chromeos::kAccountsPrefDeviceLocalAccounts, &list);
    108   if (!list)
    109     return accounts;
    110 
    111   std::set<std::string> account_ids;
    112   for (size_t i = 0; i < list->GetSize(); ++i) {
    113     const base::DictionaryValue* entry = NULL;
    114     if (!list->GetDictionary(i, &entry)) {
    115       LOG(ERROR) << "Corrupt entry in device-local account list at index " << i
    116                  << ".";
    117       continue;
    118     }
    119 
    120     std::string account_id;
    121     if (!entry->GetStringWithoutPathExpansion(
    122             chromeos::kAccountsPrefDeviceLocalAccountsKeyId, &account_id) ||
    123         account_id.empty()) {
    124       LOG(ERROR) << "Missing account ID in device-local account list at index "
    125                  << i << ".";
    126       continue;
    127     }
    128 
    129     int type;
    130     if (!entry->GetIntegerWithoutPathExpansion(
    131             chromeos::kAccountsPrefDeviceLocalAccountsKeyType, &type) ||
    132         type < 0 || type >= DeviceLocalAccount::TYPE_COUNT) {
    133       LOG(ERROR) << "Missing or invalid account type in device-local account "
    134                  << "list at index " << i << ".";
    135       continue;
    136     }
    137 
    138     std::string kiosk_app_id;
    139     std::string kiosk_app_update_url;
    140     if (type == DeviceLocalAccount::TYPE_KIOSK_APP) {
    141       if (!entry->GetStringWithoutPathExpansion(
    142               chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId,
    143               &kiosk_app_id)) {
    144         LOG(ERROR) << "Missing app ID in device-local account entry at index "
    145                    << i << ".";
    146         continue;
    147       }
    148       entry->GetStringWithoutPathExpansion(
    149           chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL,
    150           &kiosk_app_update_url);
    151     }
    152 
    153     if (!account_ids.insert(account_id).second) {
    154       LOG(ERROR) << "Duplicate entry in device-local account list at index "
    155                  << i << ": " << account_id << ".";
    156       continue;
    157     }
    158 
    159     accounts.push_back(DeviceLocalAccount(
    160         static_cast<DeviceLocalAccount::Type>(type),
    161         account_id, kiosk_app_id, kiosk_app_update_url));
    162   }
    163   return accounts;
    164 }
    165 
    166 }  // namespace policy
    167