2021-12-01 12:11:29 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2023-01-25 09:36:40 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
2021-12-01 12:11:29 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/storage"
|
|
|
|
)
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
// UserTOTPInfoGET returns the users TOTP configuration.
|
|
|
|
func UserTOTPInfoGET(ctx *middlewares.AutheliaCtx) {
|
2023-01-25 09:36:40 +00:00
|
|
|
var (
|
|
|
|
userSession session.UserSession
|
|
|
|
err error
|
|
|
|
)
|
2021-12-01 12:11:29 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if userSession, err = ctx.GetSession(); err != nil {
|
|
|
|
ctx.Logger.WithError(err).Error("Error occurred retrieving user session")
|
|
|
|
|
|
|
|
ctx.ReplyForbidden()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var config *model.TOTPConfiguration
|
|
|
|
|
|
|
|
if config, err = ctx.Providers.StorageProvider.LoadTOTPConfiguration(ctx, userSession.Username); err != nil {
|
2021-12-01 12:11:29 +00:00
|
|
|
if errors.Is(err, storage.ErrNoTOTPConfiguration) {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
2021-12-02 10:28:16 +00:00
|
|
|
ctx.SetJSONError("Could not find TOTP Configuration for user.")
|
|
|
|
ctx.Logger.Errorf("Failed to lookup TOTP configuration for user '%s'", userSession.Username)
|
2021-12-01 12:11:29 +00:00
|
|
|
} else {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
2021-12-02 10:28:16 +00:00
|
|
|
ctx.SetJSONError("Could not find TOTP Configuration for user.")
|
|
|
|
ctx.Logger.Errorf("Failed to lookup TOTP configuration for user '%s' with unknown error: %v", userSession.Username, err)
|
2021-12-01 12:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ctx.SetJSONBody(config); err != nil {
|
|
|
|
ctx.Logger.Errorf("Unable to perform TOTP configuration response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
|
|
|
}
|