create_rust_app/auth/
mod.rs

1use serde::{Deserialize, Serialize};
2
3#[cfg(feature = "plugin_utoipa")]
4use utoipa::{
5    openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme},
6    Modify,
7};
8
9// Auth guard / extractor
10mod extractors;
11pub use extractors::*;
12
13// api endpoint definitions
14pub mod controller;
15mod endpoints;
16pub use endpoints::*;
17
18#[cfg(feature = "plugin_auth-oidc")]
19pub mod oidc;
20
21pub(crate) mod mail;
22mod permissions;
23mod schema;
24mod user;
25mod user_session;
26
27pub use permissions::{
28    Permission, Role, RolePermission, RolePermissionChangeset, UserPermission,
29    UserPermissionChangeset,
30};
31pub use user::{User, UserChangeset};
32pub use user_session::{UserSession, UserSessionChangeset};
33
34#[tsync::tsync]
35type ID = i32;
36
37#[tsync::tsync]
38#[cfg(not(feature = "database_sqlite"))]
39type Utc = chrono::DateTime<chrono::Utc>;
40#[cfg(feature = "database_sqlite")]
41type Utc = chrono::NaiveDateTime;
42
43#[tsync::tsync]
44#[derive(Deserialize)]
45#[cfg_attr(feature = "plugin_utoipa", derive(utoipa::IntoParams))]
46// TODO: make "PaginationParams" something provided by this crate
47/// Rust struct that provides the information needed to allow
48/// pagination of results for requests that have a lot of results
49///
50/// often times, GET requests to a REST API will have a lot of
51/// results to return, pagination allows the server to break up
52/// those results into smaller chunks that can be more easily
53/// sent to, and used by, the client
54pub struct PaginationParams {
55    pub page: i64,
56    pub page_size: i64,
57}
58
59impl PaginationParams {
60    const MAX_PAGE_SIZE: u16 = 100;
61}
62
63#[tsync::tsync]
64#[derive(Debug, Serialize, Deserialize, Clone)]
65#[cfg_attr(feature = "plugin_utoipa", derive(utoipa::ToSchema))]
66/// Rust struct representation of a entry from the databases `user_session` table
67/// serialized into Json
68pub struct UserSessionJson {
69    pub id: ID,
70    pub device: Option<String>,
71    pub created_at: Utc,
72    #[cfg(not(feature = "database_sqlite"))]
73    pub updated_at: Utc,
74}
75
76#[tsync::tsync]
77#[derive(Debug, Serialize, Deserialize, Clone)]
78#[cfg_attr(feature = "plugin_utoipa", derive(utoipa::ToSchema))]
79/// Rust struct representation of the
80/// backends JSON response to a GET request at the /sessions endpoint
81pub struct UserSessionResponse {
82    pub sessions: Vec<UserSessionJson>,
83    pub num_pages: i64,
84}
85
86#[tsync::tsync]
87#[derive(Debug, Serialize, Deserialize)]
88/// TODO: documentation
89pub struct AccessTokenClaims {
90    pub exp: usize,
91    pub sub: ID,
92    pub token_type: String,
93    pub roles: Vec<String>,
94    pub permissions: Vec<Permission>,
95}
96
97#[cfg(feature = "plugin_utoipa")]
98pub struct JwtSecurityAddon;
99#[cfg(feature = "plugin_utoipa")]
100impl Modify for JwtSecurityAddon {
101    fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
102        let components = openapi.components.as_mut().unwrap(); // we can unwrap safely since there already is components registered.
103        components.add_security_scheme(
104            "JWT",
105            SecurityScheme::Http(
106                HttpBuilder::new()
107                    .scheme(HttpAuthScheme::Bearer)
108                    .bearer_format("JWT")
109                    .build(),
110            ),
111        );
112    }
113}
114
115#[allow(clippy::module_name_repetitions)]
116#[cfg(feature = "plugin_utoipa")]
117#[tsync::tsync]
118#[derive(Debug, Serialize, utoipa::ToSchema)]
119/// structure to help utoipa know what responses that contain a message
120pub struct AuthMessageResponse {
121    pub message: String,
122}
123
124#[allow(clippy::module_name_repetitions)]
125#[cfg(feature = "plugin_utoipa")]
126#[tsync::tsync]
127#[derive(Debug, Serialize, utoipa::ToSchema)]
128/// structure to help utoipa know what responses that contain the `access_token` should look like
129pub struct AuthTokenResponse {
130    pub access_token: String,
131}
132
133#[allow(clippy::module_name_repetitions)]
134#[derive(Clone)]
135pub struct AuthConfig {
136    #[cfg(feature = "plugin_auth-oidc")]
137    pub oidc_providers: Vec<crate::auth::oidc::OIDCProvider>,
138}