fix(campus-auth): properly handle login failures
This commit is contained in:
parent
22882c1fa0
commit
ec42d834ee
@ -189,28 +189,33 @@ campusLogin pool mode = AuthPlugin{..}
|
|||||||
searchResults <- findUser conf ldap campusIdent [ldapUserPrincipalName]
|
searchResults <- findUser conf ldap campusIdent [ldapUserPrincipalName]
|
||||||
case searchResults of
|
case searchResults of
|
||||||
[Ldap.SearchEntry (Ldap.Dn userDN) userAttrs]
|
[Ldap.SearchEntry (Ldap.Dn userDN) userAttrs]
|
||||||
| [principalName] <- fold [ v | (k, v) <- userAttrs, k == ldapUserPrincipalName ]
|
| [principalName] <- nub $ fold [ v | (k, v) <- userAttrs, k == ldapUserPrincipalName ]
|
||||||
, Right credsIdent <- Text.decodeUtf8' principalName
|
, Right credsIdent <- Text.decodeUtf8' principalName
|
||||||
-> Right (userDN, credsIdent) <$ Ldap.bind ldap (Ldap.Dn credsIdent) (Ldap.Password $ Text.encodeUtf8 campusPassword)
|
-> handleIf isInvalidCredentials (return . Left) $ do
|
||||||
other -> return $ Left other
|
Ldap.bind ldap (Ldap.Dn credsIdent) . Ldap.Password $ Text.encodeUtf8 campusPassword
|
||||||
|
return . Right $ Right (userDN, credsIdent)
|
||||||
|
other -> return . Right $ Left other
|
||||||
case ldapResult of
|
case ldapResult of
|
||||||
Left err
|
Left err -> do
|
||||||
| LdapError (Ldap.ResponseError (Ldap.ResponseErrorCode _ Ldap.InvalidCredentials _ _)) <- err
|
$logErrorS apName $ "Error during login: " <> tshow err
|
||||||
-> do
|
|
||||||
$logDebugS apName "Invalid credentials"
|
|
||||||
observeLoginOutcome apName LoginInvalidCredentials
|
|
||||||
loginErrorMessageI LoginR Msg.InvalidLogin
|
|
||||||
| otherwise -> do
|
|
||||||
$logErrorS apName $ "Error during login: " <> tshow err
|
|
||||||
observeLoginOutcome apName LoginError
|
|
||||||
loginErrorMessageI LoginR Msg.AuthError
|
|
||||||
Right (Right (userDN, credsIdent)) -> do
|
|
||||||
observeLoginOutcome apName LoginSuccessful
|
|
||||||
setCredsRedirect $ Creds apName credsIdent [("DN", userDN)]
|
|
||||||
Right (Left searchResults) -> do
|
|
||||||
$logWarnS apName $ "Could not extract principal name: " <> tshow searchResults
|
|
||||||
observeLoginOutcome apName LoginError
|
observeLoginOutcome apName LoginError
|
||||||
loginErrorMessageI LoginR Msg.AuthError
|
loginErrorMessageI LoginR Msg.AuthError
|
||||||
|
Right (Left _bindErr) -> do
|
||||||
|
$logDebugS apName "Invalid credentials"
|
||||||
|
observeLoginOutcome apName LoginInvalidCredentials
|
||||||
|
loginErrorMessageI LoginR Msg.InvalidLogin
|
||||||
|
Right (Right (Left searchResults))
|
||||||
|
| null searchResults -> do
|
||||||
|
$logDebugS apName "User not found"
|
||||||
|
observeLoginOutcome apName LoginInvalidCredentials
|
||||||
|
loginErrorMessageI LoginR Msg.InvalidLogin
|
||||||
|
| otherwise -> do
|
||||||
|
$logWarnS apName $ "Could not extract principal name: " <> tshow searchResults
|
||||||
|
observeLoginOutcome apName LoginError
|
||||||
|
loginErrorMessageI LoginR Msg.AuthError
|
||||||
|
Right (Right (Right (userDN, credsIdent))) -> do
|
||||||
|
observeLoginOutcome apName LoginSuccessful
|
||||||
|
setCredsRedirect $ Creds apName credsIdent [("DN", userDN)]
|
||||||
|
|
||||||
maybe (redirect $ tp LoginR) return resp
|
maybe (redirect $ tp LoginR) return resp
|
||||||
apDispatch _ [] = badMethod
|
apDispatch _ [] = badMethod
|
||||||
@ -228,3 +233,7 @@ campusLogin pool mode = AuthPlugin{..}
|
|||||||
, formAnchor = Just "login--campus" :: Maybe Text
|
, formAnchor = Just "login--campus" :: Maybe Text
|
||||||
}
|
}
|
||||||
$(widgetFile "widgets/campus-login/campus-login-form")
|
$(widgetFile "widgets/campus-login/campus-login-form")
|
||||||
|
|
||||||
|
isInvalidCredentials = \case
|
||||||
|
Ldap.ResponseErrorCode _ Ldap.InvalidCredentials _ _ -> True
|
||||||
|
_other -> False
|
||||||
|
|||||||
Reference in New Issue
Block a user