Skip to content

Commit

Permalink
changed to new-style class with properties and setter - v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
francisluong committed Jul 11, 2014
1 parent 52c692e commit a3bf7d6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion _hidden/travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ deploy:
password:
secure: SU7MpWxQQEPISdk2yFcNa8poGGwtDGq28TEI5iKydhNytxvS+7WlXd/5V6scwzY5be1AsJjU+zG3zG+pTzLVqDYkMXlctoLcxdR3+fGnMaOM9N7TLJHJqHrNWIr16r25Gtv7JYM02ReqWr10BEJXabrG9adgljAStlc7lbzrolI=
on:
tags: true
repo: francisluong/py-auth-userpass
branch: master
2 changes: 2 additions & 0 deletions auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


68 changes: 40 additions & 28 deletions auth/userpass.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
from Crypto.Cipher import AES
from Crypto import Random

class Userpass:
class Userpass(object):
"""A simple but not so secure object for username and password storage and retrieval"""

def __init__ (self,filepath=""):
def __init__(self,filepath=""):
self.length = dict()
self.database = dict()
self.current_user = ""
self._database = dict()
self._current_user = ""
self.mode = AES.MODE_CBC
self.bs = 32
self.key = Random.new().read(self.bs)
self.iv = Random.new().read(16)
if filepath != "":
self.load(filepath)

def add_user_passwd (self, user, passwd):
def add_user_passwd(self, user, passwd):
"""add a username/password"""
#set current_user if not set
if self.current_user == "":
self.current_user = user
#set _current_user if not set
if self.user == "":
self._current_user = user
#track length of password per user
self.length[user] = len(passwd)
c = AES.new(self.key, self.mode, self.iv)
self.database[user] = c.encrypt(passwd.ljust(self.bs))
self._database[user] = c.encrypt(passwd.ljust(self.bs))

@property
def user (self):
def user(self):
"""value: current user"""
return self.current_user
return self._current_user

@user.setter
def user(self, newuser):
"""switch current user"""
if self.has_key(newuser):
#only switch if user is already in the _database
self._current_user = newuser
else:
raise UserpassError( "New User Not in Userpass Database: '{}'".format(newuser) )

@property
def passwd (self):
def passwd(self):
"""value: password for current user"""
user = self.current_user
user = self.user
length = self.length[user]
c = AES.new(self.key, self.mode, self.iv)
return c.decrypt(self.database[user])[0:length]
return c.decrypt(self._database[user])[0:length]

def passwd_for (self, input_user="CURRENTUSER"):
def passwd_for(self, input_user="CURRENTUSER"):
"""return password for specified input_user or current user"""
if input_user == "CURRENTUSER":
user = self.current_user
user = self.user
else:
user = input_user
length = self.length[user]
c = AES.new(self.key, self.mode, self.iv)
return c.decrypt(self.database[user])[0:length]

def change_user(self, user):
"""switch current user"""
if user in self.database:
#only switch if user is already in the database
self.current_user = user
return c.decrypt(self._database[user])[0:length]

def load (self, filename):
"""read in yaml file for password database"""
def load(self, filename):
"""read in yaml file for password _database"""
import yaml
import sys
if sys.platform == "linux2":
Expand All @@ -69,16 +72,25 @@ def load (self, filename):
self.add_user_passwd(user,users[user])
if yamldict.has_key("defaultuser"):
#set default user from defaultuser key
self.change_user( yamldict["defaultuser"] )
self.user = yamldict["defaultuser"]

def users(self):
"""alias --> self.keys()"""
return self.keys()

def keys(self):
"""get list of users"""
return self.database.keys()
return self._database.keys()

def has_key(self, key):
"""return True of user exists, else return False"""
return self.database.has_key(key)
return self._database.has_key(key)


class UserpassError(Exception):

def __init__(self, value):
self.value = value

def __str__(self):
return repr(self.value)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name = "auth-userpass",
version = "0.2.0",
version = "0.3.0",
author = "Francis Luong (Franco)",
author_email = "[email protected]",
description = ("Simple but not so secure username and password storage and retrieval"),
Expand Down
15 changes: 11 additions & 4 deletions test/test_userpass.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# python
from auth.userpass import Userpass
from auth.userpass import Userpass, UserpassError
from nose.tools import assert_equal
from nose.tools import assert_not_equal
from nose.tools import assert_raises
Expand All @@ -22,7 +22,7 @@ def test_userpass_002(self):
userpass = Userpass()
userpass.add_user_passwd("user1", "passwd1")
userpass.add_user_passwd("user2", "passwd2")
userpass.change_user( "user2" )
userpass.user = "user2"
assert_equal( userpass.user, "user2" )
assert_equal( userpass.passwd, "passwd2" )

Expand Down Expand Up @@ -62,11 +62,18 @@ def test_userpass_004(self):
#perform checks per testcases above
assert_equal( userpass.user, "user1" )
assert_equal( userpass.passwd, "passwd1" )
userpass.change_user( "user2" )
userpass.user = "user2"
assert_equal( userpass.user, "user2" )
assert_equal( userpass.passwd, "passwd2" )
assert_equal( userpass.has_key("user1"), True )
assert_equal( userpass.has_key("user2"), True )
assert_equal( userpass.has_key("user3"), False )


@raises(UserpassError)
def test_userpass_005(self):
"""Switch to invalid user"""
userpass = Userpass()
userpass.add_user_passwd("user1", "passwd1")
userpass.add_user_passwd("user2", "passwd2")
userpass.user = "user3"
assert_equal( userpass.passwd, "passwd2" )

0 comments on commit a3bf7d6

Please sign in to comment.