mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-12 14:00:17 +02:00
* Hooks registration, discovery and retrieval module Will discover @hooks.register decorated functions inside the auth_hooks module in any installed django app. * Class to register modular service apps * Register service modules URLs * Example service module * Refactor services into modules Each service type has been split out into its own django app/module. A hook mechanism is provided to register a subclass of the ServiceHook class. The modules then overload functions defined in ServiceHook as required to provide interoperability with alliance auth. Service modules provide their own urls and views for user registration and account management and a partial template to display on the services page. Where possible, new modules should provide their own models for local data storage. * Added menu items hooks and template tags * Added menu item hook for broadcasts * Added str method to ServicesHook * Added exception handling to hook iterators * Refactor mumble migration and table name Upgrading will require `migrate mumble --fake-initial` to be run first and then `migrate mumble` to rename the table. * Refactor teamspeak3 migration and rename table Upgrading will require `migrate teamspeak3 --fake-initial` * Added module models and migrations for refactoring AuthServicesInfo * Migrate AuthServiceInfo fields to service modules models * Added helper for getting a users main character * Added new style celery instance * Changed Discord from AuthServicesInfo to DiscordUser model * Switch celery tasks to staticmethods * Changed Discourse from AuthServicesInfo to DiscourseUser model * Changed IPBoard from AuthServicesInfo to IpboardUser model * Changed Ips4 from AuthServicesInfo to Ips4User model Also added disable service task. This service still needs some love though. Was always missing a deactivate services hook (before refactoring) for reasons I'm unsure of so I'm reluctant to add it without knowing why. * Changed Market from AuthServicesInfo to MarketUser model * Changed Mumble from AuthServicesInfo to MumbleUser model Switched user foreign key to one to one relationship. Removed implicit password change on user exists. Combined regular and blue user creation. * Changed Openfire from AuthServicesInfo to OpenfireUser model * Changed SMF from AuthServicesInfo to SmfUser model Added disable task * Changed Phpbb3 from AuthServicesInfo to Phpbb3User model * Changed XenForo from AuthServicesInfo to XenforoUser model * Changed Teamspeak3 from AuthServicesInfo to Teamspeak3User model * Remove obsolete manager functions * Standardise URL format This will break some callback URLs Discord changes from /discord_callback/ to /discord/callback/ * Removed unnecessary imports * Mirror upstream decorator change * Setup for unit testing * Unit tests for discord service * Added add main character helper * Added Discourse unit tests * Added Ipboard unit tests * Added Ips4 unit tests * Fix naming of market manager, switch to use class methods * Remove unused hook functions * Added market service unit tests * Added corp ticker to add main character helper * Added mumble unit tests * Fix url name and remove namespace * Fix missing return and add missing URL * Added openfire unit tests * Added missing return * Added phpbb3 unit tests * Fix SmfManager naming inconsistency and switch to classmethods * Added smf unit tests * Remove unused functions, Added missing return * Added xenforo unit tests * Added missing return * Fixed reference to old model * Fixed error preventing groups from syncing on reset request * Added teamspeak3 unit tests * Added nose as test runner and some test settings * Added package requirements for running tests * Added unit tests for services signals and tasks * Remove unused tests file * Fix teamspeak3 service signals * Added unit tests for teamspeak3 signals Changed other unit tests setUp to inert signals * Fix password gen and hashing python3 compatibility Fixes #630 Adds unit tests to check the password functions run on both platforms. * Fix unit test to not rely on checking url params * Add Travis CI settings file * Remove default blank values from services models * Added dynamic user model admin actions for syncing service groups * Remove unused search fields * Add hook function for syncing nicknames * Added discord hook for sync nickname * Added user admin model menu actions for sync nickname hook * Remove obsolete code * Rename celery config app to avoid package name clash * Added new style celerybeat schedule configuration periodic_task decorator is depreciated * Added string representations * Added admin pages for services user models * Removed legacy code * Move link discord button to correct template * Remove blank default fields from example model * Disallow empty django setting * Fix typos * Added coverage configuration file * Add coverage and coveralls to travis config Should probably use nose's built in coverage, but this works for now. * Replace AuthServicesInfo get_or_create instances with get Reflects upstream changes to AuthServicesInfo behaviour. * Update mumble user table name * Split out mumble authenticator requirements zeroc-ice seems to cause long build times on travis-ci and isn't required for the core projects functionality or testing.
150 lines
6.5 KiB
Python
150 lines
6.5 KiB
Python
from __future__ import unicode_literals
|
|
import logging
|
|
import random
|
|
import string
|
|
import re
|
|
|
|
from django.db import connections
|
|
from passlib.hash import bcrypt
|
|
|
|
# requires yum install libffi-devel and pip install bcrypt
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class MarketManager:
|
|
def __init__(self):
|
|
pass
|
|
|
|
SQL_ADD_USER = r"INSERT INTO fos_user (username, username_canonical, email, email_canonical, enabled, salt," \
|
|
r"password, locked, expired, roles, credentials_expired, characterid, characterName)" \
|
|
r"VALUES (%s, %s, %s, %s, 1,%s, %s, 0, 0, 'a:0:{}', 0, %s, %s) "
|
|
SQL_GET_USER_ID = r"SELECT id FROM fos_user WHERE username = %s"
|
|
SQL_DISABLE_USER = r"UPDATE fos_user SET enabled = '0' WHERE username = %s"
|
|
SQL_ENABLE_USER = r"UPDATE fos_user SET enabled = '1' WHERE username = %s"
|
|
SQL_UPDATE_PASSWORD = r"UPDATE fos_user SET password = %s, salt = %s WHERE username = %s"
|
|
SQL_CHECK_EMAIL = r"SELECT email FROM fos_user WHERE email = %s"
|
|
SQL_CHECK_USERNAME = r"SELECT username FROM fos_user WHERE username = %s"
|
|
SQL_UPDATE_USER = r"UPDATE fos_user SET password = %s, salt = %s, enabled = '1' WHERE username = %s"
|
|
|
|
@staticmethod
|
|
def __santatize_username(username):
|
|
sanatized = username.replace(" ", "_")
|
|
return sanatized.lower()
|
|
|
|
@staticmethod
|
|
def __generate_random_pass():
|
|
return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)])
|
|
|
|
@staticmethod
|
|
def _gen_pwhash(password):
|
|
return bcrypt.encrypt(password.encode('utf-8'), rounds=13)
|
|
|
|
@staticmethod
|
|
def _get_salt(pw_hash):
|
|
search = re.compile(r"^\$2[a-z]?\$([0-9]+)\$(.{22})(.{31})$")
|
|
match = re.match(search, pw_hash)
|
|
return match.group(2)
|
|
|
|
@classmethod
|
|
def check_username(cls, username):
|
|
logger.debug("Checking alliance market username %s" % username)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_CHECK_USERNAME, [cls.__santatize_username(username)])
|
|
row = cursor.fetchone()
|
|
if row:
|
|
logger.debug("Found user %s on alliance market" % username)
|
|
return True
|
|
logger.debug("User %s not found on alliance market" % username)
|
|
return False
|
|
|
|
@classmethod
|
|
def check_user_email(cls, username, email):
|
|
logger.debug("Checking if alliance market email exists for user %s" % username)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_CHECK_EMAIL, [email])
|
|
row = cursor.fetchone()
|
|
if row:
|
|
logger.debug("Found user %s email address on alliance market" % username)
|
|
return True
|
|
logger.debug("User %s email address not found on alliance market" % username)
|
|
return False
|
|
|
|
@classmethod
|
|
def add_user(cls, username, email, characterid, charactername):
|
|
logger.debug("Adding new market user %s" % username)
|
|
plain_password = cls.__generate_random_pass()
|
|
hash = cls._gen_pwhash(plain_password)
|
|
salt = cls._get_salt(hash)
|
|
username_clean = cls.__santatize_username(username)
|
|
if not cls.check_username(username):
|
|
if not cls.check_user_email(username, email):
|
|
try:
|
|
logger.debug("Adding user %s to alliance market" % username)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_ADD_USER, [username_clean, username_clean, email, email, salt,
|
|
hash, characterid, charactername])
|
|
return username_clean, plain_password
|
|
except:
|
|
logger.debug("Unsuccessful attempt to add market user %s" % username)
|
|
return "", ""
|
|
else:
|
|
logger.debug("Alliance market email %s already exists Updating instead" % email)
|
|
username_clean, password = cls.update_user_info(username)
|
|
return username_clean, password
|
|
else:
|
|
logger.debug("Alliance market username %s already exists Updating instead" % username)
|
|
username_clean, password = cls.update_user_info(username)
|
|
return username_clean, password
|
|
|
|
@classmethod
|
|
def disable_user(cls, username):
|
|
logger.debug("Disabling alliance market user %s " % username)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_DISABLE_USER, [username])
|
|
return True
|
|
|
|
@classmethod
|
|
def update_custom_password(cls, username, plain_password):
|
|
logger.debug("Updating alliance market user %s password" % username)
|
|
if cls.check_username(username):
|
|
username_clean = cls.__santatize_username(username)
|
|
hash = cls._gen_pwhash(plain_password)
|
|
salt = cls._get_salt(hash)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_UPDATE_PASSWORD, [hash, salt, username_clean])
|
|
return plain_password
|
|
else:
|
|
logger.error("Unable to update alliance market user %s password" % username)
|
|
return ""
|
|
|
|
@classmethod
|
|
def update_user_password(cls, username):
|
|
logger.debug("Updating alliance market user %s password" % username)
|
|
if cls.check_username(username):
|
|
username_clean = cls.__santatize_username(username)
|
|
plain_password = cls.__generate_random_pass()
|
|
hash = cls._gen_pwhash(plain_password)
|
|
salt = cls._get_salt(hash)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_UPDATE_PASSWORD, [hash, salt, username_clean])
|
|
return plain_password
|
|
else:
|
|
logger.error("Unable to update alliance market user %s password" % username)
|
|
return ""
|
|
|
|
@classmethod
|
|
def update_user_info(cls, username):
|
|
logger.debug("Updating alliance market user %s" % username)
|
|
try:
|
|
username_clean = cls.__santatize_username(username)
|
|
plain_password = cls.__generate_random_pass()
|
|
hash = cls._gen_pwhash(plain_password)
|
|
salt = cls._get_salt(hash)
|
|
cursor = connections['market'].cursor()
|
|
cursor.execute(cls.SQL_UPDATE_USER, [hash, salt, username_clean])
|
|
return username_clean, plain_password
|
|
except:
|
|
logger.debug("Alliance market update user failed for %s" % username)
|
|
return "", ""
|