Skip to content

Commit

Permalink
fix: error handling improvements & refactor, add CI
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed May 13, 2024
1 parent 023be00 commit 10c114c
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 192 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Cargo checks
on:
push:
pull_request:
jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- run: cargo clippy --workspace --all-features --all-targets -- -D warnings
- run: cargo test --workspace --all-features --all-targets
- run: cargo fmt -- --check
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
description = "HTTP Client for Google OAuth2"
name = "gauth"
version = "0.8.0"
version = "0.9.0"
authors = ["Simon Makarski <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -29,7 +29,7 @@ log = { version = "0.4", optional = true }

[dev-dependencies]
mockito = "1.2.0"
tokio = { version = "1.33.0", features = ["test-util"] }
tokio = { version = "1.33.0", features = ["test-util", "rt", "macros", "rt-multi-thread"] }
env_logger = "0.10.0"

[features]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The library supports the following Google Auth flows:

```toml
[dependencies]
gauth = "0.8"
gauth = "0.9"
```

#### OAuth2
Expand Down Expand Up @@ -45,7 +45,7 @@ It is also possible to make a **blocking call** to retrieve an access token. Thi

```
[dependencies]
gauth = { version = "0.8", features = ["app-blocking"] }
gauth = { version = "0.9", features = ["app-blocking"] }
```

```rust,no_run
Expand Down Expand Up @@ -123,7 +123,7 @@ To resolve this, we adopted an experimental approach by developing a `token_prov

```
[dependencies]
gauth = { version = "0.8", features = ["token-watcher"] }
gauth = { version = "0.9", features = ["token-watcher"] }
```

```rust,no_run
Expand Down
7 changes: 5 additions & 2 deletions examples/async_token_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.nth(1)
.expect("Provide a path to the service account key file");

let service_account =
ServiceAccount::from_file(&keypath, vec!["https://www.googleapis.com/auth/pubsub"]);
let service_account = ServiceAccount::from_file(&keypath)
.unwrap()
.scopes(vec!["https://www.googleapis.com/auth/pubsub"])
.build()
.unwrap();

let tp = AsyncTokenProvider::new(service_account).with_interval(5);

Expand Down
2 changes: 1 addition & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ mod tests {

#[tokio::test]
async fn test_access_token_success() {
let mut google = mockito::Server::new();
let mut google = mockito::Server::new_async().await;
let google_host = google.url();

google
Expand Down
43 changes: 32 additions & 11 deletions src/serv_account/errors.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
use reqwest::StatusCode;
use ring::error::{KeyRejected, Unspecified};
use std::{io, path::PathBuf, result::Result as StdResult};
use std::{io, path::PathBuf};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ServiceAccountError {
pub enum ServiceAccountFromFileError {
#[error("failed to read key file: {0}: {1}")]
ReadKey(PathBuf, io::Error),
ReadFile(PathBuf, io::Error),

#[error("failed to de/serialize to json")]
SerdeJson(#[from] serde_json::Error),
DeserializeFile(#[from] serde_json::Error),

#[error("failed to decode base64")]
Base64Decode(#[from] base64::DecodeError),
#[error("Failed to initialize service account: {0}")]
ServiceAccountInitialization(ServiceAccountBuildError),

#[error("failed to create rsa key pair: {0}")]
RsaKeyPair(KeyRejected),
#[error("Failed to get access token: {0}")]
GetAccessToken(GetAccessTokenError),
}

#[derive(Debug, Error)]
pub enum ServiceAccountBuildError {
#[error("RSA private key didn't start with PEM prefix: -----BEGIN PRIVATE KEY-----")]
RsaPrivateKeyNoPrefix,

#[error("failed to rsa sign: {0}")]
#[error("RSA private key didn't end with PEM suffix: -----END PRIVATE KEY-----")]
RsaPrivateKeyNoSuffix,

#[error("RSA private key could not be decoded as base64: {0}")]
RsaPrivateKeyDecode(base64::DecodeError),

#[error("RSA private key could not be parsed: {0}")]
RsaPrivateKeyParse(KeyRejected),
}

#[derive(Debug, Error)]
pub enum GetAccessTokenError {
#[error("failed to serialize JSON: {0}")]
JsonSerialization(serde_json::Error),

#[error("failed to RSA sign: {0}")]
RsaSign(Unspecified),

#[error("failed to send request")]
Expand All @@ -31,6 +52,6 @@ pub enum ServiceAccountError {

#[error("response returned non-Bearer auth access token: {0}")]
AccessTokenNotBearer(String),
}

pub type Result<T> = StdResult<T, ServiceAccountError>;
// TODO error variant for invalid authentication
}
Loading

0 comments on commit 10c114c

Please sign in to comment.