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