Skip to content

Commit a6d41a1

Browse files
committed
Add the HTTP Basic auth scheme
1 parent 8104239 commit a6d41a1

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
@@ -40,6 +40,10 @@ var (
4040
// ErrMissingAuth is set when no authorization header or key is present but
4141
// one is required by the API description.
4242
ErrMissingAuth = errors.New("Missing auth")
43+
44+
// ErrInvalidAuth is set when the authorization scheme doesn't correspond
45+
// to the one required by the API description.
46+
ErrInvalidAuth = errors.New("Invalid auth")
4347
)
4448

4549
// ContentNegotiator is used to match a media type during content negotiation
@@ -510,9 +514,22 @@ func server(cmd *cobra.Command, args []string) {
510514
AuthenticationFunc: func(c context.Context, input *openapi3filter.AuthenticationInput) error {
511515
// TODO: support more schemes
512516
sec := input.SecurityScheme
513-
if sec.Type == "http" && sec.Scheme == "bearer" {
514-
if req.Header.Get("Authorization") == "" {
515-
return ErrMissingAuth
517+
if sec.Type == "http" {
518+
// Prefixes for each scheme.
519+
prefixes := map[string]string{
520+
"bearer": "Bearer ",
521+
"basic": "Basic ",
522+
}
523+
if prefix, ok := prefixes[sec.Scheme]; ok {
524+
auth := req.Header.Get("Authorization")
525+
// If the auth is missing
526+
if len(auth) == 0 {
527+
return ErrMissingAuth
528+
}
529+
// If the auth doesn't have a value or doesn't start with the prefix
530+
if len(auth) <= len(prefix) || !strings.HasPrefix(auth, prefix) {
531+
return ErrInvalidAuth
532+
}
516533
}
517534
}
518535
return nil

0 commit comments

Comments
 (0)