Add kind to credentials validate call
This commit is contained in:
parent
6f3d9a6fce
commit
0bcda90c6e
3 changed files with 31 additions and 15 deletions
|
@ -34,7 +34,8 @@ def validate_basic_auth(auth_header):
|
||||||
return ValidateResult(AuthKind.basic, missing=True)
|
return ValidateResult(AuthKind.basic, missing=True)
|
||||||
|
|
||||||
auth_username, auth_password_or_token = credentials
|
auth_username, auth_password_or_token = credentials
|
||||||
return validate_credentials(auth_username, auth_password_or_token).with_kind(AuthKind.basic)
|
result, _ = validate_credentials(auth_username, auth_password_or_token)
|
||||||
|
return result.with_kind(AuthKind.basic)
|
||||||
|
|
||||||
|
|
||||||
def _parse_basic_auth_header(auth):
|
def _parse_basic_auth_header(auth):
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
from app import authentication
|
from app import authentication
|
||||||
from auth.oauth import validate_oauth_token
|
from auth.oauth import validate_oauth_token
|
||||||
from auth.validateresult import ValidateResult, AuthKind
|
from auth.validateresult import ValidateResult, AuthKind
|
||||||
|
@ -11,6 +13,13 @@ logger = logging.getLogger(__name__)
|
||||||
ACCESS_TOKEN_USERNAME = '$token'
|
ACCESS_TOKEN_USERNAME = '$token'
|
||||||
OAUTH_TOKEN_USERNAME = '$oauthtoken'
|
OAUTH_TOKEN_USERNAME = '$oauthtoken'
|
||||||
|
|
||||||
|
class CredentialKind(Enum):
|
||||||
|
user = 'user'
|
||||||
|
robot = 'robot'
|
||||||
|
token = ACCESS_TOKEN_USERNAME
|
||||||
|
oauth_token = OAUTH_TOKEN_USERNAME
|
||||||
|
|
||||||
|
|
||||||
def validate_credentials(auth_username, auth_password_or_token):
|
def validate_credentials(auth_username, auth_password_or_token):
|
||||||
""" Validates a pair of auth username and password/token credentials. """
|
""" Validates a pair of auth username and password/token credentials. """
|
||||||
# Check for access tokens.
|
# Check for access tokens.
|
||||||
|
@ -19,14 +28,15 @@ def validate_credentials(auth_username, auth_password_or_token):
|
||||||
try:
|
try:
|
||||||
token = model.token.load_token_data(auth_password_or_token)
|
token = model.token.load_token_data(auth_password_or_token)
|
||||||
logger.debug('Successfully validated basic auth for access token %s', token.id)
|
logger.debug('Successfully validated basic auth for access token %s', token.id)
|
||||||
return ValidateResult(AuthKind.credentials, token=token)
|
return ValidateResult(AuthKind.credentials, token=token), CredentialKind.token
|
||||||
except model.DataModelException:
|
except model.DataModelException:
|
||||||
logger.warning('Failed to validate basic auth for access token %s', auth_password_or_token)
|
logger.warning('Failed to validate basic auth for access token %s', auth_password_or_token)
|
||||||
return ValidateResult(AuthKind.credentials, error_message='Invalid access token')
|
return (ValidateResult(AuthKind.credentials, error_message='Invalid access token'),
|
||||||
|
CredentialKind.token)
|
||||||
|
|
||||||
# Check for OAuth tokens.
|
# Check for OAuth tokens.
|
||||||
if auth_username == OAUTH_TOKEN_USERNAME:
|
if auth_username == OAUTH_TOKEN_USERNAME:
|
||||||
return validate_oauth_token(auth_password_or_token)
|
return validate_oauth_token(auth_password_or_token), CredentialKind.oauth_token
|
||||||
|
|
||||||
# Check for robots and users.
|
# Check for robots and users.
|
||||||
is_robot = parse_robot_username(auth_username)
|
is_robot = parse_robot_username(auth_username)
|
||||||
|
@ -34,19 +44,18 @@ def validate_credentials(auth_username, auth_password_or_token):
|
||||||
logger.debug('Found basic auth header for robot %s', auth_username)
|
logger.debug('Found basic auth header for robot %s', auth_username)
|
||||||
try:
|
try:
|
||||||
robot = model.user.verify_robot(auth_username, auth_password_or_token)
|
robot = model.user.verify_robot(auth_username, auth_password_or_token)
|
||||||
|
|
||||||
logger.debug('Successfully validated basic auth for robot %s', auth_username)
|
logger.debug('Successfully validated basic auth for robot %s', auth_username)
|
||||||
return ValidateResult(AuthKind.credentials, robot=robot)
|
return ValidateResult(AuthKind.credentials, robot=robot), CredentialKind.robot
|
||||||
except model.InvalidRobotException as ire:
|
except model.InvalidRobotException as ire:
|
||||||
logger.warning('Failed to validate basic auth for robot %s: %s', auth_username, ire.message)
|
logger.warning('Failed to validate basic auth for robot %s: %s', auth_username, ire.message)
|
||||||
return ValidateResult(AuthKind.credentials, error_message=ire.message)
|
return ValidateResult(AuthKind.credentials, error_message=ire.message), CredentialKind.robot
|
||||||
|
|
||||||
# Otherwise, treat as a standard user.
|
# Otherwise, treat as a standard user.
|
||||||
(authenticated, err) = authentication.verify_and_link_user(auth_username, auth_password_or_token,
|
(authenticated, err) = authentication.verify_and_link_user(auth_username, auth_password_or_token,
|
||||||
basic_auth=True)
|
basic_auth=True)
|
||||||
if authenticated:
|
if authenticated:
|
||||||
logger.debug('Successfully validated basic auth for user %s', authenticated.username)
|
logger.debug('Successfully validated basic auth for user %s', authenticated.username)
|
||||||
return ValidateResult(AuthKind.credentials, user=authenticated)
|
return ValidateResult(AuthKind.credentials, user=authenticated), CredentialKind.user
|
||||||
else:
|
else:
|
||||||
logger.warning('Failed to validate basic auth for user %s: %s', auth_username, err)
|
logger.warning('Failed to validate basic auth for user %s: %s', auth_username, err)
|
||||||
return ValidateResult(AuthKind.credentials, error_message=err)
|
return ValidateResult(AuthKind.credentials, error_message=err), CredentialKind.user
|
||||||
|
|
|
@ -1,30 +1,36 @@
|
||||||
from auth.credentials import ACCESS_TOKEN_USERNAME, OAUTH_TOKEN_USERNAME, validate_credentials
|
from auth.credentials import (ACCESS_TOKEN_USERNAME, OAUTH_TOKEN_USERNAME, validate_credentials,
|
||||||
|
CredentialKind)
|
||||||
from auth.validateresult import AuthKind, ValidateResult
|
from auth.validateresult import AuthKind, ValidateResult
|
||||||
from data import model
|
from data import model
|
||||||
|
|
||||||
from test.fixtures import *
|
from test.fixtures import *
|
||||||
|
|
||||||
def test_valid_user(app):
|
def test_valid_user(app):
|
||||||
result = validate_credentials('devtable', 'password')
|
result, kind = validate_credentials('devtable', 'password')
|
||||||
|
assert kind == CredentialKind.user
|
||||||
assert result == ValidateResult(AuthKind.credentials, user=model.user.get_user('devtable'))
|
assert result == ValidateResult(AuthKind.credentials, user=model.user.get_user('devtable'))
|
||||||
|
|
||||||
def test_valid_robot(app):
|
def test_valid_robot(app):
|
||||||
robot, password = model.user.create_robot('somerobot', model.user.get_user('devtable'))
|
robot, password = model.user.create_robot('somerobot', model.user.get_user('devtable'))
|
||||||
result = validate_credentials(robot.username, password)
|
result, kind = validate_credentials(robot.username, password)
|
||||||
|
assert kind == CredentialKind.robot
|
||||||
assert result == ValidateResult(AuthKind.credentials, robot=robot)
|
assert result == ValidateResult(AuthKind.credentials, robot=robot)
|
||||||
|
|
||||||
def test_valid_token(app):
|
def test_valid_token(app):
|
||||||
access_token = model.token.create_delegate_token('devtable', 'simple', 'sometoken')
|
access_token = model.token.create_delegate_token('devtable', 'simple', 'sometoken')
|
||||||
result = validate_credentials(ACCESS_TOKEN_USERNAME, access_token.code)
|
result, kind = validate_credentials(ACCESS_TOKEN_USERNAME, access_token.code)
|
||||||
|
assert kind == CredentialKind.token
|
||||||
assert result == ValidateResult(AuthKind.credentials, token=access_token)
|
assert result == ValidateResult(AuthKind.credentials, token=access_token)
|
||||||
|
|
||||||
def test_valid_oauth(app):
|
def test_valid_oauth(app):
|
||||||
user = model.user.get_user('devtable')
|
user = model.user.get_user('devtable')
|
||||||
oauth_token = list(model.oauth.list_access_tokens_for_user(user))[0]
|
oauth_token = list(model.oauth.list_access_tokens_for_user(user))[0]
|
||||||
result = validate_credentials(OAUTH_TOKEN_USERNAME, oauth_token.access_token)
|
result, kind = validate_credentials(OAUTH_TOKEN_USERNAME, oauth_token.access_token)
|
||||||
|
assert kind == CredentialKind.oauth_token
|
||||||
assert result == ValidateResult(AuthKind.oauth, oauthtoken=oauth_token)
|
assert result == ValidateResult(AuthKind.oauth, oauthtoken=oauth_token)
|
||||||
|
|
||||||
def test_invalid_user(app):
|
def test_invalid_user(app):
|
||||||
result = validate_credentials('devtable', 'somepassword')
|
result, kind = validate_credentials('devtable', 'somepassword')
|
||||||
|
assert kind == CredentialKind.user
|
||||||
assert result == ValidateResult(AuthKind.credentials,
|
assert result == ValidateResult(AuthKind.credentials,
|
||||||
error_message='Invalid Username or Password')
|
error_message='Invalid Username or Password')
|
||||||
|
|
Reference in a new issue