Verified Commit 26de62ca authored by Katharina Fey's avatar Katharina Fey 🏴
Browse files

Updating actix to 1.0 and adjusting handler code

parent 88fa8e73
......@@ -174,7 +174,7 @@ dependencies = [
[[package]]
name = "actix-web"
version = "1.0.0-beta.1"
source = "git+https://github.com/actix/actix-web#42644dac3f2827ac290d9f1e64591c35dbce395f"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"actix-codec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"actix-http 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -726,7 +726,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "forge"
version = "0.1.0"
dependencies = [
"actix-web 1.0.0-beta.1 (git+https://github.com/actix/actix-web)",
"actix-web 1.0.0-beta.1 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"blake2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -1896,7 +1896,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum actix-service 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcbfa034a61a48c128de169a019a5f9aa3ac2f7c63b18972e11b4c069321f36b"
"checksum actix-threadpool 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97fa58548067c1f0a16a82cdb7c8823deac793d27efd17b51d6ea7861c6d3966"
"checksum actix-utils 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "80b12b95a3550c49b8f75e80341608bceaa6c544b0c754a0a6ffcf89bdd6d723"
"checksum actix-web 1.0.0-beta.1 (git+https://github.com/actix/actix-web)" = "<none>"
"checksum actix-web 1.0.0-beta.1 (registry+https://github.com/rust-lang/crates.io-index)" = "07bb9785af50024414b64cda28b727cbe9c24424601a6bd699d16bce3e48822f"
"checksum actix-web-codegen 0.1.0-beta.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b26f9ce2dff34bda98b3c5b3ec2467a8f1bf08c69b0ae8ff02bc7d74d6af9d84"
"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c"
"checksum aes 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "54eb1d8fe354e5fc611daf4f2ea97dd45a765f4f1e4512306ec183ae2e8f20c9"
......
......@@ -7,7 +7,7 @@ edition = "2018"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
actix-web = { git = "https://github.com/actix/actix-web" }
actix-web = "1.0.0-beta.1"
clap = "2.33"
miscreant = { version = "0.4", features = ["soft-aes"] }
......
use clap::{App, AppSettings, Arg, SubCommand};
use forge::{hash, secret, setup};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
fn main() {
let app = App::new("forge-cli")
......
// use actix_web::{middleware, web, App, HttpRequest, HttpResponse, Json};
// use std::env;
// mod utils {
// pub use crate::*;
// }
// struct Params {
// bind: String,
// port: u16,
// token: String,
// salt: String,
// }
// impl Params {
// pub fn new() -> Self {
// Self {
// bind: env::var("FORGE_BIND").unwrap_or("0.0.0.0".into()).into(),
// port: env::var("FORGE_PORT")
// .unwrap_or("12220".into())
// .parse::<u16>()
// .unwrap(),
// token: env::var("FORGE_TOKEN").unwrap().into(),
// salt: env::var("FORGE_TOKEN_SALT").unwrap().into(),
// }
// }
// }
// fn handle_token(state: &Params, req: &HttpRequest) -> impl Responder {
// let h = req.headers();
// let event = h
// .get("x-gitlab-event")
// .expect("Request didn't include `x-gitlab-event`");
// let token = h
// .get("x-gitlab-token")
// .expect("Request didn't include `x-gitlab-event`");
// // utils::blake2_verify("hash: Hash", token);
// format!("")
// }
// pub fn run() {
// let params = Params::new();
// server::new(move || {
// App::new()
// .data(&params)
// .wrap(middleware::Logger::default())
// .service(web::resource("/").to(handle_token(state: &Params, req: &HttpRequest)))
// })
// .bind("127.0.0.1:8080").unwrap()
// .run();
// // server::new(move || App::new().data(&params).resource("/", |r| r.method(http::Method::POST).f(handle_token)))
// // .bind(&format!("{}:{}", params.bind, params.port))
// // .unwrap()
// // .run();
// }
// use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
// /// simple handle
// fn index(state: web::Data<Arc<Mutex<usize>>>, req: HttpRequest) -> HttpResponse {
// println!("{:?}", req);
// *(state.lock().unwrap()) += 1;
// HttpResponse::Ok().body(format!("Num of requests: {}", state.lock().unwrap()))
// }
// pub fn run() {
// let counter = Arc::new(Mutex::new(0));
// HttpServer::new(move || {
// App::new()
// .data(counter.clone())
// // register simple handler, handle all methods
// .service(web::resource("/").to(index))
// })
// .bind("127.0.0.1:8080").unwrap()
// .run();
// }
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
use std::env;
mod utils {
pub use crate::*;
}
#[derive(Clone)]
struct Params {
bind: String,
port: u16,
token: String,
salt: String,
}
impl Params {
pub fn new() -> Self {
Self {
bind: env::var("FORGE_BIND").unwrap_or("0.0.0.0".into()).into(),
port: env::var("FORGE_PORT")
.unwrap_or("12220".into())
.parse::<u16>()
.unwrap(),
token: env::var("FORGE_TOKEN").unwrap().into(),
salt: env::var("FORGE_TOKEN_SALT").unwrap().into(),
}
}
}
/// simple handle
fn index(state: web::Data<Params>, req: HttpRequest) -> HttpResponse {
println!("{:?}", req);
let h = req.headers();
let _ = h
.get("x-gitlab-event")
.expect("Request didn't include `x-gitlab-event`")
.to_str()
.unwrap();
let token = h
.get("x-gitlab-token")
.expect("Request didn't include `x-gitlab-event`")
.to_str()
.unwrap();
let hash = utils::blake2(token, state.salt.as_str());
if utils::blake2_verify(hash, token.into()) {
HttpResponse::Ok().body(format!("Done"))
} else {
HttpResponse::Forbidden().body(format!("Failed to authenticate!"))
}
}
pub fn main() {
let params = Params::new();
let bind = params.bind.clone();
HttpServer::new(move || {
App::new()
.data(params.clone())
.wrap(middleware::Logger::default())
.service(web::resource("/").to(index))
})
.bind(&bind)
.unwrap()
.run()
.unwrap();
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment