yesod-auth-ldap-mediocre (empty) → 0.1.0.0
raw patch · 4 files changed
+132/−0 lines, 4 filesdep +LDAPdep +aesondep +basesetup-changed
Dependencies added: LDAP, aeson, base, text, yesod-auth, yesod-core, yesod-form
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- src/Yesod/Auth/Ldap.hs +79/−0
- yesod-auth-ldap-mediocre.cabal +31/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Andrew Martin++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Yesod/Auth/Ldap.hs view
@@ -0,0 +1,79 @@+module Yesod.Auth.Ldap where++import Prelude+import Yesod.Core+import Yesod.Auth+import Yesod.Form+import Control.Monad+import Data.Text (Text)+import qualified Data.Text as Text+import Control.Applicative+import LDAP.Init+import LDAP.Exceptions+import Data.Monoid+import Data.Aeson++data LdapCreds = LdapCreds + { ldapUser :: Text+ , ldapPassword :: Text+ , ldapPort :: Int+ , ldapHost :: Text+ , ldapPrefix :: Text+ }++instance FromJSON LdapCreds where+ parseJSON (Object o) = LdapCreds+ <$> o .: "user"+ <*> o .: "password"+ <*> o .: "port"+ <*> o .: "host"+ <*> o .: "prefix"+ parseJSON _ = mzero++ldapPluginName :: Text+ldapPluginName = "ldap"++class YesodLdap site where+ getLdapCreds :: HandlerT site IO LdapCreds++l3AuthLdap :: (RenderMessage site FormMessage, YesodAuth site, YesodLdap site) => AuthPlugin site+l3AuthLdap = AuthPlugin ldapPluginName dispatch $ \tp -> [whamlet|+<h2>AD Login+<form action="@{tp (PluginR "ldap" ["login"])}" method=post>+ <p>Log in below using your active directory credentials.+ <div.form-group>+ <label>Username+ <input.form-control type="text" name="username">+ <div.form-group>+ <label>Password+ <input.form-control type="password" name="password">+ <button.btn.btn-primary type="submit">Submit+|]++dispatch :: (RenderMessage site FormMessage, YesodAuth site, YesodLdap site) => Text -> [Text] -> HandlerT Auth (HandlerT site IO) TypedContent+dispatch "POST" ["login"] = dispatchLdap+dispatch _ _ = notFound++dispatchLdap :: (RenderMessage site FormMessage, YesodAuth site, YesodLdap site) => HandlerT Auth (HandlerT site IO) TypedContent+dispatchLdap = do+ tp <- getRouteToParent+ creds <- lift getLdapCreds+ (username,password) <- lift $ runInputPost $ (,)+ <$> ireq textField "username"+ <*> ireq textField "password"+ let qualifiedUsername = ldapPrefix creds <> username+ success <- liftIO $ do + conn <- ldapOpen (Text.unpack $ ldapHost creds) (fromInteger $ toInteger $ ldapPort creds)+ ldapBoolifyExceptions $ ldapSimpleBind conn (Text.unpack qualifiedUsername) (Text.unpack password)+ lift $ if success+ then do+ -- setMessage $ toHtml $ ("Login was successful" :: Text)+ setCredsRedirect $ Creds ldapPluginName username []+ else do+ setMessage $ [shamlet|<div.alert.alert-danger>Could not log in|]+ redirect $ tp LoginR++ldapBoolifyExceptions :: IO () -> IO Bool+ldapBoolifyExceptions a = catchLDAP (True <$ a) (\_ -> return False)++
+ yesod-auth-ldap-mediocre.cabal view
@@ -0,0 +1,31 @@+-- Initial yesod-auth-ldap-mediocre.cabal generated by cabal init. For +-- further documentation, see http://haskell.org/cabal/users-guide/++name: yesod-auth-ldap-mediocre+version: 0.1.0.0+synopsis: Very simlple LDAP auth for yesod+-- description: +license: MIT+license-file: LICENSE+author: Andrew Martin+maintainer: amartin@layer3com.com+-- copyright: +category: Web+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: Yesod.Auth.Ldap+ default-extensions: QuasiQuotes+ , FlexibleContexts+ , OverloadedStrings+ build-depends: yesod-core+ , yesod-auth+ , yesod-form+ , text+ , LDAP+ , aeson+ build-depends: base >=4.7 && <4.8+ hs-source-dirs: src+ default-language: Haskell2010