def run(*args): #cork (app auth) client = pymongo.MongoClient(app_settings.DATABASE_URI) db = client[app_settings.DATABASE_CORK] db.authenticate(app_settings.DATABASE_USERNAME, app_settings.DATABASE_PASSWORD, app_settings.DATABASE_MAIN) existing = db.users.find_one({"login": "******"}) if existing: return mb = MongoDBBackend(db_name=app_settings.DATABASE_CORK, username=app_settings.DATABASE_USERNAME, password=app_settings.DATABASE_PASSWORD, authdb=app_settings.DATABASE_MAIN, initialize=True) cork = Cork(backend=mb, preferred_hashing_algorithm='scrypt') admin_hash = cork._hash("admin", app_settings.ADMIN_PASSWORD) mb.users._coll.insert({ "login": "******", "email_addr": "*****@*****.**", "desc": "admin user", "role": "admin", "hash": admin_hash, "creation_date": "2012-10-28 20:50:26.286723" }) mb.roles._coll.insert({'role': 'admin', 'val': 100}) #mb.roles._coll.insert({'role': 'editor', 'val': 60}) mb.roles._coll.insert({'role': 'user', 'val': 50})
def setup_mongo_db(request): # FIXME no last_login? mb = MongoDBBackend(db_name='cork-functional-test', initialize=True) # Purge DB mb.users._coll.drop() mb.roles._coll.drop() mb.pending_registrations._coll.drop() # Create admin mb.users._coll.insert({ "login": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723" }) # Create users mb.roles._coll.insert({'role': 'special', 'val': 200}) mb.roles._coll.insert({'role': 'admin', 'val': 100}) mb.roles._coll.insert({'role': 'editor', 'val': 60}) mb.roles._coll.insert({'role': 'user', 'val': 50}) def fin(): mb.users._coll.drop() mb.roles._coll.drop() request.addfinalizer(fin) return mb
def create_role(*args): new_role_name = args[0] new_role_level = args[1] mb = MongoDBBackend(db_name=app_settings.DATABASE_CORK, username=app_settings.DATABASE_USERNAME, password=app_settings.DATABASE_PASSWORD, authdb=app_settings.DATABASE_MAIN, initialize=False) mb.roles._coll.insert({'role': new_role_name, 'val': new_role_level})
def make_user_admin(*args): username = input("Username: "******"login": username}, {"$set": { "role": "admin" }})
def populate_mongodb_backend(): # admin/admin mb = MongoDBBackend(db_name='finvis-auth', initialize=True) mb.users._coll.insert({ "login": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723" }) mb.roles._coll.insert({'role': 'admin', 'val': 100}) mb.roles._coll.insert({'role': 'user', 'val': 50}) return mb
def get_cork_instance(): smtp_url = 'ssl://{}:{}@smtp.gmail.com:465'.format( app_settings.SMTP_USERNAME, app_settings.SMTP_PASSWORD) cork = Cork( backend=MongoDBBackend(db_name=app_settings.DATABASE_CORK, username=app_settings.DATABASE_USERNAME, password=app_settings.DATABASE_PASSWORD, authdb=app_settings.DATABASE_MAIN, initialize=False), email_sender=app_settings.SMTP_USERNAME + "@gmail.com", smtp_url=smtp_url, preferred_hashing_algorithm='scrypt', pbkdf2_iterations=10, ) return cork
def initiate_authmgr(self): ''' Internal function to intiate authentication manager ''' self.authBackend = MongoDBBackend(db_name=CONF.mongo_conf['db'], hostname=CONF.mongo_conf['host'], port=CONF.mongo_conf['port'], initialize=False, username=CONF.mongo_conf['username'], password=CONF.mongo_conf['password']) smtp_url = CONF.smtp_url self.authmgr = Cork(backend=self.authBackend, email_sender='*****@*****.**', smtp_url=smtp_url) return self.authmgr
def setup_mongo_db(request): # FIXME no last_login? t0 = time.time() def timer(s, max_time=None): delta = time.time() - t0 print("%s %f" % (s, delta)) if max_time is not None: assert delta < max_time mb = MongoDBBackend(db_name="cork-functional-test", initialize=True) timer("connect + init") # Purge DB mb.users._coll.drop() mb.roles._coll.drop() mb.pending_registrations._coll.drop() timer("purge") # Create admin mb.users._coll.insert( { "login": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723", } ) timer("create") # Create users mb.roles._coll.insert({"role": "special", "val": 200}) mb.roles._coll.insert({"role": "admin", "val": 100}) mb.roles._coll.insert({"role": "editor", "val": 60}) mb.roles._coll.insert({"role": "user", "val": 50}) timer("create users") def fin(): mb.users._coll.drop() mb.roles._coll.drop() request.addfinalizer(fin) timer("mongo setup", 8) return mb
def create_test_databases(*args): create_databases_with_auth(app_settings.DATABASE_CORK) create_databases_with_auth(app_settings.DATABASE_MAIN) mb = MongoDBBackend(db_name=app_settings.DATABASE_CORK, username=app_settings.DATABASE_USERNAME, password=app_settings.DATABASE_PASSWORD, authdb=app_settings.DATABASE_MAIN, initialize=True) cork = Cork(backend=mb, preferred_hashing_algorithm='scrypt') testuser_hash = cork._hash("testuser", "testuser") mb.users._coll.insert({ "login": "******", "email_addr": "*****@*****.**", "desc": "test user", "role": "user", "hash": testuser_hash, "creation_date": "2012-10-28 20:50:26.286723" })
except ImportError: import os.path if os.path.exists("settings.py.example"): print("You probably haven't made a real settings.py file.\n" "Open settings.py.example and go from there.") import auth # Various bits of the app from mongo import * import downloader import crud import bson rootdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..') aaa = Cork(backend=MongoDBBackend(db_name='finvis-auth'), email_sender=settings.email_sender, smtp_url=settings.smtp_url) app = bottle.app() session_opts = { 'session.type': 'cookie', 'session.validate_key': True, 'session.cookie_expires': True, 'session.timeout': 3600 * 24, # 1 day 'session.encrypt_key': settings.session_encrypt_key } app = SessionMiddleware(app, session_opts) @bottle.route('/index.html/:entity_id')
mb = MongoDBBackend(db_name='users', initialize=True) mb.users._coll.insert({ "login": "******", "email_addr": "*****@*****.**", "desc": "admin test user", "role": "admin", "hash": "cLzRnzbEwehP6ZzTREh3A4MXJyNo+TV8Hs4//EEbPbiDoo+dmNg22f2RJC282aSwgyWv/O6s3h42qrA6iHx8yfw=", "creation_date": "2012-10-28 20:50:26.286723" }) mb.roles._coll.insert({'role': 'admin', 'val': 100}) mb.roles._coll.insert({'role': 'editor', 'val': 60}) mb.roles._coll.insert({'role': 'user', 'val': 50}) return mb mb = populate_mongodb_backend()''' mb = MongoDBBackend(db_name='users', initialize=True) aaa = Cork(backend=mb, email_sender='*****@*****.**', smtp_url='ssl://[email protected]:[email protected]:465') # alias the authorization decorator with defaults authorize = aaa.make_auth_decorator(fail_redirect="/login", role="user") app = bottle.app() session_opts = { 'session.cookie_expires': True, 'session.encrypt_key': 'Qwwwew123141*u', 'session.httponly': True, 'session.timeout': 3600 * 24, # 1 day 'session.type': 'cookie', 'session.validate_key': True, }
# authSetup.py import os import sys from cork import Cork from cork.backends import MongoDBBackend curpath = os.path.dirname(os.path.abspath(__file__)) app_api_path = os.path.abspath( os.path.join(curpath, "../api/")) sys.path.append( app_api_path ) import appconfig as CONF mb = MongoDBBackend (db_name=CONF.mongo_conf['db'], hostname=CONF.mongo_conf['host'], port=CONF.mongo_conf['port'], initialize=True, username=CONF.mongo_conf['username'], password=CONF.mongo_conf['password']) smtp_url= CONF.smtp_url aaa = Cork(backend= mb, email_sender='*****@*****.**', smtp_url=smtp_url) ################################################################################ def create_roles(): ''' Create basic application roles ''' print("Creating roles") mb.roles._coll.insert({'role': 'admin', 'val': 100}) mb.roles._coll.insert({'role': 'user', 'val': 50})
def purge_test_db(): # Purge DB mb = MongoDBBackend(db_name='cork-functional-test', initialize=True) mb.users._coll.drop() mb.roles._coll.drop() mb.pending_registrations._coll.drop()
def test_initialize_storage(): mb = MongoDBBackend(db_name='cork-functional-test', initialize=True) Cork(backend=mb)