2019-04-24 21:52:08 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecondFactorTOTPPost validate the TOTP passcode provided by the user.
|
2020-02-01 12:54:50 +00:00
|
|
|
func SecondFactorTOTPPost(totpVerifier TOTPVerifier) middlewares.RequestHandler {
|
|
|
|
return func(ctx *middlewares.AutheliaCtx) {
|
|
|
|
bodyJSON := signTOTPRequestBody{}
|
|
|
|
err := ctx.ParseBody(&bodyJSON)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-02-01 12:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(err, mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-02-01 12:54:50 +00:00
|
|
|
userSession := ctx.GetSession()
|
|
|
|
secret, err := ctx.Providers.StorageProvider.LoadTOTPSecret(userSession.Username)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to load TOTP secret: %s", err), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-02-01 12:54:50 +00:00
|
|
|
isValid := totpVerifier.Verify(bodyJSON.Token, secret)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-02-01 12:54:50 +00:00
|
|
|
if !isValid {
|
|
|
|
ctx.Error(fmt.Errorf("Wrong passcode during TOTP validation for user %s", userSession.Username), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-02-29 23:13:33 +00:00
|
|
|
err = ctx.Providers.SessionProvider.RegenerateSession(ctx.RequestCtx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("Unable to regenerate session for user %s: %s", userSession.Username, err), authenticationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-01 12:54:50 +00:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
err = ctx.SaveSession(userSession)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-02-01 12:54:50 +00:00
|
|
|
ctx.Error(fmt.Errorf("Unable to update the authentication level with TOTP: %s", err), mfaValidationFailedMessage)
|
2019-04-24 21:52:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
Handle2FAResponse(ctx, bodyJSON.TargetURL)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|