<commit_before>import sha

from werkzeug.exceptions import Unauthorized
from flask import request, g, Blueprint

from labmanager.database import db_session
from labmanager.models import Credential

scorm_blueprint = Blueprint('basic_auth', __name__)

def check_lms_auth(lmsname, password):
    hash_password = sha.new(password).hexdigest()
    # TODO: check if there could be a conflict between two LMSs with same key??
    credential = db_session.query(Credential).filter_by(key = lmsname, secret = hash_password).first()
    if credential is None:
        return False
    g.lms = credential.lms.name
    return True

@scorm_blueprint.before_request
def requires_lms_auth():
    auth = request.authorization
    if not auth:
        from labmanager.views import get_json
        json_data = get_json()
        if json_data is None:
            raise Unauthorized("Could not verify your access level for that URL")

        username = json_data.get('lms_username','')
        password = json_data.get('lms_password','')
    else:
        username = auth.username
        password = <PASSWORD>

    if not check_lms_auth(username, password):
        raise Unauthorized("Could not verify your access level for that URL")


<commit_msg>Flask's Unauthorized does not return WWW-Authenticate header, therefore having issues with several clients (including browsers)<commit_after>import sha

from werkzeug.exceptions import Unauthorized
from flask import request, g, Blueprint, Response

from labmanager.database import db_session
from labmanager.models import Credential

scorm_blueprint = Blueprint('basic_auth', __name__)

def check_lms_auth(lmsname, password):
    hash_password = <PASSWORD>.new(password).hexdigest()
    # TODO: check if there could be a conflict between two LMSs with same key??
    credential = db_session.query(Credential).filter_by(key = lmsname, secret = hash_password).first()
    if credential is None:
        return False
    g.lms = credential.lms.name
    return True

@scorm_blueprint.before_request
def requires_lms_auth():

    UNAUTHORIZED = Response(response="Could not verify your credentials for that URL", status=401, headers = {'WWW-Authenticate':'Basic realm="Login Required"'})

    auth = request.authorization
    if not auth:
        from labmanager.views import get_json
        json_data = get_json()
        if json_data is None:
            return UNAUTHORIZED

        username = json_data.get('lms_username','')
        password = <PASSWORD>_data.get('lms_password','')
    else:
        username = auth.username
        password = <PASSWORD>

    if not check_lms_auth(username, password):
        return UNAUTHORIZED



{'fraction_non_alphanumeric': 0.08055451479955039, 'fraction_numerical': 0.0011240164855751218, 'mean_word_length': 3.717314487632509, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5495906', 'n_tokens_mistral': 783, 'n_tokens_neox': 759, 'n_words': 229}