From a871b4e2227ad67260d227cb3b16e1d1509b8ea3 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 30 Oct 2020 20:18:38 -0400 Subject: [PATCH] rename to wasmcloud, do gitea auth on /login/gitea --- Cargo.lock | 2 +- Cargo.toml | 2 +- Rocket.toml.example | 2 +- shell.nix | 2 +- src/api/handler.rs | 28 +++++++++++++++------------- src/gitea.rs | 11 ++++++----- src/main.rs | 2 +- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f40fab..dc1985c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3139,7 +3139,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" [[package]] -name = "wasmcloud-api" +name = "wasmcloud" version = "0.1.0" dependencies = [ "blake3", diff --git a/Cargo.toml b/Cargo.toml index 0ac1ea8..b1dcac3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "wasmcloud-api" +name = "wasmcloud" version = "0.1.0" authors = ["Christine Dodrill "] edition = "2018" diff --git a/Rocket.toml.example b/Rocket.toml.example index 4fdba6a..815171b 100644 --- a/Rocket.toml.example +++ b/Rocket.toml.example @@ -6,4 +6,4 @@ forms = 5242880 provider = { auth_uri = "https://tulpa.dev/login/oauth/authorize", token_uri = "https://tulpa.dev/login/oauth/access_token" } client_id = "..." client_secret = "..." -redirect_uri = "http://localhost:8000/auth/gitea" \ No newline at end of file +redirect_uri = "http://localhost:8000/login/gitea/callback" \ No newline at end of file diff --git a/shell.nix b/shell.nix index a6ef57d..b7b86f4 100644 --- a/shell.nix +++ b/shell.nix @@ -15,7 +15,7 @@ in pkgs.mkShell rec { B2_CREDFILE = "./var/secret/b2-creds.txt"; B2_MODULE_BUCKET_NAME = "wasmcloud-modules"; - RUST_LOG = "info,wasmcloud_api=debug"; + RUST_LOG = "info,wasmcloud=debug"; DATABASE_URL = "postgresql://postgres:hunter2@localhost:5432/wasmcloud"; ROCKET_DATABASES = ''{ main_data = { url = "${DATABASE_URL}" } }''; JWT_SECRET = "hunter2"; diff --git a/src/api/handler.rs b/src/api/handler.rs index 5f46515..5ee82e0 100644 --- a/src/api/handler.rs +++ b/src/api/handler.rs @@ -2,7 +2,6 @@ use super::{Error, Result}; use crate::{b2, models, schema, MainDatabase}; use chrono::prelude::*; use diesel::prelude::*; -use rocket::http::ContentType; use rocket_contrib::{json::Json, uuid::Uuid}; use rocket_upload::MultipartDatas; use schema::handlers::dsl::*; @@ -33,7 +32,11 @@ pub fn create( .get_result::(&*conn) .map_err(Error::Database)?; - info!("created handler {} with id {}", hdl.human_name, hdl.id); + info!( + handler.id = &hdl.id.to_string()[..], + handler.name = &hdl.human_name[..], + "created handler" + ); Ok(Json(hdl)) } @@ -50,9 +53,9 @@ pub fn list(user: models::User, conn: MainDatabase) -> Result")] -pub fn get(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result> { - let uuid = uuid.into_inner(); +#[get("/handler/")] +pub fn get(user: models::User, hdl_id: Uuid, conn: MainDatabase) -> Result> { + let uuid = hdl_id.into_inner(); let handler = handlers .find(uuid) .get_result::(&*conn) @@ -66,9 +69,9 @@ pub fn get(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result")] -pub fn delete(user: models::User, uuid: Uuid, conn: MainDatabase) -> Result { - let uuid = uuid.into_inner(); +#[delete("/handler/")] +pub fn delete(user: models::User, hdl_id: Uuid, conn: MainDatabase) -> Result { + let uuid = hdl_id.into_inner(); let hdl: models::Handler = handlers .find(uuid.clone()) @@ -122,15 +125,15 @@ pub struct Cfg { } #[instrument(skip(conn, cfg), err)] -#[post("/handler//config", format = "json", data = "")] +#[post("/handler//config", format = "json", data = "")] pub fn create_config( user: models::User, - handler_id_str: Uuid, + hdl_id: Uuid, cfg: Json>, conn: MainDatabase, ) -> Result { use schema::handler_config::table; - let uuid = handler_id_str.into_inner(); + let uuid = hdl_id.into_inner(); let handler = handlers .find(uuid) @@ -163,12 +166,11 @@ pub fn create_config( Ok(()) } -#[instrument(skip(conn, data, ct), err)] +#[instrument(skip(conn, data), err)] #[post("/handler//upload", data = "")] pub fn upload_version( user: models::User, hdl_id: Uuid, - ct: &ContentType, data: MultipartDatas, conn: MainDatabase, ) -> Result> { diff --git a/src/gitea.rs b/src/gitea.rs index 501c87f..769ffed 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -1,11 +1,11 @@ -use crate::{MainDatabase, Gitea, models, jwt, schema, api}; -use serde::{Deserialize, Serialize}; +use crate::{api, jwt, models, schema, Gitea, MainDatabase}; use diesel::prelude::*; use rocket::{ http::{Cookie, Cookies, SameSite}, response::Redirect, }; use rocket_oauth2::{OAuth2, TokenResponse}; +use serde::{Deserialize, Serialize}; /// A user. /// https://try.gitea.io/api/swagger#model-User @@ -22,9 +22,10 @@ pub struct User { pub login: String, } -pub fn user(token: String) -> std::io::Result { +fn user(token: String) -> std::io::Result { let resp = ureq::get("https://tulpa.dev/api/v1/user") .set("Authorization", &format!("bearer {}", token)) + .set("User-Agent", crate::APP_USER_AGENT) .call(); if !resp.ok() { todo!("error here"); @@ -34,13 +35,13 @@ pub fn user(token: String) -> std::io::Result { } #[instrument(skip(oauth2, cookies))] -#[get("/login/gitea")] +#[get("/")] pub fn login(oauth2: OAuth2, mut cookies: Cookies<'_>) -> Redirect { oauth2.get_redirect(&mut cookies, &[""]).unwrap() } #[instrument(skip(conn, token, cookies), err)] -#[get("/auth/gitea")] +#[get("/callback")] pub fn callback( conn: MainDatabase, token: TokenResponse, diff --git a/src/main.rs b/src/main.rs index 6e7258e..b7d1b60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,7 +65,7 @@ fn main() -> Result<()> { api::token::create, ], ) - .mount("/", routes![gitea::login, gitea::callback]) + .mount("/login/gitea", routes![gitea::login, gitea::callback]) .launch(); Ok(())