google_jwt_verify/
client.rs1use crate::error::Error;
2#[cfg(feature = "async")]
3use crate::key_provider::AsyncKeyProvider;
4use crate::key_provider::GoogleKeyProvider;
5#[cfg(feature = "blocking")]
6use crate::key_provider::KeyProvider;
7use crate::token::IdPayload;
8use crate::token::Token;
9use crate::unverified_token::UnverifiedToken;
10use serde::Deserialize;
11
12use std::sync::{Arc, Mutex};
13
14pub type Client = GenericClient<GoogleKeyProvider>;
15
16pub struct GenericClientBuilder<KP> {
17 client_id: String,
18 key_provider: Arc<Mutex<KP>>,
19 check_expiration: bool,
20}
21
22impl<KP: Default> GenericClientBuilder<KP> {
23 pub fn new(client_id: &str) -> GenericClientBuilder<KP> {
24 GenericClientBuilder::<KP> {
25 client_id: client_id.to_owned(),
26 key_provider: Arc::new(Mutex::new(KP::default())),
27 check_expiration: true,
28 }
29 }
30}
31
32impl<KP> GenericClientBuilder<KP> {
33 pub fn custom_key_provider<T>(self, provider: T) -> GenericClientBuilder<T> {
34 GenericClientBuilder {
35 client_id: self.client_id,
36 key_provider: Arc::new(Mutex::new(provider)),
37 check_expiration: self.check_expiration,
38 }
39 }
40 pub fn unsafe_ignore_expiration(mut self) -> Self {
41 self.check_expiration = false;
42 self
43 }
44 pub fn build(self) -> GenericClient<KP> {
45 GenericClient {
46 client_id: self.client_id,
47 key_provider: self.key_provider,
48 check_expiration: self.check_expiration,
49 }
50 }
51}
52
53pub struct GenericClient<T> {
54 client_id: String,
55 key_provider: Arc<Mutex<T>>,
56 check_expiration: bool,
57}
58
59impl<KP: Default> GenericClient<KP> {
60 pub fn builder(client_id: &str) -> GenericClientBuilder<KP> {
61 GenericClientBuilder::<KP>::new(client_id)
62 }
63 pub fn new(client_id: &str) -> GenericClient<KP> {
64 GenericClientBuilder::new(client_id).build()
65 }
66}
67
68#[cfg(feature = "blocking")]
69impl<KP: KeyProvider> GenericClient<KP> {
70 pub fn verify_token_with_payload<P>(&self, token_string: &str) -> Result<Token<P>, Error>
71 where
72 for<'a> P: Deserialize<'a>,
73 {
74 let unverified_token =
75 UnverifiedToken::<P>::validate(token_string, self.check_expiration, &self.client_id)?;
76 unverified_token.verify(&self.key_provider)
77 }
78
79 pub fn verify_token(&self, token_string: &str) -> Result<Token<()>, Error> {
80 self.verify_token_with_payload::<()>(token_string)
81 }
82
83 pub fn verify_id_token(&self, token_string: &str) -> Result<Token<IdPayload>, Error> {
84 self.verify_token_with_payload(token_string)
85 }
86}
87
88#[cfg(feature = "async")]
89impl<KP: AsyncKeyProvider> GenericClient<KP> {
90 pub async fn verify_token_with_payload_async<P>(
91 &self,
92 token_string: &str,
93 ) -> Result<Token<P>, Error>
94 where
95 for<'a> P: Deserialize<'a>,
96 {
97 let unverified_token =
98 UnverifiedToken::<P>::validate(token_string, self.check_expiration, &self.client_id)?;
99 unverified_token.verify_async(&self.key_provider).await
100 }
101
102 pub async fn verify_token_async(&self, token_string: &str) -> Result<Token<()>, Error> {
103 self.verify_token_with_payload_async::<()>(token_string)
104 .await
105 }
106
107 pub async fn verify_id_token_async(
108 &self,
109 token_string: &str,
110 ) -> Result<Token<IdPayload>, Error> {
111 self.verify_token_with_payload_async(token_string).await
112 }
113}