def init_app(): app = Flask(__name__, instance_relative_config=True, template_folder='site/templates', static_folder='site/static') app.config.from_object(app_config[get_config().get_flask_config()]) app.config['SQLALCHEMY_DATABASE_URI'] = get_config().get_db_config() app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = get_config().get_secret_key() login_manager.init_app(app) login_manager.login_message = "You must be logged in to access this page." login_manager.login_view = "auth.login" CORS(app, headers=['Content-Type'], expose_headers=['Access-Control-Allow-Origin'], supports_credentials=True) DATABASE.init(app) api = Api(version='1.0', title='api') api_user = Api(version='1.0', title='api_user') api_admin = Api(version='1.0', title='api_admin') register_routes(app, api, api_user, api_admin) register_custom_errors(app) login_manager.user_loader(auth_utils.load_user) return app
def confirm_token(token, expiration=3600): serializer = URLSafeTimedSerializer(get_config().get_secret_key()) try: email = serializer.loads( token, salt=get_config().get_security_password_salt(), max_age=expiration) except: return False return email
def test_login_activated_user(client, user_dict, expected_status_codes): response = client.post('/user', json=user_dict) assert response.status_code == expected_status_codes[0] user_id = response.json['id'] response = client.post('/auth/login', json={ 'email': user_dict['email'], 'password': user_dict['password'] }) assert response.status_code == expected_status_codes[1] assert response.json[ 'errorKey'] == Error.AUTH_LOGIN_USER_NOT_ACTIVATED.name config = get_config() response = client.post('/auth/login', json={ 'email': config.get_root_email(), 'password': config.get_root_password() }) assert response.status_code == expected_status_codes[2] response = client.put('/api-admin/user/' + str(user_id), json={'isActive': True}) assert response.status_code == expected_status_codes[3] response = client.get('/auth/logout') assert response.status_code == expected_status_codes[4] response = client.post('/auth/login', json={ 'email': user_dict['email'], 'password': user_dict['password'] }) assert response.status_code == expected_status_codes[5]
def send_email(to, subject, template): sender_email_address = get_config().get_email_sender_address() message = MIMEMultipart("alternative") message["Subject"] = subject message["From"] = sender_email_address message["To"] = to content = MIMEText(template, "html") message.attach(content) context = ssl.create_default_context() with smtplib.SMTP_SSL(get_config().get_email_sender_host(), int(get_config().get_email_sender_port()), context=context) as server: server.login(sender_email_address, get_config().get_email_sender_password()) server.sendmail(sender_email_address, to, message.as_string())
def generate_confirmation_token(email): serializer = URLSafeTimedSerializer(get_config().get_secret_key()) return serializer.dumps(email, salt=get_config().get_security_password_salt())
def to_dict(self): rv = dict() rv['errorKey'] = self.error_enum.name rv['errorMessage'] = self.error_enum.value[get_config().get_language() or ''].format() return rv
import logging from app import init_app, create_root from tools.config_properties import init_config, get_config from tools.logger_tag import LoggerTag def create_logger(): logger = logging.getLogger('tag') logger.level = logging.DEBUG handler = logging.StreamHandler() handler.setLevel(logger.level) handler.setFormatter( logging.Formatter( '%(asctime)s - %(tag)s - %(levelname)s - %(message)s')) logger.addHandler(handler) if __name__ == '__main__': create_logger() LoggerTag('app').info('Run application') init_config() application = init_app() config = get_config() create_root(config.get_root_email(), config.get_root_password()) application.run(host='0.0.0.0', port=8000)