2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2021-11-29 10:09:14 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/regulation"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecondFactorTOTPPost validate the TOTP passcode provided by the user.
|
2021-12-01 19:11:29 +07:00
|
|
|
func SecondFactorTOTPPost(ctx *middlewares.AutheliaCtx) {
|
|
|
|
requestBody := signTOTPRequestBody{}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if err := ctx.ParseBody(&requestBody); err != nil {
|
|
|
|
ctx.Logger.Errorf(logFmtErrParseRequestBody, regulation.AuthTypeTOTP, err)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
userSession := ctx.GetSession()
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
config, err := ctx.Providers.StorageProvider.LoadTOTPConfiguration(ctx, userSession.Username)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Failed to load TOTP configuration: %+v", err)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
isValid, err := ctx.Providers.TOTP.Validate(requestBody.Token, config)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger.Errorf("Failed to perform TOTP verification: %+v", err)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if !isValid {
|
|
|
|
_ = markAuthenticationAttempt(ctx, false, nil, userSession.Username, regulation.AuthTypeTOTP, nil)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if err = markAuthenticationAttempt(ctx, true, nil, userSession.Username, regulation.AuthTypeTOTP, nil); err != nil {
|
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
|
|
|
return
|
|
|
|
}
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if err = ctx.Providers.SessionProvider.RegenerateSession(ctx.RequestCtx); err != nil {
|
|
|
|
ctx.Logger.Errorf(logFmtErrSessionRegenerate, regulation.AuthTypeTOTP, userSession.Username, err)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
2020-03-01 06:13:33 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
return
|
|
|
|
}
|
2020-03-01 06:13:33 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
userSession.SetTwoFactor(ctx.Clock.Now())
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if err = ctx.SaveSession(userSession); err != nil {
|
|
|
|
ctx.Logger.Errorf(logFmtErrSessionSave, "authentication time", regulation.AuthTypeTOTP, userSession.Username, err)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
2021-11-29 10:09:14 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
return
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
if userSession.OIDCWorkflowSession != nil {
|
|
|
|
handleOIDCWorkflowResponse(ctx)
|
|
|
|
} else {
|
|
|
|
Handle2FAResponse(ctx, requestBody.TargetURL)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
}
|