def facebook(): with dbop.db() as db: c = db.cursor() app_id = param.settings.get('fb_app_id', '') secret = param.settings.get('fb_secret', '') if app_id and secret: ## XXX cannot assume https:// redir = 'https://' + flask.request.headers['host'] \ + '/facebook?op=oauth_redirect' op = flask.request.args.get('op', '') if not op: fb_url = 'https://graph.facebook.com/oauth/authorize?display=touch&client_id=' + app_id + '&scope=publish_pages,manage_pages&redirect_uri=' + redir print >> param.log, 'FB_URL =', fb_url return flask.redirect(fb_url) elif op == 'oauth_redirect': code = flask.request.args.get('code', '') if code: r = requests.get( 'https://graph.facebook.com/oauth/access_token', params={ 'client_id': app_id, 'client_secret': secret, 'code': code, 'redirect_uri': redir }) print >> param.log, 'FACEBOOK TOKEN RESPONSE', r.text if r.text.startswith('{'): token = json.loads(r.text).get('access_token') else: token = r.text.split('access_token=', 1)[-1] dbop.setting(db, c, fb_token=token) return flask.redirect('/settings#facebook') else: return settings(status='You need to set the App ID first')
def settings(status=''): op = flask.request.form.get('op', '') or flask.request.args.get('op', '') with dbop.db() as db: c = db.cursor() if op == 'refresh': __main__.updater.event.set() status = 'Manual refresh of all feeds requested.' elif op == 'debug': if flask.request.form.get('debug', '') == 'Disable verbose logging': setattr(param, 'debug', False) else: setattr(param, 'debug', True) elif op == 'facebook': api_key = flask.request.form.get('api_key', '').strip() if api_key: dbop.setting(db, c, fb_api_key=api_key) app_id = flask.request.form.get('app_id', '').strip() if app_id: dbop.setting(db, c, fb_app_id=app_id) fb_secret = flask.request.form.get('fb_secret', '').strip() if fb_secret: dbop.setting(db, c, fb_secret=fb_secret) elif op == 'del_token': dbop.setting(db, c, fb_token='') elif op == 'maint': dbop.snr_mv(db, c) db.commit() stats = filters.stats(c) return flask.render_template('settings.html', filters=filters, executable=sys.argv[0], py_version=sys.version, param_debug=param.debug, param_settings=param.settings, started=__main__.started, uptime=datetime.datetime.now() - __main__.started, len=len, max=max, **locals())
def do_bootstrap(): dir = os.path.dirname(__file__ or os.getcwd()) dir = os.getcwd() + os.sep + 'tempip' if dir == '.' else dir print """Welcome to the Temboz initial setup wizard! """ ip, port = None, None while not ip or not port: bind = raw_input( """What IP address and TCP port should the server run on? Choose 127.0.0.1 to only allow connections from this machine (default) Choose 0.0.0.0 to allow connections from outside machines Enter an IP address and port [127.0.0.1:9999]: """) bind = bind.strip() if not bind: bind = '127.0.0.1:9999' try: # IPv6 addresses can have colons too ip, port_s = bind.rsplit(':', 1) except ValueError: print >> sys.stderr, 'Invalid bind specification', bind, print >> sys.stderr, '- it should be a of the form <IP>:<port>.' continue try: port = int(port_s) except ValueError: print >> sys.stderr, 'Invalid port number', port_s, print >> sys.stderr, '- it should be a number between 1 and 65535.' continue if port < 1 or port > 65535: print >> sys.stderr, 'Invalid port number:', port, print >> sys.stderr, '- it should be a number between 1 and 65535.' port = None try: s = socket.socket() s.bind((ip, port)) s.close() except socket.error as e: print >> sys.stderr, 'Cannot bind to', bind, '-', str(e) ip, port = None, None continue login = None while not login: login = raw_input('Choose a username: '******'_.')): print >> sys.stderr, 'Invalid username', login, print >> sys.stderr, '- it should only have alphanumeric characters,', print >> sys.stderr, 'underscore or dot' login = None continue # implement NIST SP 800-63-3 password guidelines: # https://pages.nist.gov/800-63-3/ # XXX TODO not yet implementing bad password dictionary/bloom filter check passwd = None while not passwd: passwd = getpass.getpass('Enter password: '******'The password must have at least 8 characters' passwd = None continue if passwd != getpass.getpass('Confirm password: '******'The passwords do not match' passwd = None continue hash = passlib.hash.argon2.using(rounds=64, memory_cost=65536, parallelism=1, digest_size=32).hash(passwd) os.system('sqlite3 rss.db < %s/ddl.sql' % dir) import dbop with dbop.db() as db: dbop.setting(db, 'login', login) dbop.setting(db, 'passwd', hash) dbop.setting(db, 'ip', ip) dbop.setting(db, 'port', str(port))