1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import os
18 from zope.interface import Interface, Attribute, implements
19 from buildbot.status.web.base import HtmlResource, ActionResource
20 from buildbot.status.web.base import path_to_authfail
21
22 from buildbot.process.users import users
23
25 """
26 Represent an authentication method.
27
28 Note that each IAuth instance contains a link to the BuildMaster that
29 will be set once the IAuth instance is initialized.
30 """
31
32 master = Attribute('master', "Link to BuildMaster, set when initialized")
33
35 """Check whether C{user} / C{passwd} are valid."""
36
38 """return dict with user info.
39 dict( fullName="", email="", groups=[])
40 """
41
43 """Get the reason authentication failed."""
44
46 master = None
47 err = ""
48
51
53 """default dummy impl"""
54 return dict(userName=user, fullName=user, email=user+"@localhost", groups=[ user ])
55
57 implements(IAuth)
58 """Implement basic authentication against a list of user/passwd."""
59
60 userpass = []
61 """List of user/pass tuples."""
62
64 """C{userpass} is a list of (user, passwd)."""
65 for item in userpass:
66 assert isinstance(item, tuple) or isinstance(item, list)
67 u, p = item
68 assert isinstance(u, str)
69 assert isinstance(p, str)
70 self.userpass = userpass
71
73 """Check that C{user}/C{passwd} is a valid user/pass tuple."""
74 if not self.userpass:
75 self.err = "Bad self.userpass data"
76 return False
77 for u, p in self.userpass:
78 if user == u and passwd == p:
79 self.err = ""
80 return True
81 self.err = "Invalid username or password"
82 return False
83
85 implements(IAuth)
86 """Implement authentication against an .htpasswd file."""
87
88 file = ""
89 """Path to the .htpasswd file to use."""
90
92 """C{file} is a path to an .htpasswd file."""
93 assert os.path.exists(file)
94 self.file = file
95
97 """Authenticate C{user} and C{passwd} against an .htpasswd file"""
98 if not os.path.exists(self.file):
99 self.err = "No such file: " + self.file
100 return False
101
102
103 lines = [l.rstrip().split(':', 1)
104 for l in file(self.file).readlines()]
105
106 lines = [l for l in lines if l[0] == user]
107 if not lines:
108 self.err = "Invalid user/passwd"
109 return False
110
111
112 hash = lines[0][1]
113 from crypt import crypt
114 res = hash == crypt(passwd, hash[0:2])
115 if res:
116 self.err = ""
117 else:
118 self.err = "Invalid user/passwd"
119 return res
120
122 """Implement authentication against users in database"""
123 implements(IAuth)
124
126 """
127 It checks for a matching uid in the database for the credentials
128 and return True if a match is found, False otherwise.
129
130 @param user: username portion of user credentials
131 @type user: string
132
133 @param passwd: password portion of user credentials
134 @type passwd: string
135
136 @returns: boolean via deferred.
137 """
138 d = self.master.db.users.getUserByUsername(user)
139 def check_creds(user):
140 if user:
141 if users.check_passwd(passwd, user['bb_password']):
142 return True
143 self.err = "no user found with those credentials"
144 return False
145 d.addCallback(check_creds)
146 return d
147
149 pageTitle = "Authentication Failed"
150
151 - def content(self, request, cxt):
152 templates =request.site.buildbot_service.templates
153 template = templates.get_template("authfail.html")
154 return template.render(**cxt)
155
157 pageTitle = "Authorization Failed"
158
159 - def content(self, request, cxt):
160 templates =request.site.buildbot_service.templates
161 template = templates.get_template("authzfail.html")
162 return template.render(**cxt)
163
165
177 d.addBoth(on_login)
178 return d
179
188