openauth_plugins/api_key/
schema.rs1use indexmap::IndexMap;
2use openauth_core::db::{DbField, DbFieldType, DbTable, TableOptions};
3use openauth_core::plugin::PluginSchemaContribution;
4
5use super::{API_KEY_MODEL, API_KEY_TABLE};
6
7#[derive(Debug, Clone, Default, PartialEq, Eq)]
8pub struct ApiKeySchemaOptions {
9 pub table: TableOptions,
10}
11
12pub fn schema_contribution(options: &ApiKeySchemaOptions) -> PluginSchemaContribution {
13 PluginSchemaContribution::table(
14 API_KEY_MODEL,
15 table(
16 &options.table,
17 [
18 ("id", DbField::new("id", DbFieldType::String)),
19 (
20 "config_id",
21 DbField::new("config_id", DbFieldType::String).indexed(),
22 ),
23 ("name", DbField::new("name", DbFieldType::String).optional()),
24 (
25 "start",
26 DbField::new("start", DbFieldType::String).optional(),
27 ),
28 (
29 "prefix",
30 DbField::new("prefix", DbFieldType::String).optional(),
31 ),
32 ("key", DbField::new("key", DbFieldType::String).indexed()),
33 (
34 "reference_id",
35 DbField::new("reference_id", DbFieldType::String).indexed(),
36 ),
37 (
38 "refill_interval",
39 DbField::new("refill_interval", DbFieldType::Number).optional(),
40 ),
41 (
42 "refill_amount",
43 DbField::new("refill_amount", DbFieldType::Number).optional(),
44 ),
45 (
46 "last_refill_at",
47 DbField::new("last_refill_at", DbFieldType::Timestamp).optional(),
48 ),
49 ("enabled", DbField::new("enabled", DbFieldType::Boolean)),
50 (
51 "rate_limit_enabled",
52 DbField::new("rate_limit_enabled", DbFieldType::Boolean),
53 ),
54 (
55 "rate_limit_time_window",
56 DbField::new("rate_limit_time_window", DbFieldType::Number).optional(),
57 ),
58 (
59 "rate_limit_max",
60 DbField::new("rate_limit_max", DbFieldType::Number).optional(),
61 ),
62 (
63 "request_count",
64 DbField::new("request_count", DbFieldType::Number),
65 ),
66 (
67 "remaining",
68 DbField::new("remaining", DbFieldType::Number).optional(),
69 ),
70 (
71 "last_request",
72 DbField::new("last_request", DbFieldType::Timestamp).optional(),
73 ),
74 (
75 "expires_at",
76 DbField::new("expires_at", DbFieldType::Timestamp).optional(),
77 ),
78 (
79 "created_at",
80 DbField::new("created_at", DbFieldType::Timestamp),
81 ),
82 (
83 "updated_at",
84 DbField::new("updated_at", DbFieldType::Timestamp),
85 ),
86 (
87 "metadata",
88 DbField::new("metadata", DbFieldType::Json).optional(),
89 ),
90 (
91 "permissions",
92 DbField::new("permissions", DbFieldType::Json).optional(),
93 ),
94 ],
95 ),
96 )
97}
98
99fn table<const N: usize>(options: &TableOptions, fields: [(&str, DbField); N]) -> DbTable {
100 let mut fields = fields
101 .into_iter()
102 .map(|(logical_name, mut field)| {
103 if let Some(db_name) = options.field_names.get(logical_name) {
104 field.name = db_name.clone();
105 }
106 (logical_name.to_owned(), field)
107 })
108 .collect::<IndexMap<_, _>>();
109 fields.extend(options.additional_fields.clone());
110 DbTable {
111 name: options
112 .name
113 .clone()
114 .unwrap_or_else(|| API_KEY_TABLE.to_owned()),
115 fields,
116 order: Some(30),
117 }
118}