parsec_interface/requests/request/
request_auth.rs

1// Copyright 2019 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::requests::Result;
4use crate::secrecy::{ExposeSecret, Secret};
5use std::io::{Read, Write};
6
7/// Wrapper around the authentication value of a request.
8///
9/// Hides the contents and keeps them immutable.
10#[allow(missing_debug_implementations)]
11pub struct RequestAuth {
12    /// Buffer holding the authentication token as a byte vector
13    pub buffer: Secret<Vec<u8>>,
14}
15
16impl RequestAuth {
17    /// Create a new authentication field for a request.
18    pub fn new(bytes: Vec<u8>) -> Self {
19        RequestAuth {
20            buffer: Secret::new(bytes),
21        }
22    }
23
24    /// Read a request authentication field from the stream, given the length
25    /// of the byte stream contained.
26    pub(super) fn read_from_stream(mut stream: &mut impl Read, len: usize) -> Result<RequestAuth> {
27        let buffer = get_from_stream!(stream; len);
28        Ok(RequestAuth {
29            buffer: Secret::new(buffer),
30        })
31    }
32
33    /// Write request authentication field to stream.
34    pub(super) fn write_to_stream(&self, stream: &mut impl Write) -> Result<()> {
35        stream.write_all(self.buffer.expose_secret())?;
36        Ok(())
37    }
38}