Ejemplo 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
Ejemplo n.º 2
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
Ejemplo 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 user_id != str(user_details['user_id']):
            # The user is not in the database.
            return None
        elif plexpy.CONFIG.HTTP_PLEX_ADMIN and user_details['is_admin']:
            # Plex admin login
            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_token = plex_tv.get_server_token()
        if server_token:

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

                if result:
                    # Refresh the users list to make sure we have all the correct permissions.
                    refresh_users()
                    # Successful login
                    return user_details, 'guest'
                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