Compare commits

..

6 Commits

Author SHA1 Message Date
45755c498c add content-type to JSON response 2025-03-17 22:10:43 +01:00
452c292570 adjust Traefik header for legacy Traefik pre-v2
Our modern infrastructure is not yet ready for this, apparently.
2025-03-17 21:52:57 +01:00
64ccd0a51b add CORS 2025-03-05 23:17:15 +01:00
2b03c60ef2 scripts/open_aerie: fix comment; wrong creds should get 403 2025-01-20 21:56:00 +01:00
c962a53bbe simplify code 2025-01-20 21:55:31 +01:00
fab52f7f80 panic on start if env vars are not set 2025-01-20 21:55:22 +01:00
3 changed files with 39 additions and 25 deletions

View File

@ -9,6 +9,12 @@ services:
- traefik.port=3000
- traefik.frontend.passHostHeader=true
- traefik.enable=true
- traefik.frontend.headers.customResponseHeaders=Access-Control-Allow-Origin:*
# NOTE: This is for Traefik v2, apparently.
- traefik.http.middlewares.cors.headers.accesscontrolallowmethods=GET,OPTIONS,PUT
- traefik.http.middlewares.cors.headers.accesscontrolalloworiginlist=https://chaospott.de
- traefik.http.middlewares.cors.headers.accesscontrolmaxage=100
- traefik.http.middlewares.cors.headers.addvaryheader=true
networks:
- extern

View File

@ -9,8 +9,8 @@ curl -XPOST \
--data '{"consumer_key": "test123","consumer_secret":"123test","aerie":true }' \
http://localhost:3000/api/update -vvv
#should return 500
#should return 403
curl -XPOST \
-H "Content-Type: application/json" \
--data '{"consumer_key": "foo","consumer_secret":"bar","aerie":true }' \
http://localhost:3000/api/update -vvv
http://localhost:3000/api/update -vvv

View File

@ -1,9 +1,11 @@
use std::fs::File;
use std::io::prelude::*;
use std::env;
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::path::Path;
use axum::{
http::StatusCode,
body::Body,
http::{Response, StatusCode},
routing::{get, post},
Json, Router,
};
@ -17,6 +19,13 @@ const STATUS_FILE: &str = "status.json";
#[tokio::main]
async fn main() {
if env::var("consumer_key").is_err() {
panic!("env var consumer_key must be set");
}
if env::var("consumer_secret").is_err() {
panic!("env var consumer_secret must be set");
}
let app = Router::new()
.route("/status.json", get(root))
.route("/api/update", post(the_doors));
@ -39,14 +48,11 @@ fn init_status() -> Status {
// check given secret
// https://www.youtube.com/watch?v=aHKWVLH-ibY
fn check_secret(given_secret: String, given_key: String) -> bool {
fn auth(p: &TheDoors) -> bool {
let consumer_secret = env::var("consumer_secret").unwrap();
let consumer_key = env::var("consumer_key").unwrap();
if given_secret == consumer_secret && given_key == consumer_key {
return true;
}
false
p.consumer_secret == consumer_secret && p.consumer_key == consumer_key
}
// Write status to file and return JSON string.
@ -59,12 +65,17 @@ fn write_status(s: Status) -> String {
// Just output the current file. We assume it to be consistent.
// It may cease to or not yet exist. Then create an initial status and persist.
async fn root() -> String {
if std::path::Path::new(STATUS_FILE).exists() {
return std::fs::read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT"));
}
let s = init_status();
write_status(s)
async fn root() -> Response<Body> {
let res = if std::path::Path::new(STATUS_FILE).exists() {
read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT"))
} else {
write_status(init_status())
};
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(Body::from(res))
.unwrap()
}
// Input type for the API: Both fields are optional.
@ -73,21 +84,18 @@ struct TheDoors {
aerie: Option<bool>,
cellar: Option<bool>,
consumer_key: String,
consumer_secret: String
consumer_secret: String,
}
// The door can see through your soul.
// https://www.youtube.com/watch?v=bDQDp00oTP4
async fn the_doors(Json(payload): Json<TheDoors>) -> StatusCode {
if !auth(&payload) {
return StatusCode::FORBIDDEN;
}
let check_secret = check_secret(payload.consumer_secret, payload.consumer_key);
if !check_secret { return StatusCode::FORBIDDEN; }
let status: Status = if std::path::Path::new(STATUS_FILE).exists() {
let contents = std::fs::read_to_string(STATUS_FILE).expect("FCKAFD");
let status: Status = if Path::new(STATUS_FILE).exists() {
let contents = read_to_string(STATUS_FILE).expect("FCKAFD");
serde_json::from_str(&contents).unwrap_or_else(|_| init_status())
} else {
init_status()