openauth-core 0.0.6

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
444
445
446
mod support;

use std::sync::Arc;

use http::{Method, StatusCode};
#[cfg(feature = "oauth")]
use serde_json::json;

use super::shared::{
    error_response, json_response, sensitive_session, status_openapi_response, unauthorized,
};
use crate::api::{
    create_auth_endpoint, parse_request_body, AsyncAuthEndpoint, AuthEndpointOptions,
    OpenApiOperation,
};
#[cfg(feature = "oauth")]
use crate::auth::oauth::decrypt_oauth_token;
use crate::db::DbAdapter;
use crate::user::DbUserStore;

#[cfg(feature = "oauth")]
use support::{
    access_token_response_from_tokens, account_not_found, account_scopes, find_user_account,
    is_refresh_unsupported, persist_refreshed_tokens, provider_not_supported, should_refresh,
    token_body_schema, token_openapi_response, tokens_from_account, AccessTokenResponse,
    AccountInfoResponse, RefreshTokenResponse, TokenBody,
};
use support::{
    account_openapi_schema, unlink_account_body_schema, AccountResponse, StatusBody,
    UnlinkAccountBody,
};

pub(super) fn list_user_accounts_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/list-accounts",
        Method::GET,
        AuthEndpointOptions::new()
            .operation_id("listUserAccounts")
            .openapi(
                OpenApiOperation::new("listUserAccounts")
                    .description("List all accounts linked to the user")
                    .response(
                        "200",
                        super::shared::json_openapi_response(
                            "Success",
                            serde_json::json!({
                                "type": "array",
                                "items": account_openapi_schema(),
                            }),
                        ),
                    ),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let Some((_, user, cookies)) =
                    sensitive_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let accounts = DbUserStore::new(adapter.as_ref())
                    .list_accounts_for_user(&user.id)
                    .await?
                    .into_iter()
                    .map(AccountResponse::from)
                    .collect::<Vec<_>>();

                json_response(StatusCode::OK, &accounts, cookies)
            })
        },
    )
}

#[cfg(feature = "oauth")]
pub(super) fn get_access_token_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/get-access-token",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("getAccessToken")
            .body_schema(token_body_schema())
            .openapi(
                OpenApiOperation::new("getAccessToken")
                    .description("Get a valid access token, doing a refresh if needed")
                    .response("200", token_openapi_response(false)),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let body: TokenBody = parse_request_body(&request)?;
                let Some((_, session_user, cookies)) =
                    sensitive_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let requested_user_id = body.user_id.as_deref().unwrap_or(&session_user.id);
                if requested_user_id != session_user.id {
                    return unauthorized();
                }
                let users = DbUserStore::new(adapter.as_ref());
                let Some(mut account) = find_user_account(
                    &users,
                    &session_user.id,
                    &body.provider_id,
                    body.account_id.as_deref(),
                )
                .await?
                else {
                    return account_not_found();
                };
                let Some(provider) = context.social_provider(&body.provider_id) else {
                    return provider_not_supported(&body.provider_id);
                };
                if should_refresh(&account) {
                    if let Some(refresh_token) = account.refresh_token.clone() {
                        let decrypted = decrypt_oauth_token(&refresh_token, context)?;
                        match provider.refresh_access_token(decrypted).await {
                            Ok(tokens) => {
                                if tokens.access_token.is_none() {
                                    return error_response(
                                        StatusCode::BAD_REQUEST,
                                        "FAILED_TO_GET_ACCESS_TOKEN",
                                        "Failed to get a valid access token",
                                    );
                                }
                                account = persist_refreshed_tokens(
                                    context,
                                    &users,
                                    account,
                                    tokens.clone(),
                                    None,
                                )
                                .await?;
                                return json_response(
                                    StatusCode::OK,
                                    &access_token_response_from_tokens(tokens, &account),
                                    cookies,
                                );
                            }
                            Err(error) if is_refresh_unsupported(&error) => {}
                            Err(_) => {
                                return error_response(
                                    StatusCode::BAD_REQUEST,
                                    "FAILED_TO_GET_ACCESS_TOKEN",
                                    "Failed to get a valid access token",
                                );
                            }
                        }
                    }
                }
                json_response(
                    StatusCode::OK,
                    &AccessTokenResponse {
                        access_token: account
                            .access_token
                            .as_deref()
                            .map(|token| decrypt_oauth_token(token, context))
                            .transpose()?,
                        access_token_expires_at: account.access_token_expires_at,
                        scopes: account_scopes(&account),
                        id_token: account.id_token.clone(),
                        token_type: None,
                    },
                    cookies,
                )
            })
        },
    )
}

#[cfg(feature = "oauth")]
pub(super) fn refresh_token_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/refresh-token",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("refreshToken")
            .body_schema(token_body_schema())
            .openapi(
                OpenApiOperation::new("refreshToken")
                    .description("Refresh the access token using a refresh token")
                    .response("200", token_openapi_response(true)),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let body: TokenBody = parse_request_body(&request)?;
                let Some((_, session_user, cookies)) =
                    sensitive_session(adapter.as_ref(), context, &request).await?
                else {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "USER_ID_OR_SESSION_REQUIRED",
                        "Either userId or session is required",
                    );
                };
                let requested_user_id = body.user_id.as_deref().unwrap_or(&session_user.id);
                if requested_user_id != session_user.id {
                    return unauthorized();
                }
                let Some(provider) = context.social_provider(&body.provider_id) else {
                    return provider_not_supported(&body.provider_id);
                };
                let users = DbUserStore::new(adapter.as_ref());
                let Some(account) = find_user_account(
                    &users,
                    &session_user.id,
                    &body.provider_id,
                    body.account_id.as_deref(),
                )
                .await?
                else {
                    return account_not_found();
                };
                let Some(refresh_token) = account.refresh_token.as_deref() else {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "REFRESH_TOKEN_NOT_FOUND",
                        "Refresh token not found",
                    );
                };
                let decrypted = decrypt_oauth_token(refresh_token, context)?;
                let tokens = match provider.refresh_access_token(decrypted.clone()).await {
                    Ok(tokens) => tokens,
                    Err(error) if is_refresh_unsupported(&error) => {
                        return error_response(
                            StatusCode::BAD_REQUEST,
                            "TOKEN_REFRESH_NOT_SUPPORTED",
                            format!(
                                "Provider {} does not support token refreshing.",
                                body.provider_id
                            ),
                        );
                    }
                    Err(_) => {
                        return error_response(
                            StatusCode::BAD_REQUEST,
                            "FAILED_TO_REFRESH_ACCESS_TOKEN",
                            "Failed to refresh access token",
                        );
                    }
                };
                if tokens.access_token.is_none() {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "FAILED_TO_REFRESH_ACCESS_TOKEN",
                        "Failed to refresh access token",
                    );
                }
                let updated_account = persist_refreshed_tokens(
                    context,
                    &users,
                    account,
                    tokens.clone(),
                    Some(&decrypted),
                )
                .await?;
                json_response(
                    StatusCode::OK,
                    &RefreshTokenResponse {
                        access_token: tokens.access_token.unwrap_or_default(),
                        refresh_token: tokens.refresh_token.unwrap_or_else(|| decrypted.to_owned()),
                        access_token_expires_at: updated_account.access_token_expires_at,
                        refresh_token_expires_at: updated_account.refresh_token_expires_at,
                        scope: updated_account.scope.clone(),
                        id_token: updated_account.id_token.clone(),
                        provider_id: updated_account.provider_id,
                        account_id: updated_account.account_id,
                        token_type: tokens.token_type,
                    },
                    cookies,
                )
            })
        },
    )
}

#[cfg(feature = "oauth")]
pub(super) fn account_info_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/account-info",
        Method::GET,
        AuthEndpointOptions::new()
            .operation_id("accountInfo")
            .openapi(
                OpenApiOperation::new("accountInfo")
                    .description("Get the account info provided by the provider")
                    .response(
                        "200",
                        super::shared::json_openapi_response(
                            "Success",
                            json!({
                                "type": "object",
                                "properties": {
                                    "user": { "type": "object" },
                                    "data": { "type": "object", "additionalProperties": true },
                                },
                                "required": ["user", "data"],
                            }),
                        ),
                    ),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let Some((_, session_user, cookies)) =
                    sensitive_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let account_id = super::shared::query_param(&request, "accountId");
                let users = DbUserStore::new(adapter.as_ref());
                let accounts = users.list_accounts_for_user(&session_user.id).await?;
                let account = match account_id.as_deref() {
                    Some(account_id) => accounts.into_iter().find(|account| {
                        account.account_id == account_id || account.id == account_id
                    }),
                    None => accounts
                        .into_iter()
                        .find(|account| account.provider_id != "credential"),
                };
                let Some(account) = account else {
                    return account_not_found();
                };
                let Some(provider) = context.social_provider(&account.provider_id) else {
                    return error_response(
                        StatusCode::INTERNAL_SERVER_ERROR,
                        "PROVIDER_NOT_CONFIGURED",
                        format!(
                            "Provider account provider is {} but it is not configured",
                            account.provider_id
                        ),
                    );
                };
                let mut account = account;
                let tokens = if should_refresh(&account) {
                    match account.refresh_token.clone() {
                        Some(refresh_token) => {
                            let decrypted = decrypt_oauth_token(&refresh_token, context)?;
                            match provider.refresh_access_token(decrypted).await {
                                Ok(tokens) => {
                                    if tokens.access_token.is_none() {
                                        return error_response(
                                            StatusCode::BAD_REQUEST,
                                            "FAILED_TO_GET_ACCESS_TOKEN",
                                            "Failed to get a valid access token",
                                        );
                                    }
                                    account = persist_refreshed_tokens(
                                        context,
                                        &users,
                                        account,
                                        tokens.clone(),
                                        None,
                                    )
                                    .await?;
                                    tokens
                                }
                                Err(error) if is_refresh_unsupported(&error) => {
                                    tokens_from_account(context, &account)?
                                }
                                Err(_) => {
                                    return error_response(
                                        StatusCode::BAD_REQUEST,
                                        "FAILED_TO_GET_ACCESS_TOKEN",
                                        "Failed to get a valid access token",
                                    );
                                }
                            }
                        }
                        None => tokens_from_account(context, &account)?,
                    }
                } else {
                    tokens_from_account(context, &account)?
                };
                let Some(user_info) = provider.get_user_info(tokens, None).await? else {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "ACCESS_TOKEN_NOT_FOUND",
                        "Access token not found",
                    );
                };
                json_response(
                    StatusCode::OK,
                    &AccountInfoResponse {
                        user: user_info.into(),
                        data: json!({ "provider": account.provider_id }),
                    },
                    cookies,
                )
            })
        },
    )
}

pub(super) fn unlink_account_endpoint(adapter: Arc<dyn DbAdapter>) -> AsyncAuthEndpoint {
    create_auth_endpoint(
        "/unlink-account",
        Method::POST,
        AuthEndpointOptions::new()
            .operation_id("unlinkAccount")
            .body_schema(unlink_account_body_schema())
            .openapi(
                OpenApiOperation::new("unlinkAccount")
                    .description("Unlink an account")
                    .response("200", status_openapi_response("Success")),
            ),
        move |context, request| {
            let adapter = Arc::clone(&adapter);
            Box::pin(async move {
                let Some((_, user, cookies)) =
                    sensitive_session(adapter.as_ref(), context, &request).await?
                else {
                    return unauthorized();
                };
                let body: UnlinkAccountBody = parse_request_body(&request)?;
                let users = DbUserStore::new(adapter.as_ref());
                let accounts = users.list_accounts_for_user(&user.id).await?;
                if accounts.len() == 1 {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "FAILED_TO_UNLINK_LAST_ACCOUNT",
                        "Failed to unlink last account",
                    );
                }

                let Some(account) = accounts.iter().find(|account| {
                    account.provider_id == body.provider_id
                        && match body.account_id.as_ref() {
                            Some(account_id) => account.account_id == *account_id,
                            None => true,
                        }
                }) else {
                    return error_response(
                        StatusCode::BAD_REQUEST,
                        "ACCOUNT_NOT_FOUND",
                        "Account not found",
                    );
                };

                users.delete_account(&account.id).await?;
                json_response(StatusCode::OK, &StatusBody { status: true }, cookies)
            })
        },
    )
}