OLD | NEW |
| (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 #import "ios/web/public/test/response_providers/http_auth_response_provider.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "net/http/http_request_headers.h" | |
10 #include "net/http/http_response_headers.h" | |
11 | |
12 namespace web { | |
13 | |
14 HttpAuthResponseProvider::HttpAuthResponseProvider(const GURL& url, | |
15 const std::string& realm, | |
16 const std::string& username, | |
17 const std::string& password) | |
18 : url_(url), realm_(realm), username_(username), password_(password) {} | |
19 | |
20 HttpAuthResponseProvider::~HttpAuthResponseProvider() = default; | |
21 | |
22 bool HttpAuthResponseProvider::CanHandleRequest(const Request& request) { | |
23 return request.url == url_; | |
24 } | |
25 | |
26 void HttpAuthResponseProvider::GetResponseHeadersAndBody( | |
27 const Request& request, | |
28 scoped_refptr<net::HttpResponseHeaders>* headers, | |
29 std::string* response_body) { | |
30 if (HeadersHaveValidCredentials(request.headers)) { | |
31 *response_body = page_text(); | |
32 *headers = GetDefaultResponseHeaders(); | |
33 } else { | |
34 *headers = GetResponseHeaders("", net::HTTP_UNAUTHORIZED); | |
35 (*headers)->AddHeader(base::StringPrintf( | |
36 "WWW-Authenticate: Basic realm=\"%s\"", realm_.c_str())); | |
37 } | |
38 } | |
39 | |
40 bool HttpAuthResponseProvider::HeadersHaveValidCredentials( | |
41 const net::HttpRequestHeaders& headers) { | |
42 std::string header; | |
43 if (headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &header)) { | |
44 std::string auth = | |
45 base::StringPrintf("%s:%s", username_.c_str(), password_.c_str()); | |
46 std::string encoded_auth; | |
47 base::Base64Encode(auth, &encoded_auth); | |
48 return header == base::StringPrintf("Basic %s", encoded_auth.c_str()); | |
49 } | |
50 return false; | |
51 } | |
52 | |
53 } // namespace web | |
OLD | NEW |