ublubu commented on code in PR #2924: URL: https://github.com/apache/iceberg-rust/pull/2924#discussion_r3677185416
########## crates/catalog/rest/src/token.rs: ########## @@ -0,0 +1,306 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Pluggable authentication for the REST catalog client. +//! +//! Most generic trait: [`RequestAuthenticator`] +//! - Allows arbitrary mutations to the outgoing [`reqwest::Request`]. +//! +//! Less generic trait: [`TokenProvider`] +//! - Produces a bearer token. e.g. OAuth +//! - Use [`BearerTokenAuthenticator`] to wrap a [`TokenProvider`] into a [`RequestAuthenticator`]. + +use std::collections::HashMap; +use std::fmt::Debug; +use std::sync::Arc; + +use async_trait::async_trait; +use http::{Method, StatusCode}; +use iceberg::{Error, ErrorKind, Result}; +use reqwest::header::HeaderMap; +use reqwest::{Client, Request}; +use tokio::sync::Mutex; + +use crate::{ErrorResponse, TokenResponse}; + +/// Authenticates outgoing REST-catalog requests by mutating them in place. +/// +/// Implementations may add headers, rewrite the URL, or do anything else they +/// need before the request is sent. This is the lowest-level auth hook; +/// callers that just need to attach a bearer token should typically implement +/// [`TokenProvider`] and rely on the [`BearerTokenAuthenticator`] adapter. +#[async_trait] +pub trait RequestAuthenticator: Send + Sync + Debug { + /// Apply authentication to the outgoing request. + async fn authenticate_request(&self, req: &mut Request) -> Result<()>; + + /// Discard any cached auth state (tokens, signatures, credentials, etc). + async fn invalidate_cache(&self) -> Result<()>; + + /// Refresh the cached auth state (e.g. a bearer token). + /// + /// Expected flow: + /// 1. Attempt to regenerate the auth state (e.g. fetch a new bearer token). + /// 2a. If regeneration succeeded, use the new state and discard the old state. (e.g. replace the token) + /// 2b. If regeneration failed, keep the old state but surface the error. (e.g. keep the old token) + async fn regenerate_cache(&self) -> Result<()>; +} + +/// Produces bearer tokens for authenticated REST catalog requests. +/// Caching, expiry, and refresh are up to the implementer. +#[async_trait] +pub trait TokenProvider: Send + Sync + Debug { + /// Get a token for the next request. + async fn token(&self) -> Result<String>; + + /// Discard any cached token if it exists. + async fn invalidate(&self) -> Result<()>; + + /// Try generating a new cached token. + async fn regenerate(&self) -> Result<()>; +} + +/// Adapts a [`TokenProvider`] into a [`RequestAuthenticator`] by inserting +/// `Authorization: Bearer <token>` on the outgoing request. +#[derive(Debug, Clone)] +pub struct BearerTokenAuthenticator { + provider: Arc<dyn TokenProvider>, +} Review Comment: This wrapper type lets us share code between the `StaticTokenSession` and `OAuthSession` variants I suggested for #2838. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
