diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2019-09-18 17:50:45 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-09-18 18:42:21 -0700 |
commit | 9d1e2ef1c1682f49ce666a012fad70d50cb4f376 (patch) | |
tree | e773ee567d507739f92b4c2d6d4362e752746e60 | |
parent | a8fec1c8d34292d0ea74690d545db519c1ee8be6 (diff) | |
download | fatcat-9d1e2ef1c1682f49ce666a012fad70d50cb4f376.tar.gz fatcat-9d1e2ef1c1682f49ce666a012fad70d50cb4f376.zip |
rust impl token endpoint (and bump crate version)
-rw-r--r-- | rust/Cargo.lock | 10 | ||||
-rw-r--r-- | rust/Cargo.toml | 2 | ||||
-rw-r--r-- | rust/src/endpoints.rs | 51 |
3 files changed, 57 insertions, 6 deletions
diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 38f1bf29..4db7d119 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -526,7 +526,7 @@ dependencies = [ [[package]] name = "fatcat" -version = "0.3.0" +version = "0.3.1" dependencies = [ "cadence 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -538,17 +538,17 @@ dependencies = [ "dotenv 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "fatcat-openapi 0.3.0", + "fatcat-openapi 0.3.1", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "iron-slog 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "iron-test 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "macaroon 0.1.1 (git+https://github.com/bnewbold/libmacaroon-rs?branch=bnewbold-broken)", "num_cpus 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "sentry 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", @@ -567,7 +567,7 @@ dependencies = [ [[package]] name = "fatcat-openapi" -version = "0.3.0" +version = "0.3.1" dependencies = [ "bodyparser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index e590ca20..2d7d011b 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fatcat" -version = "0.3.0" +version = "0.3.1" edition = "2018" authors = ["Bryan Newbold <bnewbold@archive.org>"] description = "A scalable, versioned, API-oriented catalog for bibliographic entities and file metadata" diff --git a/rust/src/endpoints.rs b/rust/src/endpoints.rs index 2f30a7fa..0dd69efd 100644 --- a/rust/src/endpoints.rs +++ b/rust/src/endpoints.rs @@ -1221,4 +1221,55 @@ impl Api for Server { }; Box::new(futures::done(Ok(ret))) } + + fn create_auth_token( + &self, + editor_id: String, + duration_seconds: Option<i32>, + context: &Context, + ) -> Box<dyn Future<Item = CreateAuthTokenResponse, Error = ApiError> + Send> { + let conn = self.db_pool.get().expect("db_pool error"); + let ret = match conn + .transaction(|| { + let auth_context = self.auth_confectionary.require_auth( + &conn, + &context.auth_data, + Some("create_auth_token"), + )?; + auth_context.require_role(FatcatRole::Superuser)?; + // create an auth token. default to 31 day duration + let duration = match duration_seconds { + Some(seconds) => { + assert!(seconds >= 1); + chrono::Duration::seconds(seconds.into()) + } + None => chrono::Duration::days(31), + }; + // TODO: does logic checking if account is locked happen elsewhere? + let token = self + .auth_confectionary + .create_token(FatcatId::from_str(&editor_id)?, Some(duration))?; + let result = AuthTokenResult { token }; + Ok(result) + }) + .map_err(|e: Error| FatcatError::from(e)) + { + Ok(result) => { + self.metrics.incr("account.create_token").ok(); + CreateAuthTokenResponse::Success(result) + } + Err(fe) => match fe { + InvalidCredentials(_) | InsufficientPrivileges(_) => { + CreateAuthTokenResponse::Forbidden(fe.into()) + } + DatabaseError(_) | InternalError(_) => { + error!("{}", fe); + capture_fail(&fe); + CreateAuthTokenResponse::GenericError(fe.into()) + } + _ => CreateAuthTokenResponse::BadRequest(fe.into()), + }, + }; + Box::new(futures::done(Ok(ret))) + } } |