From e1fa45e39b3df847548c73e029d4a56618ae75fb Mon Sep 17 00:00:00 2001 From: Jett Chen Date: Thu, 21 Nov 2024 17:12:30 +0800 Subject: [PATCH] Add wrapper methods for grpc server --- Cargo.lock | 11 ++--- burrow/src/auth/server/db.rs | 25 +++++++++++ burrow/src/auth/server/grpc_server.rs | 8 ++++ burrow/src/auth/server/providers/mod.rs | 10 +++++ proto/burrowweb.proto | 60 ++++++++++++++++++++++--- proto/wireguard.proto | 53 ---------------------- 6 files changed, 103 insertions(+), 64 deletions(-) delete mode 100644 proto/wireguard.proto diff --git a/Cargo.lock b/Cargo.lock index a5554fb..375f2e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -455,7 +455,7 @@ dependencies = [ "tokio", "tokio-stream", "toml", - "tonic 0.12.2", + "tonic 0.12.3", "tonic-build", "tower", "tracing", @@ -3074,9 +3074,9 @@ dependencies = [ [[package]] name = "tonic" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f6ba989e4b2c58ae83d862d3a3e27690b6e3ae630d0deb59f3697f32aa88ad" +checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" dependencies = [ "async-stream 0.3.5", "async-trait", @@ -3104,13 +3104,14 @@ dependencies = [ [[package]] name = "tonic-build" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe4ee8877250136bd7e3d2331632810a4df4ea5e004656990d8d66d2f5ee8a67" +checksum = "9557ce109ea773b399c9b9e5dca39294110b74f1f342cb347a80d1fce8c26a11" dependencies = [ "prettyplease", "proc-macro2", "prost-build", + "prost-types 0.13.2", "quote", "syn 2.0.77", ] diff --git a/burrow/src/auth/server/db.rs b/burrow/src/auth/server/db.rs index 995e64b..8d3e742 100644 --- a/burrow/src/auth/server/db.rs +++ b/burrow/src/auth/server/db.rs @@ -89,3 +89,28 @@ pub fn store_device( Ok(()) } + +pub fn delete_device(id: i64) -> Result<()> { + let conn = rusqlite::Connection::open(PATH)?; + + conn.execute("DELETE FROM device WHERE id = ?", [id])?; + + Ok(()) +} + +pub fn list_devices(user_id: i64) -> Result> { + let conn = rusqlite::Connection::open(PATH)?; + let mut stmt = conn.prepare("SELECT name FROM device WHERE user_id = ?")?; + + let devices = stmt.query_map([user_id], |row| { + let name: String = row.get(0)?; + Ok(name) + })?; + + let mut result = Vec::new(); + for device in devices { + result.push(device?); + } + + Ok(result) +} diff --git a/burrow/src/auth/server/grpc_server.rs b/burrow/src/auth/server/grpc_server.rs index 23f2f6b..1e076c4 100644 --- a/burrow/src/auth/server/grpc_server.rs +++ b/burrow/src/auth/server/grpc_server.rs @@ -1,5 +1,7 @@ use tonic::{Request, Response, Status}; +use crate::auth::server::providers::OpenIdUser; + use super::{ grpc_defs::{ burrowwebrpc::burrow_web_server::{BurrowWeb, BurrowWebServer}, @@ -25,6 +27,12 @@ impl BurrowWeb for BurrowGrpcServer { &self, request: Request, ) -> Result, Status> { + let req = request.into_inner(); + let jwt = req + .jwt + .ok_or(Status::invalid_argument("JWT Not existent!"))?; + let oid_user = + OpenIdUser::try_from(&jwt).map_err(|e| Status::invalid_argument(e.to_string()))?; unimplemented!() } diff --git a/burrow/src/auth/server/providers/mod.rs b/burrow/src/auth/server/providers/mod.rs index 9901c87..89c3fad 100644 --- a/burrow/src/auth/server/providers/mod.rs +++ b/burrow/src/auth/server/providers/mod.rs @@ -1,8 +1,18 @@ pub mod slack; pub use super::{db, grpc_defs}; +use anyhow::Result; +use grpc_defs::JwtInfo; #[derive(serde::Deserialize, Default, Debug)] pub struct OpenIdUser { pub sub: String, pub name: String, } + +impl TryFrom<&JwtInfo> for OpenIdUser { + type Error = anyhow::Error; + + fn try_from(jwt_info: &JwtInfo) -> Result { + todo!() + } +} diff --git a/proto/burrowweb.proto b/proto/burrowweb.proto index 317d5aa..f9cfe85 100644 --- a/proto/burrowweb.proto +++ b/proto/burrowweb.proto @@ -2,9 +2,6 @@ syntax = "proto3"; package burrowweb; -import "wireguard.proto"; - - // TODO: Frontend sends slack token → receive JWT // TODO: create/delete/list routes @@ -17,13 +14,64 @@ service BurrowWeb { rpc ListDevices (JWTInfo) returns (ListDevicesResponse); } +message Peer { + string public_key = 1; + optional string preshared_key = 2; + repeated string allowed_ips = 3; + string endpoint = 4; + optional uint32 persistent_keepalive = 5; + optional string name = 6; +} + +message InterfaceConfig { + // Does not include private key; the client is responsible for generating & persisting that + repeated string address = 1; + optional uint32 listen_port = 2; + repeated string dns = 3; + optional uint32 mtu = 4; +} + +message Device { + int32 id = 1; + optional string name = 2; + string public_key = 3; + optional string apns_token = 4; + int32 user_id = 5; + string created_at = 6; + string ipv4 = 7; + string ipv6 = 8; + string access_token = 9; + string refresh_token = 10; + string expires_at = 11; +} + +message User { + int32 id = 1; + string created_at = 2; +} + +message UserConnection { + int32 user_id = 1; + string openid_provider = 2; + string openid_user_id = 3; + string openid_user_name = 4; + string access_token = 5; + string refresh_token = 6; +} + + +message Config { + InterfaceConfig interface = 1; + repeated Peer peers = 2; +} + + message Empty {} message SlackAuthRequest { string slack_token = 1; } - message JWTInfo { string jwt = 1; } @@ -34,9 +82,9 @@ message CreateDeviceRequest { } message CreateDeviceResponse { - wireguard.Config wg_config = 1; + Config wg_config = 1; } message ListDevicesResponse { - repeated wireguard.Device devices = 1; + repeated Device devices = 1; } diff --git a/proto/wireguard.proto b/proto/wireguard.proto deleted file mode 100644 index f740f60..0000000 --- a/proto/wireguard.proto +++ /dev/null @@ -1,53 +0,0 @@ -syntax = "proto3"; -package wireguard; - -message Peer { - string public_key = 1; - optional string preshared_key = 2; - repeated string allowed_ips = 3; - string endpoint = 4; - optional uint32 persistent_keepalive = 5; - optional string name = 6; -} - -message InterfaceConfig { - // Does not include private key; the client is responsible for generating & persisting that - repeated string address = 1; - optional uint32 listen_port = 2; - repeated string dns = 3; - optional uint32 mtu = 4; -} - -message Device { - int32 id = 1; - optional string name = 2; - string public_key = 3; - optional string apns_token = 4; - int32 user_id = 5; - string created_at = 6; - string ipv4 = 7; - string ipv6 = 8; - string access_token = 9; - string refresh_token = 10; - string expires_at = 11; -} - -message User { - int32 id = 1; - string created_at = 2; -} - -message UserConnection { - int32 user_id = 1; - string openid_provider = 2; - string openid_user_id = 3; - string openid_user_name = 4; - string access_token = 5; - string refresh_token = 6; -} - - -message Config { - InterfaceConfig interface = 1; - repeated Peer peers = 2; -}