1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::cell::RefCell;
use std::rc::Rc;

use puffin::agent::{AgentDescriptor, AgentType};
use puffin::algebra::dynamic_function::TypeShape;
use puffin::claims::GlobalClaimList;
use puffin::protocol::ProtocolBehavior;
use puffin::put::PutOptions;

use crate::claims::TlsClaim;
use crate::protocol::TLSProtocolBehavior;

/// Static configuration for creating a new agent state for the PUT
#[derive(Clone)]
pub struct TlsPutConfig {
    pub descriptor: AgentDescriptor,
    pub claims: GlobalClaimList<TlsClaim>,
    pub authenticate_peer: bool,
    pub extract_deferred: Rc<RefCell<Option<TypeShape>>>,
    pub use_clear: bool,
}

impl TlsPutConfig {
    pub fn new(
        agent_descriptor: &AgentDescriptor,
        claims: &GlobalClaimList<<TLSProtocolBehavior as ProtocolBehavior>::Claim>,
        options: &PutOptions,
    ) -> TlsPutConfig {
        let use_clear = options
            .get_option("use_clear")
            .map(|value| value.parse().unwrap_or(false))
            .unwrap_or(false);

        TlsPutConfig {
            descriptor: agent_descriptor.clone(),
            claims: claims.clone(),
            authenticate_peer: agent_descriptor.typ == AgentType::Client
                && agent_descriptor.server_authentication
                || agent_descriptor.typ == AgentType::Server
                    && agent_descriptor.client_authentication,
            extract_deferred: Rc::new(RefCell::new(None)),
            use_clear,
        }
    }
}