summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/managed_user_passphrase_dialog.cc
blob: e5f1eae5d3f0c0ffcd249555bfa8c159828d2a9b (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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/ui/webui/managed_user_passphrase_dialog.h"

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/managed_mode/managed_user_passphrase.h"
#include "chrome/browser/managed_mode/managed_user_service.h"
#include "chrome/browser/managed_mode/managed_user_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
#include "ui/gfx/size.h"

namespace {

// Handles the message when the user entered a passphrase and clicks the Unlock
// button.
class ManagedUserPassphraseDialogMessageHandler
    : public content::WebUIMessageHandler {

 public:
  ManagedUserPassphraseDialogMessageHandler();
  virtual ~ManagedUserPassphraseDialogMessageHandler() {}

  // content::WebUIMessageHandler implementation.
  virtual void RegisterMessages() OVERRIDE;

 private:
  // Gets called from the UI with the entered passphrase as a parameter. The
  // correctness of the passphrase is checked and the result is returned to the
  // UI.
  void CheckPassphrase(const base::ListValue* args) const;

  base::WeakPtrFactory<ManagedUserPassphraseDialogMessageHandler> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ManagedUserPassphraseDialogMessageHandler);
};

ManagedUserPassphraseDialogMessageHandler
    ::ManagedUserPassphraseDialogMessageHandler() : weak_factory_(this) {
}

void ManagedUserPassphraseDialogMessageHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "checkPassphrase",
      base::Bind(&ManagedUserPassphraseDialogMessageHandler::CheckPassphrase,
                 weak_factory_.GetWeakPtr()));
}

void ManagedUserPassphraseDialogMessageHandler::CheckPassphrase(
    const base::ListValue* args) const {
  // Extract the passphrase from the provided ListValue parameter.
  const base::Value* passphrase_arg = NULL;
  args->Get(0, &passphrase_arg);
  std::string passphrase;
  passphrase_arg->GetAsString(&passphrase);

  // Get the hashed passphrase and the salt that was used to calculate it.
  Profile* profile = Profile::FromWebUI(web_ui());
  PrefService* pref_service = profile->GetPrefs();
  std::string stored_passphrase_hash =
      pref_service->GetString(prefs::kManagedModeLocalPassphrase);
  std::string salt = pref_service->GetString(prefs::kManagedModeLocalSalt);

  // Calculate the hash of the entered passphrase.
  ManagedUserPassphrase passphrase_key_generator(salt);
  std::string encoded_passphrase_hash;
  passphrase_key_generator.GenerateHashFromPassphrase(passphrase,
                                                      &encoded_passphrase_hash);

  // Check if the entered passphrase is correct and possibly set the managed
  // user into the elevated state which for example allows to modify settings.
  ManagedUserService* managed_user_service =
      ManagedUserServiceFactory::GetForProfile(profile);
  if (stored_passphrase_hash == encoded_passphrase_hash) {
    managed_user_service->SetElevated(true);
    web_ui()->CallJavascriptFunction("passphraseCorrect");
  } else {
    managed_user_service->SetElevated(false);
    web_ui()->CallJavascriptFunction("passphraseIncorrect");
  }
}

}  // namespace

ManagedUserPassphraseDialog::ManagedUserPassphraseDialog(
    content::WebContents* web_contents,
    const PassphraseCheckedCallback& callback) : callback_(callback) {
  Profile* profile =
      Profile::FromBrowserContext(web_contents->GetBrowserContext());
  CreateDataSource(profile);
  CreateConstrainedWebDialog(profile, this, NULL, web_contents);
}


ui::ModalType ManagedUserPassphraseDialog::GetDialogModalType() const {
  return ui::MODAL_TYPE_WINDOW;
}

string16 ManagedUserPassphraseDialog::GetDialogTitle() const {
  return string16();
}

GURL ManagedUserPassphraseDialog::GetDialogContentURL() const {
  return GURL(chrome::kChromeUIManagedUserPassphrasePageURL);
}

void ManagedUserPassphraseDialog::GetWebUIMessageHandlers(
    std::vector<content::WebUIMessageHandler*>* handlers) const {
  DCHECK(handlers);
  // The constrained window delegate takes care of registering the handler.
  // The handler is also deleted automatically.
  handlers->push_back(new ManagedUserPassphraseDialogMessageHandler());
}

void ManagedUserPassphraseDialog::GetDialogSize(gfx::Size* size) const {
  const int kDialogWidth = 400;
  const int kDialogHeight = 310;
  size->SetSize(kDialogWidth, kDialogHeight);
}

std::string ManagedUserPassphraseDialog::GetDialogArgs() const {
  return std::string();
}

void ManagedUserPassphraseDialog::OnDialogClosed(
    const std::string& json_retval) {
  if (!callback_.is_null()) {
    callback_.Run(!json_retval.empty());
    callback_.Reset();
  }
}

void ManagedUserPassphraseDialog::OnCloseContents(
    content::WebContents* source, bool* out_close_dialog) {
}

bool ManagedUserPassphraseDialog::ShouldShowDialogTitle() const {
  return false;
}

ManagedUserPassphraseDialog::~ManagedUserPassphraseDialog() {
}

void ManagedUserPassphraseDialog::CreateDataSource(Profile* profile) const {
  content::WebUIDataSource* data_source = content::WebUIDataSource::Create(
      chrome::kChromeUIManagedUserPassphrasePageHost);
  data_source->SetDefaultResource(IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML);
  data_source->AddResourcePath("managed_user_passphrase_dialog.js",
                               IDR_MANAGED_USER_PASSPHRASE_DIALOG_JS);
  data_source->AddResourcePath("managed_user_passphrase_dialog.css",
                               IDR_MANAGED_USER_PASSPHRASE_DIALOG_CSS);
  data_source->AddResourcePath("managed_user.png",
                               IDR_MANAGED_USER_PASSPHRASE_DIALOG_IMG);
  data_source->AddLocalizedString("managedModePassphrasePage",
                                  IDS_PASSPHRASE_TITLE);
  data_source->AddLocalizedString("unlockPassphraseButton",
                                  IDS_UNLOCK_PASSPHRASE_BUTTON);
  data_source->AddLocalizedString("passphraseInstruction",
                                  IDS_PASSPHRASE_INSTRUCTION);
  data_source->AddLocalizedString("incorrectPassphraseWarning",
                                  IDS_INCORRECT_PASSPHRASE_WARNING);
  data_source->AddLocalizedString("cancelPassphraseButton", IDS_CANCEL);
  data_source->SetJsonPath("strings.js");
  data_source->SetUseJsonJSFormatV2();
  content::WebUIDataSource::Add(profile, data_source);
}