diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-12-27 00:43:31 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-12-27 00:43:31 -0800 |
commit | 946c98593cb5346fff3d1aa72c4992376ec20471 (patch) | |
tree | bc0c2613f95203c7c79903cc754f3db8a450df17 /rust/src | |
parent | 3e591d8d7f64b1092f68737ebd464be69f7e2490 (diff) | |
download | fatcat-946c98593cb5346fff3d1aa72c4992376ec20471.tar.gz fatcat-946c98593cb5346fff3d1aa72c4992376ec20471.zip |
sql codegen and WIP on auth command
Diffstat (limited to 'rust/src')
-rw-r--r-- | rust/src/auth.rs | 49 | ||||
-rw-r--r-- | rust/src/bin/fatcat-auth.rs | 40 | ||||
-rw-r--r-- | rust/src/database_models.rs | 3 | ||||
-rw-r--r-- | rust/src/database_schema.rs | 3 |
4 files changed, 70 insertions, 25 deletions
diff --git a/rust/src/auth.rs b/rust/src/auth.rs index 651f7979..6ded1188 100644 --- a/rust/src/auth.rs +++ b/rust/src/auth.rs @@ -4,17 +4,17 @@ use swagger::auth::{AuthData, Authorization, Scopes}; //use macaroon::{Macaroon, Verifier}; use std::collections::BTreeSet; -//use database_models::*; -//use database_schema::*; +use database_models::*; +use database_schema::*; use api_helpers::*; use chrono; -//use diesel; +use diesel; use iron; -//use diesel::prelude::*; +use diesel::prelude::*; use errors::*; -//use serde_json; -//use std::str::FromStr; -//use uuid::Uuid; +use serde_json; +use std::str::FromStr; +use uuid::Uuid; #[derive(Debug)] pub struct OpenAuthMiddleware; @@ -76,20 +76,41 @@ impl iron::middleware::BeforeMiddleware for MacaroonAuthMiddleware { } // DUMMY: parse macaroon +/// On success, returns Some((editor_id, scopes)), where `scopes` is a vector of strings. pub fn parse_macaroon_token(s: &str) -> Result<Option<(String,Vec<String>)>> { Ok(Some(("some_editor_id".to_string(), vec![]))) } -pub fn print_editors() -> Result<()>{ - unimplemented!(); +pub fn print_editors(conn: &DbConn) -> Result<()>{ // iterate over all editors. format id, print flags, auth_epoch + let all_editors: Vec<EditorRow> = editor::table + .load(conn)?; + println!("editor_id\t\t\tis_admin/is_bot\tauth_epoch\t\t\tusername\twrangler_id"); + for e in all_editors { + println!("{}\t{}\t{}\t{}\t{}\t{:?}", + FatCatId::from_uuid(&e.id).to_string(), + e.is_admin, + e.is_bot, + e.auth_epoch, + e.username, + e.wrangler_id, + ); + } + Ok(()) } -pub fn create_editor(username: String, is_admin: bool, is_bot: bool) -> Result<()> { // TODO: EditorRow or something - unimplemented!(); +pub fn create_editor(conn: &DbConn, username: String, is_admin: bool, is_bot: bool) -> Result<EditorRow> { + let ed: EditorRow = diesel::insert_into(editor::table) + .values(( + editor::username.eq(username), + editor::is_admin.eq(is_admin), + editor::is_bot.eq(is_bot), + )) + .get_result(conn)?; + Ok(ed) } -pub fn create_token(editor_id: FatCatId, expires: Option<chrono::NaiveDateTime>) -> Result<String> { +pub fn create_token(conn: &DbConn, editor_id: FatCatId, expires: Option<chrono::NaiveDateTime>) -> Result<String> { unimplemented!(); } @@ -97,10 +118,10 @@ pub fn inspect_token(token: &str) -> Result<()> { unimplemented!(); } -pub fn revoke_tokens(editor_id: FatCatId) -> Result<()>{ +pub fn revoke_tokens(conn: &DbConn, editor_id: FatCatId) -> Result<()>{ unimplemented!(); } -pub fn revoke_tokens_everyone() -> Result<u64> { +pub fn revoke_tokens_everyone(conn: &DbConn) -> Result<u64> { unimplemented!(); } diff --git a/rust/src/bin/fatcat-auth.rs b/rust/src/bin/fatcat-auth.rs index 7cb8af8e..a5fedc1f 100644 --- a/rust/src/bin/fatcat-auth.rs +++ b/rust/src/bin/fatcat-auth.rs @@ -2,6 +2,7 @@ #[macro_use] extern crate clap; +extern crate diesel; extern crate dotenv; #[macro_use] extern crate error_chain; @@ -16,6 +17,9 @@ use clap::{App, Arg, SubCommand}; use dotenv::dotenv; use std::env; +use diesel::prelude::*; +use diesel::r2d2::ConnectionManager; +use fatcat::ConnectionPool; use fatcat::errors::*; use fatcat::api_helpers::FatCatId; use std::str::FromStr; @@ -27,6 +31,18 @@ use std::io::prelude::*; use std::io::{BufReader, BufWriter}; +/// Instantiate a new API server with a pooled database connection +// TODO: copypasta from fatcat-export +pub fn database_worker_pool() -> Result<ConnectionPool> { + dotenv().ok(); + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let manager = ConnectionManager::<PgConnection>::new(database_url); + let pool = diesel::r2d2::Pool::builder() + .build(manager) + .expect("Failed to create database pool."); + Ok(pool) +} + fn run() -> Result<()> { let m = App::new("fatcat-auth") .version(env!("CARGO_PKG_VERSION")) @@ -67,35 +83,37 @@ fn run() -> Result<()> { ) .get_matches(); -/* - value_t_or_exit!(subm, "magic", u32) - .after_help("Reads a ident table TSV dump from stdin (aka, ident_id, rev_id, redirect_id), \ - and outputs JSON (one entity per line). Database connection info read from environment \ - (DATABASE_URL, same as fatcatd).") -*/ match m.subcommand() { ("list-editors", Some(_subm)) => { - fatcat::auth::print_editors()?; + let db_conn = database_worker_pool()?.get().expect("database pool"); + fatcat::auth::print_editors(&db_conn)?; }, ("create-editor", Some(subm)) => { - fatcat::auth::create_editor( + let db_conn = database_worker_pool()?.get().expect("database pool"); + let editor = fatcat::auth::create_editor( + &db_conn, subm.value_of("username").unwrap().to_string(), subm.is_present("admin"), subm.is_present("bot"))?; + //println!("{:?}", editor); + println!("{}", FatCatId::from_uuid(&editor.id).to_string()); }, ("create-token", Some(subm)) => { + let db_conn = database_worker_pool()?.get().expect("database pool"); let editor_id = FatCatId::from_str(subm.value_of("editor").unwrap())?; - fatcat::auth::create_token(editor_id, None)?; + fatcat::auth::create_token(&db_conn, editor_id, None)?; }, ("inspect-token", Some(subm)) => { fatcat::auth::inspect_token(subm.value_of("token").unwrap())?; }, ("revoke-tokens", Some(subm)) => { + let db_conn = database_worker_pool()?.get().expect("database pool"); let editor_id = FatCatId::from_str(subm.value_of("editor").unwrap())?; - fatcat::auth::revoke_tokens(editor_id)?; + fatcat::auth::revoke_tokens(&db_conn, editor_id)?; }, ("revoke-tokens-everyone", Some(_subm)) => { - fatcat::auth::revoke_tokens_everyone()?; + let db_conn = database_worker_pool()?.get().expect("database pool"); + fatcat::auth::revoke_tokens_everyone(&db_conn)?; }, _ => { println!("Missing or unimplemented command!"); diff --git a/rust/src/database_models.rs b/rust/src/database_models.rs index fc5fc896..55ba7fb9 100644 --- a/rust/src/database_models.rs +++ b/rust/src/database_models.rs @@ -578,7 +578,10 @@ pub struct EditorRow { pub id: Uuid, pub username: String, pub is_admin: bool, + pub is_bot: bool, pub registered: chrono::NaiveDateTime, + pub auth_epoch: chrono::NaiveDateTime, + pub wrangler_id: Option<Uuid>, pub active_editgroup_id: Option<Uuid>, } diff --git a/rust/src/database_schema.rs b/rust/src/database_schema.rs index 2777696d..c240048e 100644 --- a/rust/src/database_schema.rs +++ b/rust/src/database_schema.rs @@ -97,7 +97,10 @@ table! { id -> Uuid, username -> Text, is_admin -> Bool, + is_bot -> Bool, registered -> Timestamptz, + auth_epoch -> Timestamptz, + wrangler_id -> Nullable<Uuid>, active_editgroup_id -> Nullable<Uuid>, } } |