gestalt-sdk 0.0.1-alpha.19

Rust SDK scaffolding and generated protocol bindings for Gestalt executable providers
Documentation
// Code generated by sdkgen. DO NOT EDIT.

//! Generated native types and clients for authentication.proto.

use crate::codec::authentication::{
    from_wire_auth_session_settings, from_wire_authenticated_user, from_wire_begin_login_response,
    to_wire_begin_login_request, to_wire_complete_login_request,
    to_wire_validate_external_token_request,
};
use crate::generated::v1;
use crate::rpc_support::GestaltError;

/// AuthSessionSettings configures how the host persists authenticated sessions.
///
/// Native message type for `gestalt.provider.v1.AuthSessionSettings`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct AuthSessionSettings {
    /// The `session_ttl_seconds` field.
    pub session_ttl_seconds: i64,
}

/// AuthenticatedUser is the normalized user identity returned by an authentication
/// provider after a login or token-validation flow.
///
/// Native message type for `gestalt.provider.v1.AuthenticatedUser`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct AuthenticatedUser {
    /// The `subject` field.
    pub subject: String,
    /// The `email` field.
    pub email: String,
    /// The `email_verified` field.
    pub email_verified: bool,
    /// The `display_name` field.
    pub display_name: String,
    /// The `avatar_url` field.
    pub avatar_url: String,
    /// The `claims` field.
    pub claims: std::collections::BTreeMap<String, String>,
}

/// BeginLoginRequest starts an interactive login flow.
///
/// Native message type for `gestalt.provider.v1.BeginLoginRequest`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BeginLoginRequest {
    /// callback_url is the host-managed URL the provider should redirect back to.
    ///
    /// The `callback_url` field.
    pub callback_url: String,
    /// host_state is opaque state generated by the host and echoed back on
    /// completion.
    ///
    /// The `host_state` field.
    pub host_state: String,
    /// scopes are the provider-specific scopes the host is requesting.
    ///
    /// The `scopes` field.
    pub scopes: Vec<String>,
    /// options carries provider-specific login parameters.
    ///
    /// The `options` field.
    pub options: std::collections::BTreeMap<String, String>,
}

/// BeginLoginResponse returns the provider-managed authorization URL and opaque
/// provider state that must be preserved until completion.
///
/// Native message type for `gestalt.provider.v1.BeginLoginResponse`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BeginLoginResponse {
    /// The `authorization_url` field.
    pub authorization_url: String,
    /// The `provider_state` field.
    pub provider_state: Vec<u8>,
}

/// CompleteLoginRequest finishes an interactive login flow.
///
/// Native message type for `gestalt.provider.v1.CompleteLoginRequest`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CompleteLoginRequest {
    /// query contains the callback URL query parameters returned by the identity
    /// provider.
    ///
    /// The `query` field.
    pub query: std::collections::BTreeMap<String, String>,
    /// provider_state is the opaque state returned from BeginLoginResponse.
    ///
    /// The `provider_state` field.
    pub provider_state: Vec<u8>,
    /// callback_url is the fully qualified callback URL used by the host.
    ///
    /// The `callback_url` field.
    pub callback_url: String,
}

/// ValidateExternalTokenRequest asks the provider to validate a token minted
/// outside the interactive login flow.
///
/// Native message type for `gestalt.provider.v1.ValidateExternalTokenRequest`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ValidateExternalTokenRequest {
    /// The `token` field.
    pub token: String,
}

/// Authentication models the shared Gestalt authentication protocol.
///
/// Client for the `gestalt.provider.v1.Authentication` service.
pub struct Authentication {
    inner: v1::authentication_client::AuthenticationClient<tonic::transport::Channel>,
    timeout: Option<std::time::Duration>,
}

impl Authentication {
    /// Creates a client over an established channel.
    pub fn new(channel: tonic::transport::Channel) -> Self {
        Self {
            inner: v1::authentication_client::AuthenticationClient::new(channel),
            timeout: None,
        }
    }

    /// Sets a deadline applied to every unary call; calls that run past it
    /// fail with DEADLINE_EXCEEDED. Streaming calls are unaffected.
    pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Calls `gestalt.provider.v1.Authentication.BeginLogin`.
    pub async fn begin_login(
        &mut self,
        callback_url: String,
        host_state: String,
        scopes: Vec<String>,
    ) -> Result<BeginLoginResponse, GestaltError> {
        let request = BeginLoginRequest {
            callback_url,
            host_state,
            scopes,
            ..Default::default()
        };
        let mut tonic_request = tonic::Request::new(to_wire_begin_login_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.begin_login(tonic_request).await?;
        Ok(from_wire_begin_login_response(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Authentication.BeginLogin` with the full request and response messages.
    pub async fn begin_login_raw(
        &mut self,
        request: BeginLoginRequest,
    ) -> Result<BeginLoginResponse, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_begin_login_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.begin_login(tonic_request).await?;
        Ok(from_wire_begin_login_response(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Authentication.CompleteLogin`.
    pub async fn complete_login(
        &mut self,
        provider_state: Vec<u8>,
        callback_url: String,
        query: std::collections::BTreeMap<String, String>,
    ) -> Result<AuthenticatedUser, GestaltError> {
        let request = CompleteLoginRequest {
            provider_state,
            callback_url,
            query,
        };
        let mut tonic_request = tonic::Request::new(to_wire_complete_login_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.complete_login(tonic_request).await?;
        Ok(from_wire_authenticated_user(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Authentication.CompleteLogin` with the full request and response messages.
    pub async fn complete_login_raw(
        &mut self,
        request: CompleteLoginRequest,
    ) -> Result<AuthenticatedUser, GestaltError> {
        let mut tonic_request = tonic::Request::new(to_wire_complete_login_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.complete_login(tonic_request).await?;
        Ok(from_wire_authenticated_user(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Authentication.ValidateExternalToken`.
    pub async fn validate_external_token(
        &mut self,
        token: String,
    ) -> Result<AuthenticatedUser, GestaltError> {
        let request = ValidateExternalTokenRequest { token };
        let mut tonic_request =
            tonic::Request::new(to_wire_validate_external_token_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.validate_external_token(tonic_request).await?;
        Ok(from_wire_authenticated_user(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Authentication.ValidateExternalToken` with the full request and response messages.
    pub async fn validate_external_token_raw(
        &mut self,
        request: ValidateExternalTokenRequest,
    ) -> Result<AuthenticatedUser, GestaltError> {
        let mut tonic_request =
            tonic::Request::new(to_wire_validate_external_token_request(request));
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.validate_external_token(tonic_request).await?;
        Ok(from_wire_authenticated_user(response.into_inner()))
    }

    /// Calls `gestalt.provider.v1.Authentication.GetSessionSettings`.
    pub async fn get_session_settings(&mut self) -> Result<AuthSessionSettings, GestaltError> {
        let mut tonic_request = tonic::Request::new(());
        if let Some(timeout) = self.timeout {
            tonic_request.set_timeout(timeout);
        }
        let response = self.inner.get_session_settings(tonic_request).await?;
        Ok(from_wire_auth_session_settings(response.into_inner()))
    }
}