def deleteToken(self, args): self._start(args) print('Delete token: please authenticate...') while True: admin_username = raw_input('Enter admin username: '******'Enter admin password: '******'Please sign in using administrator credentials.') elif not User.users().get(username=admin_username).authenticate(admin_password): #Db.instance().authenticate_user(admin_username, admin_password): print("Username/password don't match.") else: break username = raw_input('Enter username for token to delete: ') user = User.users().get(username=username) if user: token = Token.tokens().get_token_for_user(user=user) if token: token.delete() print('Deleted token belonging to %s.' % username) else: print('%s has no tokens to delete.' % username) else: print('%s has no tokens to delete.' % username)
def createToken(self, args): self._start(args) # Do a check to see if any admins exist before proceeding. print('Create token: please authenticate...') while True: admin_username = raw_input('Enter admin username: '******'Enter admin password: '******'Please sign in using administrator credentials.') elif not User.users().get(username=admin_username).authenticate(admin_password): print("Username/password don't match.") else: break username = raw_input('Enter username to create a token for: ') user = User.users().get(username=username) if user: try: token = Token(user=user) token.save() print(str('Token %s created for %s' % (token.token, username))) except Exception as e: print("EXCEPTION **** %s" % e) print('A token already exists for %s. Doing nothing.' % username) else: print('A user with username %s does not exist.' % username)
def createUser(self, args): self._start(args) while True: username = raw_input('Enter email address: ') if User.users().get(username=username): print('Username already exists. Please pick another username.') else: break print('username: %s' % username) while True: is_admin = raw_input('Admininstrative user? (y/N): ') if (len(is_admin) == 0) or (is_admin.upper() == 'N'): admin = False break elif is_admin.upper() == 'Y': admin = True break while True: password_1 = getpass.getpass('Enter password: '******'Re-enter password: '******'Passwords do not match.') else: break user = User(username, password_1, admin) user.save() # Db.instance().insert_user(username, password_1, admin) print('Successfully created user %s' % username)
def listUsers(self, args): self._start(args) print("%s%sCreated" % ("Username".ljust(70), "Admin".ljust(20))) for user in User.users().all(): if user.admin: admin_str = 'Y' else: admin_str = 'N' # https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior line = "%s%s%s" % (user.username.ljust(70), admin_str.ljust(20), datetime.fromtimestamp(user.created).strftime('%d-%m-%Y %H:%M:%S')) print(str(line))
def deleteUser(self, args): self._start(args) print('Delete user: please authenticate...') while True: admin_username = raw_input('Enter admin username: '******'Enter admin password: '******'Please sign in using administrator credentials.') elif not user.authenticate(admin_password): print("Username/password don't match.") else: break username_to_delete = raw_input('Enter username to delete: ') if User.users().get(username=username_to_delete).delete(): # Db.instance().delete_user(username_to_delete) print('Deleted user %s.' % username_to_delete) else: sys.exit("User doesn't exist. Doing nothing.")
def get(self): try: username = self.request.headers.get('username') password = self.request.headers.get('password') user = User.users().get(username=username) if user.authenticate(password): token = Token.tokens().get_token_for_user(user) if not token: token = Token(user=user) token.save() data = {'token': token.token} else: data = {'error': 'invalid username/password'} except Exception as e: data = {'error': str(e)} self.set_header('Content-Type', 'application/json') self.write(json.dumps(data))