def setUp(self): self.config = testing.setUp() self.settings = { 'sqlalchemy.url': 'sqlite://', 'em.subject': 'Authentication Request', 'em.sender': '*****@*****.**', 'em.body': """ You have requested authentication. Your temporary access code is: {code}""", 'auth_tkt.secret': 'secret', 'auth_tkt.cookie_name': 'pnutbtr', 'supported_auth_schemes': [ 'Google Auth', 'Email' ] } self.app = Authenticator(test_application, {}, **self.settings) from webtest import TestApp self.testapp = TestApp(self.app) self.mailer = FakeMailer() self.testapp.app.pyramid.registry['mailer'] = self.mailer from sqlalchemy import create_engine engine = create_engine('sqlite://') from factored.models import Base DBSession.configure(bind=engine) self.session = DBSession() Base.metadata.create_all(engine)
def listuserinfo(): arguments = listuserparser.parse_args() if not arguments.config or not arguments.username: listuserparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() users = session.query(User).filter_by( username=arguments.username).all() if len(users) > 0: user = users[0] print 'username:%s, secret: %s' % ( user.username, user.secret) print 'bar code url:', get_barcode_image(user.username, user.secret, settings['appname']) else: print '"%s" user not found' % arguments.username
def listuserinfo(): arguments = listuserparser.parse_args() if not arguments.config or not arguments.username: listuserparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() users = session.query(User).filter_by( username=arguments.username).all() if len(users) > 0: user = users[0] print('username:%s, secret: %s' % (user.username, user.secret)) print( 'bar code url:', get_barcode_image(user.username, user.secret, settings['appname'])) else: print('"%s" user not found' % arguments.username)
def create_user(username, session=None): from factored.models import DBSession, User if session is None: session = DBSession() secret = generate_random_google_code() user = User(username=username, secret=secret) session.add(user) return user
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.create_all(engine)
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.create_all(engine)
def factory(settings, app): engine = engine_from_config(settings, 'sqlalchemy.') configure_db = settings.pop('configure_db', 'true').lower() == 'true' if configure_db: DBSession.configure(bind=engine) db_session_id = 'f' else: # why would you do this? # well... if you have multiple factored wsgi instances running # this allows you to change between the connection objects db_session_id = settings.pop('db_session_id') return db_session_id, DB()
def listusers(): arguments = removeparser.parse_args() if not arguments.config: removeparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) with transaction.manager: for user in DBSession.query(User).all(): print user.username
def __init__(self, *args, **settings): if len(args) == 1: # regular app app = None else: app = args[0] self.initialize_settings(app, settings) # db configuration engine = engine_from_config(settings, 'sqlalchemy.') configure_db = settings.pop('configure_db', 'true').lower() == 'true' if configure_db: DBSession.configure(bind=engine) self.db_session_id = 'f' else: self.db_session_id = settings.pop('db_session_id') self.setup_autouserfinder(settings) # start pyramid application configuration config = Configurator(settings=settings, request_factory=Request) self.setup_plugins(config, settings) from factored.views import auth_chooser, notfound config.add_route('auth', self.base_auth_url) config.add_view(auth_chooser, route_name='auth', renderer='templates/layout.pt') # setup template customization registration if TEMPLATE_CUSTOMIZATIONS not in config.registry: config.registry[TEMPLATE_CUSTOMIZATIONS] = {} # static paths for resources self.static_path = os.path.join(self.base_auth_url, 'authstatic') config.add_static_view(name=self.static_path, path='factored:static') config.add_notfound_view(notfound, append_slash=True) # add some things to registry config.registry['mailer'] = Mailer.from_settings(settings) config.registry['settings'] = self.__dict__ config.registry['formtext'] = nested_settings( get_settings(settings, 'formtext.')) config.registry['app'] = self config.scan() self.pyramid = config.make_wsgi_app()
def listusers(): arguments = removeparser.parse_args() if not arguments.config: removeparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() for user in session.query(User).all(): print user.username
def add(): arguments = addparser.parse_args() if not arguments.config or not arguments.username: addparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) with transaction.manager: username = arguments.username user = create_user(username) print 'barcode url:', get_barcode_image(username, user.secret, settings['appname']) print 'secret:', user.secret
def listusers(): arguments = removeparser.parse_args() if not arguments.config: removeparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() for user in session.query(User).all(): print(user.username)
def remove(): arguments = removeparser.parse_args() if not arguments.config or not arguments.username: removeparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) with transaction.manager: user = DBSession.query(User).filter_by( username=arguments.username).all() if len(user) > 0: DBSession.delete(user[0]) else: print '"%s" user not found' % arguments.username
def get_user(self, username): user = DBSession.query(User).filter_by(username=username).first() if user is None: if 'userfinder' in self.req.registry['settings']: finder = self.req.registry['settings']['userfinder'] if finder(username): return create_user(username) return user
def remove(): arguments = removeparser.parse_args() if not arguments.config or not arguments.username: removeparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() user = session.query(User).filter_by( username=arguments.username).all() if len(user) > 0: session.delete(user[0]) else: print '"%s" user not found' % arguments.username session.commit() session.close()
def setUp(self): self.config = testing.setUp() self.settings = { 'sqlalchemy.url': 'sqlite://', 'em.subject': 'Authentication Request', 'em.sender': '*****@*****.**', 'em.body': """ You have requested authentication. Your temporary access code is: {code}""", 'auth_tkt.secret': 'secret', 'auth_tkt.cookie_name': 'pnutbtr', 'supported_auth_schemes': ['Google Auth', 'Email'] } self.app = Authenticator(test_application, {}, **self.settings) from webtest import TestApp self.testapp = TestApp(self.app) self.mailer = FakeMailer() self.testapp.app.pyramid.registry['mailer'] = self.mailer from sqlalchemy import create_engine engine = create_engine('sqlite://') from factored.models import Base DBSession.configure(bind=engine) self.session = DBSession() Base.metadata.create_all(engine)
def add(): arguments = addparser.parse_args() if not arguments.config or not arguments.username: addparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() username = arguments.username user = create_user(username, session) print('barcode url:', get_barcode_image(username, user.secret, settings['appname'])) print('secret:', user.secret) session.commit() session.close()
def add(): arguments = addparser.parse_args() if not arguments.config or not arguments.username: addparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() username = arguments.username user = create_user(username, session) print 'barcode url:', get_barcode_image(username, user.secret, settings['appname']) print 'secret:', user.secret session.commit() session.close()
def create_user(username): from factored.models import DBSession, User secret = generate_random_google_code() user = User(username=username, secret=secret) DBSession.add(user) return user
def remove(): arguments = removeparser.parse_args() if not arguments.config or not arguments.username: removeparser.print_usage() else: config_uri = arguments.config setup_logging(config_uri) try: settings = get_appsettings(config_uri, 'factored') except LookupError: settings = get_appsettings(config_uri, 'main') engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) session = DBSession() user = session.query(User).filter_by(username=arguments.username).all() if len(user) > 0: session.delete(user[0]) else: print('"%s" user not found' % arguments.username) session.commit() session.close()