2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecondFactorTOTPPost validate the TOTP passcode provided by the user.
|
2020-02-01 19:54:50 +07:00
|
|
|
func SecondFactorTOTPPost(totpVerifier TOTPVerifier) middlewares.RequestHandler {
|
|
|
|
return func(ctx *middlewares.AutheliaCtx) {
|
|
|
|
bodyJSON := signTOTPRequestBody{}
|
|
|
|
err := ctx.ParseBody(&bodyJSON)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-02-01 19:54:50 +07:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(err, mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-02-01 19:54:50 +07: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-25 04:52:08 +07:00
|
|
|
|
2020-02-01 19:54:50 +07:00
|
|
|
isValid := totpVerifier.Verify(bodyJSON.Token, secret)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-02-01 19:54:50 +07:00
|
|
|
if !isValid {
|
|
|
|
ctx.Error(fmt.Errorf("Wrong passcode during TOTP validation for user %s", userSession.Username), mfaValidationFailedMessage)
|
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-02-01 19:54:50 +07:00
|
|
|
userSession.AuthenticationLevel = authentication.TwoFactor
|
|
|
|
err = ctx.SaveSession(userSession)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2020-02-01 19:54:50 +07:00
|
|
|
ctx.Error(fmt.Errorf("Unable to update the authentication level with TOTP: %s", err), mfaValidationFailedMessage)
|
2019-04-25 04:52:08 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:18:02 +07:00
|
|
|
Handle2FAResponse(ctx, bodyJSON.TargetURL)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
}
|