• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divviup / divviup-api / 11293888846

11 Oct 2024 02:10PM UTC coverage: 55.932%. Remained the same
11293888846

push

github

web-flow
Bump primereact from 10.8.3 to 10.8.4 in /app (#1339)

Bumps [primereact](https://github.com/primefaces/primereact) from 10.8.3 to 10.8.4.
- [Release notes](https://github.com/primefaces/primereact/releases)
- [Changelog](https://github.com/primefaces/primereact/blob/master/CHANGELOG.md)
- [Commits](https://github.com/primefaces/primereact/commits)

---
updated-dependencies:
- dependency-name: primereact
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

3932 of 7030 relevant lines covered (55.93%)

103.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.51
/src/clients/auth0_client.rs
1
use async_lock::RwLock;
2
use rand::distributions::{Alphanumeric, DistString};
3
use serde::{de::DeserializeOwned, Serialize};
4
use serde_json::{json, Value};
5
use std::{
6
    sync::Arc,
7
    time::{Duration, SystemTime},
8
};
9

10
use trillium::{
11
    Conn,
12
    KnownHeaderName::{Accept, Authorization},
13
};
14
use trillium_api::FromConn;
15
use trillium_client::Client;
16
use url::Url;
17

18
use crate::{
19
    clients::{ClientConnExt, ClientError, PostmarkClient},
20
    Config,
21
};
22

23
#[derive(Debug, Clone)]
24
pub struct Auth0Client {
25
    token: Arc<RwLock<Option<TokenWithExpiry>>>,
26
    client: Client,
27
    secret: String,
28
    client_id: String,
29
    postmark_client: PostmarkClient,
30
}
31

32
#[trillium::async_trait]
33
impl FromConn for Auth0Client {
34
    async fn from_conn(conn: &mut Conn) -> Option<Self> {
×
35
        conn.state().cloned()
×
36
    }
×
37
}
38

39
fn generate_password() -> String {
3✔
40
    Alphanumeric.sample_string(&mut rand::thread_rng(), 60)
3✔
41
}
3✔
42

43
impl Auth0Client {
44
    pub fn new(config: &Config) -> Self {
288✔
45
        let client = config
288✔
46
            .client
288✔
47
            .clone()
288✔
48
            .with_base(config.auth_url.clone())
288✔
49
            .with_default_header(Accept, "application/json");
288✔
50

288✔
51
        Self {
288✔
52
            token: Arc::new(RwLock::new(None)),
288✔
53
            client,
288✔
54
            secret: config.auth_client_secret.clone(),
288✔
55
            client_id: config.auth_client_id.clone(),
288✔
56
            postmark_client: PostmarkClient::new(config),
288✔
57
        }
288✔
58
    }
288✔
59

60
    pub async fn invite(
×
61
        &self,
×
62
        email: &str,
×
63
        account_name: &str,
×
64
    ) -> Result<(String, Url), ClientError> {
×
65
        let user_id = self.create_user(email).await?;
×
66
        let reset = self.password_reset(&user_id).await?;
×
67
        self.postmark_client
×
68
            .send_email_template(
×
69
                email,
×
70
                "user-invitation",
×
71
                &json!({
×
72
                    "email": email,
×
73
                    "account_name": account_name,
×
74
                    "action_url": reset
×
75
                }),
×
76
                None,
×
77
            )
×
78
            .await?;
×
79

80
        Ok((user_id.to_string(), reset))
×
81
    }
×
82

83
    pub async fn password_reset(&self, user_id: &str) -> Result<Url, ClientError> {
2✔
84
        self.post::<Value>(
2✔
85
            "/api/v2/tickets/password-change",
2✔
86
            &json!({ "user_id": user_id, "client_id": &self.client_id }),
2✔
87
        )
2✔
88
        .await?
×
89
        .get("ticket")
2✔
90
        .and_then(Value::as_str)
2✔
91
        .and_then(|u| Url::parse(u).ok())
2✔
92
        .ok_or(ClientError::Other("password reset".to_string()))
2✔
93
    }
2✔
94

95
    pub async fn create_user(&self, email: &str) -> Result<String, ClientError> {
3✔
96
        let user: serde_json::Value = self
3✔
97
            .post(
3✔
98
                "/api/v2/users",
3✔
99
                &json!({
3✔
100
                    "connection": "Username-Password-Authentication",
3✔
101
                    "email": email,
3✔
102
                    "password": generate_password(),
3✔
103
                    "verify_email": false
3✔
104
                }),
3✔
105
            )
3✔
106
            .await?;
×
107

108
        user.get("user_id")
3✔
109
            .ok_or_else(|| ClientError::Other("expected user_id".into()))?
3✔
110
            .as_str()
3✔
111
            .ok_or_else(|| ClientError::Other("expected user_id to be a string".into()))
3✔
112
            .map(String::from)
3✔
113
    }
3✔
114

115
    pub async fn users(&self) -> Result<Vec<Value>, ClientError> {
×
116
        self.get("/api/v2/users").await
×
117
    }
×
118

119
    // private below here
120

121
    async fn get_new_token(&self) -> Result<String, ClientError> {
4✔
122
        // we have to check again here because someone may have taken
123
        // a write lock and populated the token since we relinquished
124
        // our read lock
125
        let mut guard = self.token.write().await;
4✔
126
        if let Some(token) = &*guard {
4✔
127
            if token.is_fresh() {
×
128
                return Ok(token.token().to_string());
×
129
            }
×
130
        }
4✔
131

132
        guard.take();
4✔
133

134
        let token = self
4✔
135
            .client
4✔
136
            .post("/oauth/token")
4✔
137
            .with_json_body(&json!({
4✔
138
                "grant_type": "client_credentials",
4✔
139
                "client_id": self.client_id,
4✔
140
                "client_secret": self.secret,
4✔
141
                "audience": self.client.build_url("/api/v2/").unwrap(),
4✔
142
            }))?
4✔
143
            .success_or_client_error()
4✔
144
            .await?
×
145
            .response_json::<Token>()
4✔
146
            .await?;
×
147

148
        *guard = Some(token.clone().into());
4✔
149
        Ok(token.access_token)
4✔
150
    }
4✔
151

152
    async fn token(&self) -> Result<String, ClientError> {
5✔
153
        if let Some(token) = &*self.token.read().await {
5✔
154
            if token.is_fresh() {
1✔
155
                return Ok(token.token.clone());
1✔
156
            }
×
157
        }
4✔
158

159
        self.get_new_token().await
4✔
160
    }
5✔
161

162
    async fn post<T>(&self, path: &str, json: &impl Serialize) -> Result<T, ClientError>
5✔
163
    where
5✔
164
        T: DeserializeOwned,
5✔
165
    {
5✔
166
        let token = self.token().await?;
5✔
167
        self.client
5✔
168
            .post(path)
5✔
169
            .with_request_header(Authorization, format!("Bearer {token}"))
5✔
170
            .with_json_body(json)?
5✔
171
            .success_or_client_error()
5✔
172
            .await?
×
173
            .response_json()
5✔
174
            .await
×
175
            .map_err(ClientError::from)
5✔
176
    }
5✔
177

178
    async fn get<T>(&self, path: &str) -> Result<T, ClientError>
×
179
    where
×
180
        T: DeserializeOwned,
×
181
    {
×
182
        let token = self.token().await?;
×
183
        self.client
×
184
            .get(path)
×
185
            .with_request_header(Authorization, format!("Bearer {token}"))
×
186
            .success_or_client_error()
×
187
            .await?
×
188
            .response_json()
×
189
            .await
×
190
            .map_err(ClientError::from)
×
191
    }
×
192
}
193

194
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
20✔
195
pub struct Token {
196
    pub access_token: String,
197
    pub expires_in: u64,
198
    pub scope: String,
199
    pub token_type: String,
200
}
201

202
#[derive(Debug, Clone)]
203
struct TokenWithExpiry {
204
    token: String,
205
    expires_at: SystemTime,
206
}
207

208
impl TokenWithExpiry {
209
    pub fn is_fresh(&self) -> bool {
1✔
210
        SystemTime::now() < self.expires_at
1✔
211
    }
1✔
212

213
    fn token(&self) -> &str {
×
214
        self.token.as_ref()
×
215
    }
×
216
}
217

218
impl From<Token> for TokenWithExpiry {
219
    fn from(token: Token) -> Self {
4✔
220
        Self {
4✔
221
            token: token.access_token,
4✔
222
            expires_at: SystemTime::now() + Duration::from_secs(token.expires_in),
4✔
223
        }
4✔
224
    }
4✔
225
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc