2019-04-24 21:52:08 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-07-18 00:56:09 +00:00
|
|
|
"net/mail"
|
2022-01-20 23:46:13 +00:00
|
|
|
"time"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2021-08-03 21:38:07 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2021-12-04 04:48:22 +00:00
|
|
|
"github.com/google/uuid"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
2022-03-06 05:47:40 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/templates"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
// Return true if skip enabled at TwoFactor auth level and user's auth level is 2FA, false otherwise.
|
|
|
|
func shouldSkipIdentityVerification(args IdentityVerificationCommonArgs, ctx *AutheliaCtx) bool {
|
|
|
|
return args.SkipIfAuthLevelTwoFactor && ctx.GetSession().AuthenticationLevel >= authentication.TwoFactor
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// IdentityVerificationStart the handler for initiating the identity validation process.
|
2022-01-20 23:46:13 +00:00
|
|
|
func IdentityVerificationStart(args IdentityVerificationStartArgs, delayFunc TimingAttackDelayFunc) RequestHandler {
|
2019-04-24 21:52:08 +00:00
|
|
|
if args.IdentityRetrieverFunc == nil {
|
|
|
|
panic(fmt.Errorf("Identity verification requires an identity retriever"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx *AutheliaCtx) {
|
2022-11-19 05:48:47 +00:00
|
|
|
if shouldSkipIdentityVerification(args.IdentityVerificationCommonArgs, ctx) {
|
|
|
|
ctx.ReplyOK()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:46:13 +00:00
|
|
|
requestTime := time.Now()
|
|
|
|
success := false
|
|
|
|
|
|
|
|
if delayFunc != nil {
|
2022-06-14 07:20:13 +00:00
|
|
|
defer delayFunc(ctx, requestTime, &success)
|
2022-01-20 23:46:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
identity, err := args.IdentityRetrieverFunc(ctx)
|
|
|
|
if err != nil {
|
|
|
|
// In that case we reply ok to avoid user enumeration.
|
|
|
|
ctx.Logger.Error(err)
|
|
|
|
ctx.ReplyOK()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-04 04:48:22 +00:00
|
|
|
var jti uuid.UUID
|
|
|
|
|
2022-03-16 00:29:46 +00:00
|
|
|
if jti, err = uuid.NewRandom(); err != nil {
|
2021-12-04 04:48:22 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
verification := model.NewIdentityVerification(jti, identity.Username, args.ActionClaim, ctx.RemoteIP())
|
2021-11-30 06:58:21 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// Create the claim with the action to sign it.
|
2021-11-30 06:58:21 +00:00
|
|
|
claims := verification.ToIdentityVerificationClaim()
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
|
2022-05-08 22:43:12 +00:00
|
|
|
ss, err := token.SignedString([]byte(ctx.Configuration.JWTSecret))
|
2019-04-24 21:52:08 +00:00
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-08 22:43:12 +00:00
|
|
|
if err = ctx.Providers.StorageProvider.SaveIdentityVerification(ctx, verification); err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-26 21:39:20 +00:00
|
|
|
var (
|
|
|
|
uri string
|
|
|
|
)
|
|
|
|
|
|
|
|
if uri, err = ctx.ExternalRootURL(); err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2020-02-13 02:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-21 02:16:23 +00:00
|
|
|
disableHTML := false
|
2022-04-15 23:34:26 +00:00
|
|
|
if ctx.Configuration.Notifier.SMTP != nil {
|
2020-08-21 02:16:23 +00:00
|
|
|
disableHTML = ctx.Configuration.Notifier.SMTP.DisableHTMLEmails
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
values := templates.EmailIdentityVerificationValues{
|
|
|
|
Title: args.MailTitle,
|
2022-08-26 21:39:20 +00:00
|
|
|
LinkURL: fmt.Sprintf("%s%s?token=%s", uri, args.TargetEndpoint, ss),
|
2022-07-18 00:56:09 +00:00
|
|
|
LinkText: args.MailButtonContent,
|
|
|
|
DisplayName: identity.DisplayName,
|
|
|
|
RemoteIP: ctx.RemoteIP().String(),
|
2022-05-08 22:43:12 +00:00
|
|
|
}
|
2020-08-21 02:16:23 +00:00
|
|
|
|
2022-08-26 21:39:20 +00:00
|
|
|
bufHTML, bufText := &bytes.Buffer{}, &bytes.Buffer{}
|
|
|
|
|
2022-05-08 22:43:12 +00:00
|
|
|
if !disableHTML {
|
2022-07-18 00:56:09 +00:00
|
|
|
if err = ctx.Providers.Templates.ExecuteEmailIdentityVerificationTemplate(bufHTML, values, templates.HTMLFormat); err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2020-08-21 02:16:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
if err = ctx.Providers.Templates.ExecuteEmailIdentityVerificationTemplate(bufText, values, templates.PlainTextFormat); err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:05:46 +00:00
|
|
|
ctx.Logger.Debugf("Sending an email to user %s (%s) to confirm identity for registering a device.",
|
2019-04-24 21:52:08 +00:00
|
|
|
identity.Username, identity.Email)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-08-26 21:39:20 +00:00
|
|
|
if err = ctx.Providers.Notifier.Send(mail.Address{Name: identity.DisplayName, Address: identity.Email}, args.MailTitle, bufText.Bytes(), bufHTML.Bytes()); err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:46:13 +00:00
|
|
|
success = true
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
ctx.ReplyOK()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
func identityVerificationValidateToken(ctx *AutheliaCtx) (*jwt.Token, error) {
|
|
|
|
var finishBody IdentityVerificationFinishBody
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
b := ctx.PostBody()
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
err := json.Unmarshal(b, &finishBody)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(err, messageOperationFailed)
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
if finishBody.Token == "" {
|
|
|
|
ctx.Error(fmt.Errorf("No token provided"), messageOperationFailed)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := jwt.ParseWithClaims(finishBody.Token, &model.IdentityVerificationClaim{},
|
|
|
|
func(token *jwt.Token) (any, error) {
|
|
|
|
return []byte(ctx.Configuration.JWTSecret), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
|
|
|
switch {
|
|
|
|
case ve.Errors&jwt.ValidationErrorMalformed != 0:
|
|
|
|
ctx.Error(fmt.Errorf("Cannot parse token"), messageOperationFailed)
|
|
|
|
return nil, err
|
|
|
|
case ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0:
|
|
|
|
// Token is either expired or not active yet.
|
|
|
|
ctx.Error(fmt.Errorf("Token expired"), messageIdentityVerificationTokenHasExpired)
|
|
|
|
return nil, err
|
|
|
|
default:
|
|
|
|
ctx.Error(fmt.Errorf("Cannot handle this token: %s", ve), messageOperationFailed)
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityVerificationFinish the middleware for finishing the identity validation process.
|
|
|
|
func IdentityVerificationFinish(args IdentityVerificationFinishArgs, next func(ctx *AutheliaCtx, username string)) RequestHandler {
|
|
|
|
return func(ctx *AutheliaCtx) {
|
|
|
|
if shouldSkipIdentityVerification(args.IdentityVerificationCommonArgs, ctx) {
|
|
|
|
next(ctx, "")
|
|
|
|
return
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-11-19 05:48:47 +00:00
|
|
|
token, err := identityVerificationValidateToken(ctx)
|
|
|
|
if token == nil || err != nil {
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
claims, ok := token.Claims.(*model.IdentityVerificationClaim)
|
2019-04-24 21:52:08 +00:00
|
|
|
if !ok {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Wrong type of claims (%T != *middlewares.IdentityVerificationClaim)", claims), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-30 06:58:21 +00:00
|
|
|
verification, err := claims.ToIdentityVerification()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Token seems to be invalid: %w", err),
|
|
|
|
messageOperationFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
found, err := ctx.Providers.StorageProvider.FindIdentityVerification(ctx, verification.JTI.String())
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(err, messageOperationFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
ctx.Error(fmt.Errorf("Token is not in DB, it might have already been used"),
|
|
|
|
messageIdentityVerificationTokenAlreadyUsed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// Verify that the action claim in the token is the one expected for the given endpoint.
|
|
|
|
if claims.Action != args.ActionClaim {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("This token has not been generated for this kind of action"), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.IsTokenUserValidFunc != nil && !args.IsTokenUserValidFunc(ctx, claims.Username) {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("This token has not been generated for this user"), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
err = ctx.Providers.StorageProvider.ConsumeIdentityVerification(ctx, claims.ID, model.NewNullIP(ctx.RemoteIP()))
|
2019-04-24 21:52:08 +00:00
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next(ctx, claims.Username)
|
|
|
|
}
|
|
|
|
}
|