Have the index use the same abort method

This commit is contained in:
Joseph Schorr 2014-01-24 15:01:40 -05:00
parent aa5f669e69
commit 30a26d099f
3 changed files with 32 additions and 25 deletions

View file

@ -2,7 +2,7 @@ import json
import logging
import urlparse
from flask import request, make_response, jsonify, abort, session, Blueprint
from flask import request, make_response, jsonify, abort as flask_abort, session, Blueprint
from functools import wraps
from data import model
@ -16,6 +16,8 @@ from auth.permissions import (ModifyRepositoryPermission, UserPermission,
ReadRepositoryPermission,
CreateRepositoryPermission)
from util.http import abort
logger = logging.getLogger(__name__)
index = Blueprint('index', __name__)

View file

@ -10,8 +10,9 @@ from time import time
from data.queue import image_diff_queue
from app import app
from auth.auth import process_auth, extract_namespace_repo_from_session, get_authenticated_user, get_validated_token
from auth.auth import process_auth, extract_namespace_repo_from_session
from util import checksums, changes
from util.http import abort
from auth.permissions import (ReadRepositoryPermission,
ModifyRepositoryPermission)
from data import model
@ -21,10 +22,6 @@ registry = Blueprint('registry', __name__)
store = app.config['STORAGE']
logger = logging.getLogger(__name__)
DEFAULT_MESSAGE = {}
DEFAULT_MESSAGE[400] = 'Invalid Request'
DEFAULT_MESSAGE[403] = 'Forbidden'
DEFAULT_MESSAGE[404] = 'Not Found'
@registry.errorhandler(404)
def fallback_not_found(e):
@ -38,25 +35,6 @@ def fallback_forbidden(e):
def fallback_invalid_request(e):
return make_response('Invalid Request', 400)
def abort(status_code, message=None, **kwargs):
if status_code == 403 and not message:
# Create a default error message for auth failure.
message = 'Forbidden. '
auth_user = get_authenticated_user()
auth_token = get_validated_token()
if auth_user:
message = message + 'Current user: ' + auth_user
elif auth_token:
message = message + 'Current token: ' + auth_token
message = message % kwargs if message else DEFAULT_MESSAGE[status_code]
# Log the abort.
log.error('Error %s: %s. Arguments: %s' % (status_code, message, request.view_args))
# Report the abort to the user.
flask_abort(make_response(HTTPException(message), status_code, headers))
class SocketReader(object):
def __init__(self, fp):

27
util/http.py Normal file
View file

@ -0,0 +1,27 @@
from flask import request, abort as flask_abort
from auth.auth import process_auth, extract_namespace_repo_from_session, get_authenticated_user, get_validated_token
DEFAULT_MESSAGE = {}
DEFAULT_MESSAGE[400] = 'Invalid Request'
DEFAULT_MESSAGE[403] = 'Forbidden'
DEFAULT_MESSAGE[404] = 'Not Found'
def abort(status_code, message=None, **kwargs):
if status_code == 403 and not message:
# Create a default error message for auth failure.
message = 'Forbidden. '
auth_user = get_authenticated_user()
auth_token = get_validated_token()
if auth_user:
message = message + 'Current user: ' + auth_user
elif auth_token:
message = message + 'Current token: ' + auth_token
message = message % kwargs if message else DEFAULT_MESSAGE[status_code]
# Log the abort.
log.error('Error %s: %s. Arguments: %s' % (status_code, message, request.view_args))
# Report the abort to the user.
flask_abort(make_response(HTTPException(message), status_code, headers))