File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change 44
44
// ErrMissingAuth is set when no authorization header or key is present but
45
45
// one is required by the API description.
46
46
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" )
47
51
)
48
52
49
53
var (
@@ -488,9 +492,22 @@ var handler = func(rr *RefreshableRouter) http.Handler {
488
492
AuthenticationFunc : func (c context.Context , input * openapi3filter.AuthenticationInput ) error {
489
493
// TODO: support more schemes
490
494
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
+ }
494
511
}
495
512
}
496
513
return nil
You can’t perform that action at this time.
0 commit comments