2013-09-20 15:55:44 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from functools import wraps
|
2013-09-26 00:00:22 +00:00
|
|
|
from flask import request, make_response, _request_ctx_stack, abort, session
|
2013-09-20 22:38:17 +00:00
|
|
|
from flask.ext.principal import identity_changed, Identity
|
2013-09-20 15:55:44 +00:00
|
|
|
from base64 import b64decode
|
|
|
|
|
2013-09-25 16:45:12 +00:00
|
|
|
from data import model
|
2013-09-25 20:46:28 +00:00
|
|
|
from app import app
|
2013-10-15 18:48:49 +00:00
|
|
|
from permissions import QuayDeferredPermissionUser
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-25 20:46:28 +00:00
|
|
|
from util.names import parse_namespace_repository
|
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__)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def get_authenticated_user():
|
2013-09-20 15:55:44 +00:00
|
|
|
return getattr(_request_ctx_stack.top, 'authenticated_user', None)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def get_validated_token():
|
2013-09-20 15:55:44 +00:00
|
|
|
return getattr(_request_ctx_stack.top, 'validated_token', None)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
credentials = b64decode(normalized[1]).split(':')
|
|
|
|
|
|
|
|
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-10-16 18:24:10 +00:00
|
|
|
if credentials[0] == '$token':
|
|
|
|
# Use as token auth
|
|
|
|
try:
|
|
|
|
token = model.load_token_data(credentials[1])
|
|
|
|
logger.debug('Successfully validated token: %s' % credentials[1])
|
|
|
|
ctx = _request_ctx_stack.top
|
|
|
|
ctx.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])
|
|
|
|
|
|
|
|
else:
|
|
|
|
authenticated = model.verify_user(credentials[0], credentials[1])
|
|
|
|
|
|
|
|
if authenticated:
|
|
|
|
logger.debug('Successfully validated user: %s' % authenticated.username)
|
|
|
|
ctx = _request_ctx_stack.top
|
|
|
|
ctx.authenticated_user = authenticated
|
|
|
|
|
|
|
|
new_identity = QuayDeferredPermissionUser(authenticated.username,
|
|
|
|
'username')
|
|
|
|
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.')
|
|
|
|
abort(401)
|
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)
|
|
|
|
abort(401)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
token_vals = {val[0]: val[1] for val in
|
2013-09-20 15:55:44 +00:00
|
|
|
(detail.split('=') for detail in token_details)}
|
2013-10-16 18:24:10 +00:00
|
|
|
if 'signature' not in token_vals:
|
|
|
|
logger.warning('Token does not contain signature: %s' % auth)
|
|
|
|
abort(401)
|
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:
|
|
|
|
logger.warning('Token could not be validated: %s' %
|
|
|
|
token_vals['signature'])
|
|
|
|
abort(401)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
session['repository'] = token_data.repository.name
|
|
|
|
session['namespace'] = token_data.repository.namespace
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
logger.debug('Successfully validated token: %s' % token_data.code)
|
|
|
|
ctx = _request_ctx_stack.top
|
|
|
|
ctx.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
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def process_auth(f):
|
2013-09-20 15:55:44 +00:00
|
|
|
@wraps(f)
|
|
|
|
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.')
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
return wrapper
|
2013-09-26 00:00:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def extract_namespace_repo_from_session(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if 'namespace' not in session or 'repository' not in session:
|
|
|
|
logger.debug('Unable to load namespace or repository from session.')
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
return f(session['namespace'], session['repository'], *args, **kwargs)
|
2013-09-26 20:32:09 +00:00
|
|
|
return wrapper
|