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(
28 &self,
29 claims: &TokenHandleClaims,
30 code: &str,
31 pkce_verifier: Option<&str>,
32 ) -> ProviderResult<TokenSet>;
33 fn refresh(&self, claims: &TokenHandleClaims, refresh_token: &str) -> ProviderResult<TokenSet>;
35 fn revoke(&self, claims: &TokenHandleClaims, token: &str) -> ProviderResult<()>;
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct ProviderError {
42 kind: ProviderErrorKind,
43 message: Option<String>,
44}
45
46impl ProviderError {
47 pub fn new(kind: ProviderErrorKind, message: impl Into<Option<String>>) -> Self {
49 Self {
50 kind,
51 message: message.into(),
52 }
53 }
54
55 pub fn kind(&self) -> ProviderErrorKind {
57 self.kind
58 }
59
60 pub fn message(&self) -> Option<&str> {
62 self.message.as_deref()
63 }
64}
65
66impl fmt::Display for ProviderError {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match &self.message {
69 Some(message) => write!(f, "{}: {}", self.kind, message),
70 None => write!(f, "{}", self.kind),
71 }
72 }
73}
74
75impl Error for ProviderError {}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79pub enum ProviderErrorKind {
80 Configuration,
82 Transport,
84 Authorization,
86 InvalidResponse,
88 Unsupported,
90 Other,
92}
93
94impl fmt::Display for ProviderErrorKind {
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 let label = match self {
97 ProviderErrorKind::Configuration => "configuration error",
98 ProviderErrorKind::Transport => "transport error",
99 ProviderErrorKind::Authorization => "authorization error",
100 ProviderErrorKind::InvalidResponse => "invalid response",
101 ProviderErrorKind::Unsupported => "unsupported operation",
102 ProviderErrorKind::Other => "provider error",
103 };
104 f.write_str(label)
105 }
106}