auth: Put auth methods in API

This commit is contained in:
Łukasz Magiera
2019-07-24 00:37:05 +02:00
parent fa4bf5178a
commit 309ecc4052
9 changed files with 78 additions and 31 deletions
+5 -18
View File
@@ -1,18 +1,18 @@
package auth
import (
"context"
"net/http"
"strings"
"github.com/filecoin-project/go-lotus/api"
"github.com/gbrlsnchs/jwt/v3"
logging "github.com/ipfs/go-log"
)
var log = logging.Logger("auth")
type Handler struct {
Secret *jwt.HMACSHA
Verify func(ctx context.Context, token string) ([]string, error)
Next http.HandlerFunc
}
@@ -28,28 +28,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
token = token[len("Bearer "):]
var payload jwtPayload
if _, err := jwt.Verify([]byte(token), h.Secret, &payload); err != nil {
allow, err := h.Verify(ctx, token)
if err != nil {
log.Warnf("JWT Verification failed: %s", err)
w.WriteHeader(401)
return
}
ctx = api.WithPerm(ctx, payload.Allow)
ctx = api.WithPerm(ctx, allow)
}
h.Next(w, r.WithContext(ctx))
}
type jwtPayload struct {
Allow []string
}
/*func init() {
p := jwtPayload{
Allow: []string{"read", "write"},
}
r, _ := jwt.Sign(&p, secret)
log.Infof("WRITE TOKEN: %s", string(r))
}
*/