chifu_kube_client/client/auth/
oidc.rs

1use std::collections::HashMap;
2
3use super::TEN_SEC;
4use chrono::{TimeZone, Utc};
5use form_urlencoded::Serializer;
6use http::{
7    header::{HeaderValue, AUTHORIZATION, CONTENT_TYPE},
8    Method, Request, Uri, Version,
9};
10use http_body_util::BodyExt;
11use hyper_util::{
12    client::legacy::{connect::HttpConnector, Client},
13    rt::TokioExecutor,
14};
15use secrecy::{ExposeSecret, SecretString};
16use serde::{Deserialize, Deserializer};
17use serde_json::Number;
18
19/// Possible errors when handling OIDC authentication.
20pub mod errors {
21    use super::Oidc;
22    use http::{uri::InvalidUri, StatusCode};
23    use thiserror::Error;
24
25    /// Possible errors when extracting expiration time from an ID token.
26    #[derive(Error, Debug)]
27    pub enum IdTokenError {
28        /// Failed to extract payload from the ID token.
29        #[error("not a valid JWT token")]
30        InvalidFormat,
31        /// ID token payload is not properly encoded in base64.
32        #[error("failed to decode base64: {0}")]
33        InvalidBase64(
34            #[source]
35            #[from]
36            base64::DecodeError,
37        ),
38        /// ID token payload is not valid JSON object containing expiration timestamp.
39        #[error("failed to unmarshal JSON: {0}")]
40        InvalidJson(
41            #[source]
42            #[from]
43            serde_json::Error,
44        ),
45        /// Expiration timestamp extracted from the ID token payload is not valid.
46        #[error("invalid expiration timestamp")]
47        InvalidExpirationTimestamp,
48    }
49
50    /// Possible error when initializing the ID token refreshing.
51    #[derive(Error, Debug, Clone)]
52    pub enum RefreshInitError {
53        /// Missing field in the configuration.
54        #[error("missing field {0}")]
55        MissingField(&'static str),
56        /// Failed to create an HTTPS client.
57        #[cfg(feature = "openssl-tls")]
58        #[cfg_attr(docsrs, doc(cfg(feature = "openssl-tls")))]
59        #[error("failed to create OpenSSL HTTPS connector: {0}")]
60        CreateOpensslHttpsConnector(
61            #[source]
62            #[from]
63            openssl::error::ErrorStack,
64        ),
65        /// No valid native root CA certificates found
66        #[error("No valid native root CA certificates found")]
67        NoValidNativeRootCA,
68    }
69
70    /// Possible errors when using the refresh token.
71    #[derive(Error, Debug)]
72    pub enum RefreshError {
73        /// Failed to parse the provided issuer URL.
74        #[error("invalid URI: {0}")]
75        InvalidURI(
76            #[source]
77            #[from]
78            InvalidUri,
79        ),
80        /// [`hyper::Error`] occurred during refreshing.
81        #[error("hyper error: {0}")]
82        HyperError(
83            #[source]
84            #[from]
85            hyper::Error,
86        ),
87        /// [`hyper_util::client::legacy::Error`] occurred during refreshing.
88        #[error("hyper-util error: {0}")]
89        HyperUtilError(
90            #[source]
91            #[from]
92            hyper_util::client::legacy::Error,
93        ),
94        /// Failed to parse the metadata received from the provider.
95        #[error("invalid metadata received from the provider: {0}")]
96        InvalidMetadata(#[source] serde_json::Error),
97        /// Received an invalid status code from the provider.
98        #[error("request failed with status code: {0}")]
99        RequestFailed(StatusCode),
100        /// [`http::Error`] occurred during refreshing.
101        #[error("http error: {0}")]
102        HttpError(
103            #[source]
104            #[from]
105            http::Error,
106        ),
107        /// Failed to authorize with the provider.
108        #[error("failed to authorize with the provider using any of known authorization styles")]
109        AuthorizationFailure,
110        /// Failed to parse the token response from the provider.
111        #[error("invalid token response received from the provider: {0}")]
112        InvalidTokenResponse(#[source] serde_json::Error),
113        /// Token response from the provider did not contain an ID token.
114        #[error("no ID token received from the provider")]
115        NoIdTokenReceived,
116    }
117
118    /// Possible errors when dealing with OIDC.
119    #[derive(Error, Debug)]
120    pub enum Error {
121        /// Config did not contain the ID token.
122        #[error("missing field {}", Oidc::CONFIG_ID_TOKEN)]
123        IdTokenMissing,
124        /// Failed to retrieve expiration timestamp from the ID token.
125        #[error("invalid ID token: {0}")]
126        IdToken(
127            #[source]
128            #[from]
129            IdTokenError,
130        ),
131        /// Failed to initialize ID token refreshing.
132        #[error("ID token expired and refreshing is not possible: {0}")]
133        RefreshInit(
134            #[source]
135            #[from]
136            RefreshInitError,
137        ),
138        /// Failed to refresh the ID token.
139        #[error("ID token expired and refreshing failed: {0}")]
140        Refresh(
141            #[source]
142            #[from]
143            RefreshError,
144        ),
145    }
146}
147
148use base64::Engine as _;
149const JWT_BASE64_ENGINE: base64::engine::GeneralPurpose = base64::engine::GeneralPurpose::new(
150    &base64::alphabet::URL_SAFE,
151    base64::engine::GeneralPurposeConfig::new()
152        .with_decode_allow_trailing_bits(true)
153        .with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
154);
155use base64::engine::general_purpose::STANDARD as STANDARD_BASE64_ENGINE;
156
157#[derive(Debug)]
158pub struct Oidc {
159    id_token: SecretString,
160    refresher: Result<Refresher, errors::RefreshInitError>,
161}
162
163impl Oidc {
164    /// Config key for the ID token.
165    const CONFIG_ID_TOKEN: &'static str = "id-token";
166
167    /// Check whether the stored ID token can still be used.
168    fn token_valid(&self) -> Result<bool, errors::IdTokenError> {
169        let part = self
170            .id_token
171            .expose_secret()
172            .split('.')
173            .nth(1)
174            .ok_or(errors::IdTokenError::InvalidFormat)?;
175        let payload = JWT_BASE64_ENGINE.decode(part)?;
176        let expiry = serde_json::from_slice::<Claims>(&payload)?.expiry;
177        let timestamp = Utc
178            .timestamp_opt(expiry, 0)
179            .earliest()
180            .ok_or(errors::IdTokenError::InvalidExpirationTimestamp)?;
181
182        let valid = Utc::now() + TEN_SEC < timestamp;
183
184        Ok(valid)
185    }
186
187    /// Retrieve the ID token. If the stored ID token is or will soon be expired, try refreshing it first.
188    pub async fn id_token(&mut self) -> Result<String, errors::Error> {
189        if self.token_valid()? {
190            return Ok(self.id_token.expose_secret().clone());
191        }
192
193        let id_token = self.refresher.as_mut().map_err(|e| e.clone())?.id_token().await?;
194
195        self.id_token = id_token.clone().into();
196
197        Ok(id_token)
198    }
199
200    /// Create an instance of this struct from the auth provider config.
201    pub fn from_config(config: &HashMap<String, String>) -> Result<Self, errors::Error> {
202        let id_token = config
203            .get(Self::CONFIG_ID_TOKEN)
204            .ok_or(errors::Error::IdTokenMissing)?
205            .clone()
206            .into();
207        let refresher = Refresher::from_config(config);
208
209        Ok(Self { id_token, refresher })
210    }
211}
212
213/// Claims extracted from the ID token. Only expiration time here is important.
214#[derive(Deserialize)]
215struct Claims {
216    #[serde(rename = "exp", deserialize_with = "deserialize_expiry")]
217    expiry: i64,
218}
219
220/// Deserialize expiration time from a JSON number.
221fn deserialize_expiry<'de, D: Deserializer<'de>>(deserializer: D) -> core::result::Result<i64, D::Error> {
222    let json_number = Number::deserialize(deserializer)?;
223
224    json_number
225        .as_i64()
226        .or_else(|| Some(json_number.as_f64()? as i64))
227        .ok_or(serde::de::Error::custom("cannot be casted to i64"))
228}
229
230/// Metadata retrieved from the provider. Only token endpoint here is important.
231#[derive(Deserialize)]
232struct Metadata {
233    token_endpoint: String,
234}
235
236/// Authorization styles used by different providers.
237/// Some providers require the authorization info in the header, some in the request body.
238/// Some providers reject requests when authorization info is passed in both.
239#[derive(Debug, Clone, Copy, PartialEq, Eq)]
240enum AuthStyle {
241    Header,
242    Params,
243}
244
245impl AuthStyle {
246    /// All known authorization styles.
247    const ALL: [Self; 2] = [Self::Header, Self::Params];
248}
249
250/// Token response from the provider. Only refresh token and id token here are important.
251#[derive(Deserialize)]
252struct TokenResponse {
253    refresh_token: Option<String>,
254    id_token: Option<String>,
255}
256
257#[cfg(not(any(feature = "rustls-tls", feature = "openssl-tls")))]
258compile_error!(
259    "At least one of rustls-tls or openssl-tls feature must be enabled to use refresh-oidc feature"
260);
261// Current TLS feature precedence when more than one are set:
262// 1. rustls-tls
263// 2. openssl-tls
264#[cfg(feature = "rustls-tls")]
265type HttpsConnector = hyper_rustls::HttpsConnector<HttpConnector>;
266#[cfg(all(not(feature = "rustls-tls"), feature = "openssl-tls"))]
267type HttpsConnector = hyper_openssl::HttpsConnector<HttpConnector>;
268
269/// Struct for refreshing the ID token with the refresh token.
270#[derive(Debug)]
271struct Refresher {
272    issuer: String,
273    /// Token endpoint exposed by the provider.
274    /// Retrieved from the provider metadata with the first refresh request.
275    token_endpoint: Option<String>,
276    /// Refresh token used in the refresh requests.
277    /// Updated when a new refresh token is returned by the provider.
278    refresh_token: SecretString,
279    client_id: SecretString,
280    client_secret: SecretString,
281    https_client: Client<HttpsConnector, String>,
282    /// Authorization style used by the provider.
283    /// Determined with the first refresh request by trying all known styles.
284    auth_style: Option<AuthStyle>,
285}
286
287impl Refresher {
288    /// Config key for the client ID.
289    const CONFIG_CLIENT_ID: &'static str = "client-id";
290    /// Config key for the client secret.
291    const CONFIG_CLIENT_SECRET: &'static str = "client-secret";
292    /// Config key for the issuer url.
293    const CONFIG_ISSUER_URL: &'static str = "idp-issuer-url";
294    /// Config key for the refresh token.
295    const CONFIG_REFRESH_TOKEN: &'static str = "refresh-token";
296
297    /// Create a new instance of this struct from the provider config.
298    fn from_config(config: &HashMap<String, String>) -> Result<Self, errors::RefreshInitError> {
299        let get_field = |name: &'static str| {
300            config
301                .get(name)
302                .cloned()
303                .ok_or(errors::RefreshInitError::MissingField(name))
304        };
305
306        let issuer = get_field(Self::CONFIG_ISSUER_URL)?;
307        let refresh_token = get_field(Self::CONFIG_REFRESH_TOKEN)?.into();
308        let client_id = get_field(Self::CONFIG_CLIENT_ID)?.into();
309        let client_secret = get_field(Self::CONFIG_CLIENT_SECRET)?.into();
310
311        #[cfg(feature = "rustls-tls")]
312        let https = hyper_rustls::HttpsConnectorBuilder::new()
313            .with_native_roots()
314            .map_err(|_| errors::RefreshInitError::NoValidNativeRootCA)?
315            .https_only()
316            .enable_http1()
317            .build();
318        #[cfg(all(not(feature = "rustls-tls"), feature = "openssl-tls"))]
319        let https = hyper_openssl::HttpsConnector::new()?;
320
321        let https_client = hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build(https);
322
323        Ok(Self {
324            issuer,
325            token_endpoint: None,
326            refresh_token,
327            client_id,
328            client_secret,
329            https_client,
330            auth_style: None,
331        })
332    }
333
334    /// If the token endpoint is not yet cached in this struct, extract it from the provider metadata and store in the cache.
335    /// Provider metadata is retrieved from a well-known path.
336    async fn token_endpoint(&mut self) -> Result<String, errors::RefreshError> {
337        if let Some(endpoint) = self.token_endpoint.clone() {
338            return Ok(endpoint);
339        }
340
341        let discovery = format!("{}/.well-known/openid-configuration", self.issuer).parse::<Uri>()?;
342        let response = self.https_client.get(discovery).await?;
343
344        if response.status().is_success() {
345            let body = response.into_body().collect().await?.to_bytes();
346            let metadata = serde_json::from_slice::<Metadata>(body.as_ref())
347                .map_err(errors::RefreshError::InvalidMetadata)?;
348
349            self.token_endpoint.replace(metadata.token_endpoint.clone());
350
351            Ok(metadata.token_endpoint)
352        } else {
353            Err(errors::RefreshError::RequestFailed(response.status()))
354        }
355    }
356
357    /// Prepare a token request to the provider.
358    fn token_request(
359        &self,
360        endpoint: &str,
361        auth_style: AuthStyle,
362    ) -> Result<Request<String>, errors::RefreshError> {
363        let mut builder = Request::builder()
364            .uri(endpoint)
365            .method(Method::POST)
366            .header(
367                CONTENT_TYPE,
368                HeaderValue::from_static("application/x-www-form-urlencoded"),
369            )
370            .version(Version::HTTP_11);
371        let mut params = vec![
372            ("grant_type", "refresh_token"),
373            ("refresh_token", self.refresh_token.expose_secret()),
374        ];
375
376        match auth_style {
377            AuthStyle::Header => {
378                builder = builder.header(
379                    AUTHORIZATION,
380                    format!(
381                        "Basic {}",
382                        STANDARD_BASE64_ENGINE.encode(format!(
383                            "{}:{}",
384                            self.client_id.expose_secret(),
385                            self.client_secret.expose_secret()
386                        ))
387                    ),
388                );
389            }
390            AuthStyle::Params => {
391                params.extend([
392                    ("client_id", self.client_id.expose_secret().as_str()),
393                    ("client_secret", self.client_secret.expose_secret().as_str()),
394                ]);
395            }
396        };
397
398        let body = Serializer::new(String::new()).extend_pairs(params).finish();
399
400        builder.body(body).map_err(Into::into)
401    }
402
403    /// Fetch a new ID token from the provider.
404    async fn id_token(&mut self) -> Result<String, errors::RefreshError> {
405        let token_endpoint = self.token_endpoint().await?;
406
407        let response = match self.auth_style {
408            Some(style) => {
409                let request = self.token_request(&token_endpoint, style)?;
410                self.https_client.request(request).await?
411            }
412            None => {
413                let mut ok_response = None;
414
415                for style in AuthStyle::ALL {
416                    let request = self.token_request(&token_endpoint, style)?;
417                    let response = self.https_client.request(request).await?;
418                    if response.status().is_success() {
419                        ok_response.replace(response);
420                        self.auth_style.replace(style);
421                        break;
422                    }
423                }
424
425                ok_response.ok_or(errors::RefreshError::AuthorizationFailure)?
426            }
427        };
428
429        if !response.status().is_success() {
430            return Err(errors::RefreshError::RequestFailed(response.status()));
431        }
432
433        let body = response.into_body().collect().await?.to_bytes();
434        let token_response = serde_json::from_slice::<TokenResponse>(body.as_ref())
435            .map_err(errors::RefreshError::InvalidTokenResponse)?;
436
437        if let Some(token) = token_response.refresh_token {
438            self.refresh_token = token.into();
439        }
440
441        token_response
442            .id_token
443            .ok_or(errors::RefreshError::NoIdTokenReceived)
444    }
445}
446
447#[cfg(test)]
448mod tests {
449    use super::*;
450
451    #[test]
452    fn token_valid() {
453        let mut oidc = Oidc {
454            id_token: String::new().into(),
455            refresher: Err(errors::RefreshInitError::MissingField(
456                Refresher::CONFIG_REFRESH_TOKEN,
457            )),
458        };
459
460        // Proper JWT expiring at 2123-06-28T15:18:12.629Z
461        let token_valid = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9\
462.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODc5NjU0NTIsImV4cCI6NDg0MzYzOTA5MiwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkVtYWlsIjoiYmVlQGV4YW1wbGUuY29tIn0\
463.GKTkPMywcNQv0n01iBfv_A6VuCCCcAe72RhP0OrZsQM";
464        // Proper JWT expired at 2023-06-28T15:19:53.421Z
465        let token_expired = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9\
466.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODc5NjU0NTIsImV4cCI6MTY4Nzk2NTU5MywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkVtYWlsIjoiYmVlQGV4YW1wbGUuY29tIn0\
467.zTDnfI_zXIa6yPKY_ZE8r6GoLK7Syj-URcTU5_ryv1M";
468
469        oidc.id_token = token_valid.to_string().into();
470        assert!(oidc.token_valid().expect("proper token failed validation"));
471
472        oidc.id_token = token_expired.to_string().into();
473        assert!(!oidc.token_valid().expect("proper token failed validation"));
474
475        let malformed_token = token_expired.split_once('.').unwrap().0.to_string();
476        oidc.id_token = malformed_token.into();
477        oidc.token_valid().expect_err("malformed token passed validation");
478
479        let invalid_base64_token = token_valid
480            .split_once('.')
481            .map(|(prefix, suffix)| format!("{}.?{}", prefix, suffix))
482            .unwrap();
483        oidc.id_token = invalid_base64_token.into();
484        oidc.token_valid()
485            .expect_err("token with invalid base64 encoding passed validation");
486
487        let invalid_claims = [("sub", "jrocket@example.com"), ("aud", "www.example.com")]
488            .into_iter()
489            .collect::<HashMap<_, _>>();
490        let invalid_claims_token = format!(
491            "{}.{}.{}",
492            token_valid.split_once('.').unwrap().0,
493            JWT_BASE64_ENGINE.encode(serde_json::to_string(&invalid_claims).unwrap()),
494            token_valid.rsplit_once('.').unwrap().1,
495        );
496        oidc.id_token = invalid_claims_token.into();
497        oidc.token_valid()
498            .expect_err("token without expiration timestamp passed validation");
499    }
500
501    #[cfg(any(feature = "openssl-tls", feature = "rustls-tls"))]
502    #[test]
503    fn from_minimal_config() {
504        let minimal_config = [(Oidc::CONFIG_ID_TOKEN.into(), "some_id_token".into())]
505            .into_iter()
506            .collect();
507
508        let oidc = Oidc::from_config(&minimal_config)
509            .expect("failed to create oidc from minimal config (only id-token)");
510        assert_eq!(oidc.id_token.expose_secret(), "some_id_token");
511        assert!(oidc.refresher.is_err());
512    }
513
514    #[cfg(any(feature = "openssl-tls", feature = "rustls-tls"))]
515    #[test]
516    fn from_full_config() {
517        let full_config = [
518            (Oidc::CONFIG_ID_TOKEN.into(), "some_id_token".into()),
519            (Refresher::CONFIG_ISSUER_URL.into(), "some_issuer".into()),
520            (
521                Refresher::CONFIG_REFRESH_TOKEN.into(),
522                "some_refresh_token".into(),
523            ),
524            (Refresher::CONFIG_CLIENT_ID.into(), "some_client_id".into()),
525            (
526                Refresher::CONFIG_CLIENT_SECRET.into(),
527                "some_client_secret".into(),
528            ),
529        ]
530        .into_iter()
531        .collect();
532
533        let oidc = Oidc::from_config(&full_config).expect("failed to create oidc from full config");
534        assert_eq!(oidc.id_token.expose_secret(), "some_id_token");
535        let refresher = oidc
536            .refresher
537            .as_ref()
538            .expect("failed to create oidc refresher from full config");
539        assert_eq!(refresher.issuer, "some_issuer");
540        assert_eq!(refresher.token_endpoint, None);
541        assert_eq!(refresher.refresh_token.expose_secret(), "some_refresh_token");
542        assert_eq!(refresher.client_id.expose_secret(), "some_client_id");
543        assert_eq!(refresher.client_secret.expose_secret(), "some_client_secret");
544        assert_eq!(refresher.auth_style, None);
545    }
546}