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
use std::error::Error;
use std::fmt::{Display, Formatter};
use reqwest::Client;
use crate::rate::RateLimit;
use crate::token::TokenManager;
use crate::{Auth0, Auth0Client};
pub struct Auth0Builder {
domain: Option<String>,
audience: Option<String>,
client_id: Option<String>,
client_secret: Option<String>,
}
impl Auth0Builder {
pub fn new() -> Self {
Default::default()
}
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))
}
pub fn domain(mut self, domain: &str) -> Self {
self.domain = Some(domain.to_owned());
self
}
pub fn audience(mut self, audience: &str) -> Self {
self.audience = Some(audience.to_owned());
self
}
pub fn client_id(mut self, client_id: &str) -> Self {
self.client_id = Some(client_id.to_owned());
self
}
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,
}
}
}
#[derive(Debug, PartialOrd, PartialEq)]
pub enum Auth0BuilderError {
MissingDomain,
MissingAudience,
MissingClientID,
MissingClientSecret,
}
impl Display for Auth0BuilderError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl Error for Auth0BuilderError {}