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 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
4953var (
@@ -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
You can’t perform that action at this time.
0 commit comments