greentic_oauth_core/
provider.rs1use std::{error::Error, fmt};
2
3use crate::types::{OAuthFlowRequest, OAuthFlowResult, TokenHandleClaims, TokenSet};
4
5pub type ProviderResult<T> = Result<T, ProviderError>;
7
8pub trait Provider: Send + Sync {
10 fn auth_url(&self) -> &str;
12 fn token_url(&self) -> &str;
14 fn redirect_uri(&self) -> &str;
16 fn build_authorize_redirect(
18 &self,
19 request: &OAuthFlowRequest,
20 ) -> ProviderResult<OAuthFlowResult>;
21 fn exchange_code(&self, claims: &TokenHandleClaims, code: &str) -> ProviderResult<TokenSet>;
23 fn refresh(&self, claims: &TokenHandleClaims, refresh_token: &str) -> ProviderResult<TokenSet>;
25 fn revoke(&self, claims: &TokenHandleClaims, token: &str) -> ProviderResult<()>;
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct ProviderError {
32 kind: ProviderErrorKind,
33 message: Option<String>,
34}
35
36impl ProviderError {
37 pub fn new(kind: ProviderErrorKind, message: impl Into<Option<String>>) -> Self {
39 Self {
40 kind,
41 message: message.into(),
42 }
43 }
44
45 pub fn kind(&self) -> ProviderErrorKind {
47 self.kind
48 }
49
50 pub fn message(&self) -> Option<&str> {
52 self.message.as_deref()
53 }
54}
55
56impl fmt::Display for ProviderError {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 match &self.message {
59 Some(message) => write!(f, "{}: {}", self.kind, message),
60 None => write!(f, "{}", self.kind),
61 }
62 }
63}
64
65impl Error for ProviderError {}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub enum ProviderErrorKind {
70 Configuration,
72 Transport,
74 Authorization,
76 InvalidResponse,
78 Unsupported,
80 Other,
82}
83
84impl fmt::Display for ProviderErrorKind {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 let label = match self {
87 ProviderErrorKind::Configuration => "configuration error",
88 ProviderErrorKind::Transport => "transport error",
89 ProviderErrorKind::Authorization => "authorization error",
90 ProviderErrorKind::InvalidResponse => "invalid response",
91 ProviderErrorKind::Unsupported => "unsupported operation",
92 ProviderErrorKind::Other => "provider error",
93 };
94 f.write_str(label)
95 }
96}