Struct reqsign::GoogleSigner
source · pub struct GoogleSigner { /* private fields */ }
Expand description
Implementations§
source§impl Signer
impl Signer
sourcepub fn region(&mut self, region: &str) -> &mut Self
pub fn region(&mut self, region: &str) -> &mut Self
Set the region name that used for google v4 signing.
Default to auto
sourcepub fn sign(&self, req: &mut impl SignableRequest, token: &Token) -> Result<()>
pub fn sign(&self, req: &mut impl SignableRequest, token: &Token) -> Result<()>
Signing request.
§Example
use anyhow::Result;
use reqsign::GoogleSigner;
use reqsign::GoogleTokenLoader;
use reqwest::Client;
use reqwest::Request;
use reqwest::Url;
#[tokio::main]
async fn main() -> Result<()> {
// Signer will load region and credentials from environment by default.
let token_loader = GoogleTokenLoader::new(
"https://www.googleapis.com/auth/devstorage.read_only",
Client::new(),
);
let signer = GoogleSigner::new("storage");
// Construct request
let url = Url::parse("https://storage.googleapis.com/storage/v1/b/test")?;
let mut req = reqwest::Request::new(http::Method::GET, url);
// Signing request with Signer
let token = token_loader.load().await?.unwrap();
signer.sign(&mut req, &token)?;
// Sending already signed request.
let resp = Client::new().execute(req).await?;
println!("resp got status: {}", resp.status());
Ok(())
}
§TODO
we can also send API via signed JWT: Addendum: Service account authorization without OAuth
sourcepub fn sign_query(
&self,
req: &mut impl SignableRequest,
duration: Duration,
cred: &Credential,
) -> Result<()>
pub fn sign_query( &self, req: &mut impl SignableRequest, duration: Duration, cred: &Credential, ) -> Result<()>
Sign the query with a duration.
§Example
use std::time::Duration;
use anyhow::Result;
use reqsign::GoogleCredentialLoader;
use reqsign::GoogleSigner;
use reqwest::Client;
use reqwest::Url;
#[tokio::main]
async fn main() -> Result<()> {
// Signer will load region and credentials from environment by default.
let credential_loader = GoogleCredentialLoader::default();
let signer = GoogleSigner::new("stroage");
// Construct request
let url = Url::parse("https://storage.googleapis.com/testbucket-reqsign/CONTRIBUTING.md")?;
let mut req = reqwest::Request::new(http::Method::GET, url);
// Signing request with Signer
let credential = credential_loader.load()?.unwrap();
signer.sign_query(&mut req, Duration::from_secs(3600), &credential)?;
println!("signed request: {:?}", req);
// Sending already signed request.
let resp = Client::new().execute(req).await?;
println!("resp got status: {}", resp.status());
println!("resp got body: {}", resp.text().await?);
Ok(())
}
Auto Trait Implementations§
impl Freeze for Signer
impl RefUnwindSafe for Signer
impl Send for Signer
impl Sync for Signer
impl Unpin for Signer
impl UnwindSafe for Signer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more