Some more improvements to reduce unnecessary allocations.

This commit is contained in:
Glenn Griffin
2019-11-08 14:10:46 -08:00
parent bf0136067f
commit 29f800ba7f
6 changed files with 26 additions and 27 deletions

View File

@@ -211,11 +211,10 @@ where
return Ok(t);
}
// Implement refresh flow.
let refresh_token = t.refresh_token.clone();
let rr = RefreshFlow::refresh_token(
client,
appsecret,
refresh_token.unwrap(),
&t.refresh_token.as_ref().unwrap(),
)
.await?;
match rr {

View File

@@ -142,7 +142,7 @@ where
let (pollinf, device_code) = Self::request_code(
application_secret,
client.clone(),
self.device_code_url.clone(),
&self.device_code_url,
scopes,
)
.await?;
@@ -155,7 +155,7 @@ where
let r = Self::poll_token(
application_secret,
client.clone(),
device_code.clone(),
&device_code,
pollinf.clone(),
fd.clone(),
)
@@ -196,7 +196,7 @@ where
async fn request_code<T>(
application_secret: &ApplicationSecret,
client: hyper::Client<C>,
device_code_url: String,
device_code_url: &str,
scopes: &[T],
) -> Result<(PollInformation, String), RequestError>
where
@@ -278,7 +278,7 @@ where
async fn poll_token<'a>(
application_secret: &ApplicationSecret,
client: hyper::Client<C>,
device_code: String,
device_code: &str,
pi: PollInformation,
fd: FD,
) -> Result<Option<Token>, PollError> {
@@ -292,7 +292,7 @@ where
.extend_pairs(&[
("client_id", application_secret.client_id.as_str()),
("client_secret", application_secret.client_secret.as_str()),
("code", &device_code),
("code", device_code),
("grant_type", "http://oauth.net/grant_type/device/1.0"),
])
.finish();

View File

@@ -214,7 +214,7 @@ where
}
_ => return Err(RequestError::UserError("couldn't read code".to_string())),
};
self.exchange_auth_code(authcode, None).await
self.exchange_auth_code(&authcode, None).await
}
async fn ask_auth_code_via_http<T>(
@@ -249,12 +249,12 @@ where
.await;
let auth_code = server.wait_for_auth_code().await;
self.exchange_auth_code(auth_code, Some(bound_port)).await
self.exchange_auth_code(&auth_code, Some(bound_port)).await
}
async fn exchange_auth_code(
&self,
authcode: String,
authcode: &str,
port: Option<u16>,
) -> Result<Token, RequestError> {
let appsec = &self.appsecret;
@@ -307,7 +307,7 @@ where
/// Sends the authorization code to the provider in order to obtain access and refresh tokens.
fn request_token<'a>(
appsecret: &ApplicationSecret,
authcode: String,
authcode: &str,
custom_redirect_uri: Option<&str>,
port: Option<u16>,
) -> hyper::Request<hyper::Body> {
@@ -320,7 +320,7 @@ where
let body = form_urlencoded::Serializer::new(String::new())
.extend_pairs(vec![
("code", authcode.as_str()),
("code", authcode),
("client_id", appsecret.client_id.as_str()),
("client_secret", appsecret.client_secret.as_str()),
("redirect_uri", redirect_uri.as_ref()),

View File

@@ -33,14 +33,14 @@ impl RefreshFlow {
pub async fn refresh_token<C: 'static + hyper::client::connect::Connect>(
client: &hyper::Client<C>,
client_secret: &ApplicationSecret,
refresh_token: String,
refresh_token: &str,
) -> Result<RefreshResult, RequestError> {
// TODO: Does this function ever return RequestError? Maybe have it just return RefreshResult.
let req = form_urlencoded::Serializer::new(String::new())
.extend_pairs(&[
("client_id", client_secret.client_id.as_str()),
("client_secret", client_secret.client_secret.as_str()),
("refresh_token", refresh_token.as_str()),
("refresh_token", refresh_token),
("grant_type", "refresh_token"),
])
.finish();
@@ -106,7 +106,7 @@ mod tests {
let app_secret = r#"{"installed":{"client_id":"902216714886-k2v9uei3p1dk6h686jbsn9mo96tnbvto.apps.googleusercontent.com","project_id":"yup-test-243420","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"iuMPN6Ne1PD7cos29Tk9rlqH","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}"#;
let mut app_secret = helper::parse_application_secret(app_secret).unwrap();
app_secret.token_uri = format!("{}/token", server_url);
let refresh_token = "my-refresh-token".to_string();
let refresh_token = "my-refresh-token";
let https = HttpsConnector::new();
let client = hyper::Client::builder()
@@ -131,7 +131,7 @@ mod tests {
let rr = RefreshFlow::refresh_token(
&client,
&app_secret,
refresh_token.clone(),
refresh_token,
)
.await
.unwrap();

View File

@@ -270,16 +270,16 @@ where
{
/// Send a request for a new Bearer token to the OAuth provider.
async fn request_token<T>(
client: hyper::client::Client<C>,
sub: Option<String>,
key: ServiceAccountKey,
client: &hyper::client::Client<C>,
sub: Option<&str>,
key: &ServiceAccountKey,
scopes: &[T],
) -> Result<Token, RequestError>
where
T: AsRef<str>,
{
let mut claims = init_claims_from_key(&key, scopes);
claims.sub = sub.clone();
claims.sub = sub.map(|x| x.to_owned());
let signed = JWT::new(claims)
.sign(key.private_key.as_ref().unwrap())
.map_err(RequestError::LowLevelError)?;
@@ -289,7 +289,7 @@ where
("assertion".to_string(), signed),
])
.finish();
let request = hyper::Request::post(key.token_uri.unwrap())
let request = hyper::Request::post(key.token_uri.as_ref().unwrap())
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
.body(hyper::Body::from(rqbody))
.unwrap();
@@ -341,7 +341,7 @@ where
T: AsRef<str>,
{
let hash = hash_scopes(scopes);
let cache = self.cache.clone();
let cache = &self.cache;
match cache
.lock()
.unwrap()
@@ -351,9 +351,9 @@ where
_ => {}
}
let token = Self::request_token(
self.client.clone(),
self.sub.clone(),
self.key.clone(),
&self.client,
self.sub.as_ref().map(|x| x.as_str()),
&self.key,
scopes,
)
.await?;

View File

@@ -113,7 +113,7 @@ impl TokenStorage for MemoryStorage {
tokens.push(JSONToken {
hash: scope_hash,
scopes: Some(scopes.into_iter().map(|x| x.as_ref().to_string()).collect()),
token: t.clone(),
token: t,
});
()
}
@@ -244,7 +244,7 @@ impl TokenStorage for DiskTokenStorage {
tokens.push(JSONToken {
hash: scope_hash,
scopes: Some(scopes.into_iter().map(|x| x.as_ref().to_string()).collect()),
token: t.clone(),
token: t,
});
()
}