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