Skip to main content

rustauth_plugins/jwt/
schema.rs

1use indexmap::IndexMap;
2use rustauth_core::db::{DbField, DbFieldType, DbTable, TableOptions};
3use rustauth_core::plugin::PluginSchemaContribution;
4
5#[derive(Debug, Clone, Default, PartialEq, Eq)]
6pub struct JwtSchemaOptions {
7    pub jwks: TableOptions,
8}
9
10pub(crate) fn jwks_schema(options: &JwtSchemaOptions) -> PluginSchemaContribution {
11    let mut fields = IndexMap::new();
12    fields.insert(
13        "id".to_owned(),
14        field(options, "id", DbField::new("id", DbFieldType::String)),
15    );
16    fields.insert(
17        "public_key".to_owned(),
18        field(
19            options,
20            "public_key",
21            DbField::new("public_key", DbFieldType::String),
22        ),
23    );
24    fields.insert(
25        "private_key".to_owned(),
26        field(
27            options,
28            "private_key",
29            DbField::new("private_key", DbFieldType::String),
30        ),
31    );
32    fields.insert(
33        "created_at".to_owned(),
34        field(
35            options,
36            "created_at",
37            DbField::new("created_at", DbFieldType::Timestamp),
38        ),
39    );
40    fields.insert(
41        "expires_at".to_owned(),
42        field(
43            options,
44            "expires_at",
45            DbField::new("expires_at", DbFieldType::Timestamp).optional(),
46        ),
47    );
48    fields.insert(
49        "alg".to_owned(),
50        field(
51            options,
52            "alg",
53            DbField::new("alg", DbFieldType::String).optional(),
54        ),
55    );
56    fields.insert(
57        "crv".to_owned(),
58        field(
59            options,
60            "crv",
61            DbField::new("crv", DbFieldType::String).optional(),
62        ),
63    );
64
65    PluginSchemaContribution::table(
66        "jwks",
67        DbTable {
68            name: options
69                .jwks
70                .name
71                .clone()
72                .unwrap_or_else(|| "jwks".to_owned()),
73            fields,
74            order: None,
75        },
76    )
77}
78
79fn field(options: &JwtSchemaOptions, logical_name: &str, mut field: DbField) -> DbField {
80    if let Some(db_name) = options.jwks.field_names.get(logical_name) {
81        field.name = db_name.clone();
82    }
83    field
84}