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

Side by Side Diff: chromeos/login/auth/authpolicy_login_helper.cc

Issue 2794493002: Add AuthPolicyLoginHelper (Closed)
Patch Set: Created 3 years, 8 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "chromeos/login/auth/authpolicy_login_helper.h"
6
7 #include "base/files/file_util.h"
8 #include "base/task_scheduler/post_task.h"
9 #include "chromeos/dbus/auth_policy_client.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/upstart_client.h"
12
13 namespace chromeos {
14 namespace {
15
16 base::ScopedFD GetDataReadPipe(const std::string& data) {
17 int pipe_fds[2];
18 if (!base::CreateLocalNonBlockingPipe(pipe_fds)) {
19 DLOG(ERROR) << "Failed to create pipe";
20 return base::ScopedFD();
21 }
22 base::ScopedFD pipe_read_end(pipe_fds[0]);
23 base::ScopedFD pipe_write_end(pipe_fds[1]);
24
25 if (!base::WriteFileDescriptor(pipe_write_end.get(), data.c_str(),
26 data.size())) {
27 DLOG(ERROR) << "Failed to write to pipe";
28 return base::ScopedFD();
29 }
30 return pipe_read_end;
31 }
32
33 using OnPipeReadyCallback = base::Callback<void(base::ScopedFD fd)>;
34 // Returns file descriptor of a pipe, open for reading. Pipe keeps the passed
35 // data.
36 void GetPipeReadEnd(const std::string& data,
37 const OnPipeReadyCallback& callback) {
38 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.
39 FROM_HERE,
40 base::TaskTraits().MayBlock().WithPriority(
41 base::TaskPriority::BACKGROUND),
42 base::Bind(&GetDataReadPipe, data), callback);
43 }
44 } // namespace
45
46 AuthPolicyLoginHelper::AuthPolicyLoginHelper() : weak_factory_(this) {}
47
48 void AuthPolicyLoginHelper::JoinAdDomain(const std::string& machine_name,
49 const std::string& username,
50 const std::string& password,
51 const JoinCallback& callback) {
52 DCHECK(!weak_factory_.HasWeakPtrs()) << "Another operation is in progress";
53 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.
54 weak_factory_.GetWeakPtr(), machine_name,
55 username, callback));
56 }
57
58 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.
59 const std::string& username,
60 const JoinCallback& callback,
61 base::ScopedFD password_fd) {
62 chromeos::DBusThreadManager::Get()->GetAuthPolicyClient()->JoinAdDomain(
63 machine_name, username, password_fd.get(),
64 base::Bind(&AuthPolicyLoginHelper::OnJoinCallback,
65 weak_factory_.GetWeakPtr(), callback));
66 }
67
68 void AuthPolicyLoginHelper::OnJoinCallback(const JoinCallback& callback,
69 authpolicy::ErrorType error) {
70 callback.Run(error);
71 }
72
73 void AuthPolicyLoginHelper::AuthenticateUser(const std::string& username,
74 const std::string& password,
75 const AuthCallback& callback) {
76 DCHECK(!weak_factory_.HasWeakPtrs()) << "Another operation is in progress";
77 GetPipeReadEnd(password,
78 base::Bind(&AuthPolicyLoginHelper::DoAuthenticateUser,
79 weak_factory_.GetWeakPtr(), username, callback));
80 }
81
82 void AuthPolicyLoginHelper::DoAuthenticateUser(const std::string& username,
83 const AuthCallback& callback,
84 base::ScopedFD password_fd) {
85 chromeos::DBusThreadManager::Get()->GetAuthPolicyClient()->AuthenticateUser(
86 username, password_fd.get(),
87 base::Bind(&AuthPolicyLoginHelper::OnAuthCallback,
88 weak_factory_.GetWeakPtr(), callback));
89 }
90
91 void AuthPolicyLoginHelper::OnAuthCallback(
92 const AuthCallback& callback,
93 authpolicy::ErrorType error,
94 const authpolicy::ActiveDirectoryAccountData& account_data) {
95 callback.Run(error, account_data);
96 }
97
98 void AuthPolicyLoginHelper::CancelRequestsAndRestart() {
99 weak_factory_.InvalidateWeakPtrs();
100 chromeos::DBusThreadManager::Get()
101 ->GetUpstartClient()
102 ->RestartAuthPolicyService();
103 }
104
105 AuthPolicyLoginHelper::~AuthPolicyLoginHelper() {}
106
107 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698