<s> import json <EOL> from functools import wraps <EOL> from flask import request , Response <EOL> from mrq . context import get_current_config <EOL> from mrq . utils import MongoJSONEncoder <EOL> def jsonify ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return Response ( <EOL> json . dumps ( <EOL> dict ( <EOL> * args , <EOL> ** kwargs ) , <EOL> cls = MongoJSONEncoder ) , <EOL> mimetype = '<STR_LIT:application/json>' ) <EOL> def check_auth ( username , pwd ) : <EOL> """<STR_LIT>""" <EOL> cfg = get_current_config ( ) <EOL> return username == cfg [ "<STR_LIT>" ] . split ( <EOL> "<STR_LIT::>" ) [ <NUM_LIT:0> ] and pwd == cfg [ "<STR_LIT>" ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> def authenticate ( ) : <EOL> """<STR_LIT>""" <EOL> return Response ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <NUM_LIT> , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> def requires_auth ( f ) : <EOL> cfg = get_current_config ( ) <EOL> if not cfg [ "<STR_LIT>" ] : <EOL> return f <EOL> @ wraps ( f ) <EOL> def decorated ( * args , ** kwargs ) : <EOL> auth = request . authorization <EOL> if not auth or not check_auth ( auth . username , auth . password ) : <EOL> return authenticate ( ) <EOL> return f ( * args , ** kwargs ) <EOL> return decorated </s>
