OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/policy/core/common/cloud/policy_header_service.h" | 5 #include "components/policy/core/common/cloud/policy_header_service.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "components/policy/core/common/cloud/cloud_policy_store.h" | 10 #include "components/policy/core/common/cloud/cloud_policy_store.h" |
11 #include "components/policy/core/common/cloud/policy_header_io_helper.h" | 11 #include "components/policy/core/common/cloud/policy_header_io_helper.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 const char kUserDMTokenKey[] = "user_dmtoken"; | 14 const char kUserDMTokenKey[] = "user_dmtoken"; |
| 15 const char kUserPolicyTokenKey[] = "user_policy_token"; |
| 16 const char kVerificationKeyHashKey[] = "verification_key_hash"; |
15 } | 17 } |
16 | 18 |
17 namespace policy { | 19 namespace policy { |
18 | 20 |
19 PolicyHeaderService::PolicyHeaderService(const std::string& server_url, | 21 PolicyHeaderService::PolicyHeaderService( |
20 CloudPolicyStore* user_policy_store, | 22 const std::string& server_url, |
21 CloudPolicyStore* device_policy_store) | 23 const std::string& verification_key_hash, |
| 24 CloudPolicyStore* user_policy_store, |
| 25 CloudPolicyStore* device_policy_store) |
22 : server_url_(server_url), | 26 : server_url_(server_url), |
| 27 verification_key_hash_(verification_key_hash), |
23 user_policy_store_(user_policy_store), | 28 user_policy_store_(user_policy_store), |
24 device_policy_store_(device_policy_store) { | 29 device_policy_store_(device_policy_store) { |
25 user_policy_store_->AddObserver(this); | 30 user_policy_store_->AddObserver(this); |
26 if (device_policy_store_) | 31 if (device_policy_store_) |
27 device_policy_store_->AddObserver(this); | 32 device_policy_store_->AddObserver(this); |
28 } | 33 } |
29 | 34 |
30 PolicyHeaderService::~PolicyHeaderService() { | 35 PolicyHeaderService::~PolicyHeaderService() { |
31 user_policy_store_->RemoveObserver(this); | 36 user_policy_store_->RemoveObserver(this); |
32 if (device_policy_store_) | 37 if (device_policy_store_) |
(...skipping 14 matching lines...) Expand all Loading... |
47 // If we have no user policy or no token, return an empty header. | 52 // If we have no user policy or no token, return an empty header. |
48 if (!user_policy_store_->has_policy() || | 53 if (!user_policy_store_->has_policy() || |
49 !user_policy_store_->policy()->has_request_token()) { | 54 !user_policy_store_->policy()->has_request_token()) { |
50 return ""; | 55 return ""; |
51 } | 56 } |
52 | 57 |
53 // Generate a Base64-encoded header of the form: | 58 // Generate a Base64-encoded header of the form: |
54 // { | 59 // { |
55 // user_dmtoken: <dm_token> | 60 // user_dmtoken: <dm_token> |
56 // user_policy_token: <policy_token> | 61 // user_policy_token: <policy_token> |
| 62 // verification_key_hash: <key_hash> |
57 // } | 63 // } |
58 std::string user_dm_token = user_policy_store_->policy()->request_token(); | 64 std::string user_dm_token = user_policy_store_->policy()->request_token(); |
59 base::DictionaryValue value; | 65 base::DictionaryValue value; |
60 value.SetString(kUserDMTokenKey, user_dm_token); | 66 value.SetString(kUserDMTokenKey, user_dm_token); |
| 67 // TODO(atwilson): Enable this once policy token is available. |
| 68 //if (user_policy_store_->policy()->has_policy_token()) { |
| 69 // value.SetString(kUserPolicyTokenKey, |
| 70 // user_policy_store_->policy()->policy_token()); |
| 71 //} |
| 72 value.SetString(kUserPolicyTokenKey, ""); |
| 73 if (!verification_key_hash_.empty()) |
| 74 value.SetString(kVerificationKeyHashKey, verification_key_hash_); |
| 75 |
61 // TODO(atwilson): add user_policy_token once the server starts sending it | 76 // TODO(atwilson): add user_policy_token once the server starts sending it |
62 // down (http://crbug.com/326799). | 77 // down (http://crbug.com/326799). |
63 std::string json; | 78 std::string json; |
64 base::JSONWriter::Write(&value, &json); | 79 base::JSONWriter::Write(&value, &json); |
65 DCHECK(!json.empty()); | 80 DCHECK(!json.empty()); |
66 | 81 |
67 // Base64-encode the result so we can include it in a header. | 82 // Base64-encode the result so we can include it in a header. |
68 std::string encoded; | 83 std::string encoded; |
69 base::Base64Encode(json, &encoded); | 84 base::Base64Encode(json, &encoded); |
70 return encoded; | 85 return encoded; |
71 } | 86 } |
72 | 87 |
73 void PolicyHeaderService::OnStoreLoaded(CloudPolicyStore* store) { | 88 void PolicyHeaderService::OnStoreLoaded(CloudPolicyStore* store) { |
74 // If we have a PolicyHeaderIOHelper, notify it of the new header value. | 89 // If we have a PolicyHeaderIOHelper, notify it of the new header value. |
75 if (!helpers_.empty()) { | 90 if (!helpers_.empty()) { |
76 std::string new_header = CreateHeaderValue(); | 91 std::string new_header = CreateHeaderValue(); |
77 for (std::vector<PolicyHeaderIOHelper*>::const_iterator it = | 92 for (std::vector<PolicyHeaderIOHelper*>::const_iterator it = |
78 helpers_.begin(); it != helpers_.end(); ++it) { | 93 helpers_.begin(); it != helpers_.end(); ++it) { |
79 (*it)->UpdateHeader(new_header); | 94 (*it)->UpdateHeader(new_header); |
80 } | 95 } |
81 } | 96 } |
82 } | 97 } |
83 | 98 |
84 void PolicyHeaderService::OnStoreError(CloudPolicyStore* store) { | 99 void PolicyHeaderService::OnStoreError(CloudPolicyStore* store) { |
85 // Do nothing on errors. | 100 // Do nothing on errors. |
86 } | 101 } |
87 | 102 |
88 } // namespace policy | 103 } // namespace policy |
OLD | NEW |