commit 0e69082cbd484c1a03dc1cede04f5f3654a8176f
Author: lash <dev@holbrook.no>
Date: Mon, 13 Jun 2022 16:26:16 +0000
Initial commit
Diffstat:
5 files changed, 163 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.toml b/Cargo.toml
@@ -0,0 +1,18 @@
+[package]
+name = "wala"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+tiny_http = "^0.7.0"
+env_logger = "^0.9"
+log = "^0.4"
+
+[dependencies.pgp]
+version = "^0.7.2"
+optional = true
+
+[features]
+pgpauth = ["pgp"]
diff --git a/src/auth/mock.rs b/src/auth/mock.rs
@@ -0,0 +1,6 @@
+use crate::auth::AuthSpec;
+
+pub fn auth_check(auth: AuthSpec) {
+ let auth_fields = s.split(":").collect()
+
+}
diff --git a/src/auth/mod.rs b/src/auth/mod.rs
@@ -0,0 +1,54 @@
+use std::str::FromStr;
+use std::error::Error;
+use std::fmt;
+
+pub struct AuthSpec {
+ pub method: String,
+ key: Vec<u8>,
+ signature: Vec<u8>,
+}
+
+#[derive(Debug)]
+pub struct AuthSpecError;
+
+impl Error for AuthSpecError {
+ fn description(&self) -> &str{
+ "auth string malformed"
+ }
+}
+
+impl fmt::Display for AuthSpecError {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.write_str(self.description())
+ }
+}
+
+impl FromStr for AuthSpec {
+ type Err = AuthSpecError;
+
+ fn from_str(s: &str) -> Result<AuthSpec, AuthSpecError> {
+ let mut auth_fields = s.split(":");
+ if auth_fields.clone().count() != 3 {
+ return Err(AuthSpecError{})
+ }
+ let auth_type: String = auth_fields.next().unwrap().to_string();
+ let r = AuthSpec{
+ method: auth_type,
+ key: vec!(),
+ signature: vec!(),
+ };
+ Ok(r)
+ }
+}
+
+impl fmt::Debug for AuthSpec {
+ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+ fmt.write_str(
+ format!(
+ "{} key {:?}",
+ self.method,
+ self.key,
+ ).as_str()
+ )
+ }
+}
diff --git a/src/main.rs b/src/main.rs
@@ -0,0 +1,84 @@
+use tiny_http::{
+ Server,
+ ServerConfig,
+ Request,
+ Response,
+ StatusCode,
+};
+use std::net::{Ipv4Addr, SocketAddrV4};
+use std::str::FromStr;
+use env_logger;
+
+mod auth;
+
+use auth::AuthSpec;
+
+use log::{debug, info, error};
+
+fn main() {
+ env_logger::init();
+
+ let ip_addr = Ipv4Addr::from_str("0.0.0.0").unwrap();
+ let tcp_port: u16 = 8001;
+ let sock_addr = SocketAddrV4::new(ip_addr, tcp_port);
+ let srv_cfg = ServerConfig{
+ addr: sock_addr,
+ ssl: None,
+ };
+ let srv = Server::new(srv_cfg).unwrap();
+
+ loop {
+ let r = srv.recv();
+ let req: Request;
+ match r {
+ Ok(v) => req = v,
+ Err(e) => {
+ error!("{}", e);
+ break;
+ }
+ };
+
+ let mut res_status: StatusCode;
+ let mut m: bool = false;
+
+ let mut auth_spec: Option<AuthSpec> = None;
+
+ for h in req.headers() {
+ let k = &h.field;
+ if k.equiv("Authorization") {
+ let v = &h.value;
+ let r = AuthSpec::from_str(v.as_str());
+ match r {
+ Ok(v) => {
+ m = true;
+ auth_spec = Some(v);
+ },
+ Err(e) => {
+ error!("malformed auth string: {}", &h.value);
+ }
+ }
+ }
+ }
+
+ match auth_spec {
+ Some(v) => {
+ debug!("have auth {:?}", v);
+ },
+ None => {
+ debug!("no auth");
+ }
+ };
+
+ if m {
+ res_status = StatusCode(200);
+ let mut res = Response::from_string("foo");
+ res = res.with_status_code(res_status);
+ req.respond(res);
+ continue;
+ }
+
+ res_status = StatusCode(404);
+ let res = Response::empty(res_status);
+ req.respond(res);
+ }
+}