Attachment 'Auth_nis_login.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - NIS Yellow Pages
4
5 This code only creates a user object, the session will be established by
6 moin automatically.
7
8 place file in:
9 /usr/lib/python2.5/site-packages/MoinMoin/auth/
10
11 wikiconfig.py:
12 from MoinMoin.auth.nis_login import NISAuth
13 auth = [NISAuth(autocreate=True, maildomain='@yourdomain.com')]
14
15 @copyright: 2008 Stefan Sami-Soueiha,
16 @license: GNU GPL, see COPYING for details.
17
18 Version: 1.0 12.12.2008
19 """
20 from MoinMoin import log
21 logging = log.getLogger(__name__)
22
23 try:
24 import nis, crypt
25 except ImportError, err:
26 logging.error("You need to have python nis & crypt module's (%s)." % str(err))
27 raise
28
29 from MoinMoin import user
30 from MoinMoin.auth import BaseAuth, CancelLogin, ContinueLogin
31
32
33 class NISAuth(BaseAuth):
34 """ get authentication data from form, authenticate against NIS server
35 The session is kept by moin automatically.
36 """
37
38 login_inputs = ['username', 'password']
39 logout_possible = True
40 name = 'nis'
41
42 def __init__(self, autocreate=False,
43 maildomain='@domain.com',
44 niscat='passwd.byname' # ypcat passwd
45 ):
46
47 BaseAuth.__init__(self)
48 self.autocreate = autocreate
49 self.maildomain = maildomain
50 self.niscat = niscat
51
52 def login(self, request, user_obj, **kw):
53 username = kw.get('username')
54 password = kw.get('password')
55 _ = request.getText
56
57 # we require non-empty password
58 if not username or not password:
59 return ContinueLogin(user_obj,
60 _('Missing password. Please enter user name and password.'))
61 logging.debug("trying to authenticate NIS user: %r" % username)
62
63 try:
64 userline=nis.cat(self.niscat)[username]
65 try:
66 passwd_hash=userline.split(':')[1]
67 passwd_hash_entered = crypt.crypt(password, passwd_hash[:2])
68
69 if passwd_hash == passwd_hash_entered:
70 u = user.User(request, auth_username=username, auth_method=self.name, auth_attribs=('name', 'password', 'email', 'mailto_author', ))
71 u.name = username
72 u.aliasname = userline.split(':')[4]
73 u.email = username + self.maildomain
74 u.remember_me = 0 # 0 enforces cookie_lifetime config param
75 logging.debug("creating userprefs with name %r email %r alias %r" % (u.name, u.email, u.aliasname))
76
77 if u and self.autocreate:
78 u.create_or_update(True)
79 return ContinueLogin(u)
80
81 except:
82 logging.debug("invalid password (user: %s)" % (username))
83 return CancelLogin(_("Invalid password (user:%s)."% (username)))
84 #return CancelLogin(_("Invalid password ((%s:%s)(%s:%s)." % (username, password, passwd_hash, passwd_hash_entered)))
85
86 except: # username not in NIS
87 logging.debug("invalid username: (%s)" % (username))
88 return CancelLogin(_("Invalid username (%s)."% (username)))
89
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.