daily pastebin goal
7%
SHARE
TWEET

TAMU ENGR 102 login.py

philip_ritchey Nov 6th, 2018 (edited) 128 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import getpass
  2.  
  3.  
  4. def login() -> None:
  5.     username = input('username: ')
  6.     password = getpass.getpass('password: ')
  7.     if authenticate(username, password):
  8.         print('ACCESS GRANTED')
  9.     else:
  10.         print('ACCESS DENIED')
  11.  
  12.  
  13. def authenticate(username: str, password: str) -> bool:
  14.     try:
  15.         stored_password = get_password(username)
  16.     except (FileNotFoundError, LookupError):
  17.         return False
  18.     return stored_password == password
  19.  
  20.    
  21. def get_password(username: str) -> str:
  22.     # if user exists, return user's stored password
  23.     # else raise Exception (file not found or user does not exist)
  24.     username = username.lower()
  25.     passwd = open('passwd')
  26.     for line in passwd:
  27.         # trim the newline
  28.         line = line[:-1]
  29.         uname, pword = line.split(':')
  30.         if username == uname:
  31.             return pword
  32.     raise LookupError('user does not exist')
  33.  
  34.  
  35. if __name__ == '__main__':
  36.     login()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top