2019-12-07 11:18:22 +00:00
package handlers
import (
"fmt"
"strings"
"sync"
2020-04-05 12:37:21 +00:00
"github.com/sirupsen/logrus"
2019-12-24 02:14:52 +00:00
"github.com/authelia/authelia/internal/authentication"
"github.com/authelia/authelia/internal/middlewares"
"github.com/authelia/authelia/internal/storage"
"github.com/authelia/authelia/internal/utils"
2019-12-07 11:18:22 +00:00
)
2020-01-21 00:10:00 +00:00
func loadInfo ( username string , storageProvider storage . Provider , preferences * UserPreferences , logger * logrus . Entry ) [ ] error {
2019-12-07 11:18:22 +00:00
var wg sync . WaitGroup
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
wg . Add ( 3 )
errors := make ( [ ] error , 0 )
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
go func ( ) {
defer wg . Done ( )
2020-05-05 19:35:32 +00:00
2020-01-21 00:10:00 +00:00
method , err := storageProvider . LoadPreferred2FAMethod ( username )
2019-12-07 11:18:22 +00:00
if err != nil {
errors = append ( errors , err )
logger . Error ( err )
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
return
}
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
if method == "" {
preferences . Method = authentication . PossibleMethods [ 0 ]
} else {
preferences . Method = method
}
} ( )
go func ( ) {
defer wg . Done ( )
2020-05-05 19:35:32 +00:00
2020-01-21 00:10:00 +00:00
_ , _ , err := storageProvider . LoadU2FDeviceHandle ( username )
2019-12-07 11:18:22 +00:00
if err != nil {
if err == storage . ErrNoU2FDeviceHandle {
return
}
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
errors = append ( errors , err )
logger . Error ( err )
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
return
}
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
preferences . HasU2F = true
} ( )
go func ( ) {
defer wg . Done ( )
2020-05-05 19:35:32 +00:00
2020-01-21 00:10:00 +00:00
_ , err := storageProvider . LoadTOTPSecret ( username )
2019-12-07 11:18:22 +00:00
if err != nil {
if err == storage . ErrNoTOTPSecret {
return
}
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
errors = append ( errors , err )
logger . Error ( err )
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
return
}
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
preferences . HasTOTP = true
} ( )
wg . Wait ( )
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
return errors
}
2020-01-21 00:10:00 +00:00
// UserInfoGet get the info related to the user identified by the session.
2019-12-07 11:18:22 +00:00
func UserInfoGet ( ctx * middlewares . AutheliaCtx ) {
userSession := ctx . GetSession ( )
preferences := UserPreferences { }
errors := loadInfo ( userSession . Username , ctx . Providers . StorageProvider , & preferences , ctx . Logger )
if len ( errors ) > 0 {
2019-12-07 17:51:47 +00:00
ctx . Error ( fmt . Errorf ( "Unable to load user information" ) , operationFailedMessage )
2019-12-07 11:18:22 +00:00
return
}
2020-05-05 19:35:32 +00:00
2020-04-22 03:33:14 +00:00
ctx . SetJSONBody ( preferences ) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
2019-12-07 11:18:22 +00:00
}
2020-04-20 21:03:38 +00:00
// MethodBody the selected 2FA method.
2019-12-07 11:18:22 +00:00
type MethodBody struct {
Method string ` json:"method" valid:"required" `
}
// MethodPreferencePost update the user preferences regarding 2FA method.
func MethodPreferencePost ( ctx * middlewares . AutheliaCtx ) {
bodyJSON := MethodBody { }
2020-05-05 19:35:32 +00:00
2019-12-07 11:18:22 +00:00
err := ctx . ParseBody ( & bodyJSON )
if err != nil {
ctx . Error ( err , operationFailedMessage )
return
}
if ! utils . IsStringInSlice ( bodyJSON . Method , authentication . PossibleMethods ) {
ctx . Error ( fmt . Errorf ( "Unknown method '%s', it should be one of %s" , bodyJSON . Method , strings . Join ( authentication . PossibleMethods , ", " ) ) , operationFailedMessage )
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 )
err = ctx . Providers . StorageProvider . SavePreferred2FAMethod ( userSession . Username , bodyJSON . Method )
2019-12-07 11:18:22 +00:00
if err != nil {
2020-01-05 23:03:16 +00:00
ctx . Error ( fmt . Errorf ( "Unable to save new preferred 2FA method: %s" , err ) , operationFailedMessage )
2019-12-07 11:18:22 +00:00
return
}
ctx . ReplyOK ( )
}