2019-04-24 21:52:08 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-06-18 00:39:19 +00:00
|
|
|
"github.com/golang-jwt/jwt"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/templates"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IdentityVerificationStart the handler for initiating the identity validation process.
|
|
|
|
func IdentityVerificationStart(args IdentityVerificationStartArgs) RequestHandler {
|
|
|
|
if args.IdentityRetrieverFunc == nil {
|
|
|
|
panic(fmt.Errorf("Identity verification requires an identity retriever"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx *AutheliaCtx) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the claim with the action to sign it.
|
|
|
|
claims := &IdentityVerificationClaim{
|
|
|
|
jwt.StandardClaims{
|
|
|
|
ExpiresAt: time.Now().Add(5 * time.Minute).Unix(),
|
|
|
|
Issuer: jwtIssuer,
|
|
|
|
},
|
|
|
|
args.ActionClaim,
|
|
|
|
identity.Username,
|
|
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
ss, err := token.SignedString([]byte(ctx.Configuration.JWTSecret))
|
|
|
|
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ctx.Providers.StorageProvider.SaveIdentityVerificationToken(ss)
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
uri, err := ctx.ForwardedProtoHost()
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2020-02-13 02:12:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
link := fmt.Sprintf("%s%s%s?token=%s", uri, ctx.Configuration.Server.Path, args.TargetEndpoint, ss)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-08-21 02:16:23 +00:00
|
|
|
bufHTML := new(bytes.Buffer)
|
|
|
|
|
|
|
|
disableHTML := false
|
|
|
|
if ctx.Configuration.Notifier != nil && ctx.Configuration.Notifier.SMTP != nil {
|
|
|
|
disableHTML = ctx.Configuration.Notifier.SMTP.DisableHTMLEmails
|
|
|
|
}
|
|
|
|
|
|
|
|
if !disableHTML {
|
|
|
|
htmlParams := map[string]interface{}{
|
|
|
|
"title": args.MailTitle,
|
|
|
|
"url": link,
|
|
|
|
"button": args.MailButtonContent,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = templates.HTMLEmailTemplate.Execute(bufHTML, htmlParams)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2020-08-21 02:16:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bufText := new(bytes.Buffer)
|
|
|
|
textParams := map[string]interface{}{
|
|
|
|
"url": link,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-08-21 02:16:23 +00:00
|
|
|
|
|
|
|
err = templates.PlainTextEmailTemplate.Execute(bufText, textParams)
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-08-21 02:16:23 +00:00
|
|
|
err = ctx.Providers.Notifier.Send(identity.Email, args.MailTitle, bufText.String(), bufHTML.String())
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.ReplyOK()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityVerificationFinish the middleware for finishing the identity validation process.
|
|
|
|
func IdentityVerificationFinish(args IdentityVerificationFinishArgs, next func(ctx *AutheliaCtx, username string)) RequestHandler {
|
|
|
|
return func(ctx *AutheliaCtx) {
|
|
|
|
var finishBody IdentityVerificationFinishBody
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
b := ctx.PostBody()
|
|
|
|
|
|
|
|
err := json.Unmarshal(b, &finishBody)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if finishBody.Token == "" {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("No token provided"), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
found, err := ctx.Providers.StorageProvider.FindIdentityVerificationToken(finishBody.Token)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
ctx.Error(fmt.Errorf("Token is not in DB, it might have already been used"),
|
2021-07-22 03:52:37 +00:00
|
|
|
messageIdentityVerificationTokenAlreadyUsed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := jwt.ParseWithClaims(finishBody.Token, &IdentityVerificationClaim{},
|
|
|
|
func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return []byte(ctx.Configuration.JWTSecret), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
2020-05-06 00:52:06 +00:00
|
|
|
switch {
|
|
|
|
case ve.Errors&jwt.ValidationErrorMalformed != 0:
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Cannot parse token"), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
2020-05-06 00:52:06 +00:00
|
|
|
case ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0:
|
2019-04-24 21:52:08 +00:00
|
|
|
// Token is either expired or not active yet
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Token expired"), messageIdentityVerificationTokenHasExpired)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
2020-05-06 00:52:06 +00:00
|
|
|
default:
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Cannot handle this token: %s", ve), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(err, messageOperationFailed)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, ok := token.Claims.(*IdentityVerificationClaim)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(c.michaud): find a way to garbage collect unused tokens.
|
|
|
|
err = ctx.Providers.StorageProvider.RemoveIdentityVerificationToken(finishBody.Token)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|