-
Notifications
You must be signed in to change notification settings - Fork 28
/
checkBrokerCredentials.go
47 lines (39 loc) · 1.27 KB
/
checkBrokerCredentials.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package filters
import (
"fmt"
"net/http"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/web"
"github.com/tidwall/gjson"
)
const (
CheckBrokerCredentialsFilterName = "CheckBrokerCredentialsFilter"
credentialsPath = "credentials.basic.%s"
)
// CheckBrokerCredentialsFilter checks patch request for the broker basic credentials
type CheckBrokerCredentialsFilter struct {
}
func (*CheckBrokerCredentialsFilter) Name() string {
return CheckBrokerCredentialsFilterName
}
func (*CheckBrokerCredentialsFilter) Run(req *web.Request, next web.Handler) (*web.Response, error) {
fields := gjson.GetManyBytes(req.Body, "broker_url", fmt.Sprintf(credentialsPath, "username"), fmt.Sprintf(credentialsPath, "password"))
if fields[0].Exists() && (!fields[1].Exists() || !fields[2].Exists()) {
return nil, &util.HTTPError{
ErrorType: "BadRequest",
Description: "Updating an URL of a broker requires its basic credentials",
StatusCode: http.StatusBadRequest,
}
}
return next.Handle(req)
}
func (*CheckBrokerCredentialsFilter) FilterMatchers() []web.FilterMatcher {
return []web.FilterMatcher{
{
Matchers: []web.Matcher{
web.Path(web.ServiceBrokersURL + "/**"),
web.Methods(http.MethodPatch),
},
},
}
}