openauth-core 0.0.2

Core types and primitives for OpenAuth.
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
use std::sync::Arc;

use http::{Method, StatusCode};
use serde::{Deserialize, Serialize};
use time::{Duration, OffsetDateTime};

use super::shared::{
    current_session, error_response, json_response, status_openapi_response, unauthorized,
};
use crate::api::{
    create_auth_endpoint, parse_request_body, AsyncAuthEndpoint, AuthEndpointOptions, BodyField,
    BodySchema, JsonSchemaType, OpenApiOperation,
};
use crate::crypto::random::generate_random_string;
use crate::db::{DbAdapter, User};
use crate::session::{CreateSessionInput, DbSessionStore};
use crate::user::{CreateCredentialAccountInput, DbUserStore};
use crate::verification::{CreateVerificationInput, DbVerificationStore};

const PASSWORD_RESET_MESSAGE: &str =
    "If this email exists in our system, check your email for the reset link";

#[derive(Debug, Deserialize)]
struct ChangePasswordBody {
    #[serde(alias = "currentPassword")]
    current_password: String,
    #[serde(alias = "newPassword")]
    new_password: String,
    #[serde(default, alias = "revokeOtherSessions")]
    revoke_other_sessions: Option<bool>,
}

#[derive(Debug, Deserialize)]
struct SetPasswordBody {
    #[serde(alias = "newPassword")]
    new_password: String,
}

#[derive(Debug, Deserialize)]
struct VerifyPasswordBody {
    password: String,
}

#[derive(Debug, Deserialize)]
struct RequestPasswordResetBody {
    email: String,
    #[serde(default, alias = "redirectTo")]
    redirect_to: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ResetPasswordBody {
    #[serde(alias = "newPassword")]
    new_password: String,
    #[serde(default)]
    token: Option<String>,
}

#[derive(Debug, Serialize)]
struct StatusBody {
    status: bool,
}

#[derive(Debug, Serialize)]
struct RequestPasswordResetResponse {
    status: bool,
    message: &'static str,
}

#[derive(Debug, Serialize)]
struct TokenUserResponse {
    token: Option<String>,
    user: User,
}

pub(super) fn change_password_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/change-password",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("changePassword")
            .body_schema(change_password_body_schema())
            .openapi(
                OpenApiOperation::new("changePassword")
                    .description("Change the password of the user")
                    .response(
                        "200",
                        super::shared::json_openapi_response(
                            "Password successfully changed",
                            serde_json::json!({
                                "type": "object",
                                "properties": {
                                    "token": {
                                        "type": "string",
                                        "nullable": true,
                                        "description": "New session token if other sessions were revoked",
                                    },
                                    "user": {
                                        "$ref": "#/components/schemas/User",
                                    },
                                },
                                "required": ["user"],
                            }),
                        ),
                    ),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let Some((_session, user, mut cookies)) =
                    current_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let body: ChangePasswordBody = parse_request_body(&request)?;
                if let Some(response) = validate_password_length(context, &body.new_password)? {
                    return Ok(response);
                }

                let users = DbUserStore::new(adapter.as_ref());
                let Some(account) = users.find_credential_account(&user.id).await? else {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "CREDENTIAL_ACCOUNT_NOT_FOUND",
                        "Credential account not found",
                    );
                };
                let Some(password_hash) = account.password.as_deref() else {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "CREDENTIAL_ACCOUNT_NOT_FOUND",
                        "Credential account not found",
                    );
                };
                if !(context.password.verify)(password_hash, &body.current_password)? {
                    return invalid_password();
                }

                let new_hash = (context.password.hash)(&body.new_password)?;
                users.update_credential_password(&user.id, &new_hash).await?;

                let mut token = None;
                if body.revoke_other_sessions.unwrap_or(false) {
                    let sessions = DbSessionStore::new(adapter.as_ref());
                    sessions.delete_user_sessions(&user.id).await?;
                    let new_session = sessions
                        .create_session(CreateSessionInput::new(
                            &user.id,
                            OffsetDateTime::now_utc()
                                + Duration::seconds(context.session_config.expires_in as i64),
                        ))
                        .await?;
                    cookies = super::shared::auth_session_cookies(
                        context,
                        &new_session,
                        &user,
                        false,
                    )?;
                    token = Some(new_session.token);
                }

                json_response(StatusCode::OK, &TokenUserResponse { token, user }, cookies)
            })
        },
    )
}

pub(super) fn set_password_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/set-password",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("setPassword")
            .body_schema(set_password_body_schema())
            .openapi(
                OpenApiOperation::new("setPassword")
                    .description("Set a password for the current user")
                    .response("200", status_openapi_response("Success")),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let Some((_, user, cookies)) =
                    current_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let body: SetPasswordBody = parse_request_body(&request)?;
                if let Some(response) = validate_password_length(context, &body.new_password)? {
                    return Ok(response);
                }
                let users = DbUserStore::new(adapter.as_ref());
                if users.find_credential_account(&user.id).await?.is_some() {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "PASSWORD_ALREADY_SET",
                        "Password already set",
                    );
                }
                let hash = (context.password.hash)(&body.new_password)?;
                users
                    .create_credential_account(CreateCredentialAccountInput::new(&user.id, hash))
                    .await?;
                json_response(StatusCode::OK, &StatusBody { status: true }, cookies)
            })
        },
    )
}

pub(super) fn verify_password_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/verify-password",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("verifyPassword")
            .body_schema(verify_password_body_schema())
            .openapi(
                OpenApiOperation::new("verifyPassword")
                    .description("Verify the current user's password")
                    .response("200", status_openapi_response("Success")),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let Some((_, user, cookies)) =
                    current_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let body: VerifyPasswordBody = parse_request_body(&request)?;
                let Some(account) = DbUserStore::new(adapter.as_ref())
                    .find_credential_account(&user.id)
                    .await?
                else {
                    return invalid_password();
                };
                let Some(password_hash) = account.password.as_deref() else {
                    return invalid_password();
                };
                if !(context.password.verify)(password_hash, &body.password)? {
                    return invalid_password();
                }
                json_response(StatusCode::OK, &StatusBody { status: true }, cookies)
            })
        },
    )
}

pub(super) fn request_password_reset_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/request-password-reset",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("requestPasswordReset")
            .body_schema(request_password_reset_body_schema())
            .openapi(
                OpenApiOperation::new("requestPasswordReset")
                    .description("Send a password reset email to the user")
                    .response(
                        "200",
                        super::shared::json_openapi_response(
                            "Success",
                            serde_json::json!({
                                "type": "object",
                                "properties": {
                                    "status": { "type": "boolean" },
                                    "message": { "type": "string" },
                                },
                            }),
                        ),
                    ),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let body: RequestPasswordResetBody = parse_request_body(&request)?;
                let _redirect_to = body.redirect_to;
                if let Some(user) = DbUserStore::new(adapter.as_ref())
                    .find_user_by_email(&body.email)
                    .await?
                {
                    let token = generate_random_string(24);
                    DbVerificationStore::new(adapter.as_ref())
                        .create_verification(CreateVerificationInput::new(
                            format!("reset-password:{token}"),
                            user.id,
                            OffsetDateTime::now_utc() + Duration::hours(1),
                        ))
                        .await?;
                }
                let _ = context;
                password_reset_response()
            })
        },
    )
}

pub(super) fn reset_password_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/reset-password",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("resetPassword")
            .body_schema(reset_password_body_schema())
            .openapi(
                OpenApiOperation::new("resetPassword")
                    .description("Reset the password for a user")
                    .response("200", status_openapi_response("Success")),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let query_token = query_param(request.uri().query(), "token");
                let body: ResetPasswordBody = parse_request_body(&request)?;
                let Some(token) = body.token.or(query_token) else {
                    return invalid_token();
                };
                if let Some(response) = validate_password_length(context, &body.new_password)? {
                    return Ok(response);
                }

                let identifier = format!("reset-password:{token}");
                let verifications = DbVerificationStore::new(adapter.as_ref());
                let Some(verification) = verifications.find_verification(&identifier).await? else {
                    return invalid_token();
                };
                if verification.expires_at <= OffsetDateTime::now_utc() {
                    return invalid_token();
                }
                let user_id = verification.value;
                let users = DbUserStore::new(adapter.as_ref());
                let new_hash = (context.password.hash)(&body.new_password)?;
                if users
                    .update_credential_password(&user_id, &new_hash)
                    .await?
                    .is_none()
                {
                    users
                        .create_credential_account(CreateCredentialAccountInput::new(
                            &user_id, new_hash,
                        ))
                        .await?;
                }
                verifications.delete_verification(&identifier).await?;
                json_response(StatusCode::OK, &StatusBody { status: true }, Vec::new())
            })
        },
    )
}

fn change_password_body_schema() -> BodySchema {
    BodySchema::object([
        BodyField::new("newPassword", JsonSchemaType::String)
            .description("The new password to set"),
        BodyField::new("currentPassword", JsonSchemaType::String)
            .description("The current password is required"),
        BodyField::optional("revokeOtherSessions", JsonSchemaType::Boolean)
            .description("Must be a boolean value"),
    ])
}

fn set_password_body_schema() -> BodySchema {
    BodySchema::object([BodyField::new("newPassword", JsonSchemaType::String)
        .description("The new password to set is required")])
}

fn verify_password_body_schema() -> BodySchema {
    BodySchema::object([
        BodyField::new("password", JsonSchemaType::String).description("The password to verify")
    ])
}

fn request_password_reset_body_schema() -> BodySchema {
    BodySchema::object([
        BodyField::new("email", JsonSchemaType::String)
            .format("email")
            .description("The email address of the user to send a password reset email to"),
        BodyField::optional("redirectTo", JsonSchemaType::String)
            .description("The URL to redirect the user to reset their password"),
    ])
}

fn reset_password_body_schema() -> BodySchema {
    BodySchema::object([
        BodyField::new("newPassword", JsonSchemaType::String)
            .description("The new password to set"),
        BodyField::optional("token", JsonSchemaType::String)
            .description("The token to reset the password"),
    ])
}

fn validate_password_length(
    context: &crate::context::AuthContext,
    password: &str,
) -> Result<Option<crate::api::ApiResponse>, crate::error::OpenAuthError> {
    if password.len() < context.password.config.min_password_length {
        return error_response(
            StatusCode::BAD_REQUEST,
            "PASSWORD_TOO_SHORT",
            "Password is too short",
        )
        .map(Some);
    }
    if password.len() > context.password.config.max_password_length {
        return error_response(
            StatusCode::BAD_REQUEST,
            "PASSWORD_TOO_LONG",
            "Password is too long",
        )
        .map(Some);
    }
    Ok(None)
}

fn invalid_password() -> Result<crate::api::ApiResponse, crate::error::OpenAuthError> {
    error_response(
        StatusCode::BAD_REQUEST,
        "INVALID_PASSWORD",
        "Invalid password",
    )
}

fn invalid_token() -> Result<crate::api::ApiResponse, crate::error::OpenAuthError> {
    error_response(StatusCode::BAD_REQUEST, "INVALID_TOKEN", "Invalid token")
}

fn password_reset_response() -> Result<crate::api::ApiResponse, crate::error::OpenAuthError> {
    json_response(
        StatusCode::OK,
        &RequestPasswordResetResponse {
            status: true,
            message: PASSWORD_RESET_MESSAGE,
        },
        Vec::new(),
    )
}

fn query_param(query: Option<&str>, key: &str) -> Option<String> {
    query?.split('&').find_map(|pair| {
        let (name, value) = pair.split_once('=')?;
        (name == key).then(|| value.replace('+', " "))
    })
}