def test_invalid_token_header_no_user(): expiry = delorean.parse('2018-05-17 13:47:34').datetime payload = { 'exp': expiry, } token = token_validation.encode_token(payload, PRIVATE_KEY) token = token.decode('utf8') header = f'Bearer {token}' result = token_validation.validate_token_header(header, PUBLIC_KEY) assert not result
def test_invalid_token_header_not_expiry_time(): payload = { 'user': { 'name': 'homer', 'id': 1, }, } token = token_validation.encode_token(payload, PRIVATE_KEY) token = token.decode('utf8') header = f'Bearer {token}' result = token_validation.validate_token_header(header, PUBLIC_KEY) assert not result
def test_invalid_token_header_expired(): expiry = delorean.parse('2018-05-17 13:47:33').datetime payload = { 'user': { 'username': '******', 'id': 1, }, 'exp': expiry, } token = token_validation.encode_token(payload, PRIVATE_KEY) token = token.decode('utf8') header = f'Bearer {token}' result = token_validation.validate_token_header(header, PUBLIC_KEY) assert not result
def test_invalid_token_no_header(): header = None result = token_validation.validate_token_header(header, PUBLIC_KEY) assert not result
def test_invalid_token_header_bad_token(): header = 'Bearer baddata' result = token_validation.validate_token_header(header, PUBLIC_KEY) assert not result
def test_invalid_token_header_invalid_format(): header = 'bad header' result = token_validation.validate_token_header(header, PUBLIC_KEY) assert not result
def test_valid_token_header(): header = token_validation.generate_token_header('homer', 1, PRIVATE_KEY) result = token_validation.validate_token_header(header, PUBLIC_KEY) assert all( (isinstance(result, dict), result['name'] == 'homer', result['id'] == 1))
def test_valid_token_header_invalid_key(): header = token_validation.generate_token_header('homer', 1, PRIVATE_KEY) result = token_validation.validate_token_header(header, INVALID_PUBLIC_KEY) assert not result
def authentication_header_parser(value): user = validate_token_header(value, config.PUBLIC_KEY) if not user: abort(401) return user