Skip to content

Commit

Permalink
Improve OAuth related API
Browse files Browse the repository at this point in the history
  • Loading branch information
jutuon committed May 26, 2024
1 parent 52789ae commit 07146e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
14 changes: 8 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod response;

pub mod oauth_yup_oauth2;
mod oauth_yup_oauth2;

use std::path::{Path, PathBuf};
use std::time::Duration;
Expand All @@ -11,14 +11,16 @@ use crate::client::response::FcmResponse;
use crate::message::{Message, MessageWrapper};
use crate::RetryAfter;

use self::oauth_yup_oauth2::{YupOauth2, YupOauth2Error};
use self::oauth_yup_oauth2::OauthClient;

pub use self::oauth_yup_oauth2::OauthError;

#[derive(thiserror::Error, Debug)]
pub enum FcmClientError {
#[error("Reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("OAuth error: {0}")]
Oauth(YupOauth2Error),
Oauth(OauthError),
#[error("Dotenvy error: {0}")]
Dotenvy(#[from] dotenvy::Error),
#[error("Retry-After HTTP header value is not valid string")]
Expand Down Expand Up @@ -97,7 +99,7 @@ impl FcmClientBuilder {
/// An async client for sending the notification payload.
pub struct FcmClient {
http_client: reqwest::Client,
oauth_client: YupOauth2,
oauth_client: OauthClient,
}

impl FcmClient {
Expand All @@ -117,7 +119,7 @@ impl FcmClient {
let http_client = builder.build()?;

let oauth_client = if let Some(key_json) = fcm_builder.service_account_key_json_string {
YupOauth2::create_with_string_key(
OauthClient::create_with_string_key(
key_json,
fcm_builder.token_cache_json_path,
)
Expand All @@ -130,7 +132,7 @@ impl FcmClient {
dotenvy::var("GOOGLE_APPLICATION_CREDENTIALS")?.into()
};

YupOauth2::create_with_key_file(
OauthClient::create_with_key_file(
service_account_key_path,
fcm_builder.token_cache_json_path,
)
Expand Down
32 changes: 16 additions & 16 deletions src/client/oauth_yup_oauth2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use yup_oauth2::ServiceAccountAuthenticator;
const FIREBASE_OAUTH_SCOPE: &str = "https://www.googleapis.com/auth/firebase.messaging";

#[derive(thiserror::Error, Debug)]
pub enum YupOauth2Error {
pub enum OauthError {
#[error("Service account key reading failed: {0}")]
ServiceAccountKeyReadingFailed(std::io::Error),
#[error("OAuth error: {0}")]
Expand All @@ -21,44 +21,44 @@ pub enum YupOauth2Error {
ProjectIdIsMissing,
}

impl YupOauth2Error {
impl OauthError {
/// If this is `true` then most likely current service account
/// key is invalid.
pub(crate) fn is_access_token_missing_even_if_server_requests_completed(&self) -> bool {
matches!(
self,
YupOauth2Error::AccessTokenIsMissing |
YupOauth2Error::Oauth(
OauthError::AccessTokenIsMissing |
OauthError::Oauth(
yup_oauth2::Error::MissingAccessToken |
yup_oauth2::Error::AuthError(_)
)
)
}
}

pub(crate) struct YupOauth2 {
pub(crate) struct OauthClient {
authenticator: Authenticator<HttpsConnector<HttpConnector>>,
project_id: String,
}

impl YupOauth2 {
impl OauthClient {
pub async fn create_with_key_file(
service_account_key_path: PathBuf,
token_cache_json_path: Option<PathBuf>,
) -> Result<Self, YupOauth2Error> {
) -> Result<Self, OauthError> {
let file = tokio::fs::read_to_string(&service_account_key_path).await
.map_err(YupOauth2Error::ServiceAccountKeyReadingFailed)?;
.map_err(OauthError::ServiceAccountKeyReadingFailed)?;
Self::create_with_string_key(file, token_cache_json_path).await
}

pub async fn create_with_string_key(
service_account_key_json_string: String,
token_cache_json_path: Option<PathBuf>,
) -> Result<Self, YupOauth2Error> {
) -> Result<Self, OauthError> {
let key = yup_oauth2::parse_service_account_key(service_account_key_json_string)
.map_err(YupOauth2Error::ServiceAccountKeyReadingFailed)?;
.map_err(OauthError::ServiceAccountKeyReadingFailed)?;
let oauth_client = DefaultHyperClient.build_hyper_client()
.map_err(YupOauth2Error::Oauth)?;
.map_err(OauthError::Oauth)?;
let builder = ServiceAccountAuthenticator::with_client(key.clone(), oauth_client);
let builder = if let Some(path) = token_cache_json_path {
builder.persist_tokens_to_disk(path)
Expand All @@ -67,22 +67,22 @@ impl YupOauth2 {
};
let authenticator = builder.build()
.await
.map_err(YupOauth2Error::AuthenticatorCreatingFailed)?;
.map_err(OauthError::AuthenticatorCreatingFailed)?;

let project_id = key.project_id
.ok_or(YupOauth2Error::ProjectIdIsMissing)?;
.ok_or(OauthError::ProjectIdIsMissing)?;

Ok(YupOauth2 {
Ok(OauthClient {
authenticator,
project_id,
})
}

pub async fn get_access_token(&self) -> Result<String, YupOauth2Error> {
pub async fn get_access_token(&self) -> Result<String, OauthError> {
let scopes = [FIREBASE_OAUTH_SCOPE];
let access_token = self.authenticator.token(&scopes).await?;
let access_token = access_token.token()
.ok_or(YupOauth2Error::AccessTokenIsMissing)?;
.ok_or(OauthError::AccessTokenIsMissing)?;

Ok(access_token.to_string())
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
//! }
//! ```
pub use yup_oauth2;

pub mod message;
pub(crate) mod notification;
pub(crate) mod android;
Expand Down

0 comments on commit 07146e8

Please sign in to comment.