diff --git a/Cargo.toml b/Cargo.toml index cd5b660..06eecfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,22 +11,23 @@ publish = true exclude = [".github/", ".vscode/", ".editorconfig", ".gitattributes"] +[workspace] +resolver = "2" +members = ["src/*"] + [lib] name = "tribufu" crate-type = ["rlib"] path = "src/lib.rs" +[features] +actix = ["tribufu-actix"] + [dependencies] -base64 = "0.21.5" -mintaka-error = { version = "0.0.1" } -alnilam-consts = { version = "0.0.4" } -anyhow = "1.0.75" -chrono = { version = "0.4.22", features = ["serde", "rustc-serialize"] } -derive_more = "0.99.17" -reqwest = { version = "0.11.18", features = ["json", "stream"] } -serde = { version = "1.0", features = ["derive"] } -serde_json = { version = "1.0", features = ["raw_value"] } -serde_with = "3.4.0" +tribufu-api = { path = "./src/api" } +tribufu-constants = { path = "./src/constants" } +tribufu-types = { path = "./src/types" } +tribufu-actix = { path = "./src/actix", optional = true } [dev-dependencies] dotenv = "0.15.0" diff --git a/src/actix/Cargo.toml b/src/actix/Cargo.toml index 238cd94..9b7a3db 100644 --- a/src/actix/Cargo.toml +++ b/src/actix/Cargo.toml @@ -16,3 +16,5 @@ path = "lib.rs" [dependencies] actix-web = { version = "4", features = ["rustls"] } +mintaka-error = { version = "0.0.1" } +tribufu-api = { path = "../api" } diff --git a/src/actix/lib.rs b/src/actix/lib.rs index ae6c045..2619b3f 100644 --- a/src/actix/lib.rs +++ b/src/actix/lib.rs @@ -1,29 +1,35 @@ // Copyright (c) Tribufu. All Rights Reserved. -/* use actix_web::HttpRequest; +use tribufu_api::TribufuApi; pub trait TribufuApiActixExtension { - fn use_anonymous(req: &HttpRequest) -> Self; + fn from_actix(req: &HttpRequest) -> Self; + fn use_actix(&mut self, req: &HttpRequest); } -impl TribufuApi { - pub fn from_actix(req: &HttpRequest) -> Self { - let mut api = Self::default(); - - if let Some(api_key) = req.headers().get("X-Tribufu-Api-Key") { - api.use_api_key(api_key.to_str().unwrap().to_string()); - } +impl TribufuApiActixExtension for TribufuApi { + fn from_actix(req: &HttpRequest) -> Self { + let mut api = Self::from_env(); + api.use_actix(req); + api + } + fn use_actix(&mut self, req: &HttpRequest) { if let Some(authorization) = req.headers().get("Authorization") { let authorization = authorization.to_str().unwrap(); + if authorization.starts_with("ApiKey ") { + self.use_api_key(authorization[7..].to_string()); + } + + if authorization.starts_with("Basic ") { + self.use_basic(authorization[6..].to_string()); + } + if authorization.starts_with("Bearer ") { - api.use_token(authorization[7..].to_string()); + self.use_bearer(authorization[7..].to_string()); } } - - api } } -*/ diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml new file mode 100644 index 0000000..db97e68 --- /dev/null +++ b/src/api/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "tribufu-api" +version = "0.0.4" +description = "Tribufu API" +repository = "https://github.com/Tribufu/TribufuRust" +authors = ["Tribufu "] +license = "Apache-2.0" +readme = "README.md" +edition = "2021" +publish = true + +[lib] +name = "tribufu_api" +crate-type = ["rlib"] +path = "lib.rs" + +[dependencies] +tribufu-constants = { path = "../constants" } +tribufu-types = { path = "../types" } +base64 = "0.21.5" +mintaka-error = { version = "0.0.1" } +alnilam-consts = { version = "0.0.4" } +anyhow = "1.0.75" +chrono = { version = "0.4.22", features = ["serde", "rustc-serialize"] } +derive_more = "0.99.17" +reqwest = { version = "0.11.18", features = ["json", "stream"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0", features = ["raw_value"] } +serde_with = "3.4.0" diff --git a/src/api.rs b/src/api/lib.rs similarity index 92% rename from src/api.rs rename to src/api/lib.rs index 2397853..d03a09a 100644 --- a/src/api.rs +++ b/src/api/lib.rs @@ -1,17 +1,16 @@ // Copyright (c) Tribufu. All Rights Reserved. -use crate::games::Game; -use crate::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse}; -use crate::users::*; -use crate::VERSION; use alnilam_consts::TARGET_TRIPLE; use base64::engine::general_purpose::STANDARD as BASE64; use base64::Engine as _; -use chrono::{NaiveDateTime, Utc}; use mintaka_error::{Error, Result}; use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; use reqwest::Client; use std::env; +use tribufu_constants::VERSION; +use tribufu_types::games::Game; +use tribufu_types::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse}; +use tribufu_types::users::*; pub enum CredentialsType { Anonymous, @@ -24,8 +23,6 @@ pub struct TribufuApi { base_url: String, credentials: Option, credentials_kind: CredentialsType, - credentials_refreshed_at: Option, - credentials_expires_at: Option, http: Client, } @@ -49,8 +46,6 @@ impl TribufuApi { base_url: Self::BASE_URL.to_string(), credentials, credentials_kind, - credentials_refreshed_at: None, - credentials_expires_at: None, http, } } @@ -89,7 +84,7 @@ impl TribufuApi { pub fn with_token(token: String) -> Self { let mut api = Self::default(); - api.use_token(token); + api.use_bearer(token); api } @@ -105,8 +100,8 @@ impl TribufuApi { self.set_base_url(base_url); } - if let Ok(api_key) = env::var("TRIBUFU_API_KEY") { - self.use_api_key(api_key); + if let Ok(token) = env::var("TRIBUFU_TOKEN") { + self.use_bearer(token); } let client_id = env::var("TRIBUFU_CLIENT_ID"); @@ -116,8 +111,8 @@ impl TribufuApi { self.use_client(client_id.parse().unwrap(), client_secret); } - if let Ok(token) = env::var("TRIBUFU_TOKEN") { - self.use_token(token); + if let Ok(api_key) = env::var("TRIBUFU_API_KEY") { + self.use_api_key(api_key); } } @@ -131,15 +126,19 @@ impl TribufuApi { self.credentials = Some(api_key); } - pub fn use_client(&mut self, client_id: u64, client_secret: String) { - let credentials_str = format!("{}:{}", client_id, client_secret); + pub fn use_basic(&mut self, basic_token: String) { self.credentials_kind = CredentialsType::Basic; - self.credentials = Some(BASE64.encode(credentials_str.as_bytes())); + self.credentials = Some(basic_token); } - pub fn use_token(&mut self, token: String) { + pub fn use_bearer(&mut self, bearer_token: String) { self.credentials_kind = CredentialsType::Bearer; - self.credentials = Some(token); + self.credentials = Some(bearer_token); + } + + pub fn use_client(&mut self, client_id: u64, client_secret: String) { + let credentials_str = format!("{}:{}", client_id, client_secret); + self.use_basic(BASE64.encode(credentials_str.as_bytes())); } fn set_base_url(&mut self, base_url: String) { @@ -288,7 +287,7 @@ impl TribufuApi { } async fn get_oauth_token( - &mut self, + &self, grant_type: OAuth2GrantType, grant_value: Option, client_id: u64, @@ -365,11 +364,7 @@ impl TribufuApi { ))); } - let response_body: OAuth2TokenResponse = response.json().await?; - - self.use_token(response_body.clone().access_token); - - Ok(response_body) + Ok(response.json().await?) } pub async fn get_user_info(&self) -> Result { diff --git a/src/constants/Cargo.toml b/src/constants/Cargo.toml new file mode 100644 index 0000000..1eefc86 --- /dev/null +++ b/src/constants/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "tribufu-constants" +version = "0.0.4" +description = "Tribufu Constants" +repository = "https://github.com/Tribufu/TribufuRust" +authors = ["Tribufu "] +license = "Apache-2.0" +readme = "README.md" +edition = "2021" +publish = true + +[lib] +name = "tribufu_constants" +crate-type = ["rlib"] +path = "lib.rs" + +[dependencies] diff --git a/src/constants/lib.rs b/src/constants/lib.rs new file mode 100644 index 0000000..e0fcb7e --- /dev/null +++ b/src/constants/lib.rs @@ -0,0 +1,3 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/src/lib.rs b/src/lib.rs index 18f6a41..d5ea72e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,5 @@ // Copyright (c) Tribufu. All Rights Reserved. -#![allow(dead_code)] - -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); - -pub mod api; -pub mod games; -pub mod oauth2; -pub mod users; - -pub use api::*; +pub use tribufu_api::*; +pub use tribufu_constants::VERSION; +pub use tribufu_types as types; diff --git a/src/types/Cargo.toml b/src/types/Cargo.toml new file mode 100644 index 0000000..1933c92 --- /dev/null +++ b/src/types/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "tribufu-types" +version = "0.0.4" +description = "Tribufu Types" +repository = "https://github.com/Tribufu/TribufuRust" +authors = ["Tribufu "] +license = "Apache-2.0" +readme = "README.md" +edition = "2021" +publish = true + +[lib] +name = "tribufu_types" +crate-type = ["rlib"] +path = "lib.rs" + +[dependencies] +chrono = { version = "0.4.22", features = ["serde", "rustc-serialize"] } +derive_more = "0.99.17" +mintaka-error = { version = "0.0.1" } +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0", features = ["raw_value"] } +serde_with = "3.4.0" diff --git a/src/games.rs b/src/types/games.rs similarity index 100% rename from src/games.rs rename to src/types/games.rs diff --git a/src/types/lib.rs b/src/types/lib.rs new file mode 100644 index 0000000..b6787df --- /dev/null +++ b/src/types/lib.rs @@ -0,0 +1,5 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +pub mod games; +pub mod oauth2; +pub mod users; diff --git a/src/oauth2.rs b/src/types/oauth2.rs similarity index 99% rename from src/oauth2.rs rename to src/types/oauth2.rs index 9691e73..0c74a83 100644 --- a/src/oauth2.rs +++ b/src/types/oauth2.rs @@ -155,7 +155,7 @@ pub struct OAuth2IntrospectionResponse { } impl OAuth2IntrospectionResponse { - fn inative() -> Self { + pub fn inative() -> Self { Self { active: false, client_id: None, diff --git a/src/users.rs b/src/types/users.rs similarity index 100% rename from src/users.rs rename to src/types/users.rs