| 11 | |
| 12 | |
| 13 | class UserManager(object): |
| 14 | |
| 15 | class User(Enum): |
| 16 | """ |
| 17 | Define some default users. |
| 18 | """ |
| 19 | Admin = 0 |
| 20 | Anonymous = 1 |
| 21 | User = 3 |
| 22 | |
| 23 | def __init__(self, parent): |
| 24 | self.logger = logging.getLogger(__name__) |
| 25 | assert(hasattr(parent, 'private_key')) |
| 26 | self._parent = parent |
| 27 | self.user_manager = self.default_user_manager |
| 28 | self.allow_remote_admin = True |
| 29 | |
| 30 | @property |
| 31 | def private_key(self): |
| 32 | return self._parent.private_key |
| 33 | |
| 34 | def default_user_manager(self, isession, userName, password): |
| 35 | """ |
| 36 | Default user_manager, does nothing much but check for admin |
| 37 | """ |
| 38 | if self.allow_remote_admin and userName in ("admin", "Admin"): |
| 39 | isession.user = UserManager.User.Admin |
| 40 | return True |
| 41 | |
| 42 | def set_user_manager(self, user_manager): |
| 43 | """ |
| 44 | set up a function which that will check for authorize users. Input function takes username |
| 45 | and password as paramters and returns True of user is allowed access, False otherwise. |
| 46 | """ |
| 47 | self.user_manager = user_manager |
| 48 | |
| 49 | def check_user_token(self, isession, token): |
| 50 | """ |
| 51 | unpack the username and password for the benefit of the user defined user manager |
| 52 | """ |
| 53 | userName = token.UserName |
| 54 | passwd = token.Password |
| 55 | |
| 56 | # decrypt password is we can |
| 57 | if str(token.EncryptionAlgorithm) != "None": |
| 58 | if use_crypto == False: |
| 59 | return False; |
| 60 | try: |
| 61 | if token.EncryptionAlgorithm == "http://www.w3.org/2001/04/xmlenc#rsa-1_5": |
| 62 | raw_pw = uacrypto.decrypt_rsa15(self.private_key, passwd) |
| 63 | elif token.EncryptionAlgorithm == "http://www.w3.org/2001/04/xmlenc#rsa-oaep": |
| 64 | raw_pw = uacrypto.decrypt_rsa_oaep(self.private_key, passwd) |
| 65 | else: |
| 66 | self.logger.warning("Unknown password encoding '{0}'".format(token.EncryptionAlgorithm)) |
| 67 | return False |
| 68 | length = unpack_from('<I', raw_pw)[0] - len(isession.nonce) |
| 69 | passwd = raw_pw[4:4 + length] |
| 70 | passwd = passwd.decode('utf-8') |