pub trait ReceiveAuth:
Send
+ Sync
+ 'static {
type Principal: Send + Sync + 'static;
// Required methods
fn owner(principal: &Self::Principal) -> &str;
fn authenticate(
&self,
parts: &Parts,
kind: RequestKind,
) -> impl Future<Output = Result<Self::Principal, Refusal>> + Send;
fn open(
&self,
principal: &Self::Principal,
hello: &Hello,
) -> impl Future<Output = Result<SessionSetup, Refusal>> + Send;
}Expand description
The authentication of the requests of the receive endpoint.
A refusal of authenticate or open
gets its status, its headers, and an unauthorized frame with its
message. The endpoint adds no WWW-Authenticate header of its own.
§Call order
The endpoint calls authenticate for each request
with a known route and method, before it reads a byte of the body. It
gives the RequestKind of the route. For a session request, the
endpoint then does these steps, in this order:
- It reads
Hello. - It refuses a
Hellothat hasone-wayset totrue, with the error codeprotocol. This step comes before the check of the version. - It checks
HellowithReceiveService::check_hello. - It takes a slot of the session limit, or answers 503.
- It calls
open, which gives theSessionSetupof the session. A refusal frees the slot. - It opens the session.
The checks of Hello come before the slot. A bad Hello gets 422 also
when the sessions are at the limit or the endpoint stopped.
§Examples
An implementation with one bearer token, which gives the same receive policy to each session:
use std::sync::Arc;
use hyper::http::request::Parts;
use ostrya::remote::proto::Hello;
use ostrya::{ReceivePolicy, Repo};
use ostrya_server::{
EndpointOptions, ReceiveAuth, ReceiveEndpoint, Refusal, RequestKind, SessionSetup,
};
struct OneToken {
header: String,
policy: Arc<ReceivePolicy>,
}
impl ReceiveAuth for OneToken {
type Principal = String;
fn owner(principal: &String) -> &str {
principal
}
async fn authenticate(&self, parts: &Parts, _kind: RequestKind) -> Result<String, Refusal> {
// A real host compares a digest of the token in constant time.
match parts.headers.get("authorization") {
Some(value) if value.as_bytes() == self.header.as_bytes() => Ok("builder".into()),
_ => Err(Refusal::unauthorized("a valid token is necessary")),
}
}
async fn open(&self, _principal: &String, _hello: &Hello) -> Result<SessionSetup, Refusal> {
Ok(SessionSetup { policy: self.policy.clone(), hooks: None })
}
}
fn endpoint(
repo: Repo,
token: &str,
policy: Arc<ReceivePolicy>,
) -> ostrya_server::Result<ReceiveEndpoint<OneToken>> {
let auth = OneToken {
header: format!("Bearer {token}"),
policy,
};
ReceiveEndpoint::new(repo, auth, EndpointOptions::default())
}Required Associated Types§
Required Methods§
Sourcefn owner(principal: &Self::Principal) -> &str
fn owner(principal: &Self::Principal) -> &str
Returns the owner key of a principal.
A session belongs to the key of its session request. If a later
request of the session gives a different key, it gets 404, the answer
to an unknown session.
Sourcefn authenticate(
&self,
parts: &Parts,
kind: RequestKind,
) -> impl Future<Output = Result<Self::Principal, Refusal>> + Send
fn authenticate( &self, parts: &Parts, kind: RequestKind, ) -> impl Future<Output = Result<Self::Principal, Refusal>> + Send
Returns the principal of a request, or its refusal.
The endpoint calls it in the call order, with
the route of the request as kind. If it returns a refusal, the
endpoint reads and drops up to 1 MiB of the body before it answers.
§Errors
Refusalto refuse the request. The endpoint answers with the status, the headers, and the message of the refusal.
Sourcefn open(
&self,
principal: &Self::Principal,
hello: &Hello,
) -> impl Future<Output = Result<SessionSetup, Refusal>> + Send
fn open( &self, principal: &Self::Principal, hello: &Hello, ) -> impl Future<Output = Result<SessionSetup, Refusal>> + Send
Returns the setup of the session that hello opens for principal.
The setup holds the receive policy and the hooks of the session. The
endpoint calls open in the call order,
after the checks of Hello and after it reserves a session slot.
Because of these checks, hello holds valid ref names alone. Its
collection id, if present, is valid too.
The scope of the returned ReceivePolicy
must cover the namespace of hello. Otherwise the session refuses
the policy with 422 and ref-denied.
Each open call holds a slot, so the session limit also limits the
count of open calls that run at one time.
The host must not keep state from open alone, because a session can
fail to start after open returns. For example, a read of the
repository configuration fails, or a bare repository gets
mode-refused because the server does not run as root.
§Errors
Refusalto refuse the session. The endpoint answers with the status, the headers, and the message of the refusal, and frees the slot.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".