示例#1
0
class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or secrets.token_hex(128)
    DEBUG = bool(os.environ.get('DEBUG')) or False

    REDIS_NAME = os.environ.get('REDIS_NAME') or 'localhost'
    REDIS_URL = f'redis://{REDIS_NAME}'

    JWT_SECRET = os.environ.get(
        'JWT_SECRET') or 'ChangeMeChangeMeChangeMeChangeMeChangeMeChangeMeChangeMeChangeMeChangeMe'
    JWT_SESSION_TIME = int(os.environ.get('JWT_SESSION_TIME') or '4')

    APP_URL = os.environ.get('APP_URL') or 'http://localhost:5000'

    FILE_STORE_HOST = os.environ.get('FILE_STORE_HOST') or 'localhost'
    FILE_STORE_URL = f'http://{FILE_STORE_HOST}:8081'

    PUBLICATION_API_HOST = os.environ.get(
        'PUTLICATION_API_HOST') or 'localhost'
    PUBLICATION_API_URL = f'http://{PUBLICATION_API_HOST}:8090'

    redis = redis.Redis(REDIS_NAME)
    # user_manager = UserManager(redis)
    login_manager = LoginManager(redis)

    AUTH0_CLIENT_ID = os.environ.get('AUTH0_CLIENT_ID')
    AUTH0_CLIENT_SECRET = os.environ.get('AUTH0_CLIENT_SECRET')
    AUTH0_API_BASE_URL = os.environ.get('AUTH0_API_BASE_URL')
    AUTH0_CALLBACK_URL = APP_URL + '/callback'
示例#2
0
def test_login():
    """test"""
    login = LoginManager()

    assert isinstance(login.token, str)
    assert isinstance(login.username, str)
    assert isinstance(login.password, str)
示例#3
0
    def __init__(self, access_id: str, password: str,
                 security_questions: tuple) -> None:
        self.password = password
        self.access_id = access_id
        self.security_questions = security_questions

        self.login_manager = LoginManager(requests.Session(), self.access_id,
                                          self.password,
                                          self.security_questions)
示例#4
0
    def handle(self, *args, **options):
        # Set nick
        if not options['nick']:
            options['nick'] = raw_input('Nick > ')
        # Set family
        if not options['family']:
            options['family'] = raw_input('Family (default:wikipedia) > ') or 'wikipedia'
        # Set language
        if not options['language']:
            options['language'] = raw_input('Language (default:en) > ') or 'en'
        # Set index URL
        if not options['url']:
            options['url'] = raw_input('Index URL > ')

        # Set active or not
        if options['active'] is None:
            if raw_input('active [Y/n]') != 'n':
                options['active'] = True

        U = Wiki_User(
            nick=options['nick'],
            family=options['family'],
            language=options['language'],
            url=options['url'],
            comment=options['comment'],
            active=options['active'] or False
        )

        # Try to validate family
        try:
            validate_family(options['family'])
        except ValidationError:
            family_file = config.datafilepath('families')+('/%s_family') % options['family']
            logging.error("Your family file '%s' doesn't exist." % family_file)
            if raw_input('Do you want to create family file ? [Y/n] ') != 'n':
                with open(family_file, 'w') as outfile:
                    t = loader.get_template('family.py')
                    c = Context({
                        'opts': options,
                        'scriptpath': raw_input("scriptpath (default:'/wiki/') >") or '/wiki/',
                    })
                    outfile.write(t.render(context))
                    logging.info("Create family file '%s'." % family_file)

        # Try to validate user in DB
        try:
            U.full_clean()  # Test to validate fields
        except ValidationError as e:
            logging.error('Bad value(s) given for fields.')
        else:
            try:
                # Create bot dir
                bot_path = settings.BASEDIR+'/bots-config/'+U.nick+'/'
                mkdir(bot_path)
                logging.info(u"Create folder '%s'" % bot_path)
            except OSError as e:
                logging.info('Bots config file already exists')
            else:
                # Create families symlink from pwikipedia dir
                families_symlink = settings.WIKI['path']+'/families'
                symlink(families_symlink, bot_path+'families')
                logging.info(u"Create file '%s'" % families_symlink)
                # Create families symlink from pwikipedia dir
                userinterfaces_symlink = settings.WIKI['path']+'/userinterfaces'
                symlink(userinterfaces_symlink, bot_path+'userinterfaces')
                logging.info(u"Create file '%s'" % userinterfaces_symlink)

            # Launch pywikipedia's login.py
            if U.active:
                L = LoginManager()
                logging.info(u"User is set as active, trying to login")
                # Find if password file has been configured
                if not config.password_file:
                    logging.warning("Password file has not been configured. \
                      If you want automatic login please set it in '%s/config.py'." % \
                      settings.WIKI['path'])
                else:
                    # Try to see if user is in passwd file
                    user_found = False
                    passwd_file = wikipedia.config.datafilepath(config.password_file)
                    try:
                        with open(passwd_file, 'r') as f:
                            # Search user by syntax: tuple of 2 or 4
                            for line in f.readlines():
                                if not line.strip(): continue
                                entry = eval(line)
                                if len(entry) == 2:
                                    if entry[0] == U.nick: user_found = True
                                elif len(entry) == 4:
                                    if entry[2] == U.nick and \
                                        entry[0] == U.language and \
                                        entry[1] == U.family:
                                            user_found = True
                        if not user_found:
                            # Purpose to create it
                            logging.info(u"User '%s' hasn't a passwd row in '%s'." % (U.nick, passwd_file))
                            if raw_input('Do you want to appent it ? [Y/n] ') != 'n':
                                with open(passwd_file, 'a') as f:
                                    password = getpass('Password > ')
                                    line = '\n'+str((U.language, U.family, U.nick, password))
                                    f.write(line)
                    except IOError as e:
                        # Except files not exists and purpose to create
                        logging.warning("File '%s' does not exist" % passwd_file)
                        if raw_input('Do you want to create it ? [Y/n] ') != 'n':
                            with open(passwd_file, 'w') as f:
                                password = getpass('Password > ')
                                line = str((U.language, U.family, U.nick, password))
                                f.write(line)

                    # Try to see if user exists in user-config
                    user_found = False
                    user_file = wikipedia.config.datafilepath('user-config.py')
                    REG_USER_LINE = re.compile("usernames\['(.*)'\]\['(.*)'\] = u?'(.*)'")
                    try:
                        with open(user_file, 'r') as f:
                            for line in f.readlines():
                                if REG_USER_LINE.match(line):
                                    family, lang, nick = REG_USER_LINE.sub(r'\1 \2 \3', line).split()
                                    if family == U.family and lang == U.language and nick == U.nick:
                                        user_found = True
                                        break

                    except IOError as e:
                        # If file doesn't exist create it.
                        logging.warning("File '%s' does not exist" % user_file)
                        with open(user_file, 'w') as f:
                            f.write("# -*- coding: utf-8  -*-")
                            logging.warning("Create file '%s'" % user_file)
                    finally:
                        if user_found:
                            logging.info(u"User '%s' has a row in '%s'." % (U.nick, user_file))
                        else:
                            logging.info(u"User '%s' hasn't a row in '%s'." % (U.nick, user_file))
                            # Ask for add line
                            if raw_input('Do you want to append user line ? [Y/n] ') != 'n':
                                with open(user_file, 'a') as f:
                                    user_line = "\nusernames['%s']['%s'] = u'%s'" % (U.family, U.language, U.nick)
                                    f.write(user_line)
                                    # Ask for add sysops line
                                    if raw_input('Is user sysops ? [N/y] ') == 'y':
                                        sys_line = "sysopnames['%s']['%s'] = u'%s'" % (U.family, U.language, U.nick)
                                        f.write(user_line)

                # Launch login script
                L.readPassword()
                is_logged = L.login()

        U.save()
        logging.info(u"Create user '%s' in Db" % U.nick)
示例#5
0
	wf = codecs.open('%s/converted.wiki' % tempdir, "r", "utf-8")
	wikilines = wf.readlines()
	wf.close()
	wikilines.append("\n[[Category:word2mediawiki]]\n")

	# Grab images out of the XML data
	parser = XMLParser(target=ImageGrabber())
	parser.feed(open('%s/converted.xml' % tempdir).read())
	parser.close()

	# Prepare for uploading to mediawiki
	# Make sure we are logged in
	with IOWrapper("Logging in..."):
		site = wikipedia.getSite(language, family)
		if not site.loggedInAs():
			lm = LoginManager(site=site, password=password)
			if lm.login(retry=True):
				site._isLoggedIn[0] = True
				site._userName[0] = lm.username
				site._userData[0] = False

	# Make sure all the filenames we want to use are empty, and if not, twiddle
	# the filename until they are.
	outnamecounter = 0
	while True:
		if outnamecounter > 0:
			outname = "%s_%s" % (baseoutname, outnamecounter)
		else:
			outname = baseoutname
		outnamecounter += 1
		if outnamecounter > 50:
示例#6
0
	wikilines.append("\n[[Category:word2mediawiki]]\n")

	# Grab images out of the XML data
	parser = XMLParser(target=ImageGrabber())

	# XXX: rewrite the namespace so as not to confuse certain XML parsers
	# (changes either in newer open/libreoffice or newer XML parsers require this)
	parser.feed(open('%s/converted.xml' % tempdir).read().replace("\"http://www.w3.org/XML/1998/namespace\"","\"http://www.w3.org/XML/1998/namespace_bugfix\""))
	parser.close()

	# Prepare for uploading to mediawiki
	# Make sure we are logged in
	with IOWrapper("Logging in..."):
		site = wikipedia.getSite(language, family)
		if not site.loggedInAs():
			lm = LoginManager(site=site, password=password)
			if lm.login(retry=True):
				site._isLoggedIn[0] = True
				site._userName[0] = lm.username
				site._userData[0] = False

	# Make sure all the filenames we want to use are empty, and if not, twiddle
	# the filename until they are.
	outnamecounter = 0
	while True:
		if outnamecounter > 0:
			outname = "%s_%s" % (baseoutname, outnamecounter)
		else:
			outname = baseoutname
		outnamecounter += 1
		if outnamecounter > 50: