pub async fn redeem(
        form: RedeemContents,
        session: Session,
    ) -> Result<Response<Body>, ServerError> {
        let conn = session.context.db.lock();
        let mut selected_peer = DatabasePeer::get(&conn, session.peer.id)?;

        let old_public_key = wireguard_control::Key::from_base64(&selected_peer.public_key)
            .map_err(|_| ServerError::WireGuard)?;

        selected_peer.redeem(&conn, &form.public_key)?;

        if cfg!(not(test)) {
            let Context {
                interface, backend, ..
            } = session.context;

            // If we were to modify the WireGuard interface immediately, the HTTP response wouldn't
            // get through. Instead, we need to wait a reasonable amount for the HTTP response to
            // flush, then update the interface.
            //
            // The client is also expected to wait the same amount of time after receiving a success
            // response from /redeem.
            //
            // This might be avoidable if we were able to run code after we were certain the response
            // had flushed over the TCP socket, but that isn't easily accessible from this high-level
            // web framework.
            //
            // Related: https://github.com/hyperium/hyper/issues/2181
            tokio::task::spawn(async move {
                tokio::time::sleep(REDEEM_TRANSITION_WAIT).await;
                log::info!(
                    "WireGuard: adding new peer {}, removing old pubkey {}",
                    &*selected_peer,
                    old_public_key.to_base64()
                );
                DeviceUpdate::new()
                    .remove_peer_by_key(&old_public_key)
                    .add_peer((&*selected_peer).into())
                    .apply(&interface, backend)
                    .map_err(|e| log::error!("{:?}", e))
                    .ok();
            });
        }
        status_response(StatusCode::NO_CONTENT)
    }
/// Redeems an invitation. An invitation includes a WireGuard keypair generated by either the server
/// or a peer with admin rights.
///
/// Redemption is the process of an invitee generating their own keypair and exchanging their temporary
/// key with their permanent one.
///
/// Until this API endpoint is called, the invited peer will not show up to other peers, and once
/// it is called and succeeds, it cannot be called again.
Redeems an invitation. An invitation includes a WireGuard keypair generated by either the server
or a peer with admin rights.

Redemption is the process of an invitee generating their own keypair and exchanging their temporary
key with their permanent one.

Until this API endpoint is called, the invited peer will not show up to other peers, and once
it is called and succeeds, it cannot be called again.
Redeems an invitation . An invitation includes a WireGuard keypair generated by either the server or a peer with admin rights . Redemption is the process of an invitee generating their own keypair and exchanging their temporary key with their permanent one . Until this API endpoint is called the invited peer will not show up to other peers and once it is called and succeeds it cannot be called again .
pub async fn redeem(
        form: RedeemContents,
        session: Session,
    ) -> Result<Response<Body>, ServerError> {
        let conn = session.context.db.lock();
        let mut selected_peer = DatabasePeer::get(&conn, session.peer.id)?;
        let old_public_key = wireguard_control::Key::from_base64(&selected_peer.public_key)
            .map_err(|_| ServerError::WireGuard)?;
        selected_peer.redeem(&conn, &form.public_key)?;
        if cfg!(not(test)) {
            let Context {
                interface, backend, ..
            } = session.context;
            // If we were to modify the WireGuard interface immediately, the HTTP response wouldn't
            // get through. Instead, we need to wait a reasonable amount for the HTTP response to
            // flush, then update the interface.
            //
            // The client is also expected to wait the same amount of time after receiving a success
            // response from /redeem.
            //
            // This might be avoidable if we were able to run code after we were certain the response
            // had flushed over the TCP socket, but that isn't easily accessible from this high-level
            // web framework.
            //
            // Related: https://github.com/hyperium/hyper/issues/2181
            tokio::task::spawn(async move {
                tokio::time::sleep(REDEEM_TRANSITION_WAIT).await;
                log::info!(
                    "WireGuard: adding new peer {}, removing old pubkey {}",
                    &*selected_peer,
                    old_public_key.to_base64()
                );
                DeviceUpdate::new()
                    .remove_peer_by_key(&old_public_key)
                    .add_peer((&*selected_peer).into())
                    .apply(&interface, backend)
                    .map_err(|e| log::error!("{:?}", e))
                    .ok();
            });
        }
        status_response(StatusCode::NO_CONTENT)
    }
pub async fn redeem ( form : RedeemContents , session : Session , ) -> Result < Response < Body > , ServerError > { let conn = session . context . db . lock ( ) ; let mut selected_peer = DatabasePeer :: get ( & conn , session . peer . id ) ? ; let old_public_key = wireguard_control :: Key :: from_base64 ( & selected_peer . public_key ) . map_err ( | _ | ServerError :: WireGuard ) ? ; selected_peer . redeem ( & conn , & form . public_key ) ? ; if cfg ! ( not ( test ) ) { let Context { interface , backend , .. } = session . context ; // If we were to modify the WireGuard interface immediately, the HTTP response wouldn't // get through. Instead, we need to wait a reasonable amount for the HTTP response to // flush, then update the interface. // // The client is also expected to wait the same amount of time after receiving a success // response from /redeem. // // This might be avoidable if we were able to run code after we were certain the response // had flushed over the TCP socket, but that isn't easily accessible from this high-level // web framework. // // Related: https://github.com/hyperium/hyper/issues/2181 tokio :: task :: spawn ( async move { tokio :: time :: sleep ( REDEEM_TRANSITION_WAIT ) . await ; log :: info ! ( " " selected_peer old_public_key to_base64 ( ) ) ; DeviceUpdate :: new ( ) . remove_peer_by_key ( & old_public_key ) . add_peer ( ( & * selected_peer ) . into ( ) ) . apply ( & interface , backend ) . map_err ( | e | log :: error ! ( " " e ) ) . ok ( ) ; } ) ; } status_response ( StatusCode :: NO_CONTENT ) }
Redeems an invitation.
Redeems an invitation .
form
RedeemContents
session
Session