Skip to main content

shield_workos/
method.rs

1use async_trait::async_trait;
2use shield::{Method, MethodAction, ShieldError, erased_method};
3use workos::Client;
4
5use crate::{
6    actions::{WorkosIndexAction, WorkosSignInAction, WorkosSignUpAction},
7    options::WorkosOptions,
8    provider::WorkosProvider,
9};
10
11// TODO: Add hook for WorkOS sign out.
12
13pub const WORKOS_METHOD_ID: &str = "workos";
14
15pub struct WorkosMethod {
16    options: WorkosOptions,
17    client: Client,
18}
19
20impl WorkosMethod {
21    pub fn new(client: Client, options: WorkosOptions) -> Self {
22        Self { options, client }
23    }
24
25    pub fn from_api_key(api_key: &str, client_id: &str, options: WorkosOptions) -> Self {
26        Self::new(
27            Client::builder()
28                .api_key(api_key)
29                .client_id(client_id)
30                .build(),
31            options,
32        )
33    }
34
35    pub fn with_options(mut self, options: WorkosOptions) -> Self {
36        self.options = options;
37        self
38    }
39}
40
41#[async_trait]
42impl Method for WorkosMethod {
43    type Provider = WorkosProvider;
44    type Connection = ();
45    type Session = ();
46
47    fn id(&self) -> String {
48        WORKOS_METHOD_ID.to_owned()
49    }
50
51    fn actions(&self) -> Vec<Box<dyn MethodAction<Self::Provider, Self::Session>>> {
52        vec![
53            Box::new(WorkosIndexAction::new(
54                self.options.clone(),
55                self.client.clone(),
56            )),
57            Box::new(WorkosSignInAction::new(self.client.clone())),
58            Box::new(WorkosSignUpAction::new(self.client.clone())),
59        ]
60    }
61
62    async fn providers(&self) -> Result<Vec<Self::Provider>, ShieldError> {
63        Ok(vec![WorkosProvider])
64    }
65
66    async fn user_connections(
67        &self,
68        _user_id: &str,
69        _provider_id: Option<&str>,
70    ) -> Result<Vec<Self::Connection>, ShieldError> {
71        // TODO
72        Ok(vec![])
73    }
74}
75
76erased_method!(WorkosMethod);