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