lotus/lib/auth/handler.go

43 lines
829 B
Go
Raw Normal View History

package auth
import (
2019-07-23 17:27:45 +00:00
"context"
"net/http"
"strings"
"github.com/filecoin-project/go-lotus/api"
logging "github.com/ipfs/go-log"
)
var log = logging.Logger("auth")
type Handler struct {
2019-07-23 17:27:45 +00:00
Verify func(ctx context.Context, token string) ([]string, error)
2019-07-23 18:49:09 +00:00
Next http.HandlerFunc
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
token := r.Header.Get("Authorization")
if token != "" {
if !strings.HasPrefix(token, "Bearer ") {
log.Warn("missing Bearer prefix in auth header")
w.WriteHeader(401)
return
}
2019-07-23 19:38:10 +00:00
token = strings.TrimPrefix(token, "Bearer ")
2019-07-23 17:27:45 +00:00
allow, err := h.Verify(ctx, token)
if err != nil {
log.Warnf("JWT Verification failed: %s", err)
w.WriteHeader(401)
return
}
2019-07-23 17:27:45 +00:00
ctx = api.WithPerm(ctx, allow)
}
h.Next(w, r.WithContext(ctx))
}