]> git.proxmox.com Git - rustc.git/blob - vendor/gix/src/remote/connection/access.rs
New upstream version 1.70.0+dfsg2
[rustc.git] / vendor / gix / src / remote / connection / access.rs
1 use crate::{
2 remote::{connection::AuthenticateFn, Connection},
3 Remote,
4 };
5
6 /// Builder
7 impl<'a, 'repo, T, P> Connection<'a, 'repo, T, P> {
8 /// Set a custom credentials callback to provide credentials if the remotes require authentication.
9 ///
10 /// Otherwise we will use the git configuration to perform the same task as the `git credential` helper program,
11 /// which is calling other helper programs in succession while resorting to a prompt to obtain credentials from the
12 /// user.
13 ///
14 /// A custom function may also be used to prevent accessing resources with authentication.
15 ///
16 /// Use the [configured_credentials()][Connection::configured_credentials()] method to obtain the implementation
17 /// that would otherwise be used, which can be useful to proxy the default configuration and obtain information about the
18 /// URLs to authenticate with.
19 pub fn with_credentials(
20 mut self,
21 helper: impl FnMut(gix_credentials::helper::Action) -> gix_credentials::protocol::Result + 'a,
22 ) -> Self {
23 self.authenticate = Some(Box::new(helper));
24 self
25 }
26
27 /// Provide configuration to be used before the first handshake is conducted.
28 /// It's typically created by initializing it with [`Repository::transport_options()`][crate::Repository::transport_options()], which
29 /// is also the default if this isn't set explicitly. Note that all of the default configuration is created from `git`
30 /// configuration, which can also be manipulated through overrides to affect the default configuration.
31 ///
32 /// Use this method to provide transport configuration with custom backend configuration that is not configurable by other means and
33 /// custom to the application at hand.
34 pub fn with_transport_options(mut self, config: Box<dyn std::any::Any>) -> Self {
35 self.transport_options = Some(config);
36 self
37 }
38 }
39
40 /// Access
41 impl<'a, 'repo, T, P> Connection<'a, 'repo, T, P> {
42 /// A utility to return a function that will use this repository's configuration to obtain credentials, similar to
43 /// what `git credential` is doing.
44 ///
45 /// It's meant to be used by users of the [`with_credentials()`][Self::with_credentials()] builder to gain access to the
46 /// default way of handling credentials, which they can call as fallback.
47 pub fn configured_credentials(
48 &self,
49 url: gix_url::Url,
50 ) -> Result<AuthenticateFn<'static>, crate::config::credential_helpers::Error> {
51 let (mut cascade, _action_with_normalized_url, prompt_opts) =
52 self.remote.repo.config_snapshot().credential_helpers(url)?;
53 Ok(Box::new(move |action| cascade.invoke(action, prompt_opts.clone())) as AuthenticateFn<'_>)
54 }
55 /// Return the underlying remote that instantiate this connection.
56 pub fn remote(&self) -> &Remote<'repo> {
57 self.remote
58 }
59
60 /// Provide a mutable transport to allow interacting with it according to its actual type.
61 /// Note that the caller _should not_ call [`configure()`][gix_protocol::transport::client::TransportWithoutIO::configure()]
62 /// as we will call it automatically before performing the handshake. Instead, to bring in custom configuration,
63 /// call [`with_transport_options()`][Connection::with_transport_options()].
64 pub fn transport_mut(&mut self) -> &mut T {
65 &mut self.transport
66 }
67 }