Skip to content

Commit 9f63454

Browse files
Merge branch 'go-playground:master' into main
2 parents 27af3ec + 02d182a commit 9f63454

File tree

8 files changed

+549
-5
lines changed

8 files changed

+549
-5
lines changed

azuredevops/azuredevops.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313

1414
// parse errors
1515
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")
1819
)
1920

2021
// Event defines an Azure DevOps server hook event type
@@ -30,13 +31,38 @@ const (
3031
GitPullRequestCommentEventType Event = "ms.vss-code.git-pullrequest-comment-event"
3132
)
3233

34+
// Option is a configuration option for the webhook
35+
type Option func(*Webhook) error
36+
37+
// Options is a namespace var for configuration options
38+
var Options = WebhookOptions{}
39+
40+
// WebhookOptions is a namespace for configuration option methods
41+
type WebhookOptions struct{}
42+
43+
// BasicAuth verifies payload using basic auth
44+
func (WebhookOptions) BasicAuth(username, password string) Option {
45+
return func(hook *Webhook) error {
46+
hook.username = username
47+
hook.password = password
48+
return nil
49+
}
50+
}
51+
3352
// Webhook instance contains all methods needed to process events
3453
type Webhook struct {
54+
username string
55+
password string
3556
}
3657

3758
// New creates and returns a WebHook instance
38-
func New() (*Webhook, error) {
59+
func New(options ...Option) (*Webhook, error) {
3960
hook := new(Webhook)
61+
for _, opt := range options {
62+
if err := opt(hook); err != nil {
63+
return nil, errors.New("Error applying Option")
64+
}
65+
}
4066
return hook, nil
4167
}
4268

@@ -47,6 +73,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
4773
_ = r.Body.Close()
4874
}()
4975

76+
if !hook.verifyBasicAuth(r) {
77+
return nil, ErrBasicAuthVerificationFailed
78+
}
79+
5080
if r.Method != http.MethodPost {
5181
return nil, ErrInvalidHTTPMethod
5282
}
@@ -83,3 +113,13 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
83113
return nil, fmt.Errorf("unknown event %s", pl.EventType)
84114
}
85115
}
116+
117+
func (hook Webhook) verifyBasicAuth(r *http.Request) bool {
118+
// skip validation if username or password was not provided
119+
if hook.username == "" && hook.password == "" {
120+
return true
121+
}
122+
username, password, ok := r.BasicAuth()
123+
124+
return ok && username == hook.username && password == hook.password
125+
}

azuredevops/azuredevops_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package azuredevops
22

33
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/stretchr/testify/assert"
47
"log"
58
"net/http"
69
"net/http/httptest"
@@ -117,3 +120,66 @@ func TestWebhooks(t *testing.T) {
117120
})
118121
}
119122
}
123+
124+
func TestParseBasicAuth(t *testing.T) {
125+
const validUser = "validUser"
126+
const validPass = "pass123"
127+
tests := []struct {
128+
name string
129+
webhookUser string
130+
webhookPass string
131+
reqUser string
132+
reqPass string
133+
expectedErr error
134+
}{
135+
{
136+
name: "valid basic auth",
137+
webhookUser: validUser,
138+
webhookPass: validPass,
139+
reqUser: validUser,
140+
reqPass: validPass,
141+
expectedErr: fmt.Errorf("unknown event "), // no event passed, so this is expected
142+
},
143+
{
144+
name: "no basic auth provided",
145+
expectedErr: fmt.Errorf("unknown event "), // no event passed, so this is expected
146+
},
147+
{
148+
name: "invalid basic auth",
149+
webhookUser: validUser,
150+
webhookPass: validPass,
151+
reqUser: "fakeUser",
152+
reqPass: "fakePass",
153+
expectedErr: ErrBasicAuthVerificationFailed,
154+
},
155+
}
156+
157+
for _, tt := range tests {
158+
h := Webhook{
159+
username: tt.webhookUser,
160+
password: tt.webhookPass,
161+
}
162+
body := []byte(`{}`)
163+
r, err := http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body))
164+
assert.NoError(t, err)
165+
r.SetBasicAuth(tt.reqUser, tt.reqPass)
166+
167+
p, err := h.Parse(r)
168+
169+
assert.Equal(t, err, tt.expectedErr)
170+
assert.Nil(t, p)
171+
}
172+
}
173+
174+
func TestBasicAuth(t *testing.T) {
175+
const user = "user"
176+
const pass = "pass123"
177+
178+
opt := Options.BasicAuth(user, pass)
179+
h := &Webhook{}
180+
err := opt(h)
181+
182+
assert.NoError(t, err)
183+
assert.Equal(t, h.username, user)
184+
assert.Equal(t, h.password, pass)
185+
}

github/github.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const (
7474
WorkflowJobEvent Event = "workflow_job"
7575
WorkflowRunEvent Event = "workflow_run"
7676
GitHubAppAuthorizationEvent Event = "github_app_authorization"
77+
CodeScanningAlertEvent Event = "code_scanning_alert"
7778
)
7879

7980
// EventSubtype defines a GitHub Hook Event subtype
@@ -353,6 +354,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
353354
var pl GitHubAppAuthorizationPayload
354355
err = json.Unmarshal([]byte(payload), &pl)
355356
return pl, err
357+
case CodeScanningAlertEvent:
358+
var pl CodeScanningAlertPayload
359+
err = json.Unmarshal([]byte(payload), &pl)
360+
return pl, err
356361
default:
357362
return nil, fmt.Errorf("unknown event %s", gitHubEvent)
358363
}

github/github_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,15 @@ func TestWebhooks(t *testing.T) {
555555
"X-Github-Event": []string{"github_app_authorization"},
556556
},
557557
},
558+
{
559+
name: "CodeScanningAlertEvent",
560+
event: CodeScanningAlertEvent,
561+
typ: CodeScanningAlertPayload{},
562+
filename: "../testdata/github/code_scanning_alert.json",
563+
headers: http.Header{
564+
"X-Github-Event": []string{"code_scanning_alert"},
565+
},
566+
},
558567
}
559568

560569
for _, tt := range tests {

0 commit comments

Comments
 (0)