File: printer_authenticator.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (212 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download | duplicates (6)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/printing/printer_authenticator.h"

#include <memory>
#include <string>
#include <string_view>
#include <utility>

#include "base/check.h"
#include "base/functional/bind.h"
#include "chrome/browser/ash/printing/cups_printers_manager.h"
#include "chrome/browser/ash/printing/oauth2/authorization_zones_manager.h"
#include "chrome/browser/ash/printing/oauth2/log_entry.h"
#include "chrome/browser/ash/printing/oauth2/signin_dialog.h"
#include "chrome/browser/ash/printing/oauth2/status_code.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chromeos/printing/cups_printer_status.h"
#include "chromeos/printing/printer_configuration.h"
#include "chromeos/printing/uri.h"
#include "components/device_event_log/device_event_log.h"
#include "ui/views/window/dialog_delegate.h"
#include "url/gurl.h"

namespace ash::printing {

namespace {

// Logs results to device-log and calls `callback` with parameters `status` and
// `data`.
void LogAndCall(oauth2::StatusCallback callback,
                std::string_view method,
                const GURL& auth_server,
                oauth2::StatusCode status,
                std::string data) {
  if (status == oauth2::StatusCode::kOK) {
    PRINTER_LOG(EVENT) << oauth2::LogEntry("", method, auth_server, status);
  } else {
    PRINTER_LOG(ERROR) << oauth2::LogEntry(data, method, auth_server, status);
  }
  std::move(callback).Run(status, std::move(data));
}

}  // namespace

PrinterAuthenticator::PrinterAuthenticator(
    CupsPrintersManager* printers_manager,
    oauth2::AuthorizationZonesManager* auth_manager,
    const chromeos::Printer& printer)
    : cups_manager_(printers_manager),
      auth_manager_(auth_manager),
      printer_(printer) {
  DCHECK(printers_manager);
  DCHECK(auth_manager);
}

PrinterAuthenticator::~PrinterAuthenticator() = default;

void PrinterAuthenticator::ObtainAccessTokenIfNeeded(
    oauth2::StatusCallback callback) {
  DCHECK(!callback_);
  callback_ = std::move(callback);
  cups_manager_->FetchPrinterStatus(
      printer_.id(), base::BindOnce(&PrinterAuthenticator::OnGetPrinterStatus,
                                    weak_factory_.GetWeakPtr()));
}

void PrinterAuthenticator::SetUIResponsesForTesting(
    oauth2::StatusCode is_trusted_dialog_response,
    oauth2::StatusCode signin_dialog_response) {
  is_trusted_dialog_response_for_testing_ = is_trusted_dialog_response;
  signin_dialog_response_for_testing_ = signin_dialog_response;
}

void PrinterAuthenticator::OnGetPrinterStatus(
    const chromeos::CupsPrinterStatus& printer_status) {
  const chromeos::PrinterAuthenticationInfo& auth_mode =
      printer_status.GetAuthenticationInfo();
  if (auth_mode.oauth_server.empty()) {
    // A printer does not require authentication.
    std::move(callback_).Run(oauth2::StatusCode::kOK, "");
    return;
  }

  oauth_server_ = GURL(auth_mode.oauth_server);
  if (!oauth_server_.is_valid()) {
    std::move(callback_).Run(oauth2::StatusCode::kInvalidURL, "");
    return;
  }
  oauth_scope_ = auth_mode.oauth_scope;

  auth_manager_->GetEndpointAccessToken(oauth_server_, printer_.uri(),
                                        oauth_scope_,
                                        OnComplete(Step::kGetAccessToken));
}

oauth2::StatusCallback PrinterAuthenticator::OnComplete(Step step) {
  return base::BindOnce(&PrinterAuthenticator::ToNextStep,
                        weak_factory_.GetWeakPtr(), step);
}

void PrinterAuthenticator::ToNextStep(PrinterAuthenticator::Step current_step,
                                      oauth2::StatusCode status,
                                      std::string data) {
  switch (current_step) {
    case Step::kGetAccessToken:
      if (status == oauth2::StatusCode::kOK) {
        // Success, return the endpoint access token.
        std::move(callback_).Run(status, std::move(data));
        return;
      }
      if (status == oauth2::StatusCode::kUntrustedAuthorizationServer) {
        ShowIsTrustedDialog(oauth_server_,
                            OnComplete(Step::kShowIsTrustedDialog));
        return;
      }
      if (status == oauth2::StatusCode::kAuthorizationNeeded) {
        auth_manager_->InitAuthorization(oauth_server_, oauth_scope_,
                                         OnComplete(Step::kInitAuthorization));
        return;
      }
      break;
    case Step::kShowIsTrustedDialog:
      if (status == oauth2::StatusCode::kOK) {
        status = auth_manager_->SaveAuthorizationServerAsTrusted(oauth_server_);
        if (status == oauth2::StatusCode::kOK) {
          auth_manager_->GetEndpointAccessToken(
              oauth_server_, printer_.uri(), oauth_scope_,
              OnComplete(Step::kGetAccessToken));
          return;
        }
      }
      break;
    case Step::kInitAuthorization:
      if (status == oauth2::StatusCode::kOK) {
        ShowSigninDialog(data, OnComplete(Step::kShowSigninDialog));
        return;
      }
      if (status == oauth2::StatusCode::kUntrustedAuthorizationServer) {
        ShowIsTrustedDialog(oauth_server_,
                            OnComplete(Step::kShowIsTrustedDialog));
        return;
      }
      break;
    case Step::kShowSigninDialog:
      if (status == oauth2::StatusCode::kOK) {
        auth_manager_->FinishAuthorization(
            oauth_server_, GURL(data), OnComplete(Step::kFinishAuthorization));
        return;
      }
      break;
    case Step::kFinishAuthorization:
      if (status == oauth2::StatusCode::kOK) {
        auth_manager_->GetEndpointAccessToken(
            oauth_server_, printer_.uri(), oauth_scope_,
            OnComplete(Step::kGetAccessToken));
        return;
      }
      break;
  }

  // An error occurred.
  std::move(callback_).Run(status, "");
}

void PrinterAuthenticator::ShowIsTrustedDialog(
    const GURL& auth_url,
    oauth2::StatusCallback callback) {
  // Log the callback to device-log.
  callback =
      base::BindOnce(&LogAndCall, std::move(callback), __func__, oauth_server_);

  if (is_trusted_dialog_response_for_testing_) {
    std::move(callback).Run(*is_trusted_dialog_response_for_testing_,
                            "response from mock");
    return;
  }
  // TODO(https://crbug.com/1223535): Add dialog asking the user if
  // the server is trusted. For now, we just save the server as trusted.
  std::move(callback).Run(oauth2::StatusCode::kOK, "");
}

void PrinterAuthenticator::ShowSigninDialog(const std::string& auth_url,
                                            oauth2::StatusCallback callback) {
  // Log the callback to device-log.
  callback =
      base::BindOnce(&LogAndCall, std::move(callback), __func__, oauth_server_);

  const GURL url(auth_url);
  if (!url.is_valid()) {
    std::move(callback).Run(oauth2::StatusCode::kInvalidURL,
                            "auth_url=" + url.possibly_invalid_spec());
    return;
  }

  if (signin_dialog_response_for_testing_) {
    std::move(callback).Run(*signin_dialog_response_for_testing_,
                            "response from mock");
    return;
  }

  auto dialog = std::make_unique<oauth2::SigninDialog>(
      ProfileManager::GetPrimaryUserProfile());
  oauth2::SigninDialog* dialog_ptr = dialog.get();
  views::DialogDelegate::CreateDialogWidget(
      std::move(dialog), /*context=*/nullptr, /*parent=*/nullptr);
  dialog_ptr->StartAuthorizationProcedure(url, std::move(callback));
}

}  // namespace ash::printing