github.com/zak-blake/goa@v1.4.1/middleware/security/basicauth/basicauth.go (about) 1 package basicauth 2 3 import ( 4 "net/http" 5 6 "context" 7 8 "github.com/goadesign/goa" 9 ) 10 11 // ErrBasicAuthFailed means it wasn't able to authenticate you with your login/password. 12 var ErrBasicAuthFailed = goa.NewErrorClass("basic_auth_failed", 401) 13 14 // New creates a static username/password auth middleware. 15 // 16 // Example: 17 // app.UseBasicAuth(basicauth.New("admin", "password")) 18 // 19 // It doesn't get simpler than that. 20 // 21 // If you want to handle the username and password checks dynamically, 22 // copy the source of `New`, it's 8 lines and you can tweak at will. 23 func New(username, password string) goa.Middleware { 24 middleware, _ := goa.NewMiddleware(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { 25 u, p, ok := r.BasicAuth() 26 if !ok || u != username || p != password { 27 return ErrBasicAuthFailed("Authentication failed") 28 } 29 return nil 30 }) 31 return middleware 32 }