ublubu commented on code in PR #2924: URL: https://github.com/apache/iceberg-rust/pull/2924#discussion_r3677171098
########## 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<()>; +} Review Comment: This is roughly equivalent to #2838's `AuthSession`. -- 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]
