Changeset - e049a28fbddc
[Not reviewed]
version_5
0 3 1
Dennis Fink - 10 years ago 2015-08-26 18:32:58
dennis.fink@c3l.lu
Add new HTTPAuth feature
4 files changed with 31 insertions and 9 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/auth.py
Show inline comments
 
new file 100644
 
from flask import current_app
 
from flask.ext.httpauth import HTTPDigestAuth
 

	
 
httpauth = HTTPDigestAuth()
 

	
 

	
 
@httpauth.get_password
 
def get_pw(username):
 

	
 
    if username in current_app.config['ENNSTATUS_SERVERS']:
 
        return current_app.confg['ENNSTATUS_SERVERS'][username]['PASSWORD']
 

	
 
    return None
ennstatus/api/views.py
Show inline comments
 
@@ -12,7 +12,7 @@ import pygeoip
 
from ennstatus.status.functions import (single_server, all_servers,
 
                                        all_servers_by_type)
 
from .model import Server
 

	
 
from .auth import httpauth
 

	
 
api_page = Blueprint('api', __name__)
 
gi4 = pygeoip.GeoIP('/usr/share/GeoIP/GeoIP.dat', pygeoip.MEMORY_CACHE)
 
@@ -20,18 +20,25 @@ gi6 = pygeoip.GeoIP('/usr/share/GeoIP/Ge
 

	
 

	
 
@api_page.route('/update', methods=('POST',))
 
@httpauth.login_required
 
def update():
 

	
 
    current_app.logger.info('Handling update')
 
    if current_app.debug:
 
        accepted_ips = ['127.0.0.1']
 
    else:
 
        accepted_ips = current_app.config.get('ENNSTATUS_ACCEPTED_IPS', [])
 

	
 
    try:
 
        servers = current_app.config['ENNSTATUS_SERVERS']
 
    except KeyError as e:
 
        current_app.logger.error(str(e))
 
        return abort(500)
 

	
 
    if request.remote_addr not in accepted_ips:
 
        current_app.logger.warn('Unallowed IP %s tried to update data!'
 
                                % request.remote_addr)
 
        return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'}
 
    try:
 
        if request.remote_addr not in servers[httpauth.username()]['IPS']:
 
            current_app.logger.warn('Unallowed IP %s tried to update data!'
 
                                    % request.remote_addr)
 
            return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'}
 
    except KeyError as e:
 
        current_app.logger.error(str(e))
 
        return abort(500)
 

	
 
    data = request.get_json()
 

	
requirements.in
Show inline comments
 
Flask-Bootstrap==3.3.5.6
 
Flask-HTTPAuth==2.6.0
 
Flask-Mail==0.9.1
 
Flask-Moment==0.5.1
 
Flask-WTF==0.12
requirements.txt
Show inline comments
 
@@ -7,6 +7,7 @@
 
blinker==1.4              # via flask-mail
 
dominate==2.1.12          # via flask-bootstrap
 
Flask-Bootstrap==3.3.5.6
 
Flask-HTTPAuth==2.6.0
 
Flask-Mail==0.9.1
 
Flask-Moment==0.5.1
 
Flask-WTF==0.12
0 comments (0 inline, 0 general)