Skip to content

Commit fdbf913

Browse files
committed
feat: add authorization header to webhook configuration
- Add authorization_header field to CreateHookRequest in setup_webhook - Generate HMAC-SHA256 signature for admin password using crypto utils - Add AdminBasic authentication to handle_gitea_webhook endpoint - Update matching function to include authorization_header comparison
1 parent 40d627e commit fdbf913

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ harbor-client = { path = "crates/harbor-client" }
3535
anyhow = "1.0.98"
3636
axum = { version = "0.8.4" }
3737
axum-extra = {version = "0.10.1", features = ["typed-header"] }
38+
base64 = "0.22.1"
3839
bytes = "1.10.1"
3940
chrono = { version = "0.4.41", features = ["serde"] }
4041
clap = { version = "4.5.46", features = ["derive", "env"] }

crates/gitea-client/src/types/hook.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub fn matching(hook: &Hook, req: &CreateHookRequest) -> bool {
5757
hook.kind == req.kind &&
5858
hook.branch_filter == req.branch_filter &&
5959
hook.events == req.events &&
60+
hook.authorization_header == req.authorization_header &&
6061
hook.config.get("url") == req.config.get("url") &&
6162
hook.config.get("content_type") == req.config.get("content_type")
6263
}

src/handler/webhook.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use uuid::Uuid;
2121
use crate::{
2222
context::Context,
2323
errors::{ApiError, Result},
24+
extractor::AdminBasic,
2425
repository::CourseRepository,
2526
request::event::PipelineEvent,
2627
service::{PipelineCleanupGuard, RepoService, StageService},
@@ -29,6 +30,7 @@ use crate::{
2930

3031
/// Handle Gitea Webhook Event.
3132
pub async fn handle_gitea_webhook(
33+
_: AdminBasic,
3234
State(ctx): State<Arc<Context>>,
3335
Json(event): Json<Event>,
3436
) -> Result<impl IntoResponse> {

src/service/repository.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use std::{collections::HashMap, sync::Arc};
1616

17+
use base64::{Engine, prelude::BASE64_STANDARD as Base64};
1718
use fs_extra::dir::CopyOptions;
1819
use gitea_client::{ClientError, types::*};
1920
use tracing::{debug, info};
@@ -25,7 +26,7 @@ use crate::{
2526
errors::Result,
2627
repository::CourseRepository,
2728
service::{CourseService, PipelineService, StorageError, StorageService},
28-
utils::{git, url},
29+
utils::{crypto, git, url},
2930
};
3031

3132
pub struct RepoService {
@@ -193,18 +194,24 @@ impl RepoService {
193194
let webhook_endpoint = &self.ctx.config.webhook_endpoint;
194195
let url = format!("{webhook_endpoint}/v1/webhooks/gitea");
195196

197+
// Generate the HMAC-SHA256 signature for the webhook authorization header
198+
// using the admin username and the auth_secret from the configuration.
199+
// This ensures that incoming webhook requests are authenticated.
200+
let password = crypto::hmac_sha256_sign("admin", &self.ctx.config.auth_secret)?;
201+
let auth_header = format!("Basic {}", Base64.encode(format!("admin:{}", password)));
202+
196203
// Define the webhook request body to listen for push events on the main branch
197204
// and send them to the specified webhook endpoint in JSON format.
198205
let req = CreateHookRequest {
199206
active: true,
207+
authorization_header: Some(auth_header),
200208
branch_filter: Some("main".to_string()),
201209
config: HashMap::from([
202210
("content_type".to_string(), "json".to_string()),
203211
("url".to_string(), url.clone()),
204212
]),
205213
events: vec!["push".to_string()],
206214
kind: "gitea".to_string(),
207-
..Default::default()
208215
};
209216

210217
// List all existing hooks

0 commit comments

Comments
 (0)