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
32 """
33 Take a Change author and source and translate them into a User Object,
34 storing the user in master.db, or returning None if the src is not
35 specified.
36
37 @param master: link to Buildmaster for database operations
38 @type master: master.Buildmaster instance
39
40 @param authors: Change author if string or Authz instance
41 @type authors: string or status.web.authz instance
42
43 @param src: source from which the User Object will be created
44 @type src: string
45 """
46
47 if not src:
48 log.msg("No vcs information found, unable to create User Object")
49 return
50
51 if src in srcs:
52 log.msg("checking for User Object from %s Change for: %s" % (src,
53 author))
54 usdict = dict(identifier=author, attr_type=src, attr_data=author)
55 else:
56 log.msg("Unrecognized source argument: %s" % src)
57 return
58
59 d = master.db.users.findUserByAttr(identifier=usdict['identifier'],
60 attr_type=usdict['attr_type'],
61 attr_data=usdict['attr_data'])
62 wfd = defer.waitForDeferred(d)
63 yield wfd
64 uid = wfd.getResult()
65
66 yield uid
67
89
91 """
92 Encrypts the incoming password after adding some salt to store
93 it in the database.
94
95 @param passwd: password portion of user credentials
96 @type passwd: string
97
98 @returns: encrypted/salted string
99 """
100 try:
101 m = sha()
102 except TypeError:
103 m = sha.new()
104
105 salt = os.urandom(salt_len).encode('hex_codec')
106 m.update(passwd + salt)
107 crypted = salt + m.hexdigest()
108 return crypted
109
111 """
112 Tests to see if the guess, after salting and hashing, matches the
113 passwd from the database.
114
115 @param guess: incoming password trying to be used for authentication
116 @param passwd: already encrypted password from the database
117
118 @returns: boolean
119 """
120 try:
121 m = sha()
122 except TypeError:
123 m = sha.new()
124
125 salt = passwd[:salt_len * 2]
126 m.update(guess + salt)
127 crypted_guess = salt + m.hexdigest()
128
129 return (crypted_guess == passwd)
130