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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Builder for Auth0.
use std::error::Error;
use std::fmt::{Display, Formatter};

use reqwest::Client;

use crate::rate::RateLimit;
use crate::token::TokenManager;
use crate::{Auth0, Auth0Client};

/// Management client interface.
pub struct Auth0Builder {
  domain: Option<String>,
  audience: Option<String>,
  client_id: Option<String>,
  client_secret: Option<String>,
}

impl Auth0Builder {
  /// Get instance of management client builder.
  pub fn new() -> Self {
    Default::default()
  }

  /// Get instance of management client.
  ///
  /// Creates instance of management client and validates builder options.  Valid builder options
  /// requires all fields to be populated.
  pub fn build(self) -> Result<Auth0, Auth0BuilderError> {
    let client = Client::new();
    let domain = self.domain.ok_or(Auth0BuilderError::MissingDomain)?;
    let audience = self.audience.ok_or(Auth0BuilderError::MissingAudience)?;
    let client_id = self.client_id.ok_or(Auth0BuilderError::MissingClientID)?;
    let client_secret = self
      .client_secret
      .ok_or(Auth0BuilderError::MissingClientSecret)?;
    let client = Auth0Client::new(
      RateLimit::new(),
      TokenManager::new(
        client.clone(),
        &domain,
        &audience,
        &client_id,
        &client_secret,
      ),
      client,
      &domain,
    );

    Ok(Auth0::new(client))
  }

  /// The auth0 tenant domain.
  ///
  /// The domain can be found in the Auth0 dashboard under your application settings.  Domain
  /// will be the field labeled `Domain`.  Domains will be typically formatted as
  /// `example.[us|eu|au].auth0.com`.
  pub fn domain(mut self, domain: &str) -> Self {
    self.domain = Some(domain.to_owned());
    self
  }

  /// The value of the Identifier field of the `Auth0 Management API`.  
  ///
  /// The audience can be found in the Auth0 dashboard under you `API` settings.  Audience will be
  /// in the field labeled `Identifier`.  The default management API identifier will be formatted as
  /// `https://example.eu.auth0.com/api/v2`
  pub fn audience(mut self, audience: &str) -> Self {
    self.audience = Some(audience.to_owned());
    self
  }

  /// The value of the Client ID field of the Machine-to-Machine application.
  ///
  /// The client id can be found in the Auth0 dashboard under your application settings.  Client ID
  /// will be the field labeled `Client ID`.
  pub fn client_id(mut self, client_id: &str) -> Self {
    self.client_id = Some(client_id.to_owned());
    self
  }

  /// The value of the Client Secret field of the Machine-to-Machine application.
  ///
  /// The client secret can be found in the Auth0 dashboard under your application settings.  
  /// Client Secret will be the field labeled `Client Secret`.
  pub fn client_secret(mut self, client_secret: &str) -> Self {
    self.client_secret = Some(client_secret.to_owned());
    self
  }
}

impl Default for Auth0Builder {
  fn default() -> Self {
    Self {
      domain: None,
      audience: None,
      client_id: None,
      client_secret: None,
    }
  }
}

/// The error type which is returned from building a [Auth0].
#[derive(Debug, PartialOrd, PartialEq)]
pub enum Auth0BuilderError {
  /// Indicates builder didn't set [Auth0Builder::domain].
  MissingDomain,
  /// Indicates builder didn't set [Auth0Builder::audience].
  MissingAudience,
  /// Indicates builder didn't set [Auth0Builder::client_id].
  MissingClientID,
  /// Indicates builder didn't set [Auth0Builder::client_secret].
  MissingClientSecret,
}

impl Display for Auth0BuilderError {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{:?}", self)
  }
}

impl Error for Auth0BuilderError {}