Prompt user for credentials when http request needs it (#34620)

* prompt user to get their credentials

Signed-off-by: Lloyd Massiah artmis9@protonmail.com

move credential prompt to a function

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* add prompt for step 15.4

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* add new prompt definition for user credentials

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* remove default implementation for HttpState which allowed making the embedder_proxy non-optional

- default implementation was only used in tests so created an alternative create_http_state function

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

add credentials to authentication cache

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* add tests that are successful for the happy path

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* add test for user cancels prompt and user inputs incorrect credentials, and refactor shared code between tests

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* handle error when setting username and password in Url and ran formatting

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

renaming test functions

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* change authentication flag to false for proxy authentication. The spec doesn't specify that the flag should be true, and the flag is by default false

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* clean up test code a bit

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* add skeleton implementation to support open harmony and android

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* update warning message to include Android

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* fix build error for OH os and Android

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* remove unused import to fix warning

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

---------

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>
Co-authored-by: lazypassion <25536767+lazypassion@users.noreply.github.com>
This commit is contained in:
arthmis 2024-12-28 15:24:11 -05:00 committed by GitHub
parent a9539d8b03
commit aa40b8f820
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 379 additions and 51 deletions

View file

@ -19,21 +19,23 @@ mod resource_thread;
mod subresource_integrity;
use core::convert::Infallible;
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufReader};
use std::net::TcpListener as StdTcpListener;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex, Weak};
use std::sync::{Arc, LazyLock, Mutex, RwLock, Weak};
use crossbeam_channel::{unbounded, Sender};
use devtools_traits::DevtoolsControlMsg;
use embedder_traits::{EmbedderProxy, EventLoopWaker};
use embedder_traits::{EmbedderProxy, EmbedderReceiver, EventLoopWaker};
use futures::future::ready;
use futures::StreamExt;
use hyper::server::conn::Http;
use hyper::server::Server as HyperServer;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request as HyperRequest, Response as HyperResponse};
use net::connector::{create_http_client, create_tls_config};
use net::fetch::cors_cache::CorsCache;
use net::fetch::methods::{self, CancellationListener, FetchContext};
use net::filemanager_thread::FileManager;
@ -95,6 +97,76 @@ fn create_embedder_proxy() -> EmbedderProxy {
}
}
fn create_embedder_proxy_and_receiver() -> (EmbedderProxy, EmbedderReceiver) {
let (sender, receiver) = unbounded();
let event_loop_waker = || {
struct DummyEventLoopWaker {}
impl DummyEventLoopWaker {
fn new() -> DummyEventLoopWaker {
DummyEventLoopWaker {}
}
}
impl embedder_traits::EventLoopWaker for DummyEventLoopWaker {
fn wake(&self) {}
fn clone_box(&self) -> Box<dyn embedder_traits::EventLoopWaker> {
Box::new(DummyEventLoopWaker {})
}
}
Box::new(DummyEventLoopWaker::new())
};
let embedder_proxy = embedder_traits::EmbedderProxy {
sender: sender.clone(),
event_loop_waker: event_loop_waker(),
};
let embedder_receiver = EmbedderReceiver { receiver };
(embedder_proxy, embedder_receiver)
}
fn receive_credential_prompt_msgs(
mut embedder_receiver: EmbedderReceiver,
username: Option<String>,
password: Option<String>,
) -> std::thread::JoinHandle<()> {
std::thread::spawn(move || {
let (_browser_context_id, embedder_msg) = embedder_receiver.recv_embedder_msg();
match embedder_msg {
embedder_traits::EmbedderMsg::Prompt(prompt_definition, _prompt_origin) => {
match prompt_definition {
embedder_traits::PromptDefinition::Credentials(ipc_sender) => {
ipc_sender
.send(embedder_traits::PromptCredentialsInput { username, password })
.unwrap();
},
_ => unreachable!(),
}
},
_ => unreachable!(),
}
})
}
fn create_http_state(fc: Option<EmbedderProxy>) -> HttpState {
let override_manager = net::connector::CertificateErrorOverrideManager::new();
HttpState {
hsts_list: RwLock::new(net::hsts::HstsList::default()),
cookie_jar: RwLock::new(net::cookie_storage::CookieStorage::new(150)),
auth_cache: RwLock::new(net::resource_thread::AuthCache::default()),
history_states: RwLock::new(HashMap::new()),
http_cache: RwLock::new(net::http_cache::HttpCache::default()),
http_cache_state: Mutex::new(HashMap::new()),
client: create_http_client(create_tls_config(
net::connector::CACertificates::Default,
false, /* ignore_certificate_errors */
override_manager.clone(),
)),
override_manager,
embedder_proxy: Mutex::new(fc.unwrap_or_else(|| create_embedder_proxy())),
}
}
fn new_fetch_context(
dc: Option<Sender<DevtoolsControlMsg>>,
fc: Option<EmbedderProxy>,
@ -103,7 +175,7 @@ fn new_fetch_context(
let sender = fc.unwrap_or_else(|| create_embedder_proxy());
FetchContext {
state: Arc::new(HttpState::default()),
state: Arc::new(create_http_state(Some(sender.clone()))),
user_agent: DEFAULT_USER_AGENT.into(),
devtools_chan: dc.map(|dc| Arc::new(Mutex::new(dc))),
filemanager: Arc::new(Mutex::new(FileManager::new(