2019-12-07 11:18:22 +00:00
package handlers
import (
2022-03-28 01:26:30 +00:00
"database/sql"
"errors"
2019-12-07 11:18:22 +00:00
"fmt"
"strings"
2020-04-05 12:37:21 +00:00
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/middlewares"
2022-03-28 01:26:30 +00:00
"github.com/authelia/authelia/v4/internal/model"
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/utils"
2019-12-07 11:18:22 +00:00
)
2022-03-28 01:26:30 +00:00
// UserInfoPOST handles setting up info for users if necessary when they login.
func UserInfoPOST ( ctx * middlewares . AutheliaCtx ) {
userSession := ctx . GetSession ( )
var (
userInfo model . UserInfo
err error
)
if _ , err = ctx . Providers . StorageProvider . LoadPreferred2FAMethod ( ctx , userSession . Username ) ; err != nil {
if errors . Is ( err , sql . ErrNoRows ) {
if err = ctx . Providers . StorageProvider . SavePreferred2FAMethod ( ctx , userSession . Username , "" ) ; err != nil {
ctx . Error ( fmt . Errorf ( "unable to load user information: %v" , err ) , messageOperationFailed )
}
} else {
ctx . Error ( fmt . Errorf ( "unable to load user information: %v" , err ) , messageOperationFailed )
}
}
if userInfo , err = ctx . Providers . StorageProvider . LoadUserInfo ( ctx , userSession . Username ) ; err != nil {
ctx . Error ( fmt . Errorf ( "unable to load user information: %v" , err ) , messageOperationFailed )
return
}
var (
changed bool
)
2022-04-17 23:58:24 +00:00
if changed = userInfo . SetDefaultPreferred2FAMethod ( ctx . AvailableSecondFactorMethods ( ) , ctx . Configuration . Default2FAMethod ) ; changed {
2022-03-28 01:26:30 +00:00
if err = ctx . Providers . StorageProvider . SavePreferred2FAMethod ( ctx , userSession . Username , userInfo . Method ) ; err != nil {
ctx . Error ( fmt . Errorf ( "unable to save user two factor method: %v" , err ) , messageOperationFailed )
return
}
}
userInfo . DisplayName = userSession . DisplayName
err = ctx . SetJSONBody ( userInfo )
if err != nil {
ctx . Logger . Errorf ( "Unable to set user info response in body: %s" , err )
}
}
// UserInfoGET get the info related to the user identified by the session.
func UserInfoGET ( ctx * middlewares . AutheliaCtx ) {
2019-12-07 11:18:22 +00:00
userSession := ctx . GetSession ( )
2021-11-23 09:45:38 +00:00
userInfo , err := ctx . Providers . StorageProvider . LoadUserInfo ( ctx , userSession . Username )
if err != nil {
ctx . Error ( fmt . Errorf ( "unable to load user information: %v" , err ) , messageOperationFailed )
2019-12-07 11:18:22 +00:00
return
}
2020-05-05 19:35:32 +00:00
2020-06-21 13:40:37 +00:00
userInfo . DisplayName = userSession . DisplayName
2021-11-23 09:45:38 +00:00
err = ctx . SetJSONBody ( userInfo )
2020-12-16 01:47:31 +00:00
if err != nil {
ctx . Logger . Errorf ( "Unable to set user info response in body: %s" , err )
}
2019-12-07 11:18:22 +00:00
}
2022-04-08 04:13:47 +00:00
// MethodPreferencePOST update the user preferences regarding 2FA method.
func MethodPreferencePOST ( ctx * middlewares . AutheliaCtx ) {
2022-10-20 02:16:36 +00:00
bodyJSON := bodyPreferred2FAMethod { }
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
err := ctx . ParseBody ( & bodyJSON )
if err != nil {
2021-07-22 03:52:37 +00:00
ctx . Error ( err , messageOperationFailed )
2019-12-07 11:18:22 +00:00
return
}
2022-03-28 01:26:30 +00:00
if ! utils . IsStringInSlice ( bodyJSON . Method , ctx . AvailableSecondFactorMethods ( ) ) {
ctx . Error ( fmt . Errorf ( "unknown or unavailable method '%s', it should be one of %s" , bodyJSON . Method , strings . Join ( ctx . AvailableSecondFactorMethods ( ) , ", " ) ) , messageOperationFailed )
2019-12-07 11:18:22 +00:00
return
}
userSession := ctx . GetSession ( )
2020-01-05 23:03:16 +00:00
ctx . Logger . Debugf ( "Save new preferred 2FA method of user %s to %s" , userSession . Username , bodyJSON . Method )
2021-11-23 09:45:38 +00:00
err = ctx . Providers . StorageProvider . SavePreferred2FAMethod ( ctx , userSession . Username , bodyJSON . Method )
2019-12-07 11:18:22 +00:00
if err != nil {
2021-09-17 05:53:40 +00:00
ctx . Error ( fmt . Errorf ( "unable to save new preferred 2FA method: %s" , err ) , messageOperationFailed )
2019-12-07 11:18:22 +00:00
return
}
ctx . ReplyOK ( )
}