Skip to main content

openauth_cli/
plugins.rs

1use openauth_core::db::DbSchema;
2use openauth_core::error::OpenAuthError;
3use openauth_core::plugin::AuthPlugin;
4use openauth_plugins::{
5    admin::{admin, AdminOptions},
6    anonymous::{anonymous, AnonymousOptions},
7    device_authorization::device_authorization,
8    jwt::jwt,
9    organization::organization,
10    two_factor::{two_factor, TwoFactorOptions},
11    username::username,
12    PLUGIN_IDS,
13};
14use serde::Serialize;
15
16#[derive(Debug, Clone, Serialize)]
17pub struct PluginInfo {
18    pub id: &'static str,
19    pub schema: bool,
20    pub scaffold_supported: bool,
21}
22
23pub fn official_plugins() -> Vec<PluginInfo> {
24    PLUGIN_IDS
25        .iter()
26        .map(|id| PluginInfo {
27            id,
28            schema: schema_plugin(id).is_some(),
29            scaffold_supported: schema_plugin(id).is_some(),
30        })
31        .collect()
32}
33
34pub fn is_official_plugin(plugin: &str) -> bool {
35    PLUGIN_IDS.contains(&plugin)
36}
37
38pub fn apply_configured_plugins(
39    schema: &mut DbSchema,
40    plugins: &[String],
41) -> Result<(), OpenAuthError> {
42    for plugin in plugins {
43        let Some(auth_plugin) = schema_plugin(plugin) else {
44            continue;
45        };
46        for contribution in auth_plugin.schema {
47            contribution.apply(schema)?;
48        }
49    }
50    Ok(())
51}
52
53pub fn schema_plugin(plugin: &str) -> Option<AuthPlugin> {
54    match plugin {
55        "admin" => Some(admin(AdminOptions::default())),
56        "anonymous" => Some(anonymous(AnonymousOptions::default())),
57        "device-authorization" => Some(device_authorization()),
58        "jwt" => jwt().ok(),
59        "organization" => Some(organization()),
60        "two-factor" => Some(two_factor(TwoFactorOptions::default())),
61        "username" => Some(username()),
62        _ => None,
63    }
64}
65
66pub fn rust_snippet(plugin: &str) -> Option<&'static str> {
67    match plugin {
68        "two-factor" => {
69            Some("openauth::plugins::two_factor::two_factor(TwoFactorOptions::default())")
70        }
71        "organization" => Some("openauth::plugins::organization::organization()"),
72        "username" => Some("openauth::plugins::username::username()"),
73        "admin" => Some("openauth::plugins::admin::admin(AdminOptions::default())"),
74        "api-key" => Some("openauth::plugins::api_key::api_key()"),
75        "device-authorization" => {
76            Some("openauth::plugins::device_authorization::device_authorization()")
77        }
78        "anonymous" => Some("openauth::plugins::anonymous::anonymous(AnonymousOptions::default())"),
79        "jwt" => Some("openauth::plugins::jwt::jwt()?"),
80        _ => None,
81    }
82}