supabase-rust 0.3.1

Rust client for Supabase
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use reqwest::Response;
use serde::{Deserialize, Serialize};

use crate::Supabase;

#[derive(Serialize)]
struct Credentials<'a> {
    email: &'a str,
    password: &'a str,
}

#[derive(Serialize)]
struct RefreshTokenRequest<'a> {
    refresh_token: &'a str,
}

/// JWT claims extracted from a valid token.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Claims {
    pub sub: String,
    pub email: String,
    pub exp: usize,
}

/// Response returned by authentication endpoints that issue tokens.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthResponse {
    /// The JWT access token.
    pub access_token: String,
    /// The token type (typically `"bearer"`).
    pub token_type: String,
    /// Seconds until the access token expires.
    pub expires_in: u64,
    /// Unix timestamp when the access token expires.
    #[serde(default)]
    pub expires_at: Option<u64>,
    /// Token used to obtain a new access token.
    pub refresh_token: String,
    /// User information, if returned by the endpoint.
    #[serde(default)]
    pub user: Option<serde_json::Value>,
}

/// Response for endpoints that return no body on success.
#[derive(Debug, Clone)]
pub struct EmptyResponse {
    /// HTTP status code.
    pub status: u16,
}

#[derive(Serialize)]
struct RecoverRequest<'a> {
    email: &'a str,
}

#[derive(Serialize)]
struct PhoneCredentials<'a> {
    phone: &'a str,
    password: &'a str,
}

#[derive(Serialize)]
struct OtpRequest<'a> {
    phone: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    channel: Option<&'a str>,
}

#[derive(Serialize)]
struct VerifyOtpRequest<'a> {
    phone: &'a str,
    token: &'a str,
    #[serde(rename = "type")]
    verification_type: &'a str,
}

#[derive(Serialize)]
struct ResendOtpRequest<'a> {
    phone: &'a str,
    #[serde(rename = "type")]
    verification_type: &'a str,
}

impl Supabase {
    /// Sends a POST request to the given auth endpoint path with standard headers.
    async fn auth_post(
        &self,
        path: &str,
        body: &impl Serialize,
    ) -> Result<Response, crate::Error> {
        let url = format!("{}/auth/v1/{path}", self.url);

        let resp = self
            .client
            .post(&url)
            .header("apikey", &self.api_key)
            .header("Content-Type", "application/json")
            .json(body)
            .send()
            .await?;

        Ok(resp)
    }

    /// Checks the response status and deserializes as `AuthResponse`.
    async fn parse_auth_response(response: Response) -> Result<AuthResponse, crate::Error> {
        let status = response.status().as_u16();
        if !(200..300).contains(&status) {
            let message = response.text().await.unwrap_or_default();
            return Err(crate::Error::Api { status, message });
        }
        let auth: AuthResponse = response.json().await?;
        Ok(auth)
    }

    /// Checks the response status and returns an `EmptyResponse`.
    async fn parse_empty_response(response: Response) -> Result<EmptyResponse, crate::Error> {
        let status = response.status().as_u16();
        if !(200..300).contains(&status) {
            let message = response.text().await.unwrap_or_default();
            return Err(crate::Error::Api { status, message });
        }
        Ok(EmptyResponse { status })
    }

    /// Validates a JWT token and returns its claims.
    ///
    /// Returns an error if the token is invalid or expired.
    pub fn jwt_valid(&self, jwt: &str) -> Result<Claims, crate::Error> {
        let decoding_key = DecodingKey::from_secret(self.jwt.as_bytes());
        let validation = Validation::new(Algorithm::HS256);
        let token_data = decode::<Claims>(jwt, &decoding_key, &validation)?;
        Ok(token_data.claims)
    }

    /// Signs in a user with email and password.
    ///
    /// Returns an [`AuthResponse`] containing access and refresh tokens.
    pub async fn sign_in_password(
        &self,
        email: &str,
        password: &str,
    ) -> Result<AuthResponse, crate::Error> {
        let resp = self
            .auth_post(
                "token?grant_type=password",
                &Credentials { email, password },
            )
            .await?;
        Self::parse_auth_response(resp).await
    }

    /// Refreshes an access token using a refresh token.
    ///
    /// Note: This may fail if "Enable automatic reuse detection" is enabled in Supabase.
    pub async fn refresh_token(
        &self,
        refresh_token: &str,
    ) -> Result<AuthResponse, crate::Error> {
        let resp = self
            .auth_post(
                "token?grant_type=refresh_token",
                &RefreshTokenRequest { refresh_token },
            )
            .await?;
        Self::parse_auth_response(resp).await
    }

    /// Logs out the current user.
    ///
    /// Requires a bearer token to be set on the client.
    pub async fn logout(&self) -> Result<EmptyResponse, crate::Error> {
        let token = self.bearer_token.as_ref().ok_or_else(|| {
            crate::Error::AuthRequired("bearer token required for logout".into())
        })?;
        let url = format!("{}/auth/v1/logout", self.url);

        let resp = self
            .client
            .post(&url)
            .header("apikey", &self.api_key)
            .header("Content-Type", "application/json")
            .bearer_auth(token)
            .send()
            .await?;

        Self::parse_empty_response(resp).await
    }

    /// Sends a password recovery email to the given address.
    pub async fn recover_password(
        &self,
        email: &str,
    ) -> Result<EmptyResponse, crate::Error> {
        let resp = self.auth_post("recover", &RecoverRequest { email }).await?;
        Self::parse_empty_response(resp).await
    }

    /// Signs up a new user with phone and password.
    pub async fn signup_phone_password(
        &self,
        phone: &str,
        password: &str,
    ) -> Result<AuthResponse, crate::Error> {
        let resp = self
            .auth_post("signup", &PhoneCredentials { phone, password })
            .await?;
        Self::parse_auth_response(resp).await
    }

    /// Sends a one-time password to the given phone number.
    ///
    /// The `channel` parameter can be `"sms"` or `"whatsapp"`. Defaults to SMS when `None`.
    pub async fn sign_in_otp(
        &self,
        phone: &str,
        channel: Option<&str>,
    ) -> Result<EmptyResponse, crate::Error> {
        let resp = self
            .auth_post("otp", &OtpRequest { phone, channel })
            .await?;
        Self::parse_empty_response(resp).await
    }

    /// Verifies a one-time password token.
    ///
    /// Returns an [`AuthResponse`] containing access and refresh tokens on success.
    pub async fn verify_otp(
        &self,
        phone: &str,
        token: &str,
        verification_type: &str,
    ) -> Result<AuthResponse, crate::Error> {
        let resp = self
            .auth_post(
                "verify",
                &VerifyOtpRequest {
                    phone,
                    token,
                    verification_type,
                },
            )
            .await?;
        Self::parse_auth_response(resp).await
    }

    /// Resends a one-time password to the given phone number.
    pub async fn resend_otp(
        &self,
        phone: &str,
        verification_type: &str,
    ) -> Result<EmptyResponse, crate::Error> {
        let resp = self
            .auth_post(
                "resend",
                &ResendOtpRequest {
                    phone,
                    verification_type,
                },
            )
            .await?;
        Self::parse_empty_response(resp).await
    }

    /// Signs up a new user with email and password.
    pub async fn signup_email_password(
        &self,
        email: &str,
        password: &str,
    ) -> Result<AuthResponse, crate::Error> {
        let resp = self
            .auth_post("signup", &Credentials { email, password })
            .await?;
        Self::parse_auth_response(resp).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn client() -> Supabase {
        Supabase::new(None, None, None).unwrap_or_else(|_| {
            Supabase::new(
                Some("https://example.supabase.co"),
                Some("test-key"),
                None,
            )
            .unwrap()
        })
    }

    async fn sign_in_password() -> Result<AuthResponse, crate::Error> {
        let client = client();
        let test_email = std::env::var("SUPABASE_TEST_EMAIL").unwrap_or_default();
        let test_pass = std::env::var("SUPABASE_TEST_PASS").unwrap_or_default();
        client.sign_in_password(&test_email, &test_pass).await
    }

    #[tokio::test]
    async fn test_token_with_password() {
        let auth = match sign_in_password().await {
            Ok(auth) => auth,
            Err(e) => {
                println!("Test skipped due to error: {e}");
                return;
            }
        };

        assert!(!auth.access_token.is_empty());
        assert!(!auth.refresh_token.is_empty());
    }

    #[tokio::test]
    async fn test_refresh() {
        let auth = match sign_in_password().await {
            Ok(auth) => auth,
            Err(e) => {
                println!("Test skipped due to error: {e}");
                return;
            }
        };

        let refreshed = match client().refresh_token(&auth.refresh_token).await {
            Ok(auth) => auth,
            Err(crate::Error::Api { status: 400, .. }) => {
                println!("Skipping: automatic reuse detection is enabled");
                return;
            }
            Err(e) => {
                println!("Test skipped due to error: {e}");
                return;
            }
        };

        assert!(!refreshed.access_token.is_empty());
    }

    #[tokio::test]
    async fn test_logout() {
        let auth = match sign_in_password().await {
            Ok(auth) => auth,
            Err(e) => {
                println!("Test skipped due to error: {e}");
                return;
            }
        };

        let mut client = client();
        client.set_bearer_token(&auth.access_token);

        let resp = match client.logout().await {
            Ok(resp) => resp,
            Err(e) => {
                println!("Test skipped: {e}");
                return;
            }
        };

        assert_eq!(resp.status, 204);
    }

    #[tokio::test]
    async fn test_signup_email_password() {
        use rand::distr::Alphanumeric;
        use rand::{rng, Rng};

        let client = client();

        let rand_string: String = rng()
            .sample_iter(&Alphanumeric)
            .take(20)
            .map(char::from)
            .collect();

        let email = format!("{rand_string}@a-rust-domain-that-does-not-exist.com");

        match client.signup_email_password(&email, &rand_string).await {
            Ok(auth) => {
                assert!(!auth.access_token.is_empty());
            }
            Err(e) => {
                println!("Test skipped due to error: {e}");
            }
        }
    }

    #[tokio::test]
    async fn test_authenticate_token() {
        let client = client();

        let auth = match sign_in_password().await {
            Ok(auth) => auth,
            Err(e) => {
                println!("Test skipped due to error: {e}");
                return;
            }
        };

        assert!(client.jwt_valid(&auth.access_token).is_ok());
    }

    #[test]
    fn test_logout_requires_bearer_token() {
        let err = crate::Error::AuthRequired("bearer token required for logout".into());
        assert!(format!("{err}").contains("bearer token required for logout"));
    }

    #[tokio::test]
    async fn test_recover_password() {
        let client = client();

        match client
            .recover_password("test@a-rust-domain-that-does-not-exist.com")
            .await
        {
            Ok(resp) => {
                assert!(resp.status >= 200);
            }
            Err(e) => {
                println!("Test skipped due to error: {e}");
            }
        }
    }

    #[tokio::test]
    async fn test_signup_phone_password() {
        let client = client();

        match client
            .signup_phone_password("+10000000000", "test-password-123")
            .await
        {
            Ok(_auth) => {}
            Err(crate::Error::Api { status, .. }) => {
                assert!(
                    status == 422 || status == 401 || status == 403,
                    "unexpected API error status: {status}"
                );
            }
            Err(e) => {
                println!("Test skipped due to error: {e}");
            }
        }
    }

    #[tokio::test]
    async fn test_sign_in_otp() {
        let client = client();

        match client.sign_in_otp("+10000000000", Some("sms")).await {
            Ok(_resp) => {}
            Err(crate::Error::Api { .. }) => {}
            Err(e) => {
                println!("Test skipped due to error: {e}");
            }
        }
    }

    #[tokio::test]
    async fn test_verify_otp() {
        let client = client();

        match client.verify_otp("+10000000000", "000000", "sms").await {
            Ok(_auth) => {}
            Err(crate::Error::Api { .. }) => {}
            Err(e) => {
                println!("Test skipped due to error: {e}");
            }
        }
    }

    #[tokio::test]
    async fn test_resend_otp() {
        let client = client();

        match client.resend_otp("+10000000000", "sms").await {
            Ok(_resp) => {}
            Err(crate::Error::Api { .. }) => {}
            Err(e) => {
                println!("Test skipped due to error: {e}");
            }
        }
    }
}