aboutsummaryrefslogtreecommitdiff
blob: a70fd95cc60d76b43f41763ec81b68a65e77cacc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
"""
    grumpy.utils
    ~~~~~~~~~~~~

    Miscellaneous utils for authentication and pagination.

    :copyright: (c) 2010 Priit Laes
"""
from flask import request, Response
from functools import wraps

from . import app

def authenticate():
    """Sends a 401 response that enables basic HTTP auth"""
    return Response('Could not verify your access level for that URL.\n'
                    'You have to login with proper credentials', 401,
                    {'WWW-Authenticate': 'Basic realm="Login required"'})

def check_auth(username, password):
    """Checks username password against ones stored in configuration."""
    return username == app.config['TINDERBOX_USER'] and \
        password == app.config['TINDERBOX_PASS']

def requires_auth_basic(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)
    return decorated