Skip to content

Commit 55a1793

Browse files
Merge pull request #42 from ZakCodes/basic-auth
Add the HTTP Basic auth scheme
2 parents bd449d5 + c609df5 commit 55a1793

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

apisprout.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ var (
4444
// ErrMissingAuth is set when no authorization header or key is present but
4545
// one is required by the API description.
4646
ErrMissingAuth = errors.New("Missing auth")
47+
48+
// ErrInvalidAuth is set when the authorization scheme doesn't correspond
49+
// to the one required by the API description.
50+
ErrInvalidAuth = errors.New("Invalid auth")
4751
)
4852

4953
var (
@@ -488,9 +492,22 @@ var handler = func(rr *RefreshableRouter) http.Handler {
488492
AuthenticationFunc: func(c context.Context, input *openapi3filter.AuthenticationInput) error {
489493
// TODO: support more schemes
490494
sec := input.SecurityScheme
491-
if sec.Type == "http" && sec.Scheme == "bearer" {
492-
if req.Header.Get("Authorization") == "" {
493-
return ErrMissingAuth
495+
if sec.Type == "http" {
496+
// Prefixes for each scheme.
497+
prefixes := map[string]string{
498+
"bearer": "BEARER ",
499+
"basic": "BASIC ",
500+
}
501+
if prefix, ok := prefixes[sec.Scheme]; ok {
502+
auth := req.Header.Get("Authorization")
503+
// If the auth is missing
504+
if len(auth) == 0 {
505+
return ErrMissingAuth
506+
}
507+
// If the auth doesn't have a value or doesn't start with the case insensitive prefix
508+
if len(auth) <= len(prefix) || !strings.HasPrefix(strings.ToUpper(auth), prefix) {
509+
return ErrInvalidAuth
510+
}
494511
}
495512
}
496513
return nil

0 commit comments

Comments
 (0)