2024-02-27 14:14:30 +01:00
|
|
|
package user
|
2023-03-07 23:10:42 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2023-04-01 19:30:45 +02:00
|
|
|
"time"
|
2023-03-07 23:10:42 +01:00
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/auth"
|
2024-02-26 18:20:40 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
2023-03-07 23:10:42 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
|
2023-07-26 15:14:49 +02:00
|
|
|
"github.com/lestrrat-go/jwx/v2/jwt"
|
2023-03-07 23:10:42 +01:00
|
|
|
"github.com/pkg/errors"
|
2024-02-26 18:20:40 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-03-07 23:10:42 +01:00
|
|
|
)
|
|
|
|
|
2023-04-01 19:30:45 +02:00
|
|
|
const DefaultAcceptableSkew = 5 * time.Minute
|
|
|
|
|
2023-07-26 15:14:49 +02:00
|
|
|
type (
|
2024-02-26 18:20:40 +01:00
|
|
|
GetKeySet func(context.Context) (jwk.Set, error)
|
|
|
|
GetTokenRole func(context.Context, jwt.Token) (string, error)
|
|
|
|
GetTokenTenant func(context.Context, jwt.Token) (string, error)
|
2023-07-26 15:14:49 +02:00
|
|
|
)
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
type Authenticator struct {
|
2023-07-26 15:14:49 +02:00
|
|
|
getKeySet GetKeySet
|
|
|
|
getTokenRole GetTokenRole
|
2024-02-26 18:20:40 +01:00
|
|
|
getTokenTenant GetTokenTenant
|
2023-04-01 19:30:45 +02:00
|
|
|
acceptableSkew time.Duration
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate implements auth.Authenticator.
|
|
|
|
func (a *Authenticator) Authenticate(ctx context.Context, r *http.Request) (auth.User, error) {
|
|
|
|
authorization := r.Header.Get("Authorization")
|
|
|
|
if authorization == "" {
|
|
|
|
return nil, errors.WithStack(auth.ErrUnauthenticated)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawToken := strings.TrimPrefix(authorization, "Bearer ")
|
|
|
|
if rawToken == "" {
|
|
|
|
return nil, errors.WithStack(auth.ErrUnauthenticated)
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:14:49 +02:00
|
|
|
keys, err := a.getKeySet(ctx)
|
2023-03-07 23:10:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:14:49 +02:00
|
|
|
token, err := parseToken(ctx, keys, rawToken, a.acceptableSkew)
|
|
|
|
if err != nil {
|
2024-02-26 18:20:40 +01:00
|
|
|
logger.Debug(ctx, "could not parse jwt token", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
return nil, errors.WithStack(auth.ErrUnauthenticated)
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:14:49 +02:00
|
|
|
rawRole, err := a.getTokenRole(ctx, token)
|
|
|
|
if err != nil {
|
2024-02-26 18:20:40 +01:00
|
|
|
logger.Debug(ctx, "could not retrieve token role", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
return nil, errors.WithStack(auth.ErrUnauthenticated)
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:14:49 +02:00
|
|
|
if !isValidRole(rawRole) {
|
2024-02-26 18:20:40 +01:00
|
|
|
return nil, errors.WithStack(auth.ErrUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawTenantID, err := a.getTokenTenant(ctx, token)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug(ctx, "could not retrieve token tenant", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
return nil, errors.WithStack(auth.ErrUnauthenticated)
|
|
|
|
}
|
|
|
|
|
|
|
|
tenantID, err := datastore.ParseTenantID(rawTenantID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug(ctx, "could not retrieve token tenant", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
return nil, errors.WithStack(auth.ErrUnauthenticated)
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
user := &User{
|
2024-02-26 18:20:40 +01:00
|
|
|
subject: token.Subject(),
|
|
|
|
role: Role(rawRole),
|
|
|
|
tenantID: tenantID,
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 18:20:40 +01:00
|
|
|
func NewAuthenticator(getKeySet GetKeySet, getTokenRole GetTokenRole, getTokenTenant GetTokenTenant, acceptableSkew time.Duration) *Authenticator {
|
2023-03-07 23:10:42 +01:00
|
|
|
return &Authenticator{
|
2023-07-26 15:14:49 +02:00
|
|
|
getTokenRole: getTokenRole,
|
2024-02-26 18:20:40 +01:00
|
|
|
getTokenTenant: getTokenTenant,
|
2023-07-26 15:14:49 +02:00
|
|
|
getKeySet: getKeySet,
|
2023-04-01 19:30:45 +02:00
|
|
|
acceptableSkew: acceptableSkew,
|
2023-03-07 23:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ auth.Authenticator = &Authenticator{}
|