From e09449b870e9ba5af5a4c9ec2425e93512db6e52 Mon Sep 17 00:00:00 2001 From: Abdulla Abdurakhmanov Date: Mon, 1 Jan 2024 20:56:23 +0100 Subject: [PATCH] Support for provided external token source (#152) * Support for provided external token source * Fixed clippy warning * Example fix * Fixed warnings in tests * Fixed warnings in tests --- Cargo.toml | 3 +- examples/token_auth.rs | 65 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++- tests/common/mod.rs | 1 + 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 examples/token_auth.rs diff --git a/Cargo.toml b/Cargo.toml index d1a94ae..411008d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ caching-persistent = ["caching", "dep:redb"] [dependencies] tracing = "0.1" -gcloud-sdk = { version = "0.23", features = ["google-firestore-v1"] } +gcloud-sdk = { version = "0.24", features = ["google-firestore-v1"] } hyper = { version ="0.14" } struct-path = "0.2" rvstruct = "0.3.2" @@ -71,4 +71,3 @@ required-features = ["caching-memory"] name = "caching_persistent_test" path = "tests/caching_persistent_test.rs" required-features = ["caching-persistent"] - diff --git a/examples/token_auth.rs b/examples/token_auth.rs new file mode 100644 index 0000000..429ab67 --- /dev/null +++ b/examples/token_auth.rs @@ -0,0 +1,65 @@ +use chrono::{DateTime, Utc}; +use firestore::*; +use futures::stream::BoxStream; +use futures::TryStreamExt; +use serde::{Deserialize, Serialize}; +use std::ops::Add; + +pub fn config_env_var(name: &str) -> Result { + std::env::var(name).map_err(|e| format!("{}: {}", name, e)) +} + +// Example structure to play with +#[derive(Debug, Clone, Deserialize, Serialize)] +struct MyTestStructure { + some_id: String, + some_string: String, + one_more_string: String, + some_num: u64, + created_at: DateTime, +} + +async fn my_token() -> gcloud_sdk::error::Result { + Ok(gcloud_sdk::Token::new( + "Bearer".to_string(), + config_env_var("TOKEN_VALUE") + .expect("TOKEN_VALUE must be specified") + .into(), + chrono::Utc::now().add(std::time::Duration::from_secs(3600)), + )) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + // Logging with debug enabled + let subscriber = tracing_subscriber::fmt() + .with_env_filter("firestore=debug") + .finish(); + tracing::subscriber::set_global_default(subscriber)?; + + // Create an instance + let db = FirestoreDb::with_options_token_source( + FirestoreDbOptions::new(config_env_var("PROJECT_ID")?), + gcloud_sdk::GCP_DEFAULT_SCOPES.clone(), + gcloud_sdk::TokenSourceType::ExternalSource(Box::new( + gcloud_sdk::ExternalJwtFunctionSource::new(my_token), + )), + ) + .await?; + + const TEST_COLLECTION_NAME: &'static str = "test-query"; + + // Query as a stream our data + let object_stream: BoxStream> = db + .fluent() + .select() + .from(TEST_COLLECTION_NAME) + .obj() + .stream_query_with_errors() + .await?; + + let as_vec: Vec = object_stream.try_collect().await?; + println!("{:?}", as_vec); + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index 70f0280..2c14727 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -135,11 +135,14 @@ mod firestore_serde; pub use firestore_serde::*; mod struct_path_macro; -use crate::errors::FirestoreError; + +#[allow(unused_imports)] pub use struct_path_macro::*; pub mod timestamp_utils; +use crate::errors::FirestoreError; + pub type FirestoreResult = std::result::Result; pub type FirestoreDocument = gcloud_sdk::google::firestore::v1::Document; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index b1ea056..9578a63 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -89,6 +89,7 @@ pub struct CustomUserError { details: String, } +#[allow(dead_code)] impl CustomUserError { pub fn new(msg: &str) -> CustomUserError { CustomUserError {