pub async fn join_realm(
    core: &mut Core<'_>,
    uri: WampString,
    roles: HashSet<ClientRole>,
    agent_str: Option<WampString>,
    authentication_methods: Vec<AuthenticationMethod>,
    authid: Option<WampString>,
    on_challenge_handler: Option<AuthenticationChallengeHandler<'_>>,
    res: JoinResult,
) -> Status {
    let mut details: WampDict = WampDict::new();
    let mut client_roles: WampDict = WampDict::new();
    // Add all of our roles
    for role in &roles {
        client_roles.insert(String::from(role.to_str()), Arg::Dict(WampDict::new()));
    }
    details.insert("roles".to_owned(), Arg::Dict(client_roles));

    if let Some(agent) = agent_str {
        details.insert("agent".to_owned(), Arg::String(agent));
    }

    if !authentication_methods.is_empty() {
        details.insert(
            "authmethods".to_owned(),
            Arg::List(
                authentication_methods
                    .iter()
                    .map(|authentication_method| {
                        Arg::String(authentication_method.as_ref().to_owned())
                    })
                    .collect::<Vec<_>>(),
            ),
        );
    }

    if let Some(authid) = authid {
        details.insert("authid".to_owned(), Arg::String(authid));
    }

    // Send hello with our info
    if let Err(e) = core
        .send(&Msg::Hello {
            realm: uri,
            details,
        })
        .await
    {
        let _ = res.send(Err(e));
        return Status::Shutdown;
    }

    // Make sure the server responded with the proper message
    let (session_id, server_roles) = loop {
        // Receive the response to the HELLO message (either WELCOME or CHALLENGE are expected)
        let resp = match core.recv().await {
            Ok(r) => r,
            Err(e) => {
                let _ = res.send(Err(e));
                return Status::Shutdown;
            }
        };

        match resp {
            Msg::Welcome { session, details } => break (session, details),
            Msg::Challenge {
                authentication_method,
                extra,
            } => {
                if let Some(ref on_challenge_handler) = on_challenge_handler {
                    match on_challenge_handler(authentication_method, extra).await {
                        Ok(AuthenticationChallengeResponse { signature, extra }) => {
                            if let Err(e) = core.send(&Msg::Authenticate { signature, extra }).await
                            {
                                let _ = res.send(Err(e));
                                return Status::Shutdown;
                            }
                        }
                        Err(e) => {
                            let _ = res.send(Err(e));
                            return Status::Shutdown;
                        }
                    }
                } else {
                    let _ = res.send(Err(From::from(
                        "Server requested a CHALLENGE to authenticate, but there was no challenge handler provided".to_string()
                    )));
                    return Status::Shutdown;
                }
            }
            m => {
                let _ = res.send(Err(From::from(format!(
                    "Server did not respond with WELCOME : {:?}",
                    m
                ))));
                return Status::Shutdown;
            }
        }
    };

    // Return the pertinent info to the caller
    core.valid_session = true;
    let _ = res.send(Ok((session_id, server_roles)));

    Status::Ok
}
/// Handler for any join realm request. This will send a HELLO and wait for the WELCOME response
Handler for any join realm request. This will send a HELLO and wait for the WELCOME response
Handler for any join realm request . This will send a HELLO and wait for the WELCOME response
pub async fn join_realm(
    core: &mut Core<'_>,
    uri: WampString,
    roles: HashSet<ClientRole>,
    agent_str: Option<WampString>,
    authentication_methods: Vec<AuthenticationMethod>,
    authid: Option<WampString>,
    on_challenge_handler: Option<AuthenticationChallengeHandler<'_>>,
    res: JoinResult,
) -> Status {
    let mut details: WampDict = WampDict::new();
    let mut client_roles: WampDict = WampDict::new();
    // Add all of our roles
    for role in &roles {
        client_roles.insert(String::from(role.to_str()), Arg::Dict(WampDict::new()));
    }
    details.insert("roles".to_owned(), Arg::Dict(client_roles));
    if let Some(agent) = agent_str {
        details.insert("agent".to_owned(), Arg::String(agent));
    }
    if !authentication_methods.is_empty() {
        details.insert(
            "authmethods".to_owned(),
            Arg::List(
                authentication_methods
                    .iter()
                    .map(|authentication_method| {
                        Arg::String(authentication_method.as_ref().to_owned())
                    })
                    .collect::<Vec<_>>(),
            ),
        );
    }
    if let Some(authid) = authid {
        details.insert("authid".to_owned(), Arg::String(authid));
    }
    // Send hello with our info
    if let Err(e) = core
        .send(&Msg::Hello {
            realm: uri,
            details,
        })
        .await
    {
        let _ = res.send(Err(e));
        return Status::Shutdown;
    }
    // Make sure the server responded with the proper message
    let (session_id, server_roles) = loop {
        // Receive the response to the HELLO message (either WELCOME or CHALLENGE are expected)
        let resp = match core.recv().await {
            Ok(r) => r,
            Err(e) => {
                let _ = res.send(Err(e));
                return Status::Shutdown;
            }
        };
        match resp {
            Msg::Welcome { session, details } => break (session, details),
            Msg::Challenge {
                authentication_method,
                extra,
            } => {
                if let Some(ref on_challenge_handler) = on_challenge_handler {
                    match on_challenge_handler(authentication_method, extra).await {
                        Ok(AuthenticationChallengeResponse { signature, extra }) => {
                            if let Err(e) = core.send(&Msg::Authenticate { signature, extra }).await
                            {
                                let _ = res.send(Err(e));
                                return Status::Shutdown;
                            }
                        }
                        Err(e) => {
                            let _ = res.send(Err(e));
                            return Status::Shutdown;
                        }
                    }
                } else {
                    let _ = res.send(Err(From::from(
                        "Server requested a CHALLENGE to authenticate, but there was no challenge handler provided".to_string()
                    )));
                    return Status::Shutdown;
                }
            }
            m => {
                let _ = res.send(Err(From::from(format!(
                    "Server did not respond with WELCOME : {:?}",
                    m
                ))));
                return Status::Shutdown;
            }
        }
    };
    // Return the pertinent info to the caller
    core.valid_session = true;
    let _ = res.send(Ok((session_id, server_roles)));
    Status::Ok
}
pub async fn join_realm ( core : & mut Core < ' _ > , uri : WampString , roles : HashSet < ClientRole > , agent_str : Option < WampString > , authentication_methods : Vec < AuthenticationMethod > , authid : Option < WampString > , on_challenge_handler : Option < AuthenticationChallengeHandler < ' _ > > , res : JoinResult , ) -> Status { let mut details : WampDict = WampDict :: new ( ) ; let mut client_roles : WampDict = WampDict :: new ( ) ; // Add all of our roles for role in & roles { client_roles . insert ( String :: from ( role . to_str ( ) ) , Arg :: Dict ( WampDict :: new ( ) ) ) ; } details . insert ( " " . to_owned ( ) , Arg :: Dict ( client_roles ) ) ; if let Some ( agent ) = agent_str { details . insert ( " " . to_owned ( ) , Arg :: String ( agent ) ) ; } if ! authentication_methods . is_empty ( ) { details . insert ( " " . to_owned ( ) , Arg :: List ( authentication_methods . iter ( ) . map ( | authentication_method | { Arg :: String ( authentication_method . as_ref ( ) . to_owned ( ) ) } ) . collect :: < Vec < _ > > ( ) , ) , ) ; } if let Some ( authid ) = authid { details . insert ( " " . to_owned ( ) , Arg :: String ( authid ) ) ; } // Send hello with our info if let Err ( e ) = core . send ( & Msg :: Hello { realm : uri , details , } ) . await { let _ = res . send ( Err ( e ) ) ; return Status :: Shutdown ; } // Make sure the server responded with the proper message let ( session_id , server_roles ) = loop { // Receive the response to the HELLO message (either WELCOME or CHALLENGE are expected) let resp = match core . recv ( ) . await { Ok ( r ) => r , Err ( e ) => { let _ = res . send ( Err ( e ) ) ; return Status :: Shutdown ; } } ; match resp { Msg :: Welcome { session , details } => break ( session , details ) , Msg :: Challenge { authentication_method , extra , } => { if let Some ( ref on_challenge_handler ) = on_challenge_handler { match on_challenge_handler ( authentication_method , extra ) . await { Ok ( AuthenticationChallengeResponse { signature , extra } ) => { if let Err ( e ) = core . send ( & Msg :: Authenticate { signature , extra } ) . await { let _ = res . send ( Err ( e ) ) ; return Status :: Shutdown ; } } Err ( e ) => { let _ = res . send ( Err ( e ) ) ; return Status :: Shutdown ; } } } else { let _ = res . send ( Err ( From :: from ( " " . to_string ( ) ) ) ) ; return Status :: Shutdown ; } } m => { let _ = res . send ( Err ( From :: from ( format ! ( " " m ) ) ) ) ; return Status :: Shutdown ; } } } ; // Return the pertinent info to the caller core . valid_session = true ; let _ = res . send ( Ok ( ( session_id , server_roles ) ) ) ; Status :: Ok }
Handler for any join realm request.
Handler for any join realm request .
core
&mut Core<'_>
uri
WampString
roles
HashSet<ClientRole>
agent_str
Option<WampString>
authentication_methods
Vec<AuthenticationMethod>
authid
Option<WampString>
on_challenge_handler
Option<AuthenticationChallengeHandler<'_>>
res
JoinResult