Skip to main content

rustauth_plugins/phone_number/
mod.rs

1//! Phone number authentication plugin.
2
3mod endpoints;
4mod errors;
5mod hooks;
6mod options;
7mod otp;
8mod schema;
9mod store;
10
11use std::sync::Arc;
12
13use rustauth_core::error::RustAuthError;
14use rustauth_core::options::RateLimitRule;
15use rustauth_core::plugin::{AuthPlugin, PluginRateLimitRule};
16
17pub use errors::PHONE_NUMBER_ERROR_CODES;
18pub use options::{
19    PhoneNumberCallback, PhoneNumberOptions, PhoneNumberOptionsBuilder, PhoneNumberSender,
20    PhoneNumberValidator, PhoneNumberVerifier, SignUpOnVerification,
21};
22pub use schema::PhoneNumberSchemaOptions;
23
24pub const UPSTREAM_PLUGIN_ID: &str = "phone-number";
25
26/// Build the phone number plugin.
27pub fn phone_number(options: PhoneNumberOptions) -> Result<AuthPlugin, RustAuthError> {
28    options.validate()?;
29    let options = Arc::new(options.with_defaults());
30    let schema = options.schema.clone();
31    let plugin = AuthPlugin::new(UPSTREAM_PLUGIN_ID)
32        .with_version(crate::VERSION)
33        .with_endpoint(endpoints::sign_in::endpoint(Arc::clone(&options)))
34        .with_endpoint(endpoints::send_otp::endpoint(Arc::clone(&options)))
35        .with_endpoint(endpoints::verify::endpoint(Arc::clone(&options)))
36        .with_endpoint(endpoints::password_reset::request_endpoint(Arc::clone(
37            &options,
38        )))
39        .with_endpoint(endpoints::password_reset::reset_endpoint(options))
40        .with_schema(schema::phone_number_field(&schema))
41        .with_schema(schema::phone_number_verified_field(&schema))
42        .with_rate_limit(PluginRateLimitRule::new(
43            "/phone-number/*",
44            RateLimitRule {
45                window: time::Duration::seconds(60),
46                max: 10,
47            },
48        ))
49        .with_error_code(errors::invalid_phone_number())
50        .with_error_code(errors::phone_number_exists())
51        .with_error_code(errors::phone_number_not_exists())
52        .with_error_code(errors::invalid_phone_number_or_password())
53        .with_error_code(errors::unexpected_error())
54        .with_error_code(errors::otp_not_found())
55        .with_error_code(errors::otp_expired())
56        .with_error_code(errors::invalid_otp())
57        .with_error_code(errors::phone_number_not_verified())
58        .with_error_code(errors::phone_number_cannot_be_updated())
59        .with_error_code(errors::send_otp_not_implemented())
60        .with_error_code(errors::too_many_attempts())
61        .with_before_hook("/update-user", hooks::block_unsafe_update_user)
62        .with_database_hook(hooks::reset_verified_when_clearing_phone());
63    Ok(plugin)
64}