@@ -13,8 +13,9 @@ import (
13
13
14
14
// parse errors
15
15
var (
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" )
18
19
)
19
20
20
21
// Event defines an Azure DevOps server hook event type
@@ -29,13 +30,38 @@ const (
29
30
GitPushEventType Event = "git.push"
30
31
)
31
32
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
+
32
51
// Webhook instance contains all methods needed to process events
33
52
type Webhook struct {
53
+ username string
54
+ password string
34
55
}
35
56
36
57
// New creates and returns a WebHook instance
37
- func New () (* Webhook , error ) {
58
+ func New (options ... Option ) (* Webhook , error ) {
38
59
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
+ }
39
65
return hook , nil
40
66
}
41
67
@@ -46,6 +72,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
46
72
_ = r .Body .Close ()
47
73
}()
48
74
75
+ if ! hook .verifyBasicAuth (r ) {
76
+ return nil , ErrBasicAuthVerificationFailed
77
+ }
78
+
49
79
if r .Method != http .MethodPost {
50
80
return nil , ErrInvalidHTTPMethod
51
81
}
@@ -78,3 +108,13 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
78
108
return nil , fmt .Errorf ("unknown event %s" , pl .EventType )
79
109
}
80
110
}
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