@@ -69,11 +69,34 @@ type ScopedTokenAuthentication func(string, []string) (interface{}, error)
6969// ScopedTokenAuthenticationCtx authentication function with context.Context
7070type ScopedTokenAuthenticationCtx func (context.Context , string , []string ) (context.Context , interface {}, error )
7171
72+ var DefaultRealmName = "API"
73+
74+ type secCtxKey uint8
75+
76+ const (
77+ failedBasicAuth secCtxKey = iota
78+ )
79+
80+ func FailedBasicAuth (r * http.Request ) string {
81+ return FailedBasicAuthCtx (r .Context ())
82+ }
83+
84+ func FailedBasicAuthCtx (ctx context.Context ) string {
85+ v , ok := ctx .Value (failedBasicAuth ).(string )
86+ if ! ok {
87+ return ""
88+ }
89+ return v
90+ }
91+
7292// BasicAuth creates a basic auth authenticator with the provided authentication function
7393func BasicAuth (authenticate UserPassAuthentication ) runtime.Authenticator {
7494 return HttpAuthenticator (func (r * http.Request ) (bool , interface {}, error ) {
7595 if usr , pass , ok := r .BasicAuth (); ok {
7696 p , err := authenticate (usr , pass )
97+ if err != nil {
98+ * r = * r .WithContext (context .WithValue (r .Context (), failedBasicAuth , DefaultRealmName ))
99+ }
77100 return true , p , err
78101 }
79102
@@ -86,6 +109,9 @@ func BasicAuthCtx(authenticate UserPassAuthenticationCtx) runtime.Authenticator
86109 return HttpAuthenticator (func (r * http.Request ) (bool , interface {}, error ) {
87110 if usr , pass , ok := r .BasicAuth (); ok {
88111 ctx , p , err := authenticate (r .Context (), usr , pass )
112+ if err != nil {
113+ ctx = context .WithValue (ctx , failedBasicAuth , DefaultRealmName )
114+ }
89115 * r = * r .WithContext (ctx )
90116 return true , p , err
91117 }
0 commit comments