diff --git a/alliance_auth/settings.py.example b/alliance_auth/settings.py.example index d89ca0ba..977cd9c0 100755 --- a/alliance_auth/settings.py.example +++ b/alliance_auth/settings.py.example @@ -107,6 +107,7 @@ DATABASES = { 'HOST': os.environ.get('AA_DB_IPS4_HOST', '127.0.0.1'), 'PORT': os.environ.get('AA_DB_IPS4_PORT', '3306'), }, + 'smf': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'alliance_smf', @@ -114,6 +115,15 @@ DATABASES = { 'PASSWORD': os.environ.get('AA_DB_SMF_PASSWORD', 'password'), 'HOST': os.environ.get('AA_DB_SMF_HOST', '127.0.0.1'), 'PORT': os.environ.get('AA_DB_SMF_PORT', '3306'), + }, + + 'market': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'alliance_market', + 'USER': os.environ.get('AA_DB_MARKET_USER', 'allianceserver'), + 'PASSWORD': os.environ.get('AA_DB_MARKET_PASSWORD', 'password'), + 'HOST': os.environ.get('AA_DB_MARKET_HOST', '127.0.0.1'), + 'PORT': os.environ.get('AA_DB_MARKET_PORT', '3306'), } } @@ -246,6 +256,7 @@ BLUE_ALLIANCE_GROUPS = 'True' == os.environ.get('AA_BLUE_ALLIANCE_GROUPS', 'Fals # ENABLE_AUTH_DISCORD - Enable Discord support in the auth for auth'd members # ENABLE_AUTH_IPS4 - Enable IPS4 support in the auth for auth'd members # ENABLE_AUTH_SMF - Enable SMF forum support in the auth for auth'd members +# ENABLE_AUTH_MARKET = Enable Alliance Market support in auth for auth'd members ######################### ENABLE_AUTH_FORUM = 'True' == os.environ.get('AA_ENABLE_AUTH_FORUM', 'False') ENABLE_AUTH_JABBER = 'True' == os.environ.get('AA_ENABLE_AUTH_JABBER', 'False') @@ -255,6 +266,7 @@ ENABLE_AUTH_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_AUTH_TEAMSPEAK3', ' ENABLE_AUTH_DISCORD = 'True' == os.environ.get('AA_ENABLE_AUTH_DISCORD', 'False') ENABLE_AUTH_IPS4 = 'True' == os.environ.get('AA_ENABLE_AUTH_IPS4', 'False') ENABLE_AUTH_SMF = 'True' == os.environ.get('AA_ENABLE_AUTH_SMF', 'False') +ENABLE_AUTH_MARKET = 'True' == os.environ.get('AA_ENABLE_AUTH_MARKET', 'False') ##################### # Blue service Setup @@ -267,6 +279,7 @@ ENABLE_AUTH_SMF = 'True' == os.environ.get('AA_ENABLE_AUTH_SMF', 'False') # ENABLE_BLUE_DISCORD - Enable Discord support in the auth for blues # ENABLE_BLUE_IPS4 - Enable IPS4 forum support in the auth for blues # ENABLE_BLUE_SMF - Enable SMF forum support in the auth for blues +# ENABLE_BLUE_MARKET - Enable Alliance Market in the auth for blues ##################### BLUE_STANDING = float(os.environ.get('AA_BLUE_STANDING', '5.0')) ENABLE_BLUE_FORUM = 'True' == os.environ.get('AA_ENABLE_BLUE_FORUM', 'False') @@ -277,6 +290,7 @@ ENABLE_BLUE_TEAMSPEAK3 = 'True' == os.environ.get('AA_ENABLE_BLUE_TEAMSPEAK3', ' ENABLE_BLUE_DISCORD = 'True' == os.environ.get('AA_ENABLE_BLUE_DISCORD', 'False') ENABLE_BLUE_IPS4 = 'True' == os.environ.get('AA_ENABLE_BLUE_IPS4', 'False') ENABLE_BLUE_SMF = 'True' == os.environ.get('AA_ENABLE_BLUE_SMF', 'False') +ENABLE_BLUE_MARKET = 'True' == os.environ.get('AA_ENABLE_BLUE_MARKET', 'False') ######################### # Corp Configuration @@ -314,6 +328,11 @@ MEMBER_API_ACCOUNT = 'True' == os.environ.get('AA_MEMBER_API_ACCOUNT', 'True') BLUE_API_MASK = os.environ.get('AA_BLUE_API_MASK', 8388608) BLUE_API_ACCOUNT = 'True' == os.environ.get('AA_BLUE_API_ACCOUNT', 'False') +##################### +# Alliance Market +##################### +MARKET_URL = os.environ.get('AA_MARKET_URL', 'http://yourdomain.com/market') + ##################### # HR Configuration ##################### diff --git a/alliance_auth/urls.py b/alliance_auth/urls.py index ad22c0e0..6016057c 100755 --- a/alliance_auth/urls.py +++ b/alliance_auth/urls.py @@ -153,6 +153,13 @@ urlpatterns = patterns('', name='auth_reset_smf_password'), url(r'^set_smf_password/$', 'services.views.set_smf_password', name='auth_set_smf_password'), + # Alliance Market Control + url(r'^activate_market/$', 'services.views.activate_market', name='auth_activate_market'), + url(r'^deactivate_market/$', 'services.views.deactivate_market', name='auth_deactivate_market'), + url(r'^reset_market_password/$', 'services.views.reset_market_password', + name='auth_reset_market_password'), + url(r'^set_market_password/$', 'services.views.set_market_password', name='auth_set_market_password'), + # Tools url(r'^tool/fleet_formatter_tool/$', 'services.views.fleet_formatter_view', name='auth_fleet_format_tool_view'), diff --git a/authentication/managers.py b/authentication/managers.py index 91332899..5ce01acf 100755 --- a/authentication/managers.py +++ b/authentication/managers.py @@ -146,3 +146,15 @@ class AuthServicesInfoManager: logger.info("Updated user %s smf info in authservicesinfo model." % user) else: logger.error("Failed to update user %s smf info: user does not exist." % user) + + @staticmethod + def update_user_market_info(username, password, user): + if User.objects.filter(username=user.username).exists(): + logger.debug("Updating user %s market info: username %s" % (user, username)) + authserviceinfo = AuthServicesInfoManager.__get_or_create(user) + authserviceinfo.market_username = username + authserviceinfo.market_password = password + authserviceinfo.save(update_fields=['market_username', 'market_password']) + logger.info("Updated user %s market info in authservicesinfo model." % user) + else: + logger.error("Failed to update user %s market info: user does not exist." % user) diff --git a/authentication/models.py b/authentication/models.py index ce6c0172..5a7df68b 100755 --- a/authentication/models.py +++ b/authentication/models.py @@ -19,6 +19,8 @@ class AuthServicesInfo(models.Model): ips4_id = models.CharField(max_length=254, blank=True, default="") smf_username = models.CharField(max_length=254, blank=True, default="") smf_password = models.CharField(max_length=254, blank=True, default="") + market_username = models.CharField(max_length=254, blank=True, default="") + market_password = models.CharField(max_length=254, blank=True, default="") main_char_id = models.CharField(max_length=64, blank=True, default="") is_blue = models.BooleanField(default=False) user = models.ForeignKey(User) diff --git a/services/managers/market_manager.py b/services/managers/market_manager.py new file mode 100644 index 00000000..e2d64338 --- /dev/null +++ b/services/managers/market_manager.py @@ -0,0 +1,145 @@ +import logging +from django.conf import settings +import requests +import os + +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: + + SQL_ADD_USER = r"INSERT INTO fos_user (username, username_canonical, email, email_canonical, enabled, salt, password," \ + r"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(" ", "_") + sanatized = sanatized.replace("'", "") + return sanatized.lower() + + @staticmethod + def __generate_random_pass(): + return os.urandom(8).encode('hex') + + @staticmethod + def check_username(username): + logger.debug("Checking alliance market username %s" % username) + cursor = connections['market'].cursor() + cursor.execute(marketManager.SQL_CHECK_USERNAME, [marketManager.__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 + + @staticmethod + def check_user_email(username, email): + logger.debug("Checking if alliance market email exists for user %s" % username) + cursor = connections['market'].cursor() + cursor.execute(marketManager.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 + + + @staticmethod + def add_user(username, email, characterid, charactername): + logger.debug("Adding new market user %s" % username) + plain_password = marketManager.__generate_random_pass() + hash = bcrypt.encrypt(plain_password, rounds=13) + hash_result = hash + rounds_striped = hash_result.strip('$2a$13$') + salt = rounds_striped[:22] + username_clean = marketManager.__santatize_username(username) + if marketManager.check_username(username)== False: + if marketManager.check_user_email(username, email) == False: + try: + logger.debug("Adding user %s to alliance market" % username) + cursor = connections['market'].cursor() + cursor.execute(marketManager.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 = marketManager.update_user_info(username) + return username_clean, password + else: + logger.debug("Alliance market username %s already exists Updating instead" % username) + username_clean, password = marketManager.update_user_info(username) + return username_clean, password + + @staticmethod + def disable_user(username): + logger.debug("Disabling alliance market user %s " % username) + cursor = connections['market'].cursor() + cursor.execute(marketManager.SQL_DISABLE_USER, [username]) + return True + + @staticmethod + def update_custom_password(username, plain_password): + logger.debug("Updating alliance market user %s password" % username) + if marketManager.check_username(username): + username_clean = marketManager.__santatize_username(username) + hash = bcrypt.encrypt(plain_password, rounds=13) + hash_result = hash + rounds_striped = hash_result.strip('$2a$13$') + salt = rounds_striped[:22] + cursor = connections['market'].cursor() + cursor.execute(marketManager.SQL_UPDATE_PASSWORD, [hash, salt, username_clean]) + return plain_password + else: + logger.error("Unable to update alliance market user %s password" % username) + return "" + + @staticmethod + def update_user_password(username): + logger.debug("Updating alliance market user %s password" % username) + if marketManager.check_username(username): + username_clean = marketManager.__santatize_username(username) + plain_password = marketManager.__generate_random_pass() + hash = bcrypt.encrypt(plain_password, rounds=13) + hash_result = hash + rounds_striped = hash_result.strip('$2a$13$') + salt = rounds_striped[:22] + cursor = connections['market'].cursor() + cursor.execute(marketManager.SQL_UPDATE_PASSWORD, [hash, salt, username_clean]) + return plain_password + else: + logger.error("Unable to update alliance market user %s password" % username) + return "" + + @staticmethod + def update_user_info(username): + logger.debug("Updating alliance market user %s" % username) + try: + username_clean = marketManager.__santatize_username(username) + plain_password = marketManager.__generate_random_pass() + hash = bcrypt.encrypt(plain_password, rounds=13) + hash_result = hash + rounds_striped = hash_result.strip('$2a$13$') + salt = rounds_striped[:22] + cursor = connections['market'].cursor() + cursor.execute(marketManager.SQL_UPDATE_USER, [hash, salt, username_clean]) + return username_clean, plain_password + except: + logger.debug("Alliance market update user failed for %s" % username) + return "", "" diff --git a/services/views.py b/services/views.py index e5ba42f0..15f3697a 100755 --- a/services/views.py +++ b/services/views.py @@ -16,6 +16,7 @@ from managers.teamspeak3_manager import Teamspeak3Manager from managers.discord_manager import DiscordManager from managers.ips4_manager import Ips4Manager from managers.smf_manager import smfManager +from managers.market_manager import marketManager from authentication.managers import AuthServicesInfoManager from eveonline.managers import EveManager from celerytask.tasks import update_jabber_groups @@ -763,5 +764,84 @@ def set_smf_password(request): form = ServicePasswordForm() logger.debug("Rendering form for user %s" % request.user) - context = {'form': form, 'service': 'Forum'} + context = {'form': form, 'service': 'SMF'} return render_to_response('registered/service_password.html', context, context_instance=RequestContext(request)) + +@login_required +@user_passes_test(service_blue_alliance_test) +def activate_market(request): + logger.debug("activate_market called by user %s" % request.user) + authinfo = AuthServicesInfoManager.get_auth_service_info(request.user) + # Valid now we get the main characters + character = EveManager.get_character_by_id(authinfo.main_char_id) + logger.debug("Adding market user for user %s with main character %s" % (request.user, character)) + result = marketManager.add_user(character.character_name, request.user.email, authinfo.main_char_id, character.character_name) + # if empty we failed + if result[0] != "": + AuthServicesInfoManager.update_user_market_info(result[0], result[1], request.user) + logger.debug("Updated authserviceinfo for user %s with market credentials." % request.user) + logger.info("Succesfully activated market for user %s" % request.user) + return HttpResponseRedirect("/services/") + logger.error("Unsuccesful attempt to activate market for user %s" % request.user) + return HttpResponseRedirect("/dashboard") + + +@login_required +@user_passes_test(service_blue_alliance_test) +def deactivate_market(request): + logger.debug("deactivate_market called by user %s" % request.user) + authinfo = AuthServicesInfoManager.get_auth_service_info(request.user) + result = marketManager.disable_user(authinfo.market_username) + # false we failed + if result: + AuthServicesInfoManager.update_user_market_info("", "", request.user) + logger.info("Succesfully deactivated market for user %s" % request.user) + return HttpResponseRedirect("/services/") + logger.error("Unsuccesful attempt to activate market for user %s" % request.user) + return HttpResponseRedirect("/dashboard") + + +@login_required +@user_passes_test(service_blue_alliance_test) +def reset_market_password(request): + logger.debug("reset_market_password called by user %s" % request.user) + authinfo = AuthServicesInfoManager.get_auth_service_info(request.user) + result = marketManager.update_user_password(authinfo.market_username) + # false we failed + if result != "": + AuthServicesInfoManager.update_user_market_info(authinfo.market_username, result, request.user) + logger.info("Succesfully reset market password for user %s" % request.user) + return HttpResponseRedirect("/services/") + logger.error("Unsuccessful attempt to reset market password for user %s" % request.user) + return HttpResponseRedirect("/dashboard") + +@login_required +@user_passes_test(service_blue_alliance_test) +def set_market_password(request): + logger.debug("set_market_password called by user %s" % request.user) + error = None + if request.method == 'POST': + logger.debug("Received POST request with form.") + form = ServicePasswordForm(request.POST) + logger.debug("Form is valid: %s" % form.is_valid()) + if form.is_valid(): + password = form.cleaned_data['password'] + logger.debug("Form contains password of length %s" % len(password)) + authinfo = AuthServicesInfoManager.get_auth_service_info(request.user) + result = marketManager.update_custom_password(authinfo.market_username, password) + if result != "": + AuthServicesInfoManager.update_user_market_info(authinfo.market_username, result, request.user) + logger.info("Succesfully reset market password for user %s" % request.user) + return HttpResponseRedirect("/services/") + else: + logger.error("Failed to install custom market password for user %s" % request.user) + error = "Failed to install custom password." + else: + error = "Invalid password provided" + else: + logger.debug("Request is not type POST - providing empty form.") + form = ServicePasswordForm() + + logger.debug("Rendering form for user %s" % request.user) + context = {'form': form, 'service': 'Market'} + return render_to_response('registered/service_password.html', context, context_instance=RequestContext(request)) \ No newline at end of file diff --git a/stock/templates/registered/services.html b/stock/templates/registered/services.html index 1e5bb23c..1ff6b002 100755 --- a/stock/templates/registered/services.html +++ b/stock/templates/registered/services.html @@ -99,6 +99,33 @@ {% endifequal %} {% endif %} + {% if ENABLE_BLUE_MARKET %} + Alliance Market + {{ authinfo.market_username }} + {{ authinfo.market_password }} + {{ MARKET_URL }} + + {% ifequal authinfo.market_username "" %} + + + + {% else %} + + + + + + + + + + {% endifequal %} + + {% endif %} {% if ENABLE_BLUE_JABBER %} Jabber @@ -343,6 +370,33 @@ {% endifequal %} {% endif %} + {% if ENABLE_AUTH_MARKET %} + Alliance Market + {{ authinfo.market_username }} + {{ authinfo.market_password }} + {{ MARKET_URL }} + + {% ifequal authinfo.market_username "" %} + + + + {% else %} + + + + + + + + + + {% endifequal %} + + {% endif %} {% if ENABLE_AUTH_JABBER %} Jabber diff --git a/util/context_processors.py b/util/context_processors.py index 02572ddc..c9a74e19 100755 --- a/util/context_processors.py +++ b/util/context_processors.py @@ -39,6 +39,7 @@ def domain_url(request): 'ENABLE_AUTH_DISCORD': settings.ENABLE_AUTH_DISCORD, 'ENABLE_AUTH_IPS4': settings.ENABLE_AUTH_IPS4, 'ENABLE_AUTH_SMF': settings.ENABLE_AUTH_SMF, + 'ENABLE_AUTH_MARKET': settings.ENABLE_AUTH_MARKET, 'ENABLE_BLUE_JABBER': settings.ENABLE_BLUE_JABBER, 'ENABLE_BLUE_FORUM': settings.ENABLE_BLUE_FORUM, 'ENABLE_BLUE_MUMBLE': settings.ENABLE_BLUE_MUMBLE, @@ -47,11 +48,13 @@ def domain_url(request): 'ENABLE_BLUE_DISCORD': settings.ENABLE_BLUE_DISCORD, 'ENABLE_BLUE_IPS4': settings.ENABLE_BLUE_IPS4, 'ENABLE_BLUE_SMF': settings.ENABLE_BLUE_SMF, + 'ENABLE_BLUE_MARKET': settings.ENABLE_BLUE_MARKET, 'TEAMSPEAK3_PUBLIC_URL': settings.TEAMSPEAK3_PUBLIC_URL, 'JACK_KNIFE_URL': settings.JACK_KNIFE_URL, 'DISCORD_SERVER_ID': settings.DISCORD_SERVER_ID, 'KILLBOARD_URL': settings.KILLBOARD_URL, 'IPS4_URL': settings.IPS4_URL, 'SMF_URL': settings.SMF_URL, + 'MARKET_URL': settings.MARKET_URL, 'EXTERNAL_MEDIA_URL': settings.EXTERNAL_MEDIA_URL, 'CURRENT_UTC_TIME': timezone.now()}