reuse of the env variable in the base settings

This commit is contained in:
wcolmenares 2019-05-13 03:44:09 -04:00
parent 5e2e906f48
commit 69ec7d2b46
3 changed files with 17 additions and 23 deletions

View file

@ -1,9 +1,8 @@
import logging import logging
import os
import pyotp import pyotp
import requests import requests
import dotenv
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.conf import settings
from datacenterlight.tasks import create_vm_task from datacenterlight.tasks import create_vm_task
from hosting.models import HostingOrder, HostingBill, OrderDetail from hosting.models import HostingOrder, HostingBill, OrderDetail
@ -15,18 +14,6 @@ from .models import VMPricing, VMTemplate
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
PROJECT_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__)),
)
# load .env file
dotenv.read_dotenv("{0}/.env".format(PROJECT_DIR))
def env(env_name):
return os.environ.get(env_name)
def get_cms_integration(name): def get_cms_integration(name):
current_site = Site.objects.get_current() current_site = Site.objects.get_current()
try: try:
@ -119,17 +106,17 @@ def clear_all_session_vars(request):
def check_otp(name, realm, token): def check_otp(name, realm, token):
data = { data = {
"auth_name": env('AUTH_NAME'), "auth_name": settings.AUTH_NAME,
"auth_token": pyotp.TOTP(env('AUTH_SEED')).now(), "auth_token": pyotp.TOTP(settings.AUTH_SEED).now(),
"auth_realm": env('AUTH_REALM'), "auth_realm": settings.AUTH_REALM,
"name": name, "name": name,
"realm": realm, "realm": realm,
"token": token "token": token
} }
response = requests.post( response = requests.post(
"https://{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format( "https://{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=env('OTP_SERVER'), OTP_SERVER=settings.OTP_SERVER,
OTP_VERIFY_ENDPOINT=env('OTP_VERIFY_ENDPOINT') OTP_VERIFY_ENDPOINT=settings.OTP_VERIFY_ENDPOINT
), ),
data=data data=data
) )

View file

@ -721,6 +721,14 @@ X_FRAME_OPTIONS = ('SAMEORIGIN' if X_FRAME_OPTIONS_ALLOW_FROM_URI is None else
DEBUG = bool_env('DEBUG') DEBUG = bool_env('DEBUG')
ACCOUNT_NAME = env('ACCOUNT_NAME')
AUTH_NAME = env('AUTH_NAME')
AUTH_SEED = env('AUTH_SEED')
AUTH_REALM = env('AUTH_REALM')
OTP_SERVER = env('OTP_SERVER')
OTP_VERIFY_ENDPOINT = env('OTP_VERIFY_ENDPOINT')
if DEBUG: if DEBUG:
from .local import * # flake8: noqa from .local import * # flake8: noqa
else: else:

View file

@ -1,7 +1,5 @@
import logging import logging
import uuid import uuid
import os
import dotenv
from datetime import datetime from datetime import datetime
from time import sleep from time import sleep
@ -39,7 +37,7 @@ from stored_messages.settings import stored_messages_settings
from datacenterlight.cms_models import DCLCalculatorPluginModel from datacenterlight.cms_models import DCLCalculatorPluginModel
from datacenterlight.models import VMTemplate, VMPricing from datacenterlight.models import VMTemplate, VMPricing
from datacenterlight.utils import create_vm, get_cms_integration, check_otp, env from datacenterlight.utils import create_vm, get_cms_integration, check_otp
from hosting.models import UserCardDetail from hosting.models import UserCardDetail
from membership.models import CustomUser, StripeCustomer from membership.models import CustomUser, StripeCustomer
from opennebula_api.models import OpenNebulaManager from opennebula_api.models import OpenNebulaManager
@ -1775,7 +1773,8 @@ class CheckUserVM(APIView):
user = request.data['user'] user = request.data['user']
realm = request.data['realm'] realm = request.data['realm']
token = request.data['token'] token = request.data['token']
if user != env('ACCOUNT_NAME'): print(settings.ACCOUNT_NAME)
if user != settings.ACCOUNT_NAME:
return Response("User not allowed", 403) return Response("User not allowed", 403)
response = check_otp(user, realm, token) response = check_otp(user, realm, token)
if response != 200: if response != 200: