@@ -13,8 +13,9 @@ import (
1313
1414// parse errors
1515var (
16- ErrInvalidHTTPMethod = errors .New ("invalid HTTP Method" )
17- ErrParsingPayload = errors .New ("error parsing payload" )
16+ ErrInvalidHTTPMethod = errors .New ("invalid HTTP Method" )
17+ ErrParsingPayload = errors .New ("error parsing payload" )
18+ ErrBasicAuthVerificationFailed = errors .New ("basic auth verification failed" )
1819)
1920
2021// Event defines an Azure DevOps server hook event type
@@ -29,13 +30,38 @@ const (
2930 GitPushEventType Event = "git.push"
3031)
3132
33+ // Option is a configuration option for the webhook
34+ type Option func (* Webhook ) error
35+
36+ // Options is a namespace var for configuration options
37+ var Options = WebhookOptions {}
38+
39+ // WebhookOptions is a namespace for configuration option methods
40+ type WebhookOptions struct {}
41+
42+ // BasicAuth verifies payload using basic auth
43+ func (WebhookOptions ) BasicAuth (username , password string ) Option {
44+ return func (hook * Webhook ) error {
45+ hook .username = username
46+ hook .password = password
47+ return nil
48+ }
49+ }
50+
3251// Webhook instance contains all methods needed to process events
3352type Webhook struct {
53+ username string
54+ password string
3455}
3556
3657// New creates and returns a WebHook instance
37- func New () (* Webhook , error ) {
58+ func New (options ... Option ) (* Webhook , error ) {
3859 hook := new (Webhook )
60+ for _ , opt := range options {
61+ if err := opt (hook ); err != nil {
62+ return nil , errors .New ("Error applying Option" )
63+ }
64+ }
3965 return hook , nil
4066}
4167
@@ -46,6 +72,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
4672 _ = r .Body .Close ()
4773 }()
4874
75+ if ! hook .verifyBasicAuth (r ) {
76+ return nil , ErrBasicAuthVerificationFailed
77+ }
78+
4979 if r .Method != http .MethodPost {
5080 return nil , ErrInvalidHTTPMethod
5181 }
@@ -78,3 +108,13 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
78108 return nil , fmt .Errorf ("unknown event %s" , pl .EventType )
79109 }
80110}
111+
112+ func (hook Webhook ) verifyBasicAuth (r * http.Request ) bool {
113+ // skip validation if username or password was not provided
114+ if hook .username == "" && hook .password == "" {
115+ return true
116+ }
117+ username , password , ok := r .BasicAuth ()
118+
119+ return ok && username == hook .username && password == hook .password
120+ }
0 commit comments