1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
use anyhow::anyhow;
use anyhow::Result;
use http::header;
use log::debug;
use super::credential::CredentialLoader;
use super::credential::Token;
use super::credential::TokenLoad;
use super::v4;
use crate::request::SignableRequest;
use crate::time::Duration;
/// Builder for Signer.
#[derive(Default)]
pub struct Builder {
scope: Option<String>,
service_account: Option<String>,
credential_path: Option<String>,
credential_content: Option<String>,
allow_anonymous: bool,
disable_load_from_env: bool,
disable_load_from_well_known_location: bool,
disable_load_from_vm_metadata: bool,
customed_token_loader: Option<Box<dyn TokenLoad>>,
}
impl Builder {
/// Specify scope for Signer.
///
/// For example, valid scopes for google cloud services should be
///
/// - read-only: `https://www.googleapis.com/auth/devstorage.read_only`
/// - read-write: `https://www.googleapis.com/auth/devstorage.read_write`
/// - full-control: `https://www.googleapis.com/auth/devstorage.full_control`
///
/// Reference: [Cloud Storage authentication](https://cloud.google.com/storage/docs/authentication)
pub fn scope(&mut self, scope: &str) -> &mut Self {
self.scope = Some(scope.to_string());
self
}
/// Specify service account for Signer.
///
/// If not set, use `default` instead.
pub fn service_account(&mut self, service_account: &str) -> &mut Self {
self.service_account = Some(service_account.to_string());
self
}
/// Load credential from path.
///
/// The credential should be generated by Google Cloud Platform.
///
/// # Notes
///
/// We will load from default credential by default, `credential_path`
/// only used to for user customed credential path.
///
/// Read more in [Authenticating as a service account](https://cloud.google.com/docs/authentication/production)
pub fn credential_path(&mut self, path: &str) -> &mut Self {
self.credential_path = Some(path.to_string());
self
}
/// Load credential from base64 content.
///
/// The credential should be generated by Google Cloud Platform.
///
/// # Notes
///
/// We will load from default credential by default, `credential_content`
/// only used to for user customed credential content.
///
/// Read more in [Authenticating as a service account](https://cloud.google.com/docs/authentication/production)
pub fn credential_content(&mut self, credential: &str) -> &mut Self {
self.credential_content = Some(credential.to_string());
self
}
/// Set customed token loader for builder.
///
/// We will load token from customed_token_loader first if set.
pub fn customed_token_loader(&mut self, f: impl TokenLoad) -> &mut Self {
self.customed_token_loader = Some(Box::new(f));
self
}
/// Use exising information to build a new signer.
///
///
/// The builder should not be used anymore.
pub fn build(&mut self) -> Result<Signer> {
let scope = match &self.scope {
Some(v) => v.clone(),
None => return Err(anyhow!("google signer requires scope, but not set")),
};
let mut cred_loader = if let Some(path) = &self.credential_path {
CredentialLoader::from_path(path)?
} else if let Some(content) = &self.credential_content {
CredentialLoader::from_base64(content)?
} else {
CredentialLoader::default()
};
cred_loader = cred_loader.with_scope(&scope);
if self.disable_load_from_env {
cred_loader = cred_loader.with_disable_env();
}
if self.disable_load_from_well_known_location {
cred_loader = cred_loader.with_disable_well_known_location();
}
if self.disable_load_from_vm_metadata {
cred_loader = cred_loader.with_disable_vm_metadata();
}
if self.allow_anonymous {
cred_loader = cred_loader.with_allow_anonymous();
}
if let Some(acc) = &self.service_account {
cred_loader = cred_loader.with_service_account(acc);
}
if let Some(f) = self.customed_token_loader.take() {
cred_loader = cred_loader.with_customed_token_loader(f);
}
Ok(Signer {
credential_loader: cred_loader,
allow_anonymous: self.allow_anonymous,
})
}
}
/// Singer that implement Google OAuth2 Authentication.
///
/// ## Reference
///
/// - [Authenticating as a service account](https://cloud.google.com/docs/authentication/production)
pub struct Signer {
credential_loader: CredentialLoader,
/// Allow anonymous request if credential is not loaded.
allow_anonymous: bool,
}
impl Signer {
/// Create a builder of Signer.
pub fn builder() -> Builder {
Builder::default()
}
/// Load credential via credential load chain specified while building.
///
/// # Note
///
/// This function should never be exported to avoid credential leaking by
/// mistake.
fn token(&self) -> Option<Token> {
self.credential_loader.load()
}
/// Signing request.
///
/// # Example
///
/// ```no_run
/// use anyhow::Result;
/// use reqsign::GoogleSigner;
/// 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 signer = GoogleSigner::builder()
/// .scope("https://www.googleapis.com/auth/devstorage.read_only")
/// .build()?;
///
/// // 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
/// signer.sign(&mut req)?;
///
/// // 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](https://developers.google.com/identity/protocols/oauth2/service-account#jwt-auth)
pub fn sign(&self, req: &mut impl SignableRequest) -> Result<()> {
if let Some(token) = self.token() {
req.insert_header(header::AUTHORIZATION, {
let mut value: http::HeaderValue =
format!("Bearer {}", token.access_token()).parse()?;
value.set_sensitive(true);
value
})?;
return Ok(());
}
if self.allow_anonymous {
debug!("token not found and anonymous is allowed, skipping signing.");
return Ok(());
}
Err(anyhow!("token not found"))
}
/// Sign the query with a duration.
///
/// # Example
/// ```no_run
/// use time::Duration;
/// use anyhow::Result;
/// use reqsign::GoogleSigner;
/// use reqwest::{Client, Url};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// // Signer will load region and credentials from environment by default.
/// let signer = GoogleSigner::builder()
/// .credential_path("/Users/wolfv/Downloads/noted-throne-361708-bf95cdbf3fea.json")
/// .scope("storage")
/// .build()?;
///
/// // 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
/// signer.sign_query(&mut req, Duration::hours(1))?;
///
/// 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(())
/// }
/// ```
pub fn sign_query(&self, query: &mut impl SignableRequest, duration: Duration) -> Result<()> {
let credentials = self.credential_loader.load_credential().unwrap();
let v4_signer = v4::Signer::builder()
// TODO set this from outside?
.service("storage")
.region("auto")
.credential(credentials)
.build()?;
v4_signer.sign_query(query, duration)
}
}
#[cfg(test)]
mod tests {
use reqwest::blocking::Client;
use super::*;
#[derive(Debug)]
struct TestLoader {
client: Client,
}
impl TokenLoad for TestLoader {
fn load_token(&self) -> Result<Option<Token>> {
self.client.get("https://xuanwo.io").send()?;
Ok(None)
}
}
#[test]
fn test_with_customed_token_loader() -> Result<()> {
let client = Client::default();
let _ = Builder::default()
.scope("test")
.customed_token_loader(TestLoader { client })
.build()?;
Ok(())
}
#[test]
fn test_sign_query() -> Result<()> {
let credential_path = format!(
"{}/testdata/services/google/testbucket_credential.json",
std::env::current_dir()
.expect("current_dir must exist")
.to_string_lossy()
);
let signer = Signer::builder()
.credential_path(&credential_path)
.scope("storage")
.build()?;
let mut req = http::Request::new("");
*req.method_mut() = http::Method::GET;
*req.uri_mut() = "https://storage.googleapis.com/testbucket-reqsign/CONTRIBUTING.md"
.parse()
.expect("url must be valid");
signer.sign_query(&mut req, time::Duration::hours(1))?;
let query = req.query().unwrap();
assert!(query.contains("X-Goog-Algorithm=GOOG4-RSA-SHA256"));
assert!(query.contains("X-Goog-Credential"));
Ok(())
}
}