shield_workos/
method.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use shield::{Action, Method, ShieldError, erased_method};
5use workos::{ApiKey, WorkOs};
6
7use crate::{
8    actions::{WorkosIndexAction, WorkosSignInAction, WorkosSignOutAction, WorkosSignUpAction},
9    client::WorkosClient,
10    options::WorkosOptions,
11    provider::WorkosProvider,
12};
13
14pub const WORKOS_METHOD_ID: &str = "workos";
15
16pub struct WorkosMethod {
17    options: WorkosOptions,
18    client: Arc<WorkosClient>,
19}
20
21impl WorkosMethod {
22    pub fn new(client: WorkOs, client_id: &str, options: WorkosOptions) -> Self {
23        Self {
24            options,
25            client: Arc::new(WorkosClient::new(client, client_id)),
26        }
27    }
28
29    pub fn from_api_key(api_key: &str, client_id: &str, options: WorkosOptions) -> Self {
30        Self::new(WorkOs::new(&ApiKey::from(api_key)), client_id, options)
31    }
32
33    pub fn with_options(mut self, options: WorkosOptions) -> Self {
34        self.options = options;
35        self
36    }
37}
38
39#[async_trait]
40impl Method for WorkosMethod {
41    type Provider = WorkosProvider;
42    type Session = ();
43
44    fn id(&self) -> String {
45        WORKOS_METHOD_ID.to_owned()
46    }
47
48    fn actions(&self) -> Vec<Box<dyn Action<Self::Provider, Self::Session>>> {
49        vec![
50            Box::new(WorkosIndexAction::new(
51                self.options.clone(),
52                self.client.clone(),
53            )),
54            Box::new(WorkosSignInAction::new(self.client.clone())),
55            Box::new(WorkosSignUpAction::new(self.client.clone())),
56            Box::new(WorkosSignOutAction::new(self.client.clone())),
57        ]
58    }
59
60    async fn providers(&self) -> Result<Vec<Self::Provider>, ShieldError> {
61        Ok(vec![WorkosProvider])
62    }
63}
64
65erased_method!(WorkosMethod);