Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Unified Diff: chromeos/login/auth/authpolicy_login_helper.cc

Issue 2794493002: Add AuthPolicyLoginHelper (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chromeos/login/auth/authpolicy_login_helper.cc
diff --git a/chromeos/login/auth/authpolicy_login_helper.cc b/chromeos/login/auth/authpolicy_login_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8ce12d9335349cb2075dfc7aa9513842c044d239
--- /dev/null
+++ b/chromeos/login/auth/authpolicy_login_helper.cc
@@ -0,0 +1,107 @@
+// Copyright 2017 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 "chromeos/login/auth/authpolicy_login_helper.h"
+
+#include "base/files/file_util.h"
+#include "base/task_scheduler/post_task.h"
+#include "chromeos/dbus/auth_policy_client.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/dbus/upstart_client.h"
+
+namespace chromeos {
+namespace {
+
+base::ScopedFD GetDataReadPipe(const std::string& data) {
+ int pipe_fds[2];
+ if (!base::CreateLocalNonBlockingPipe(pipe_fds)) {
+ DLOG(ERROR) << "Failed to create pipe";
+ return base::ScopedFD();
+ }
+ base::ScopedFD pipe_read_end(pipe_fds[0]);
+ base::ScopedFD pipe_write_end(pipe_fds[1]);
+
+ if (!base::WriteFileDescriptor(pipe_write_end.get(), data.c_str(),
+ data.size())) {
+ DLOG(ERROR) << "Failed to write to pipe";
+ return base::ScopedFD();
+ }
+ return pipe_read_end;
+}
+
+using OnPipeReadyCallback = base::Callback<void(base::ScopedFD fd)>;
+// Returns file descriptor of a pipe, open for reading. Pipe keeps the passed
+// data.
+void GetPipeReadEnd(const std::string& data,
+ const OnPipeReadyCallback& callback) {
+ base::PostTaskWithTraitsAndReplyWithResult(
ljusten (tachyonic) 2017/03/31 15:19:33 As discussed offline, this seems overkill. You're
Roman Sorokin (ftl) 2017/04/04 10:10:49 Done.
+ FROM_HERE,
+ base::TaskTraits().MayBlock().WithPriority(
+ base::TaskPriority::BACKGROUND),
+ base::Bind(&GetDataReadPipe, data), callback);
+}
+} // namespace
+
+AuthPolicyLoginHelper::AuthPolicyLoginHelper() : weak_factory_(this) {}
+
+void AuthPolicyLoginHelper::JoinAdDomain(const std::string& machine_name,
+ const std::string& username,
+ const std::string& password,
+ const JoinCallback& callback) {
+ DCHECK(!weak_factory_.HasWeakPtrs()) << "Another operation is in progress";
+ GetPipeReadEnd(password, base::Bind(&AuthPolicyLoginHelper::DoJoinAdDomain,
ljusten (tachyonic) 2017/03/31 15:19:33 This could be much simpler, see above.
Roman Sorokin (ftl) 2017/04/04 10:10:49 Done.
+ weak_factory_.GetWeakPtr(), machine_name,
+ username, callback));
+}
+
+void AuthPolicyLoginHelper::DoJoinAdDomain(const std::string& machine_name,
ljusten (tachyonic) 2017/03/31 15:19:33 Order of method definition doesn't match declarati
Roman Sorokin (ftl) 2017/04/04 10:10:49 Done.
+ const std::string& username,
+ const JoinCallback& callback,
+ base::ScopedFD password_fd) {
+ chromeos::DBusThreadManager::Get()->GetAuthPolicyClient()->JoinAdDomain(
+ machine_name, username, password_fd.get(),
+ base::Bind(&AuthPolicyLoginHelper::OnJoinCallback,
+ weak_factory_.GetWeakPtr(), callback));
+}
+
+void AuthPolicyLoginHelper::OnJoinCallback(const JoinCallback& callback,
+ authpolicy::ErrorType error) {
+ callback.Run(error);
+}
+
+void AuthPolicyLoginHelper::AuthenticateUser(const std::string& username,
+ const std::string& password,
+ const AuthCallback& callback) {
+ DCHECK(!weak_factory_.HasWeakPtrs()) << "Another operation is in progress";
+ GetPipeReadEnd(password,
+ base::Bind(&AuthPolicyLoginHelper::DoAuthenticateUser,
+ weak_factory_.GetWeakPtr(), username, callback));
+}
+
+void AuthPolicyLoginHelper::DoAuthenticateUser(const std::string& username,
+ const AuthCallback& callback,
+ base::ScopedFD password_fd) {
+ chromeos::DBusThreadManager::Get()->GetAuthPolicyClient()->AuthenticateUser(
+ username, password_fd.get(),
+ base::Bind(&AuthPolicyLoginHelper::OnAuthCallback,
+ weak_factory_.GetWeakPtr(), callback));
+}
+
+void AuthPolicyLoginHelper::OnAuthCallback(
+ const AuthCallback& callback,
+ authpolicy::ErrorType error,
+ const authpolicy::ActiveDirectoryAccountData& account_data) {
+ callback.Run(error, account_data);
+}
+
+void AuthPolicyLoginHelper::CancelRequestsAndRestart() {
+ weak_factory_.InvalidateWeakPtrs();
+ chromeos::DBusThreadManager::Get()
+ ->GetUpstartClient()
+ ->RestartAuthPolicyService();
+}
+
+AuthPolicyLoginHelper::~AuthPolicyLoginHelper() {}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698