def terminate_a_session(self):
     authenticator = StatefulAuthenticator()
     sessions = list(authenticator.get_member_sessions(context.identity.id))
     for session_id in sessions:
         if session_id != context.identity.session_id:
             authenticator.unregister_session(session_id)
     return dict()
 def login(self, role_name: str = 'supervisor'):
     session_id = str(uuid4()).replace('-', '')
     authenticator = StatefulAuthenticator()
     access_token = authenticator.__class__.create_token(
         dict(id=context.form.get('mobile'),
              mobile=context.form.get('mobile'),
              roles=[role_name],
              sessionId=session_id))
     authenticator.register_session(member_id=context.form.get('mobile'),
                                    session_id=session_id)
     return dict(accessToken=access_token)
class MyApplication(Application):
    def __init__(self):
        super().__init__(root=RootController())
        self.__authenticator__ = StatefulAuthenticator()

    def configure(self, *args, **kwargs):
        super().configure(*args, **kwargs)
        settings.merge("""
            auth:
              jwt_algorithm: HS256
              jwt_secret_key: DSXdzzxxOiIxMjM0NTY3ODkwIiwFtZSI6IkpvadG4gRG9lrr
              redis:
                host: localhost
                port: 6379
        """)

    def begin_request(self):
        self.__authenticator__.authenticate_request()

    def prepare(self):
        pass
 def terminate_all_sessions(self):
     authenticator = StatefulAuthenticator()
     authenticator.invalidate_member(context.identity.id)
     return dict()
 def get_session_info(self, session_id: str = None):
     authenticator = StatefulAuthenticator()
     return authenticator.get_session_info(
         session_id or context.identity.session_id) or dict()
def test_authenticator(app):
    _ = app
    from nanohttp.contexts import Context
    with Context({}):
        authenticator = StatefulAuthenticator()
        member_id = 500

        for session_id in ('bla_bla1', 'bla_bla2', 'bla_bla3'):
            authenticator.register_session(member_id=member_id,
                                           session_id=session_id)

            # Get session info
            info = authenticator.get_session_info(session_id=session_id)
            assert 'remoteAddress' in info

        last_sessions = sessions = authenticator.get_member_sessions(member_id)
        assert len(sessions) >= 0

        authenticator.unregister_session(session_id)

        sessions = authenticator.get_member_sessions(member_id)
        assert len(sessions) == len(last_sessions) - 1

        authenticator.invalidate_member(member_id)

        sessions = authenticator.get_member_sessions(member_id)
        assert len(sessions) == 0
 def __init__(self):
     super().__init__(root=RootController())
     self.__authenticator__ = StatefulAuthenticator()
 def get_sessions(self):
     authenticator = StatefulAuthenticator()
     return list(authenticator.get_member_sessions(context.identity.id))