-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patch Auth to support LDAP Authentication #77
Open
mlite
wants to merge
2
commits into
snapframework:master
Choose a base branch
from
mlite:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{-# LANGUAGE TypeSynonymInstances #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
|
||
|
||
module Snap.Snaplet.Auth.Backends.Ldap | ||
( initLdapAuthManager | ||
, mkLdapAuthMgr | ||
) where | ||
|
||
|
||
import Control.Applicative | ||
import Control.Monad.CatchIO (throw) | ||
import Control.Monad.State | ||
import Control.Concurrent.STM | ||
import Data.Aeson | ||
import qualified Data.Attoparsec as Atto | ||
import qualified Data.ByteString.Lazy as LB | ||
import qualified Data.ByteString as B | ||
import qualified Data.Map as HM | ||
import Data.Map (Map) | ||
import Data.Maybe (fromJust, isJust) | ||
import Data.Text (Text, unpack) | ||
import qualified Data.Text as T | ||
import Data.Time | ||
import Web.ClientSession | ||
import System.Directory | ||
|
||
import Snap.Snaplet | ||
import Snap.Snaplet.Auth.Types | ||
import Snap.Snaplet.Auth.AuthManager | ||
import Snap.Snaplet.Session | ||
import qualified LDAP as L | ||
import qualified Data.ByteString.UTF8 as Butf8 | ||
|
||
------------------------------------------------------------------------------ | ||
-- | Initialize a JSON file backed 'AuthManager' | ||
initLdapAuthManager :: AuthSettings | ||
-- ^ Authentication settings for your app | ||
-> Text | ||
-- ^ LDAP hostname | ||
-> Integer | ||
-- ^ LDAP port | ||
-> Maybe Text | ||
-- ^ username postfix | ||
-> SnapletLens b SessionManager | ||
-- ^ Lens into a 'SessionManager' auth snaplet will | ||
-- use | ||
-> SnapletInit b (AuthManager b) | ||
initLdapAuthManager s hn prt postfix l = do | ||
makeSnaplet | ||
"LdapAuthManager" | ||
"A snaplet providing user authentication using LDAP backend" | ||
Nothing $ liftIO $ do | ||
rng <- liftIO mkRNG | ||
key <- getKey (asSiteKey s) | ||
ldapMgr <- mkLdapAuthMgr hn prt postfix | ||
return $! AuthManager { | ||
backend = ldapMgr | ||
, session = l | ||
, activeUser = Nothing | ||
, minPasswdLen = asMinPasswdLen s | ||
, rememberCookieName = asRememberCookieName s | ||
, rememberPeriod = asRememberPeriod s | ||
, siteKey = key | ||
, lockout = asLockout s | ||
, randomNumberGenerator = rng | ||
} | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Load/create a datafile into memory cache and return the manager. | ||
-- | ||
-- This data type can be used by itself for batch/non-handler processing. | ||
mkLdapAuthMgr :: Text -> Integer -> Maybe Text -> IO LdapAuthManager | ||
mkLdapAuthMgr hn prt postfix = do | ||
return $! LdapAuthManager { | ||
hostname = hn | ||
, port = prt | ||
, queryUser = Nothing | ||
, queryPwd = Nothing | ||
, usernamePostfix = postfix | ||
} | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
data LdapAuthManager = LdapAuthManager { | ||
hostname :: Text | ||
, port :: Integer | ||
, queryUser :: Maybe Text | ||
, queryPwd :: Maybe Text | ||
, usernamePostfix :: Maybe Text | ||
} | ||
|
||
------------------------------------------------------------------------------ | ||
instance IAuthBackend LdapAuthManager where | ||
save r = return . Right | ||
|
||
destroy = error "LdapAuthManager: destroy is not yet implemented" | ||
|
||
lookupByUserId mgr u@(UserId uid) = return $ Just (defAuthUser { userId = Just $ u | ||
, userLogin = uid }) | ||
lookupByLogin mgr login = return $ Just (defAuthUser { userId = Just $ UserId login | ||
, userLogin = login }) | ||
|
||
lookupByRememberToken mgr token = error "LdapAuthManager : lookupByRememberToken is not yet implemented" | ||
authenticate mgr usr pwd = case pwd of | ||
ClearText pwd' -> do | ||
ld <- L.ldapInit (unpack $ hostname mgr) (fromInteger $ port mgr) | ||
x' <- L.handleLDAP (return . Just . AuthError . show) | ||
(L.ldapSimpleBind ld getDn (Butf8.toString pwd') >> return Nothing) | ||
return x' | ||
Encrypted _ -> return $ Just $ AuthError "cannot do LDAP authentication with encrypted password" | ||
where getDn = let usrStr = T.unpack $ userLogin usr | ||
in maybe usrStr (\x -> usrStr ++ T.unpack x) (usernamePostfix mgr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I'm not crazy about introducing LDAP as a dependency, especially since it binds to a C API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed -- is there some way we can make this a third-party downloadable package? The auth mechanism should be extensible, if it isn't we should put some work into that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IAuthBackend is not general enough to support remote authentication.
I'm ok if the patch only includes:
AuthManager.hs
Backends/JsonFile.hs
Handlers.hs
This patch will not introduce extra dependency.
Thank,
Ning
On 02/19/2013 01:24 AM, Gregory Collins wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to making the necessary changes to snap to allow for a third party LDAP library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope to get around to investigating this soon. Thank you for the patch.
On Friday, February 22, 2013 at 5:00 AM, Oliver Charles wrote: