From f2190adfc2963e4067c5facf4656702db8803d58 Mon Sep 17 00:00:00 2001 From: Baptiste Gelez Date: Sun, 21 Oct 2018 17:22:27 +0100 Subject: [PATCH] Add an API endpoint to register apps --- plume-api/src/apps.rs | 13 ++++++++ plume-api/src/lib.rs | 1 + plume-models/src/apps.rs | 64 +++++++++++++++++++++++++++++++++++++++- src/api/apps.rs | 16 ++++++++++ src/api/mod.rs | 1 + src/main.rs | 3 ++ 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 plume-api/src/apps.rs create mode 100644 src/api/apps.rs diff --git a/plume-api/src/apps.rs b/plume-api/src/apps.rs new file mode 100644 index 00000000..099511da --- /dev/null +++ b/plume-api/src/apps.rs @@ -0,0 +1,13 @@ +use canapi::Endpoint; + +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct AppEndpoint { + pub id: Option, + pub name: Option, + pub website: Option, + pub redirect_uri: Option, + pub client_id: Option, + pub client_secret: Option, +} + +api!("/api/v1/apps" => AppEndpoint); diff --git a/plume-api/src/lib.rs b/plume-api/src/lib.rs index a7d66d39..315b6be9 100644 --- a/plume-api/src/lib.rs +++ b/plume-api/src/lib.rs @@ -15,4 +15,5 @@ macro_rules! api { }; } +pub mod apps; pub mod posts; diff --git a/plume-models/src/apps.rs b/plume-models/src/apps.rs index ff398c41..a636c410 100755 --- a/plume-models/src/apps.rs +++ b/plume-models/src/apps.rs @@ -1,7 +1,13 @@ +use canapi::{Error, Provider}; use chrono::NaiveDateTime; +use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods}; +use openssl::rand::rand_bytes; +use plume_api::apps::AppEndpoint; +use Connection; use schema::apps; +#[derive(Clone, Queryable)] pub struct App { pub id: i32, pub name: String, @@ -12,7 +18,63 @@ pub struct App { pub creation_date: NaiveDateTime, } +#[derive(Insertable)] +#[table_name= "apps"] +pub struct NewApp { + pub name: String, + pub client_id: String, + pub client_secret: String, + pub redirect_uri: Option, + pub website: Option, +} + +impl Provider for App { + type Data = AppEndpoint; + + fn get(conn: &Connection, id: i32) -> Result { + unimplemented!() + } + + fn list(conn: &Connection, query: AppEndpoint) -> Vec { + unimplemented!() + } + + fn create(conn: &Connection, data: AppEndpoint) -> Result { + let mut id = [0; 32]; + rand_bytes(&mut id).expect("Error while generating client id"); + let client_id = id.into_iter().fold(String::new(), |res, byte| format!("{}{:x}", res, byte)); + + let mut secret = [0; 32]; + rand_bytes(&mut secret).expect("Error while generating client secret"); + let client_secret = secret.into_iter().fold(String::new(), |res, byte| format!("{}{:x}", res, byte)); + let app = App::insert(conn, NewApp { + name: data.name.expect("App::create: name is required"), + client_id: client_id, + client_secret: client_secret, + redirect_uri: data.redirect_uri, + website: data.website, + }); + + Ok(AppEndpoint { + id: Some(app.id), + name: Some(app.name), + client_id: Some(app.client_id), + client_secret: Some(app.client_secret), + redirect_uri: app.redirect_uri, + website: app.website, + }) + } + + fn update(conn: &Connection, id: i32, new_data: AppEndpoint) -> Result { + unimplemented!() + } + + fn delete(conn: &Connection, id: i32) { + unimplemented!() + } +} + impl App { - get!(apps, App); + get!(apps); insert!(apps, NewApp); } diff --git a/src/api/apps.rs b/src/api/apps.rs new file mode 100644 index 00000000..b69a225d --- /dev/null +++ b/src/api/apps.rs @@ -0,0 +1,16 @@ +use canapi::Provider; +use rocket_contrib::Json; +use serde_json; + +use plume_api::apps::AppEndpoint; +use plume_models::{ + Connection, + db_conn::DbConn, + apps::App, +}; + +#[post("/apps", data = "")] +fn create(conn: DbConn, data: Json) -> Json { + let post = >::create(&*conn, (*data).clone()).ok(); + Json(json!(post)) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 83d81e78..1e1d1680 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1 +1,2 @@ +pub mod apps; pub mod posts; diff --git a/src/main.rs b/src/main.rs index 8a1cec2d..a2395a8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,6 +156,8 @@ fn main() { routes::errors::csrf_violation ]) .mount("/api/v1", routes![ + api::apps::create, + api::posts::get, api::posts::list, ]) @@ -176,6 +178,7 @@ fn main() { ("/@//inbox".to_owned(), "/@//inbox".to_owned(), rocket::http::Method::Post), ("/login".to_owned(), "/login".to_owned(), rocket::http::Method::Post), ("/users/new".to_owned(), "/users/new".to_owned(), rocket::http::Method::Post), + ("/api/v1/".to_owned(), "/api/v1/".to_owned(), rocket::http::Method::Post) ]) .finalize().expect("main: csrf fairing creation error")) .launch();