2013-09-20 15:55:44 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from functools import wraps
|
2014-11-21 15:28:50 +00:00
|
|
|
from uuid import UUID
|
2014-03-12 20:31:37 +00:00
|
|
|
from datetime import datetime
|
2014-03-17 16:01:13 +00:00
|
|
|
from flask import request, session
|
2013-09-20 22:38:17 +00:00
|
|
|
from flask.ext.principal import identity_changed, Identity
|
2014-03-17 16:01:13 +00:00
|
|
|
from flask.ext.login import current_user
|
2013-09-20 15:55:44 +00:00
|
|
|
from base64 import b64decode
|
|
|
|
|
2014-03-17 16:01:13 +00:00
|
|
|
import scopes
|
|
|
|
|
2013-09-25 16:45:12 +00:00
|
|
|
from data import model
|
2014-03-12 20:31:37 +00:00
|
|
|
from data.model import oauth
|
2014-05-13 16:17:26 +00:00
|
|
|
from app import app, authentication
|
2013-10-15 18:48:49 +00:00
|
|
|
from permissions import QuayDeferredPermissionUser
|
2014-03-17 16:01:13 +00:00
|
|
|
from auth_context import (set_authenticated_user, set_validated_token,
|
2014-03-18 20:45:18 +00:00
|
|
|
set_authenticated_user_deferred, set_validated_oauth_token)
|
2014-02-25 19:15:12 +00:00
|
|
|
from util.http import abort
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-03-12 16:37:06 +00:00
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
def _load_user_from_cookie():
|
|
|
|
if not current_user.is_anonymous():
|
2014-11-21 15:28:50 +00:00
|
|
|
try:
|
|
|
|
# Attempt to parse the user uuid to make sure the cookie has the right value type
|
|
|
|
UUID(current_user.get_id())
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
logger.debug('Loading user from cookie: %s', current_user.get_id())
|
|
|
|
set_authenticated_user_deferred(current_user.get_id())
|
2014-11-20 23:44:36 +00:00
|
|
|
loaded = QuayDeferredPermissionUser(current_user.get_id(), 'user_uuid', {scopes.DIRECT_LOGIN})
|
2014-03-26 19:52:24 +00:00
|
|
|
identity_changed.send(app, identity=loaded)
|
|
|
|
return current_user.db_user()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _validate_and_apply_oauth_token(token):
|
2014-03-20 16:09:25 +00:00
|
|
|
validated = oauth.validate_access_token(token)
|
|
|
|
if not validated:
|
|
|
|
logger.warning('OAuth access token could not be validated: %s', token)
|
|
|
|
authenticate_header = {
|
|
|
|
'WWW-Authenticate': ('Bearer error="invalid_token", '
|
|
|
|
'error_description="The access token is invalid"'),
|
|
|
|
}
|
|
|
|
abort(401, message='OAuth access token could not be validated: %(token)s',
|
2014-03-25 20:50:39 +00:00
|
|
|
issue='invalid-oauth-token', token=token, headers=authenticate_header)
|
2014-05-23 18:16:26 +00:00
|
|
|
elif validated.expires_at <= datetime.utcnow():
|
2014-03-20 16:09:25 +00:00
|
|
|
logger.info('OAuth access with an expired token: %s', token)
|
|
|
|
authenticate_header = {
|
|
|
|
'WWW-Authenticate': ('Bearer error="invalid_token", '
|
|
|
|
'error_description="The access token expired"'),
|
|
|
|
}
|
|
|
|
abort(401, message='OAuth access token has expired: %(token)s',
|
|
|
|
issue='invalid-oauth-token', token=token, headers=authenticate_header)
|
|
|
|
|
|
|
|
# We have a valid token
|
|
|
|
scope_set = scopes.scopes_from_scope_string(validated.scope)
|
|
|
|
logger.debug('Successfully validated oauth access token: %s with scope: %s', token,
|
|
|
|
scope_set)
|
|
|
|
|
|
|
|
set_authenticated_user(validated.authorized_user)
|
|
|
|
set_validated_oauth_token(validated)
|
|
|
|
|
2014-11-20 23:44:36 +00:00
|
|
|
new_identity = QuayDeferredPermissionUser(validated.authorized_user.uuid, 'user_uuid', scope_set)
|
2014-03-20 16:09:25 +00:00
|
|
|
identity_changed.send(app, identity=new_identity)
|
|
|
|
|
|
|
|
|
2013-10-01 04:37:28 +00:00
|
|
|
def process_basic_auth(auth):
|
2013-09-20 15:55:44 +00:00
|
|
|
normalized = [part.strip() for part in auth.split(' ') if part]
|
|
|
|
if normalized[0].lower() != 'basic' or len(normalized) != 2:
|
|
|
|
logger.debug('Invalid basic auth format.')
|
2013-09-26 19:58:11 +00:00
|
|
|
return
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-05-13 19:22:31 +00:00
|
|
|
credentials = [part.decode('utf-8') for part in b64decode(normalized[1]).split(':', 1)]
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
if len(credentials) != 2:
|
2013-10-16 18:24:10 +00:00
|
|
|
logger.debug('Invalid basic auth credential format.')
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-12-19 20:18:14 +00:00
|
|
|
elif credentials[0] == '$token':
|
2013-10-16 18:24:10 +00:00
|
|
|
# Use as token auth
|
|
|
|
try:
|
|
|
|
token = model.load_token_data(credentials[1])
|
|
|
|
logger.debug('Successfully validated token: %s' % credentials[1])
|
2014-03-17 16:01:13 +00:00
|
|
|
set_validated_token(token)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
identity_changed.send(app, identity=Identity(token.code, 'token'))
|
|
|
|
return
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
except model.DataModelException:
|
|
|
|
logger.debug('Invalid token: %s' % credentials[1])
|
|
|
|
|
2014-03-20 16:09:25 +00:00
|
|
|
elif credentials[0] == '$oauthtoken':
|
|
|
|
oauth_token = credentials[1]
|
2014-03-26 19:52:24 +00:00
|
|
|
_validate_and_apply_oauth_token(oauth_token)
|
2014-03-20 16:09:25 +00:00
|
|
|
|
2013-11-21 00:43:19 +00:00
|
|
|
elif '+' in credentials[0]:
|
|
|
|
logger.debug('Trying robot auth with credentials %s' % str(credentials))
|
|
|
|
# Use as robot auth
|
|
|
|
try:
|
|
|
|
robot = model.verify_robot(credentials[0], credentials[1])
|
|
|
|
logger.debug('Successfully validated robot: %s' % credentials[0])
|
2014-03-17 16:01:13 +00:00
|
|
|
set_authenticated_user(robot)
|
2014-11-11 22:22:37 +00:00
|
|
|
|
2014-11-20 23:44:36 +00:00
|
|
|
deferred_robot = QuayDeferredPermissionUser(robot.uuid, 'user_uuid', {scopes.DIRECT_LOGIN})
|
2014-03-12 20:31:37 +00:00
|
|
|
identity_changed.send(app, identity=deferred_robot)
|
2013-11-21 00:43:19 +00:00
|
|
|
return
|
|
|
|
except model.InvalidRobotException:
|
|
|
|
logger.debug('Invalid robot or password for robot: %s' % credentials[0])
|
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
else:
|
2014-05-13 16:17:26 +00:00
|
|
|
authenticated = authentication.verify_user(credentials[0], credentials[1])
|
2013-10-16 18:24:10 +00:00
|
|
|
|
|
|
|
if authenticated:
|
|
|
|
logger.debug('Successfully validated user: %s' % authenticated.username)
|
2014-03-17 16:01:13 +00:00
|
|
|
set_authenticated_user(authenticated)
|
2013-10-16 18:24:10 +00:00
|
|
|
|
2014-11-20 23:44:36 +00:00
|
|
|
new_identity = QuayDeferredPermissionUser(authenticated.uuid, 'user_uuid',
|
2014-03-19 22:21:58 +00:00
|
|
|
{scopes.DIRECT_LOGIN})
|
2013-10-16 18:24:10 +00:00
|
|
|
identity_changed.send(app, identity=new_identity)
|
|
|
|
return
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
# We weren't able to authenticate via basic auth.
|
2013-09-26 19:58:11 +00:00
|
|
|
logger.debug('Basic auth present but could not be validated.')
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
2013-10-01 04:37:28 +00:00
|
|
|
def process_token(auth):
|
2013-09-20 15:55:44 +00:00
|
|
|
normalized = [part.strip() for part in auth.split(' ') if part]
|
2013-09-26 00:00:22 +00:00
|
|
|
if normalized[0].lower() != 'token' or len(normalized) != 2:
|
2013-10-16 18:24:10 +00:00
|
|
|
logger.debug('Not an auth token: %s' % auth)
|
2013-09-26 19:58:11 +00:00
|
|
|
return
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-26 00:00:22 +00:00
|
|
|
token_details = normalized[1].split(',')
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
if len(token_details) != 1:
|
|
|
|
logger.warning('Invalid token format: %s' % auth)
|
2014-03-17 18:38:50 +00:00
|
|
|
abort(401, message='Invalid token format: %(auth)s', issue='invalid-auth-token', auth=auth)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-09-15 15:27:33 +00:00
|
|
|
def safe_get(lst, index, default_value):
|
|
|
|
try:
|
|
|
|
return lst[index]
|
|
|
|
except IndexError:
|
|
|
|
return default_value
|
|
|
|
|
|
|
|
token_vals = {val[0]: safe_get(val, 1, '') for val in
|
2013-09-20 15:55:44 +00:00
|
|
|
(detail.split('=') for detail in token_details)}
|
2014-09-15 15:27:33 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
if 'signature' not in token_vals:
|
|
|
|
logger.warning('Token does not contain signature: %s' % auth)
|
2014-03-17 18:38:50 +00:00
|
|
|
abort(401, message='Token does not contain a valid signature: %(auth)s',
|
2014-03-12 16:37:06 +00:00
|
|
|
issue='invalid-auth-token', auth=auth)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
try:
|
|
|
|
token_data = model.load_token_data(token_vals['signature'])
|
2013-09-26 00:00:22 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
except model.InvalidTokenException:
|
2014-03-12 20:31:37 +00:00
|
|
|
logger.warning('Token could not be validated: %s', token_vals['signature'])
|
2014-03-17 18:38:50 +00:00
|
|
|
abort(401, message='Token could not be validated: %(auth)s', issue='invalid-auth-token',
|
2014-03-12 16:37:06 +00:00
|
|
|
auth=auth)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-03-12 20:31:37 +00:00
|
|
|
logger.debug('Successfully validated token: %s', token_data.code)
|
2014-03-17 16:01:13 +00:00
|
|
|
set_validated_token(token_data)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
identity_changed.send(app, identity=Identity(token_data.code, 'token'))
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
def process_oauth(func):
|
|
|
|
@wraps(func)
|
2014-03-13 00:33:57 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
auth = request.headers.get('authorization', '')
|
|
|
|
if auth:
|
|
|
|
normalized = [part.strip() for part in auth.split(' ') if part]
|
|
|
|
if normalized[0].lower() != 'bearer' or len(normalized) != 2:
|
|
|
|
logger.debug('Invalid oauth bearer token format.')
|
|
|
|
return
|
|
|
|
|
|
|
|
token = normalized[1]
|
2014-03-26 19:52:24 +00:00
|
|
|
_validate_and_apply_oauth_token(token)
|
|
|
|
elif _load_user_from_cookie() is None:
|
2014-03-17 18:38:50 +00:00
|
|
|
logger.debug('No auth header or login cookie.')
|
2014-03-26 19:52:24 +00:00
|
|
|
return func(*args, **kwargs)
|
2014-03-13 00:33:57 +00:00
|
|
|
return wrapper
|
2014-03-12 20:31:37 +00:00
|
|
|
|
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
def process_auth(func):
|
|
|
|
@wraps(func)
|
2013-09-20 15:55:44 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
2013-10-01 04:37:28 +00:00
|
|
|
auth = request.headers.get('authorization', '')
|
|
|
|
|
|
|
|
if auth:
|
|
|
|
logger.debug('Validating auth header: %s' % auth)
|
|
|
|
process_token(auth)
|
|
|
|
process_basic_auth(auth)
|
|
|
|
else:
|
|
|
|
logger.debug('No auth header.')
|
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def require_session_login(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
loaded = _load_user_from_cookie()
|
|
|
|
if loaded is None or loaded.organization:
|
|
|
|
abort(401, message='Method requires login and no valid login could be loaded.')
|
|
|
|
return func(*args, **kwargs)
|
2013-09-20 15:55:44 +00:00
|
|
|
return wrapper
|
2013-09-26 00:00:22 +00:00
|
|
|
|
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
def extract_namespace_repo_from_session(func):
|
|
|
|
@wraps(func)
|
2013-09-26 00:00:22 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if 'namespace' not in session or 'repository' not in session:
|
2014-03-12 20:31:37 +00:00
|
|
|
logger.error('Unable to load namespace or repository from session: %s' % session)
|
2014-03-17 18:38:50 +00:00
|
|
|
abort(400, message='Missing namespace in request')
|
2013-09-26 00:00:22 +00:00
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
return func(session['namespace'], session['repository'], *args, **kwargs)
|
2013-09-26 20:32:09 +00:00
|
|
|
return wrapper
|