workos 1.0.0

Official Rust SDK for the WorkOS API
Documentation
// Code generated by oagen. DO NOT EDIT.

use crate::client::Client;
#[allow(unused_imports)]
use crate::enums::*;
use crate::error::Error;
#[allow(unused_imports)]
use crate::models::*;
use serde::Serialize;

pub struct MultiFactorAuthApi<'a> {
    pub(crate) client: &'a Client,
}

#[derive(Debug, Clone, Serialize)]
pub struct VerifyChallengeParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: AuthenticationChallengesVerifyRequest,
}

impl VerifyChallengeParams {
    /// Construct a new `VerifyChallengeParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: AuthenticationChallengesVerifyRequest) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct EnrollFactorParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: AuthenticationFactorsCreateRequest,
}

impl EnrollFactorParams {
    /// Construct a new `EnrollFactorParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: AuthenticationFactorsCreateRequest) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ChallengeFactorParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: ChallengeAuthenticationFactor,
}

impl ChallengeFactorParams {
    /// Construct a new `ChallengeFactorParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: ChallengeAuthenticationFactor) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListUserAuthFactorsParams {
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before: Option<String>,
    /// An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after: Option<String>,
    /// Upper limit on the number of objects to return, between `1` and `100`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    /// Order the results by the creation time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<PaginationOrder>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateUserAuthFactorParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: EnrollUserAuthenticationFactor,
}

impl CreateUserAuthFactorParams {
    /// Construct a new `CreateUserAuthFactorParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: EnrollUserAuthenticationFactor) -> Self {
        Self { body }
    }
}

impl<'a> MultiFactorAuthApi<'a> {
    /// Verify Challenge
    ///
    /// Verifies an Authentication Challenge.
    pub async fn verify_challenge(
        &self,
        id: &str,
        params: VerifyChallengeParams,
    ) -> Result<AuthenticationChallengeVerifyResponse, Error> {
        self.verify_challenge_with_options(id, params, None).await
    }

    /// Variant of [`Self::verify_challenge`] that accepts per-request [`crate::RequestOptions`].
    pub async fn verify_challenge_with_options(
        &self,
        id: &str,
        params: VerifyChallengeParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<AuthenticationChallengeVerifyResponse, Error> {
        let id = crate::client::path_segment(id);
        let path = format!("/auth/challenges/{id}/verify");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Enroll Factor
    ///
    /// Enrolls an Authentication Factor to be used as an additional factor of authentication. The returned ID should be used to create an authentication Challenge.
    pub async fn enroll_factor(
        &self,
        params: EnrollFactorParams,
    ) -> Result<AuthenticationFactorEnrolled, Error> {
        self.enroll_factor_with_options(params, None).await
    }

    /// Variant of [`Self::enroll_factor`] that accepts per-request [`crate::RequestOptions`].
    pub async fn enroll_factor_with_options(
        &self,
        params: EnrollFactorParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<AuthenticationFactorEnrolled, Error> {
        let path = "/auth/factors/enroll".to_string();
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Get Factor
    ///
    /// Gets an Authentication Factor.
    pub async fn get_factor(&self, id: &str) -> Result<AuthenticationFactor, Error> {
        self.get_factor_with_options(id, None).await
    }

    /// Variant of [`Self::get_factor`] that accepts per-request [`crate::RequestOptions`].
    pub async fn get_factor_with_options(
        &self,
        id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<AuthenticationFactor, Error> {
        let id = crate::client::path_segment(id);
        let path = format!("/auth/factors/{id}");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Delete Factor
    ///
    /// Permanently deletes an Authentication Factor. It cannot be undone.
    pub async fn delete_factor(&self, id: &str) -> Result<(), Error> {
        self.delete_factor_with_options(id, None).await
    }

    /// Variant of [`Self::delete_factor`] that accepts per-request [`crate::RequestOptions`].
    pub async fn delete_factor_with_options(
        &self,
        id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<(), Error> {
        let id = crate::client::path_segment(id);
        let path = format!("/auth/factors/{id}");
        let method = http::Method::DELETE;
        self.client
            .request_with_query_opts_empty(method, &path, &(), options)
            .await
    }

    /// Challenge Factor
    ///
    /// Creates a Challenge for an Authentication Factor.
    pub async fn challenge_factor(
        &self,
        id: &str,
        params: ChallengeFactorParams,
    ) -> Result<AuthenticationChallenge, Error> {
        self.challenge_factor_with_options(id, params, None).await
    }

    /// Variant of [`Self::challenge_factor`] that accepts per-request [`crate::RequestOptions`].
    pub async fn challenge_factor_with_options(
        &self,
        id: &str,
        params: ChallengeFactorParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<AuthenticationChallenge, Error> {
        let id = crate::client::path_segment(id);
        let path = format!("/auth/factors/{id}/challenge");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// List authentication factors
    ///
    /// Lists the [authentication factors](https://workos.com/docs/reference/authkit/mfa/authentication-factor) for a user.
    pub async fn list_user_auth_factors(
        &self,
        userland_user_id: &str,
        params: ListUserAuthFactorsParams,
    ) -> Result<UserAuthenticationFactorList, Error> {
        self.list_user_auth_factors_with_options(userland_user_id, params, None)
            .await
    }

    /// Variant of [`Self::list_user_auth_factors`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_user_auth_factors_with_options(
        &self,
        userland_user_id: &str,
        params: ListUserAuthFactorsParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<UserAuthenticationFactorList, Error> {
        let userland_user_id = crate::client::path_segment(userland_user_id);
        let path = format!("/user_management/users/{userland_user_id}/auth_factors");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Returns an async [`futures_util::Stream`] that yields every `AuthenticationFactor`
    /// across all pages, advancing the `after` cursor under the hood.
    ///
    /// ```ignore
    /// use futures_util::TryStreamExt;
    /// let all: Vec<AuthenticationFactor> = self
    ///     .list_user_auth_factors_auto_paging(userland_user_id, params)
    ///     .try_collect()
    ///     .await?;
    /// ```
    pub fn list_user_auth_factors_auto_paging(
        &self,
        userland_user_id: impl Into<String>,
        params: ListUserAuthFactorsParams,
    ) -> impl futures_util::Stream<Item = Result<AuthenticationFactor, Error>> + '_ {
        let userland_user_id: String = userland_user_id.into();
        crate::pagination::auto_paginate_pages(move |after| {
            let userland_user_id = userland_user_id.clone();
            let mut params = params.clone();
            params.after = after;
            async move {
                let page = self
                    .list_user_auth_factors(&userland_user_id, params)
                    .await?;
                Ok((page.data, page.list_metadata.after))
            }
        })
    }

    /// Enroll an authentication factor
    ///
    /// Enrolls a user in a new [authentication factor](https://workos.com/docs/reference/authkit/mfa/authentication-factor).
    pub async fn create_user_auth_factor(
        &self,
        userland_user_id: &str,
        params: CreateUserAuthFactorParams,
    ) -> Result<UserAuthenticationFactorEnrollResponse, Error> {
        self.create_user_auth_factor_with_options(userland_user_id, params, None)
            .await
    }

    /// Variant of [`Self::create_user_auth_factor`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_user_auth_factor_with_options(
        &self,
        userland_user_id: &str,
        params: CreateUserAuthFactorParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<UserAuthenticationFactorEnrollResponse, Error> {
        let userland_user_id = crate::client::path_segment(userland_user_id);
        let path = format!("/user_management/users/{userland_user_id}/auth_factors");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }
}