From 67dbc8ad19e6ee1cc7f919ea837dd497a7e15bf9 Mon Sep 17 00:00:00 2001 From: Marian Sigler Date: Fri, 21 Sep 2012 06:17:43 +0200 Subject: utils: use flask.current_app to avoid circular import --- utils.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'utils.py') diff --git a/utils.py b/utils.py index 889a65f..b749444 100644 --- a/utils.py +++ b/utils.py @@ -6,7 +6,7 @@ import re from Crypto.Cipher import AES from email.mime.text import MIMEText from functools import wraps -from flask import flash, g, redirect, render_template, request, session, url_for +from flask import current_app, flash, g, redirect, render_template, request, session, url_for from hashlib import sha1 from random import randint from werkzeug.exceptions import Forbidden @@ -68,13 +68,13 @@ def encrypt_password(password): Encrypt the given password with `config.PASSWORD_ENCRYPTION_KEY`. The key must be 32 bytes long. """ - assert len(app.config['PASSWORD_ENCRYPTION_KEY']) == 32 + assert len(current_app.config['PASSWORD_ENCRYPTION_KEY']) == 32 if isinstance(password, unicode): password = password.encode('utf8') iv = ''.join(chr(randint(0, 0xff)) for i in range(16)) - encryptor = AES.new(app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) + encryptor = AES.new(current_app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) return iv + encryptor.encrypt(pad(password)) def decrypt_password(ciphertext): @@ -82,7 +82,7 @@ def decrypt_password(ciphertext): Decrypt the given password with `config.PASSWORD_ENCRYPTION_KEY`. """ iv = ciphertext[:16] - encryptor = AES.new(app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) + encryptor = AES.new(current_app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) return encryptor.decrypt(ciphertext[16:]).rstrip('\0') @@ -93,7 +93,7 @@ def create_confirmation(realm, data): Expects as input a realm to distinguish data for several applications and some data (pickle-able). """ - key = '\0'.join((app.config['SECRET_KEY'], realm)) + key = '\0'.join((current_app.config['SECRET_KEY'], realm)) payload = pickle.dumps(data) mac = hmac.new(key, payload, sha1) return ''.join((mac.digest(), payload)).encode('base64').strip() @@ -106,7 +106,7 @@ def verify_confirmation(realm, token): Verify a confirmation created by `create_confirmation` and, if it is valid, return the original data. """ - key = '\0'.join((app.config['SECRET_KEY'], realm)) + key = '\0'.join((current_app.config['SECRET_KEY'], realm)) token = token.decode('base64') mac = token[:20] @@ -137,5 +137,3 @@ def send_mail(recipient, subject, body, sender=None): if p.wait() != 0: raise RuntimeError('sendmail terminated with %d' % p.returncode) -# circular import -from app import app -- cgit v1.2.3-1-g7c22