Package buildbot :: Package process :: Package users :: Module users
[frames] | no frames]

Source Code for Module buildbot.process.users.users

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 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      # For Python 2.4 
 25      import sha 
 26   
 27  srcs = ['git', 'svn', 'hg', 'cvs', 'darcs', 'bzr'] 
 28  salt_len = 8 
29 30 @defer.deferredGenerator 31 -def createUserObject(master, author, src=None):
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
68 -def getUserContact(master, contact_type=None, uid=None):
69 """ 70 This is a simple getter function that returns a user attribute 71 that matches the contact_type argument, or returns None if no 72 uid/match is found. 73 74 @param master: BuildMaster used to query the database 75 @type master: BuildMaster instance 76 77 @param contact_type: type of contact attribute to look for in 78 in a given user, such as 'email' or 'nick' 79 @type contact_type: string 80 81 @param uid: user that is searched for the contact_type match 82 @type uid: integer 83 84 @returns: string of contact information or None via deferred 85 """ 86 d = master.db.users.getUser(uid) 87 d.addCallback(lambda usdict: usdict and usdict.get(contact_type)) 88 return d
89
90 -def encrypt(passwd):
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
110 -def check_passwd(guess, passwd):
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] # salt_len * 2 due to encode('hex_codec') 126 m.update(guess + salt) 127 crypted_guess = salt + m.hexdigest() 128 129 return (crypted_guess == passwd)
130