1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 from twisted.python import log
18 from twisted.internet import defer
19
20 try:
21 from hashlib import sha1 as sha
22 assert sha
23 except ImportError:
24
25 import sha
26
27 srcs = ['git', 'svn', 'hg', 'cvs', 'darcs', 'bzr']
28 salt_len = 8
29
31 """
32 Take a Change author and source and translate them into a User Object,
33 storing the user in master.db, or returning None if the src is not
34 specified.
35
36 @param master: link to Buildmaster for database operations
37 @type master: master.Buildmaster instance
38
39 @param authors: Change author if string or Authz instance
40 @type authors: string or status.web.authz instance
41
42 @param src: source from which the User Object will be created
43 @type src: string
44 """
45
46 if not src:
47 log.msg("No vcs information found, unable to create User Object")
48 return defer.succeed(None)
49
50 if src in srcs:
51 log.msg("checking for User Object from %s Change for: %s" % (src,
52 author))
53 usdict = dict(identifier=author, attr_type=src, attr_data=author)
54 else:
55 log.msg("Unrecognized source argument: %s" % src)
56 return defer.succeed(None)
57
58 return master.db.users.findUserByAttr(
59 identifier=usdict['identifier'],
60 attr_type=usdict['attr_type'],
61 attr_data=usdict['attr_data'])
62
84
86 """
87 Encrypts the incoming password after adding some salt to store
88 it in the database.
89
90 @param passwd: password portion of user credentials
91 @type passwd: string
92
93 @returns: encrypted/salted string
94 """
95 try:
96 m = sha()
97 except TypeError:
98 m = sha.new()
99
100 salt = os.urandom(salt_len).encode('hex_codec')
101 m.update(passwd + salt)
102 crypted = salt + m.hexdigest()
103 return crypted
104
106 """
107 Tests to see if the guess, after salting and hashing, matches the
108 passwd from the database.
109
110 @param guess: incoming password trying to be used for authentication
111 @param passwd: already encrypted password from the database
112
113 @returns: boolean
114 """
115 try:
116 m = sha()
117 except TypeError:
118 m = sha.new()
119
120 salt = passwd[:salt_len * 2]
121 m.update(guess + salt)
122 crypted_guess = salt + m.hexdigest()
123
124 return (crypted_guess == passwd)
125