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
|
|
|
)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
// TimeBasedOneTimePasswordPOST validate the TOTP passcode provided by the user.
|
|
|
|
func TimeBasedOneTimePasswordPOST(ctx *middlewares.AutheliaCtx) {
|
2022-07-26 12:43:39 +07:00
|
|
|
bodyJSON := signTOTPRequestBody{}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-07-26 12:43:39 +07:00
|
|
|
if err := ctx.ParseBody(&bodyJSON); err != nil {
|
2021-12-01 19:11:29 +07:00
|
|
|
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
|
|
|
|
2022-07-26 12:43:39 +07:00
|
|
|
isValid, err := ctx.Providers.TOTP.Validate(bodyJSON.Token, config)
|
2021-12-01 19:11:29 +07:00
|
|
|
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
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
config.UpdateSignInInfo(ctx.Clock.Now())
|
|
|
|
|
|
|
|
if err = ctx.Providers.StorageProvider.UpdateTOTPConfigurationSignIn(ctx, config.ID, config.LastUsedAt); err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to save %s device sign in metadata for user '%s': %v", regulation.AuthTypeTOTP, userSession.Username, err)
|
|
|
|
|
|
|
|
respondUnauthorized(ctx, messageMFAValidationFailed)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-01 18:18:58 +07:00
|
|
|
userSession.SetTwoFactorTOTP(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
|
|
|
|
2022-07-26 12:43:39 +07:00
|
|
|
if bodyJSON.Workflow == workflowOpenIDConnect {
|
|
|
|
handleOIDCWorkflowResponse(ctx, bodyJSON.TargetURL)
|
2021-12-01 19:11:29 +07:00
|
|
|
} else {
|
2022-07-26 12:43:39 +07:00
|
|
|
Handle2FAResponse(ctx, bodyJSON.TargetURL)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
}
|