flake8 style

main
Bob Mottram 2020-04-01 19:29:56 +00:00
parent 65d3ace372
commit 805aef6a74
1 changed files with 77 additions and 69 deletions

38
auth.py
View File

@ -10,45 +10,47 @@ import base64
import hashlib
import binascii
import os
import shutil
import random
def hashPassword(password: str) -> str:
"""Hash a password for storing
"""
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash= \
hashlib.pbkdf2_hmac('sha512', \
password.encode('utf-8'), \
pwdhash = hashlib.pbkdf2_hmac('sha512',
password.encode('utf-8'),
salt, 100000)
pwdhash = binascii.hexlify(pwdhash)
return (salt + pwdhash).decode('ascii')
def verifyPassword(storedPassword: str, providedPassword: str) -> bool:
"""Verify a stored password against one provided by user
"""
salt = storedPassword[:64]
storedPassword = storedPassword[64:]
pwdhash= \
hashlib.pbkdf2_hmac('sha512', \
providedPassword.encode('utf-8'), \
salt.encode('ascii'), \
pwdhash = hashlib.pbkdf2_hmac('sha512',
providedPassword.encode('utf-8'),
salt.encode('ascii'),
100000)
pwdhash = binascii.hexlify(pwdhash).decode('ascii')
return pwdhash == storedPassword
def createBasicAuthHeader(nickname: str, password: str) -> str:
"""This is only used by tests
"""
authStr = nickname.replace('\n', '') + ':' + password.replace('\n', '')
return 'Basic ' + base64.b64encode(authStr.encode('utf-8')).decode('utf-8')
def authorizeBasic(baseDir: str,path: str,authHeader: str,debug: bool) -> bool:
def authorizeBasic(baseDir: str, path: str, authHeader: str,
debug: bool) -> bool:
"""HTTP basic auth
"""
if ' ' not in authHeader:
if debug:
print('DEBUG: Authorixation header does not '+ \
print('DEBUG: Authorixation header does not ' +
'contain a space character')
return False
if '/users/' not in path and \
@ -67,14 +69,14 @@ def authorizeBasic(baseDir: str,path: str,authHeader: str,debug: bool) -> bool:
plain = base64.b64decode(base64Str).decode('utf-8')
if ':' not in plain:
if debug:
print('DEBUG: Basic Auth header does not contain a ":" '+ \
print('DEBUG: Basic Auth header does not contain a ":" ' +
'separator for username:password')
return False
nickname = plain.split(':')[0]
if nickname != nicknameFromPath:
if debug:
print('DEBUG: Nickname given in the path ('+nicknameFromPath+ \
') does not match the one in the Authorization header ('+ \
print('DEBUG: Nickname given in the path (' + nicknameFromPath +
') does not match the one in the Authorization header (' +
nickname + ')')
return False
passwordFile = baseDir+'/accounts/passwords'
@ -92,9 +94,11 @@ def authorizeBasic(baseDir: str,path: str,authHeader: str,debug: bool) -> bool:
if debug:
print('DEBUG: Password check failed for ' + nickname)
return success
print('DEBUG: Did not find credentials for '+nickname+' in '+passwordFile)
print('DEBUG: Did not find credentials for ' + nickname +
' in ' + passwordFile)
return False
def storeBasicCredentials(baseDir: str, nickname: str, password: str) -> bool:
"""Stores login credentials to a file
"""
@ -127,6 +131,7 @@ def storeBasicCredentials(baseDir: str,nickname: str,password: str) -> bool:
passfile.write(storeStr + '\n')
return True
def removePassword(baseDir: str, nickname: str) -> None:
"""Removes the password entry for the given nickname
This is called during account removal
@ -140,6 +145,7 @@ def removePassword(baseDir: str,nickname: str) -> None:
fout.write(line)
os.rename(passwordFile + '.new', passwordFile)
def authorize(baseDir: str, path: str, authHeader: str, debug: bool) -> bool:
"""Authorize using http header
"""
@ -147,6 +153,8 @@ def authorize(baseDir: str,path: str,authHeader: str,debug: bool) -> bool:
return authorizeBasic(baseDir, path, authHeader, debug)
return False
def createPassword(length=10):
validChars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
validChars = 'abcdefghijklmnopqrstuvwxyz' + \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
return ''.join((random.choice(validChars) for i in range(length)))