Merge branch 'auth-refactor'

This commit is contained in:
Sebastian Thiel
2022-10-18 15:50:11 +08:00
8 changed files with 222 additions and 146 deletions

View File

@@ -58,6 +58,8 @@ path = "../${api_name}"
version = "${util.crate_version()}"
% endif
## TODO: Make yup-oauth2 optional
# [features]
# default = ["yup-oauth2"]
% if not cargo.get("is_executable", False):
[features]
yup-oauth2 = ["google-apis-common/yup-oauth2"]
default = ["yup-oauth2"]
% endif

View File

@@ -31,7 +31,7 @@ use tokio::time::sleep;
use tower_service;
use serde::{Serialize, Deserialize};
use crate::{client, client::GetToken, client::oauth2, client::serde_with};
use crate::{client, client::GetToken, client::serde_with};
// ##############
// UTILITIES ###

View File

@@ -49,5 +49,8 @@ pub mod api;
// Re-export the hub type and some basic client structs
pub use api::${hub_type};
pub use client::{Result, Error, Delegate, FieldMask};
// Re-export the yup_oauth2 crate, that is required to call some methods of the hub and the client
pub use client::{Result, Error, Delegate, oauth2, FieldMask};
#[cfg(feature = "yup-oauth2")]
pub use client::oauth2;

View File

@@ -711,24 +711,13 @@ else {
loop {
% if default_scope:
let token = match ${auth_call}.get_token(&self.${api.properties.scopes}.iter().map(String::as_str).collect::<Vec<_>>()[..]).await {
// TODO: remove Ok / Err branches
Ok(Some(token)) => token.clone(),
Ok(None) => {
let err = oauth2::Error::OtherError(anyhow::Error::msg("unknown error occurred while generating oauth2 token"));
match dlg.token(&err) {
Some(token) => token,
None => {
Ok(token) => token,
Err(e) => {
match dlg.token(e) {
Ok(token) => token,
Err(e) => {
${delegate_finish}(false);
return Err(client::Error::MissingToken(err))
}
}
}
Err(err) => {
match dlg.token(&err) {
Some(token) => token,
None => {
${delegate_finish}(false);
return Err(client::Error::MissingToken(err))
return Err(client::Error::MissingToken(e));
}
}
}
@@ -767,11 +756,13 @@ else {
let client = &self.hub.client;
dlg.pre_request();
let mut req_builder = hyper::Request::builder().method(${method_name_to_variant(m.httpMethod)}).uri(url.clone().into_string())
.header(USER_AGENT, self.hub._user_agent.clone())\
% if default_scope:
.header(AUTHORIZATION, format!("Bearer {}", token.as_str()))\
% endif
;
.header(USER_AGENT, self.hub._user_agent.clone());
% if default_scope:
if let Some(token) = token.as_ref() {
req_builder = req_builder.header(AUTHORIZATION, format!("Bearer {}", token));
}
% endif
% if resumable_media_param:
upload_url_from_server = true;
@@ -865,7 +856,8 @@ else {
start_at: if upload_url_from_server { Some(0) } else { None },
auth: &${auth_call},
user_agent: &self.hub._user_agent,
auth_header: format!("Bearer {}", token.as_str()),
// TODO: Check this assumption
auth_header: format!("Bearer {}", token.ok_or_else(|| client::Error::MissingToken("resumable upload requires token".into()))?.as_str()),
url: url_str,
reader: &mut reader,
media_type: reader_mime_type.clone(),