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