chore(admin): switch to generic Aeson Value for oauth response parsing

This commit is contained in:
Sarah Vaupel 2024-03-11 11:09:59 +01:00
parent e1ebd528b8
commit 504490f593
3 changed files with 28 additions and 21 deletions

View File

@ -15,10 +15,9 @@ import Auth.LDAP
import Handler.Utils import Handler.Utils
import qualified Data.Text as Text import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.Text.Encoding as Text import qualified Data.Text.Lazy as Lazy
import qualified Data.Text.Lazy.Encoding as Lazy
import qualified Ldap.Client as Ldap
getAdminExternalUserR, postAdminExternalUserR :: Handler Html getAdminExternalUserR, postAdminExternalUserR :: Handler Html
@ -28,25 +27,27 @@ postAdminExternalUserR = do
flip (renderAForm FormStandard) html $ areq textField (fslI MsgAdminUserIdent) Nothing flip (renderAForm FormStandard) html $ areq textField (fslI MsgAdminUserIdent) Nothing
let let
presentUtf8 v = Text.intercalate ", " (either tshow id . Text.decodeUtf8' <$> v) -- presentUtf8 v = Text.intercalate ", " (either tshow id . Text.decodeUtf8' <$> v)
presentLatin1 v = Text.intercalate ", " ( Text.decodeLatin1 <$> v) -- presentLatin1 v = Text.intercalate ", " ( Text.decodeLatin1 <$> v)
procFormPerson :: Text -> Handler (Maybe [(AuthSourceIdent, [(Text,(Int,Text,Text))])]) procFormPerson :: Text -> Handler (Maybe [(AuthSourceIdent,Lazy.Text)]) -- (Maybe [(AuthSourceIdent, [(Text,(Int,Text,Text))])])
procFormPerson needle = getsYesod (view _appUserAuthConf) >>= \case procFormPerson needle = getsYesod (view _appUserAuthConf) >>= \case
UserAuthConfSingleSource{..} -> case userAuthConfSingleSource of UserAuthConfSingleSource{..} -> case userAuthConfSingleSource of
AuthSourceConfAzureAdV2 AzureConf{..} -> do AuthSourceConfAzureAdV2 AzureConf{..} -> do
-- only singleton results supported right now, i.e. lookups by email, userPrincipalName (aka fraport ident), or id -- only singleton results supported right now, i.e. lookups by email, userPrincipalName (aka fraport ident), or id
queryOAuth2User @[(Text,[ByteString])] needle >>= \case queryOAuth2User @Value needle >>= \case
Left _ -> addMessage Error (text2Html "Encountered UserDataException while Azure user query!") >> return Nothing Left _ -> addMessage Error (text2Html "Encountered UserDataException while Azure user query!") >> return Nothing
Right azureData -> return . Just . singleton . (AuthSourceIdAzure azureConfTenantId,) $ azureData <&> \(k,vs) -> (k, (length vs, presentUtf8 vs, presentLatin1 vs)) Right azureResponse -> return . Just . singleton . (AuthSourceIdAzure azureConfTenantId,) . Lazy.decodeUtf8 $ encodePretty azureResponse
AuthSourceConfLdap LdapConf{ ldapConfSourceId = authSourceIdLdapHost } -> do -- Right azureData -> return . Just . singleton . (AuthSourceIdAzure azureConfTenantId,) $ azureData <&> \(k,vs) -> (k, (length vs, presentUtf8 vs, presentLatin1 vs))
AuthSourceConfLdap LdapConf{..} -> do
getsYesod (view _appLdapPool) >>= \case getsYesod (view _appLdapPool) >>= \case
Nothing -> addMessage Error (text2Html "LDAP Pool configuration missing!") >> return Nothing Nothing -> addMessage Error (text2Html "LDAP Pool configuration missing!") >> return Nothing
Just pool -> do Just pool -> do
ldapData <- ldapSearch pool needle ldapData <- ldapSearch pool needle
-- decodedErr <- decodeUserTest $ NonEmpty.singleton UpsertUserDataLdap{ upsertUserLdapHost = ldapConfSourceId, upsertUserLdapData = concat ldapData } -- decodedErr <- decodeUserTest $ NonEmpty.singleton UpsertUserDataLdap{ upsertUserLdapHost = ldapConfSourceId, upsertUserLdapData = concat ldapData }
-- whenIsLeft decodedErr $ addMessageI Error -- whenIsLeft decodedErr $ addMessageI Error
return . Just $ ldapData <&> \(Ldap.SearchEntry _dn attrs) -> (AuthSourceIdLdap{..}, (\(k,v) -> (tshow k, (length v, presentUtf8 v, presentLatin1 v))) <$> attrs) return . Just . singleton . (AuthSourceIdLdap ldapConfSourceId,) . Lazy.decodeUtf8 $ encodePretty ldapData
-- return . Just $ ldapData <&> \(Ldap.SearchEntry _dn attrs) -> (AuthSourceIdLdap{..}, (\(k,v) -> (tshow k, (length v, presentUtf8 v, presentLatin1 v))) <$> attrs)
mbData <- formResultMaybe presult procFormPerson mbData <- formResultMaybe presult procFormPerson

View File

@ -19,6 +19,8 @@ import Utils.PathPiece (derivePathPiece)
import Ldap.Client import Ldap.Client
import Network.HTTP.Types.Method.Instances () -- for FromJSON instance for ByteString
deriving instance Ord Attr deriving instance Ord Attr
deriving instance Ord Dn deriving instance Ord Dn
@ -54,4 +56,6 @@ derivePersistField "Password"
derivePersistField "Scope" derivePersistField "Scope"
deriveJSON defaultOptions ''Attr deriveJSON defaultOptions ''Attr
deriveJSON defaultOptions ''Dn
deriveJSON defaultOptions ''Scope deriveJSON defaultOptions ''Scope
deriveJSON defaultOptions ''SearchEntry

View File

@ -22,16 +22,18 @@ $# SPDX-License-Identifier: AGPL-3.0-or-later
LDAP host: # LDAP host: #
#{ldapHost} #{ldapHost}
<dd .deflist__dd> <dd .deflist__dd>
<dl .deflist> <pre>
$forall (k,(numv,vUtf8,vLatin1)) <- responses #{responses}
<dt .deflist__dt> $# <dl .deflist>
#{k} $# $forall (k,(numv,vUtf8,vLatin1)) <- responses
$if 1 < numv $# <dt .deflist__dt>
\ (#{show numv}) $# #{k}
<dd .deflist__dd> $# $if 1 < numv
UTF8: #{vUtf8} $# \ (#{show numv})
&#8212; $# <dd .deflist__dd>
Latin: #{vLatin1} $# UTF8: #{vUtf8}
$# &#8212;
$# Latin: #{vLatin1}
<section> <section>
<p> <p>