File: register_basic_auth_handler.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 4,064 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/test/embedded_test_server/register_basic_auth_handler.h"

#include <ios>

#include "base/base64.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

namespace net::test_server {

namespace {

// Constructs an expected authorization header value (e.g., "Basic
// dXNlcm5hbWU6cGFzc3dvcmQ="). Works with both "WWW-Authenticate" and
// "Proxy-Authenticate" request lines.
std::string CreateExpectedBasicAuthHeader(std::string_view username,
                                          std::string_view password) {
  const std::string credentials = base::StrCat({username, ":", password});
  const std::string encoded_credentials = base::Base64Encode(credentials);
  return base::StrCat({"Basic ", encoded_credentials});
}

// Creates a 401 Unauthorized error response with the required WWW-Authenticate
// header.
std::unique_ptr<HttpResponse> CreateUnauthorizedResponse(bool is_proxy_auth) {
  auto response = std::make_unique<BasicHttpResponse>();
  response->set_code(is_proxy_auth
                         ? HttpStatusCode::HTTP_PROXY_AUTHENTICATION_REQUIRED
                         : HttpStatusCode::HTTP_UNAUTHORIZED);
  response->AddCustomHeader(
      is_proxy_auth ? "Proxy-Authenticate" : "WWW-Authenticate",
      "Basic realm=\"TestServer\"");
  response->set_content("Unauthorized");
  response->set_content_type("text/plain");
  return response;
}

// Callback to handle BasicAuth validation.
std::unique_ptr<HttpResponse> HandleBasicAuth(
    const std::string& expected_auth_header,
    bool is_proxy_auth,
    const HttpRequest& request) {
  auto auth_header = request.headers.find(is_proxy_auth ? "Proxy-Authorization"
                                                        : "Authorization");

  if (auth_header == request.headers.end() ||
      auth_header->second != expected_auth_header) {
    VLOG(1) << "Authorization failed or header missing. For Proxy: "
            << std::boolalpha << is_proxy_auth;
    return CreateUnauthorizedResponse(is_proxy_auth);
  }

  VLOG(3) << "Authorization successful. For Proxy: " << is_proxy_auth;
  return nullptr;
}

}  // namespace

void RegisterBasicAuthHandler(EmbeddedTestServer& server,
                              std::string_view username,
                              std::string_view password) {
  // Register the BasicAuth handler with the server.
  server.RegisterAuthHandler(base::BindRepeating(
      &HandleBasicAuth, CreateExpectedBasicAuthHeader(username, password),
      /*is_proxy_auth=*/false));
}

void RegisterProxyBasicAuthHandler(EmbeddedTestServer& server,
                                   std::string_view username,
                                   std::string_view password) {
  // Register the BasicAuth handler with the server.
  server.RegisterAuthHandler(base::BindRepeating(
      &HandleBasicAuth, CreateExpectedBasicAuthHeader(username, password),
      /*is_proxy_auth=*/true));
}

GURL GetURLWithUser(const EmbeddedTestServer& server,
                    std::string_view path,
                    std::string_view user) {
  GURL url = server.GetURL(path);
  GURL::Replacements replacements;
  replacements.SetUsernameStr(user);
  return url.ReplaceComponents(replacements);
}

GURL GetURLWithUserAndPassword(const EmbeddedTestServer& server,
                               std::string_view path,
                               std::string_view user,
                               std::string_view password) {
  GURL url = server.GetURL(path);
  GURL::Replacements replacements;
  replacements.SetUsernameStr(user);
  replacements.SetPasswordStr(password);
  return url.ReplaceComponents(replacements);
}

}  // namespace net::test_server