Esempio n. 1
0
def user_login(username=None, password=None):
    if not username or not password:
        return None

    # Try to login to Plex.tv to check if the user has a vaild account
    plex_tv = PlexTV(username=username, password=password)
    plex_user = plex_tv.get_token()
    if plex_user:
        user_token = plex_user['auth_token']
        user_id = plex_user['user_id']

        # Try to retrieve the user from the database.
        # Also make sure guest access is enabled for the user and the user is not deleted.
        user_data = Users()
        user_details = user_data.get_details(user_id=user_id)
        if user_id != str(user_details['user_id']):
            # The user is not in the database.
            return None
        elif not user_details['allow_guest'] or user_details['deleted_user']:
            # Guest access is disabled or the user is deleted.
            return None

        # The user is in the database, and guest access is enabled, so try to retrieve a server token.
        # If a server token is returned, then the user is a valid friend of the server.
        plex_tv = PlexTV(token=user_token)
        server_token = plex_tv.get_server_token()
        if server_token:

            # Register the new user / update the access tokens.
            monitor_db = MonitorDatabase()
            try:
                logger.debug(u"PlexPy WebAuth :: Regestering tokens for user '%s' in the database." % username)
                result = monitor_db.action('UPDATE users SET user_token = ?, server_token = ? WHERE user_id = ?',
                                            [user_token, server_token, user_id])

                if result:
                    # Refresh the users list to make sure we have all the correct permissions.
                    plextv.refresh_users()
                    # Successful login
                    return True
                else:
                    logger.warn(u"PlexPy WebAuth :: Unable to register user '%s' in database." % username)
                    return None
            except Exception as e:
                logger.warn(u"PlexPy WebAuth :: Unable to register user '%s' in database: %s." % (username, e))
                return None
        else:
            logger.warn(u"PlexPy WebAuth :: Unable to retrieve Plex.tv server token for user '%s'." % username)
            return None
    else:
        logger.warn(u"PlexPy WebAuth :: Unable to retrieve Plex.tv user token for user '%s'." % username)
        return None

    return None
Esempio n. 2
0
    def signin(self,
               username=None,
               password=None,
               remember_me='0',
               admin_login='******',
               *args,
               **kwargs):
        if cherrypy.request.method != 'POST':
            cherrypy.response.status = 405
            return {'status': 'error', 'message': 'Sign in using POST.'}

        error_message = {
            'status': 'error',
            'message': 'Incorrect username or password.'
        }

        valid_login, user_group = check_credentials(username, password,
                                                    admin_login)

        if valid_login:
            if user_group == 'tautulli admin':
                user_group = 'admin'
                user_id = None
            else:
                if re.match(r"[^@]+@[^@]+\.[^@]+", username):
                    user_details = Users().get_details(email=username)
                else:
                    user_details = Users().get_details(user=username)

                user_id = user_details['user_id']

            time_delta = timedelta(
                days=30) if remember_me == '1' else timedelta(minutes=60)
            expiry = datetime.utcnow() + time_delta

            payload = {
                'user_id': user_id,
                'user': username,
                'user_group': user_group,
                'exp': expiry
            }

            jwt_token = jwt.encode(payload,
                                   plexpy.CONFIG.JWT_SECRET,
                                   algorithm=JWT_ALGORITHM)

            self.on_login(username=username,
                          user_id=user_id,
                          user_group=user_group,
                          success=1)

            jwt_cookie = JWT_COOKIE_NAME + plexpy.CONFIG.PMS_UUID
            cherrypy.response.cookie[jwt_cookie] = jwt_token
            cherrypy.response.cookie[jwt_cookie]['expires'] = int(
                time_delta.total_seconds())
            cherrypy.response.cookie[jwt_cookie]['path'] = '/'

            cherrypy.request.login = payload
            cherrypy.response.status = 200
            return {
                'status': 'success',
                'token': jwt_token.decode('utf-8'),
                'uuid': plexpy.CONFIG.PMS_UUID
            }

        elif admin_login == '1':
            self.on_login(username=username)
            logger.debug(
                u"Tautulli WebAuth :: Invalid admin login attempt from '%s'." %
                username)
            cherrypy.response.status = 401
            return error_message

        else:
            self.on_login(username=username)
            logger.debug(
                u"Tautulli WebAuth :: Invalid login attempt from '%s'." %
                username)
            cherrypy.response.status = 401
            return error_message
Esempio n. 3
0
def plex_user_login(username=None, password=None, token=None, headers=None):
    user_token = None
    user_id = None

    # Try to login to Plex.tv to check if the user has a vaild account
    if username and password:
        plex_tv = PlexTV(username=username, password=password, headers=headers)
        plex_user = plex_tv.get_token()
        if plex_user:
            user_token = plex_user['auth_token']
            user_id = plex_user['user_id']
    elif token:
        plex_tv = PlexTV(token=token, headers=headers)
        plex_user = plex_tv.get_plex_account_details()
        if plex_user:
            user_token = token
            user_id = plex_user['user_id']
    else:
        return None

    if user_token and user_id:
        # Try to retrieve the user from the database.
        # Also make sure guest access is enabled for the user and the user is not deleted.
        user_data = Users()
        user_details = user_data.get_details(user_id=user_id)
        if int(user_id) != int(user_details['user_id']):
            # The user is not in the database.
            return None
        elif user_details['is_admin']:
            # Plex admin login
            if user_details['is_plextv']:
                account = plexpy.PLEXTV_ACCOUNTS.get_account(
                    user_id=user_details['user_id'])
                if not account.is_validated:
                    account.reinit()
                    account.is_validated = True
                    account.refresh_servers()
                    account.start_servers()
            return user_details, 'admin'
        elif not user_details['allow_guest'] or user_details['deleted_user']:
            # Guest access is disabled or the user is deleted.
            return None

        # Stop here if guest access is not enabled
        if not plexpy.CONFIG.ALLOW_GUEST_ACCESS:
            return None

        # The user is in the database, and guest access is enabled, so try to retrieve a server token.
        # If a server token is returned, then the user is a valid friend of the server.
        plex_tv = PlexTV(token=user_token, headers=headers)
        server_tokens = plex_tv.get_server_token()
        if server_tokens:

            # Register the new user / update the access tokens.
            monitor_db = MonitorDatabase()
            try:
                logger.debug(
                    u"Tautulli WebAuth :: Registering tokens for user '%s' in the database."
                    % user_details['username'])
                for server_id, server_token in server_tokens.items():
                    result = monitor_db.action(
                        'UPDATE user_shared_libraries '
                        '   SET server_token = ? '
                        ' WHERE id = (SELECT id FROM users WHERE user_id = ?) '
                        '   AND server_id = ?',
                        [server_token, user_details['user_id'], server_id])
                if server_tokens:
                    # Refresh the users list to make sure we have all the correct permissions.
                    #refresh_users()
                    threading.Thread(target=refresh_users).start()
                    # Successful login
                    if user_details['allow_guest'] in GUEST_ACCESS_LEVELS:
                        guest_level = GUEST_ACCESS_LEVELS[
                            user_details['allow_guest']].lower()
                    else:
                        guest_level = None
                    return user_details, guest_level
                else:
                    logger.warn(
                        u"Tautulli WebAuth :: Unable to register user '%s' in database."
                        % user_details['username'])
                    return None

            except Exception as e:
                logger.warn(
                    u"Tautulli WebAuth :: Unable to register user '%s' in database: %s."
                    % (user_details['username'], e))
                return None
        else:
            logger.warn(
                u"Tautulli WebAuth :: Unable to retrieve Plex.tv server token for user '%s'."
                % user_details['username'])
            return None
    elif username:
        logger.warn(
            u"Tautulli WebAuth :: Unable to retrieve Plex.tv user token for user '%s'."
            % username)
        return None

    elif token:
        logger.warn(
            u"Tautulli WebAuth :: Unable to retrieve Plex.tv user token for Plex OAuth."
        )
        return None
def user_login(username=None, password=None):
    if not username or not password:
        return None

    # Try to login to Plex.tv to check if the user has a vaild account
    plex_tv = PlexTV(username=username, password=password)
    plex_user = plex_tv.get_token()
    if plex_user:
        user_token = plex_user['auth_token']
        user_id = plex_user['user_id']

        # Try to retrieve the user from the database.
        # Also make sure guest access is enabled for the user and the user is not deleted.
        user_data = Users()
        user_details = user_data.get_details(user_id=user_id)
        if user_id != str(user_details['user_id']):
            # The user is not in the database.
            return None
        elif not user_details['allow_guest'] or user_details['deleted_user']:
            # Guest access is disabled or the user is deleted.
            return None

        # The user is in the database, and guest access is enabled, so try to retrieve a server token.
        # If a server token is returned, then the user is a valid friend of the server.
        plex_tv = PlexTV(token=user_token)
        server_token = plex_tv.get_server_token()
        if server_token:

            # Register the new user / update the access tokens.
            monitor_db = MonitorDatabase()
            try:
                logger.debug(
                    u"PlexPy WebAuth :: Regestering tokens for user '%s' in the database."
                    % username)
                result = monitor_db.action(
                    'UPDATE users SET user_token = ?, server_token = ? WHERE user_id = ?',
                    [user_token, server_token, user_id])

                if result:
                    # Refresh the users list to make sure we have all the correct permissions.
                    plextv.refresh_users()
                    # Successful login
                    return True
                else:
                    logger.warn(
                        u"PlexPy WebAuth :: Unable to register user '%s' in database."
                        % username)
                    return None
            except Exception as e:
                logger.warn(
                    u"PlexPy WebAuth :: Unable to register user '%s' in database: %s."
                    % (username, e))
                return None
        else:
            logger.warn(
                u"PlexPy WebAuth :: Unable to retrieve Plex.tv server token for user '%s'."
                % username)
            return None
    else:
        logger.warn(
            u"PlexPy WebAuth :: Unable to retrieve Plex.tv user token for user '%s'."
            % username)
        return None

    return None