Skip to main content

rig_core/operation/
verify.rs

1//! Status-based credential verification.
2//!
3//! ```
4//! use rig_core::operation::{Verify, VerifyDecoder};
5//! use rig_core::wire::Decoder;
6//!
7//! fn decodes<'id, D: Decoder<'id, Verify>>(_: D) {}
8//! decodes(VerifyDecoder);
9//! ```
10
11use super::Whole;
12use crate::error::ProviderError;
13use crate::wire::{Call, Decoder, Flow, Free, Operation, Out, WireEvent, WireFrame};
14
15/// Checks credentials using response status. HTTP 401/403 indicate invalid
16/// authentication; status-only decoders do not interpret the response body.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct Verify;
19
20impl Operation for Verify {
21    type Request = ();
22    type Event = std::convert::Infallible;
23    type End = ();
24    type Response = ();
25    type Fold = Whole<Self>;
26    type Emit = Free;
27
28    fn fold(_request: &Self::Request, _call: &mut Call<'_>) -> Self::Fold {
29        Whole::new()
30    }
31}
32
33/// Accepts any body, including an empty one, after driver status validation.
34/// Endpoints requiring body validation must use another decoder.
35#[derive(Debug, Default)]
36pub struct VerifyDecoder;
37
38impl<'id> Decoder<'id, Verify> for VerifyDecoder {
39    type Event = ();
40
41    fn classify(&self, _frame: WireFrame) -> WireEvent<Self::Event> {
42        WireEvent::Known(())
43    }
44
45    fn decode(&mut self, _event: (), out: Out<'id, Verify>) -> Result<Flow, ProviderError> {
46        Ok(out.end(()))
47    }
48
49    /// A 2xx with an empty body still verifies: the driver only reaches EOF
50    /// when nothing framed, and the status already said yes.
51    fn eof(&mut self, out: Out<'id, Verify>) -> Result<Flow, ProviderError> {
52        Ok(out.end(()))
53    }
54}