<commit_before># -*- encoding: utf-8 -*-
"""
@authors: <NAME> <EMAIL>
          <NAME>. <EMAIL>

SIIM2 Server is the web server of SIIM2 Framework
Copyright (C) 2013 Infometrika Ltda.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
from uuid import uuid4
from functools import wraps

#CaliopeStorage
from neomodel import DoesNotExist
from cid.core.models import CaliopeUser

from tinyrpc.protocols.jsonrpc import JSONRPCInternalError
from tinyrpc.dispatch import public
from flask import current_app, g

prefix_session_manager = "prefix_session_manager_"


class LoginManager(object):
    @staticmethod
    @public
    def authenticate(username, password, domain=None):
        try:
            #: TODO: Add support to domain
            userNode = CaliopeUser.index.get(username=username)
            if userNode.password == password:
                session_uuid = str(uuid4()).decode('utf-8')

                #!!!!!!!!!!!!!!!!!!!!thread safe untested!!!!!!!!!!!!!!!!!!
                g.connection_thread_pool_id[g.connection_thread_id] = session_uuid
                current_app.storekv.put(prefix_session_manager + session_uuid, username)

                return {'login': True, 'uuid': session_uuid, 'user': username}
            else:
                return {'login': False}
        except DoesNotExist:
            return {'login': False}
        except Exception as e:
            raise JSONRPCInternalError(e)


    @staticmethod
    @public
    def authenticate_with_uuid(uuid, domain=None):
        if current_app.storekv.__contains__(prefix_session_manager + uuid):
            username = current_app.storekv.get(prefix_session_manager + uuid)
            g.connection_thread_pool_id[g.connection_thread_id] = uuid
            return {'login': True, 'uuid': uuid, 'user': username}
        else:
            raise JSONRPCInternalError('No valid session found')


    @staticmethod
    @public
    def logout(uuid):
        if current_app.storekv.__contains__(prefix_session_manager + uuid):
            username = current_app.storekv.delete(prefix_session_manager + uuid)
        return {'logout': True, 'uuid': uuid}

    @staticmethod
    def check():
        #!!!!!!!!!!!!!!!!!!!!thread safe untested!!!!!!!!!!!!!!!!!!
        if g.connection_thread_id in g.connection_thread_pool_id:
            key = prefix_session_manager + g.connection_thread_pool_id[g.connection_thread_id]
            if current_app.storekv.__contains__(key):<commit_msg>returned first_name and last_name in authentication methods<commit_after># -*- encoding: utf-8 -*-
"""
@authors: <NAME> <EMAIL>
          <NAME>. <EMAIL>

SIIM2 Server is the web server of SIIM2 Framework
Copyright (C) 2013 Infometrika Ltda.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
from uuid import uuid4
from functools import wraps

#CaliopeStorage
from neomodel import DoesNotExist
from cid.core.models import CaliopeUser

from tinyrpc.protocols.jsonrpc import JSONRPCInternalError
from tinyrpc.dispatch import public
from flask import current_app, g

prefix_session_manager = "prefix_session_manager_"


class LoginManager(object):
    @staticmethod
    @public
    def authenticate(username, password, domain=None):
        try:
            #: TODO: Add support to domain
            userNode = CaliopeUser.index.get(username=username)
            if userNode.password == password:
                session_uuid = str(uuid4()).decode('utf-8')

                #!!!!!!!!!!!!!!!!!!!!thread safe untested!!!!!!!!!!!!!!!!!!
                g.connection_thread_pool_id[g.connection_thread_id] = session_uuid
                current_app.storekv.put(prefix_session_manager + session_uuid, username)

                return {'login': True, 'uuid': session_uuid, 'user': username, "first_name": userNode.first_name, "last_name": userNode.last_name}
            else:
                return {'login': False}
        except DoesNotExist:
            return {'login': False}
        except Exception as e:
            raise JSONRPCInternalError(e)


    @staticmethod
    @public
    def authenticate_with_uuid(uuid, domain=None):
        if current_app.storekv.__contains__(prefix_session_manager + uuid):
            try:
                username = current_app.storekv.get(prefix_session_manager + uuid)
                g.connection_thread_pool_id[g.connection_thread_id] = uuid
                userNode = CaliopeUser.index.get(username=username)
                return {'login': True, 'uuid': uuid, 'user': username, "first_name": userNode.first_name, "last_name": userNode.last_name}
            except Exception as e:
                raise JSONRPCInternalError(e)
        else:
            raise JSONRPCInternalError('No valid session found')


    @staticmethod
    @public
    def logout(uuid):
        if current_app.storekv.__contains__(prefix_session_manager + uuid):
            username = current_app.storekv.delete(prefix_session_manager + uuid)
        return {'logout': True, 'uuid': uuid}

    @staticmethod
    def check():
        #!!!!!!!!!!!!!!!!!!!!thread safe untested!!!!!!!!!!!!!!!!!!
        if g.connection_thread_id in g.connection_thread_pool_id:
            key = prefix_session_manager + g.connection_thread_pool_id[g.connection_thread_id]
            if current_app.storekv.__contains__(key):
{'fraction_non_alphanumeric': 0.09749388753056235, 'fraction_numerical': 0.0033618581907090463, 'mean_word_length': 3.0079608083282303, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2244512', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1730, 'n_words': 548}