oxide_auth_actix/
operations.rs

1use crate::{OAuthRequest, OAuthResponse, OAuthOperation, WebError};
2use oxide_auth::{
3    endpoint::{
4        AccessTokenFlow, AuthorizationFlow, Endpoint, RefreshFlow, ResourceFlow, ClientCredentialsFlow,
5    },
6    primitives::grant::Grant,
7};
8
9/// Authorization-related operations
10pub struct Authorize(pub OAuthRequest);
11
12impl OAuthOperation for Authorize {
13    type Item = OAuthResponse;
14    type Error = WebError;
15
16    fn run<E>(self, endpoint: E) -> Result<Self::Item, Self::Error>
17    where
18        E: Endpoint<OAuthRequest>,
19        WebError: From<E::Error>,
20    {
21        AuthorizationFlow::prepare(endpoint)?
22            .execute(self.0)
23            .map_err(WebError::from)
24    }
25}
26
27/// Token-related operations
28pub struct Token(pub OAuthRequest);
29
30impl OAuthOperation for Token {
31    type Item = OAuthResponse;
32    type Error = WebError;
33
34    fn run<E>(self, endpoint: E) -> Result<Self::Item, Self::Error>
35    where
36        E: Endpoint<OAuthRequest>,
37        WebError: From<E::Error>,
38    {
39        AccessTokenFlow::prepare(endpoint)?
40            .execute(self.0)
41            .map_err(WebError::from)
42    }
43}
44
45/// Client Credentials related operations
46pub struct ClientCredentials(pub OAuthRequest);
47
48impl OAuthOperation for ClientCredentials {
49    type Item = OAuthResponse;
50    type Error = WebError;
51
52    fn run<E>(self, endpoint: E) -> Result<Self::Item, Self::Error>
53    where
54        E: Endpoint<OAuthRequest>,
55        WebError: From<E::Error>,
56    {
57        ClientCredentialsFlow::prepare(endpoint)?
58            .execute(self.0)
59            .map_err(WebError::from)
60    }
61}
62
63/// Refresh-related operations
64pub struct Refresh(pub OAuthRequest);
65
66impl OAuthOperation for Refresh {
67    type Item = OAuthResponse;
68    type Error = WebError;
69
70    fn run<E>(self, endpoint: E) -> Result<Self::Item, Self::Error>
71    where
72        E: Endpoint<OAuthRequest>,
73        WebError: From<E::Error>,
74    {
75        RefreshFlow::prepare(endpoint)?
76            .execute(self.0)
77            .map_err(WebError::from)
78    }
79}
80
81/// Resource-related operations
82pub struct Resource(pub OAuthRequest);
83
84impl OAuthOperation for Resource {
85    type Item = Grant;
86    type Error = Result<OAuthResponse, WebError>;
87
88    fn run<E>(self, endpoint: E) -> Result<Self::Item, Self::Error>
89    where
90        E: Endpoint<OAuthRequest>,
91        WebError: From<E::Error>,
92    {
93        ResourceFlow::prepare(endpoint)
94            .map_err(|e| Err(WebError::from(e)))?
95            .execute(self.0)
96            .map_err(|r| r.map_err(WebError::from))
97    }
98}