<commit_before>#!/usr/bin/env python3

import json
import os
import sys

from flask import Flask, send_from_directory, request, Response, redirect, render_template
from flask_socketio import SocketIO, join_room
import flask_login
from distutils.util import strtobool
from copy import deepcopy
import logging

from alarmcode.Worker import Worker

logging = logging.getLogger('alarmpi')


class User(flask_login.UserMixin):
    pass


class AlarmPiServer(object):
    """Initialize the AlarmPI Server by defying the files to use,
    the RESTful Services and calling the class (Worker)"""

    def __init__(self, wd):
        """ Initialize the global variables for AlarmPIServer """
        self.wd = wd
        self.webDirectory = os.path.join(self.wd, 'web')
        self.staticDirectory = os.path.join(self.webDirectory, 'static')
        self.templateDirectory = os.path.join(self.webDirectory, 'template')

    def setServerConfig(self, jsonfile):
        """ Set the server file to use and initialize the users """

        self.serverfile = os.path.join(self.wd, jsonfile)
        with open(self.serverfile) as data_file:
            self.serverJson = json.load(data_file)
        self.users = deepcopy(self.serverJson['users'])

    def create_app(self):
        """ Define the RESTfull Services and call the
            accordingly method in the Worker class """

        # some_queue = None
        self.app = Flask(
            __name__,
            template_folder=self.templateDirectory,
            static_folder=self.staticDirectory
        )
        self.app.secret_key = 'super secret string'
        self.login_manager = flask_login.LoginManager()
        self.login_manager.init_app(self.app)
        self.socketio = SocketIO(self.app)

        @self.login_manager.user_loader
        def user_loader(email):
            if email not in self.users:
                return
            user = User()
            user.id = email
            return user

        @self.login_manager.request_loader
        def request_loader(request):
            username = None
            password = <PASSWORD>
            if len(request.form) > 0:
                username = request.form.get('email')
                password = request.form.get('pw')
            elif request.authorization:
                username = request.authorization['username']
                password = request.authorization['password']
            if username not in self.users:
                return
            user = User()
            user.id = username
            if password == self.users[username]['pw']:
                flask_login.login_user(user)
            return user

        @self.app.route('/login', methods=['GET', 'POST'])
        def login():
            if flask_login.current_user.is_authenticated:
                return redirect('/')
            if request.method == 'GET':
                return render_template('login.html')
            request_loader(request)
            return redirect('/')

        @self.app.route('/logout')
        def logout():
            flask_login.logout_user()
            return json.dumps("done")

        @self.login_manager.unauthorized_handler
        def unauthorized_handler():
            return redirect('/login')<commit_msg>added authentication through get argument<commit_after>#!/usr/bin/env python3

import json
import os
import sys

from flask import Flask, send_from_directory, request, Response, redirect, render_template
from flask_socketio import SocketIO, join_room
import flask_login
from distutils.util import strtobool
from copy import deepcopy
import logging

from alarmcode.Worker import Worker

logging = logging.getLogger('alarmpi')


class User(flask_login.UserMixin):
    pass


class AlarmPiServer(object):
    """Initialize the AlarmPI Server by defying the files to use,
    the RESTful Services and calling the class (Worker)"""

    def __init__(self, wd):
        """ Initialize the global variables for AlarmPIServer """
        self.wd = wd
        self.webDirectory = os.path.join(self.wd, 'web')
        self.staticDirectory = os.path.join(self.webDirectory, 'static')
        self.templateDirectory = os.path.join(self.webDirectory, 'template')

    def setServerConfig(self, jsonfile):
        """ Set the server file to use and initialize the users """

        self.serverfile = os.path.join(self.wd, jsonfile)
        with open(self.serverfile) as data_file:
            self.serverJson = json.load(data_file)
        self.users = deepcopy(self.serverJson['users'])

    def create_app(self):
        """ Define the RESTfull Services and call the
            accordingly method in the Worker class """

        # some_queue = None
        self.app = Flask(
            __name__,
            template_folder=self.templateDirectory,
            static_folder=self.staticDirectory
        )
        self.app.secret_key = 'super secret string'
        self.login_manager = flask_login.LoginManager()
        self.login_manager.init_app(self.app)
        self.socketio = SocketIO(self.app)

        @self.login_manager.user_loader
        def user_loader(email):
            if email not in self.users:
                return
            user = User()
            user.id = email
            return user

        @self.login_manager.request_loader
        def request_loader(request):
            username = None
            password = <PASSWORD>
            if len(request.form) > 0:
                username = request.form.get('email')
                password = request.form.get('pw')
            elif request.authorization:
                username = request.authorization['username']
                password = request.authorization['password']
            elif len(request.args) > 0:
                username = request.args.get('username')
                password = request.args.get('password')
            if username not in self.users:
                return
            user = User()
            user.id = username
            if password == self.users[username]['pw']:
                flask_login.login_user(user)
            return user

        @self.app.route('/login', methods=['GET', 'POST'])
        def login():
            if flask_login.current_user.is_authenticated:
                return redirect('/')
            if request.method == 'GET':
                return render_template('login.html')
            request_loader(request)
            return redirect('/')

        @self.app.route('/logout')
        def logout():
            flask_login.logout_user()
            return json.dumps("done")

        @self.login_manager.unauthorized_handler
        def unauthorized_handler():
            return redirect('/login')
{'fraction_non_alphanumeric': 0.09484812388326384, 'fraction_numerical': 0.0007444907683144729, 'mean_word_length': 2.168396226415094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '3700565', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1744, 'n_words': 444}