Skip to content

Commit

Permalink
webhook error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 committed Dec 21, 2023
1 parent 1d6ab61 commit 8cc1a77
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
38 changes: 26 additions & 12 deletions scripty_webserver/src/endpoints/webhooks/discordservices_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,38 @@ impl<S> FromRequestParts<S> for DiscordServicesNetAuthorization {
let authorization = parts
.headers
.get("Authorization")
.ok_or((StatusCode::UNAUTHORIZED, "No Authorization header provided"))?;
let authorization = authorization.to_str().map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Authorization header was not valid UTF-8",
)
})?;
.ok_or((StatusCode::UNAUTHORIZED, "No Authorization header provided"))?
.to_str()
.map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Authorization header was not valid UTF-8",
)
})?
.trim();

let scripty_config::BotListsConfig::FullConfig { token: _, webhook } =
scripty_config::get_config()
.bot_lists
.get("discordservices_net")
.ok_or((StatusCode::UNAUTHORIZED, "Invalid token"))?
let Some(webhook_config) = scripty_config::get_config()
.bot_lists
.get("discordservices_net")
else {
warn!("couldn't find valid configuration for discordservices.net (got none)");
return Err((StatusCode::UNAUTHORIZED, "Invalid token"));
};

let scripty_config::BotListsConfig::FullConfig { token: _, webhook } = webhook_config
else {
warn!(
"couldn't find valid configuration for discordservices.net (got single key, need \
double)"
);
return Err((StatusCode::UNAUTHORIZED, "Invalid token"));
};

if authorization != webhook {
debug!(
"webhook request had invalid authorization header: got <{}>",
authorization
);
Err((StatusCode::UNAUTHORIZED, "Invalid token"))
} else {
Ok(Self)
Expand Down
32 changes: 20 additions & 12 deletions scripty_webserver/src/endpoints/webhooks/top_gg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,32 @@ impl<S> FromRequestParts<S> for TopGgAuthorization {
let authorization = parts
.headers
.get("Authorization")
.ok_or((StatusCode::UNAUTHORIZED, "No Authorization header provided"))?;
let authorization = authorization.to_str().map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Authorization header was not valid UTF-8",
)
})?;
.ok_or((StatusCode::UNAUTHORIZED, "No Authorization header provided"))?
.to_str()
.map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Authorization header was not valid UTF-8",
)
})?
.trim();

let scripty_config::BotListsConfig::FullConfig { token: _, webhook } =
scripty_config::get_config()
.bot_lists
.get("top_gg")
.ok_or((StatusCode::UNAUTHORIZED, "Invalid token"))?
let Some(webhook_config) = scripty_config::get_config().bot_lists.get("top_gg") else {
warn!("couldn't find valid configuration for top.gg (got none)");
return Err((StatusCode::UNAUTHORIZED, "Invalid token"));
};

let scripty_config::BotListsConfig::FullConfig { token: _, webhook } = webhook_config
else {
warn!("couldn't find valid configuration for top.gg (got single key, need double)");
return Err((StatusCode::UNAUTHORIZED, "Invalid token"));
};

if authorization != webhook {
debug!(
"webhook request had invalid authorization header: got <{}>",
authorization
);
Err((StatusCode::UNAUTHORIZED, "Invalid token"))
} else {
Ok(Self)
Expand Down
35 changes: 23 additions & 12 deletions scripty_webserver/src/endpoints/webhooks/wumpus_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,35 @@ impl<S> FromRequestParts<S> for WumpusStoreAuthorization {
let authorization = parts
.headers
.get("Authorization")
.ok_or((StatusCode::UNAUTHORIZED, "No Authorization header provided"))?;
let authorization = authorization.to_str().map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Authorization header was not valid UTF-8",
)
})?;
.ok_or((StatusCode::UNAUTHORIZED, "No Authorization header provided"))?
.to_str()
.map_err(|_| {
(
StatusCode::BAD_REQUEST,
"Authorization header was not valid UTF-8",
)
})?
.trim();

let scripty_config::BotListsConfig::FullConfig { token: _, webhook } =
scripty_config::get_config()
.bot_lists
.get("wumpus_store")
.ok_or((StatusCode::UNAUTHORIZED, "Invalid token"))?
let Some(webhook_config) = scripty_config::get_config().bot_lists.get("wumpus_store")
else {
warn!("couldn't find valid configuration for wumpus.store (got none)");
return Err((StatusCode::UNAUTHORIZED, "Invalid token"));
};

let scripty_config::BotListsConfig::FullConfig { token: _, webhook } = webhook_config
else {
warn!(
"couldn't find valid configuration for wumpus.store (got single key, need double)"
);
return Err((StatusCode::UNAUTHORIZED, "Invalid token"));
};

if authorization != webhook {
debug!(
"webhook request had invalid authorization header: got <{}>",
authorization
);
Err((StatusCode::UNAUTHORIZED, "Invalid token"))
} else {
Ok(Self)
Expand Down

0 comments on commit 8cc1a77

Please sign in to comment.