2019-04-24 21:52:08 +00:00
package handlers
import (
"fmt"
"net/url"
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/duo"
"github.com/authelia/authelia/v4/internal/middlewares"
2022-03-06 05:47:40 +00:00
"github.com/authelia/authelia/v4/internal/model"
2021-11-29 03:09:14 +00:00
"github.com/authelia/authelia/v4/internal/regulation"
2021-12-01 03:32:58 +00:00
"github.com/authelia/authelia/v4/internal/session"
"github.com/authelia/authelia/v4/internal/utils"
2019-04-24 21:52:08 +00:00
)
2022-04-08 04:13:47 +00:00
// DuoPOST handler for sending a push notification via duo api.
func DuoPOST ( duoAPI duo . API ) middlewares . RequestHandler {
2019-04-24 21:52:08 +00:00
return func ( ctx * middlewares . AutheliaCtx ) {
2021-12-01 03:32:58 +00:00
var (
2022-10-20 02:16:36 +00:00
bodyJSON = & bodySignDuoRequest { }
2021-12-01 03:32:58 +00:00
device , method string
2023-01-25 09:36:40 +00:00
userSession session . UserSession
err error
2021-12-01 03:32:58 +00:00
)
2019-04-24 21:52:08 +00:00
2023-01-25 09:36:40 +00:00
if err = ctx . ParseBody ( bodyJSON ) ; err != nil {
2021-12-02 10:28:16 +00:00
ctx . Logger . Errorf ( logFmtErrParseRequestBody , regulation . AuthTypeDuo , err )
2021-11-29 03:09:14 +00:00
respondUnauthorized ( ctx , messageMFAValidationFailed )
2019-04-24 21:52:08 +00:00
return
}
2023-01-25 09:36:40 +00:00
if userSession , err = ctx . GetSession ( ) ; err != nil {
ctx . Error ( fmt . Errorf ( "error occurred retrieving user session: %w" , err ) , messageMFAValidationFailed )
return
}
2020-03-01 00:51:11 +00:00
remoteIP := ctx . RemoteIP ( ) . String ( )
2021-12-01 03:32:58 +00:00
duoDevice , err := ctx . Providers . StorageProvider . LoadPreferredDuoDevice ( ctx , userSession . Username )
if err != nil {
ctx . Logger . Debugf ( "Error identifying preferred device for user %s: %s" , userSession . Username , err )
ctx . Logger . Debugf ( "Starting Duo PreAuth for initial device selection of user: %s" , userSession . Username )
2022-07-26 05:43:39 +00:00
device , method , err = HandleInitialDeviceSelection ( ctx , & userSession , duoAPI , bodyJSON )
2021-12-01 03:32:58 +00:00
} else {
ctx . Logger . Debugf ( "Starting Duo PreAuth to check preferred device of user: %s" , userSession . Username )
2022-07-26 05:43:39 +00:00
device , method , err = HandlePreferredDeviceCheck ( ctx , & userSession , duoAPI , duoDevice . Device , duoDevice . Method , bodyJSON )
2021-12-01 03:32:58 +00:00
}
2021-11-29 03:09:14 +00:00
2021-12-01 03:32:58 +00:00
if err != nil {
ctx . Error ( err , messageMFAValidationFailed )
return
}
2020-05-05 19:35:32 +00:00
2021-12-01 03:32:58 +00:00
if device == "" || method == "" {
return
2019-04-24 21:52:08 +00:00
}
2021-12-01 03:32:58 +00:00
ctx . Logger . Debugf ( "Starting Duo Auth attempt for %s with device %s and method %s from IP %s" , userSession . Username , device , method , remoteIP )
2022-07-26 05:43:39 +00:00
values , err := SetValues ( userSession , device , method , remoteIP , bodyJSON . TargetURL , bodyJSON . Passcode )
2019-04-24 21:52:08 +00:00
if err != nil {
2021-12-01 03:32:58 +00:00
ctx . Logger . Errorf ( "Failed to set values for Duo Auth Call for user '%s': %+v" , userSession . Username , err )
2021-11-29 03:09:14 +00:00
respondUnauthorized ( ctx , messageMFAValidationFailed )
2019-04-24 21:52:08 +00:00
return
}
2023-01-25 09:36:40 +00:00
authResponse , err := duoAPI . AuthCall ( ctx , & userSession , values )
2021-12-01 03:32:58 +00:00
if err != nil {
ctx . Logger . Errorf ( "Failed to perform Duo Auth Call for user '%s': %+v" , userSession . Username , err )
respondUnauthorized ( ctx , messageMFAValidationFailed )
return
2020-03-01 00:51:11 +00:00
}
2021-12-01 03:32:58 +00:00
if authResponse . Result != allow {
2021-12-02 10:28:16 +00:00
_ = markAuthenticationAttempt ( ctx , false , nil , userSession . Username , regulation . AuthTypeDuo ,
2021-12-01 03:32:58 +00:00
fmt . Errorf ( "duo auth result: %s, status: %s, message: %s" , authResponse . Result , authResponse . Status ,
authResponse . StatusMessage ) )
2021-11-29 03:09:14 +00:00
respondUnauthorized ( ctx , messageMFAValidationFailed )
2019-04-24 21:52:08 +00:00
return
}
2021-12-02 10:28:16 +00:00
if err = markAuthenticationAttempt ( ctx , true , nil , userSession . Username , regulation . AuthTypeDuo , nil ) ; err != nil {
2021-11-29 03:09:14 +00:00
respondUnauthorized ( ctx , messageMFAValidationFailed )
return
}
2023-01-25 09:36:40 +00:00
HandleAllow ( ctx , & userSession , bodyJSON )
2021-12-01 03:32:58 +00:00
}
}
2021-11-29 03:09:14 +00:00
2021-12-01 03:32:58 +00:00
// HandleInitialDeviceSelection handler for retrieving all available devices.
2022-10-20 02:16:36 +00:00
func HandleInitialDeviceSelection ( ctx * middlewares . AutheliaCtx , userSession * session . UserSession , duoAPI duo . API , bodyJSON * bodySignDuoRequest ) ( device string , method string , err error ) {
2023-01-25 09:36:40 +00:00
result , message , devices , enrollURL , err := DuoPreAuth ( ctx , userSession , duoAPI )
2021-12-01 03:32:58 +00:00
if err != nil {
ctx . Logger . Errorf ( "Failed to perform Duo PreAuth for user '%s': %+v" , userSession . Username , err )
2020-02-29 23:13:33 +00:00
2021-12-01 03:32:58 +00:00
respondUnauthorized ( ctx , messageMFAValidationFailed )
return "" , "" , err
}
switch result {
case enroll :
ctx . Logger . Debugf ( "Duo user: %s not enrolled" , userSession . Username )
if err := ctx . SetJSONBody ( DuoSignResponse { Result : enroll , EnrollURL : enrollURL } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
2020-02-29 23:13:33 +00:00
}
2021-12-01 03:32:58 +00:00
return "" , "" , nil
case deny :
ctx . Logger . Infof ( "Duo user: %s not allowed to authenticate: %s" , userSession . Username , message )
2019-04-24 21:52:08 +00:00
2021-12-01 03:32:58 +00:00
if err := ctx . SetJSONBody ( DuoSignResponse { Result : deny } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
}
return "" , "" , nil
case allow :
ctx . Logger . Debugf ( "Duo authentication was bypassed for user: %s" , userSession . Username )
2023-01-25 09:36:40 +00:00
HandleAllow ( ctx , userSession , bodyJSON )
2021-12-01 03:32:58 +00:00
return "" , "" , nil
case auth :
device , method , err = HandleAutoSelection ( ctx , devices , userSession . Username )
2019-04-24 21:52:08 +00:00
if err != nil {
2021-12-01 03:32:58 +00:00
return "" , "" , err
}
2021-11-29 03:09:14 +00:00
2021-12-01 03:32:58 +00:00
return device , method , nil
}
2021-11-29 03:09:14 +00:00
2021-12-01 03:32:58 +00:00
return "" , "" , fmt . Errorf ( "unknown result: %s" , result )
}
// HandlePreferredDeviceCheck handler to check if the saved device and method is still valid.
2022-10-20 02:16:36 +00:00
func HandlePreferredDeviceCheck ( ctx * middlewares . AutheliaCtx , userSession * session . UserSession , duoAPI duo . API , device string , method string , bodyJSON * bodySignDuoRequest ) ( string , string , error ) {
2023-01-25 09:36:40 +00:00
result , message , devices , enrollURL , err := DuoPreAuth ( ctx , userSession , duoAPI )
2021-12-01 03:32:58 +00:00
if err != nil {
ctx . Logger . Errorf ( "Failed to perform Duo PreAuth for user '%s': %+v" , userSession . Username , err )
respondUnauthorized ( ctx , messageMFAValidationFailed )
return "" , "" , nil
}
switch result {
case enroll :
ctx . Logger . Debugf ( "Duo user: %s no longer enrolled removing preferred device" , userSession . Username )
if err := ctx . Providers . StorageProvider . DeletePreferredDuoDevice ( ctx , userSession . Username ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to delete preferred Duo device and method for user %s: %s" , userSession . Username , err )
}
if err := ctx . SetJSONBody ( DuoSignResponse { Result : enroll , EnrollURL : enrollURL } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
}
return "" , "" , nil
case deny :
ctx . Logger . Infof ( "Duo user: %s not allowed to authenticate: %s" , userSession . Username , message )
ctx . ReplyUnauthorized ( )
return "" , "" , nil
case allow :
ctx . Logger . Debugf ( "Duo authentication was bypassed for user: %s" , userSession . Username )
2023-01-25 09:36:40 +00:00
HandleAllow ( ctx , userSession , bodyJSON )
2021-12-01 03:32:58 +00:00
return "" , "" , nil
case auth :
if devices == nil {
ctx . Logger . Debugf ( "Duo user: %s has no compatible device/method available removing preferred device" , userSession . Username )
if err := ctx . Providers . StorageProvider . DeletePreferredDuoDevice ( ctx , userSession . Username ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to delete preferred Duo device and method for user %s: %s" , userSession . Username , err )
}
if err := ctx . SetJSONBody ( DuoSignResponse { Result : enroll } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
}
return "" , "" , nil
}
if len ( devices ) > 0 {
for i := range devices {
if devices [ i ] . Device == device {
if utils . IsStringInSlice ( method , devices [ i ] . Capabilities ) {
return device , method , nil
}
}
}
}
return HandleAutoSelection ( ctx , devices , userSession . Username )
}
return "" , "" , fmt . Errorf ( "unknown result: %s" , result )
}
// HandleAutoSelection handler automatically selects preferred device if there is only one suitable option.
func HandleAutoSelection ( ctx * middlewares . AutheliaCtx , devices [ ] DuoDevice , username string ) ( string , string , error ) {
if devices == nil {
ctx . Logger . Debugf ( "No compatible device/method available for Duo user: %s" , username )
if err := ctx . SetJSONBody ( DuoSignResponse { Result : enroll } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
}
return "" , "" , nil
}
if len ( devices ) > 1 {
ctx . Logger . Debugf ( "Multiple devices available for Duo user: %s require manual selection" , username )
if err := ctx . SetJSONBody ( DuoSignResponse { Result : auth , Devices : devices } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
}
return "" , "" , nil
}
if len ( devices [ 0 ] . Capabilities ) > 1 {
ctx . Logger . Debugf ( "Multiple methods available for Duo user: %s require manual selection" , username )
if err := ctx . SetJSONBody ( DuoSignResponse { Result : auth , Devices : devices } ) ; err != nil {
return "" , "" , fmt . Errorf ( "unable to set JSON body in response" )
2019-04-24 21:52:08 +00:00
}
2021-12-01 03:32:58 +00:00
return "" , "" , nil
}
device := devices [ 0 ] . Device
method := devices [ 0 ] . Capabilities [ 0 ]
ctx . Logger . Debugf ( "Exactly one device: '%s' and method: '%s' found, saving as new preferred Duo device and method for user: %s" , device , method , username )
2022-03-06 05:47:40 +00:00
if err := ctx . Providers . StorageProvider . SavePreferredDuoDevice ( ctx , model . DuoDevice { Username : username , Method : method , Device : device } ) ; err != nil {
2021-12-01 03:32:58 +00:00
return "" , "" , fmt . Errorf ( "unable to save new preferred Duo device and method for user %s: %s" , username , err )
}
return device , method , nil
}
// HandleAllow handler for successful logins.
2023-01-25 09:36:40 +00:00
func HandleAllow ( ctx * middlewares . AutheliaCtx , userSession * session . UserSession , bodyJSON * bodySignDuoRequest ) {
var (
err error
)
2021-12-01 03:32:58 +00:00
2023-01-25 09:36:40 +00:00
if err = ctx . RegenerateSession ( ) ; err != nil {
2021-12-02 10:28:16 +00:00
ctx . Logger . Errorf ( logFmtErrSessionRegenerate , regulation . AuthTypeDuo , userSession . Username , err )
2021-12-01 03:32:58 +00:00
respondUnauthorized ( ctx , messageMFAValidationFailed )
return
}
2022-04-01 11:18:58 +00:00
userSession . SetTwoFactorDuo ( ctx . Clock . Now ( ) )
2021-12-01 03:32:58 +00:00
2023-01-25 09:36:40 +00:00
if err = ctx . SaveSession ( * userSession ) ; err != nil {
2021-12-01 03:32:58 +00:00
ctx . Logger . Errorf ( logFmtErrSessionSave , "authentication time" , regulation . AuthTypeTOTP , userSession . Username , err )
respondUnauthorized ( ctx , messageMFAValidationFailed )
return
}
2022-07-26 05:43:39 +00:00
if bodyJSON . Workflow == workflowOpenIDConnect {
2022-10-20 02:16:36 +00:00
handleOIDCWorkflowResponse ( ctx , bodyJSON . TargetURL , bodyJSON . WorkflowID )
2021-12-01 03:32:58 +00:00
} else {
2022-07-26 05:43:39 +00:00
Handle2FAResponse ( ctx , bodyJSON . TargetURL )
2021-12-01 03:32:58 +00:00
}
}
// SetValues sets all appropriate Values for the Auth Request.
func SetValues ( userSession session . UserSession , device string , method string , remoteIP string , targetURL string , passcode string ) ( url . Values , error ) {
values := url . Values { }
values . Set ( "username" , userSession . Username )
values . Set ( "ipaddr" , remoteIP )
values . Set ( "factor" , method )
switch method {
case duo . Push :
values . Set ( "device" , device )
if userSession . DisplayName != "" {
values . Set ( "display_username" , userSession . DisplayName )
}
if targetURL != "" {
values . Set ( "pushinfo" , fmt . Sprintf ( "target%%20url=%s" , targetURL ) )
}
case duo . Phone :
values . Set ( "device" , device )
case duo . SMS :
values . Set ( "device" , device )
case duo . OTP :
if passcode != "" {
values . Set ( "passcode" , passcode )
2021-05-04 22:06:05 +00:00
} else {
2021-12-01 03:32:58 +00:00
return nil , fmt . Errorf ( "no passcode received from user: %s" , userSession . Username )
2021-05-04 22:06:05 +00:00
}
2019-04-24 21:52:08 +00:00
}
2021-12-01 03:32:58 +00:00
return values , nil
2019-04-24 21:52:08 +00:00
}