If you are using Cork, catch the AuthException
exception raised only if you didn't set the fail_redirect
keyword; you can then just catch that exception:
from cork.cork import AuthException
try:
aaa.require()
except AuthException as ae:
# send back a JSON response