Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_post(req, resp):
login_info = uri.parse_query_string(req.context['body'])
user = login_info.get('username')
password = login_info.get('password')
if user is None or password is None:
raise HTTPBadRequest('Invalid login attempt', 'Missing user/password')
if not auth_manager.authenticate(user, password):
raise HTTPUnauthorized('Authentication failure', 'bad login credentials', '')
connection = db.connect()
cursor = connection.cursor(db.DictCursor)
data = get_user_data(None, {'name': user}, dbinfo=(connection, cursor))
if not data:
cursor.close()
connection.close()
raise HTTPNotFound()
session = req.env['beaker.session']
session['user'] = user
session.save()
csrf_token = '%x' % SystemRandom().getrandbits(128)
try:
cursor.execute('INSERT INTO `session` (`id`, `csrf_token`) VALUES (%s, %s)',
(req.env['beaker.session']['_id'], csrf_token))
except db.IntegrityError:
raise HTTPBadRequest('Invalid login attempt', 'User already logged in')
connection.commit()
else:
ldap_contacts[key] = ldap_attrs.get(val)
else:
ldap_contacts[key] = val
connection.simple_bind_s(auth_user, password)
except ldap.INVALID_CREDENTIALS:
return False
except (ldap.SERVER_DOWN, ldap.INVALID_DN_SYNTAX) as err:
logger.warn("%s", err)
return None
if self.import_user:
connection = db.connect()
cursor = connection.cursor(db.DictCursor)
if user_exists(username, cursor):
logger.info("user %s already exists, updating from ldap", username)
update_user(username, ldap_contacts, cursor)
else:
logger.info("user %s does not exists. importing.", username)
import_user(username, ldap_contacts, cursor)
connection.commit()
cursor.close()
connection.close()
return True
def main():
config = utils.read_config(sys.argv[1])
db.init(config['db'])
cycle_time = config.get('scheduler_cycle_time', 3600)
schedulers = {}
while 1:
connection = db.connect()
db_cursor = connection.cursor(db.DictCursor)
start = time.time()
# Load all schedulers
db_cursor.execute('SELECT name FROM scheduler')
schedulers = {}
for row in db_cursor:
try:
scheduler_name = row['name']
if scheduler_name not in schedulers:
schedulers[scheduler_name] = load_scheduler(scheduler_name)
except (ImportError, AttributeError):
logger.exception('Failed to load scheduler %s, skipping', row['name'])
# Iterate through all teams
db_cursor.execute('SELECT id, name, scheduling_timezone FROM team WHERE active = TRUE')
teams = db_cursor.fetchall()
def poll():
query = '''SELECT `user`.`name` AS `user`, `contact_mode`.`name` AS `mode`, `notification_queue`.`send_time`,
`user`.`time_zone`,`notification_type`.`subject`, `notification_queue`.`context`,
`notification_type`.`body`, `notification_queue`.`id`
FROM `notification_queue` JOIN `user` ON `notification_queue`.`user_id` = `user`.`id`
JOIN `contact_mode` ON `notification_queue`.`mode_id` = `contact_mode`.`id`
JOIN `notification_type` ON `notification_queue`.`type_id` = `notification_type`.`id`
WHERE `notification_queue`.`active` = 1 AND `notification_queue`.`send_time` <= UNIX_TIMESTAMP()'''
logger.info('[-] start send task...')
connection = db.connect()
cursor = connection.cursor(db.DictCursor)
cursor.execute(query)
for row in cursor:
send_queue.put(row)
cursor.close()
connection.close()