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 -def createUserObject(master, author, src=None):
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
63 -def getUserContact(master, contact_type=None, uid=None):
64 """ 65 This is a simple getter function that returns a user attribute 66 that matches the contact_type argument, or returns None if no 67 uid/match is found. 68 69 @param master: BuildMaster used to query the database 70 @type master: BuildMaster instance 71 72 @param contact_type: type of contact attribute to look for in 73 in a given user, such as 'email' or 'nick' 74 @type contact_type: string 75 76 @param uid: user that is searched for the contact_type match 77 @type uid: integer 78 79 @returns: string of contact information or None via deferred 80 """ 81 d = master.db.users.getUser(uid) 82 d.addCallback(lambda usdict: usdict and usdict.get(contact_type)) 83 return d
84
85 -def encrypt(passwd):
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
105 -def check_passwd(guess, passwd):
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] # salt_len * 2 due to encode('hex_codec') 121 m.update(guess + salt) 122 crypted_guess = salt + m.hexdigest() 123 124 return (crypted_guess == passwd)
125