mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Use encryption module from utils
Now that the encryption module has been moved from this app to utils, we can remove it from here (along with its tests) and import it from utils instead. This also renames the `encryption.py` file to `hashing.py`, since it no longer contains the encryption class.
This commit is contained in:
@@ -11,6 +11,7 @@ from time import monotonic
|
||||
from notifications_utils.clients.zendesk.zendesk_client import ZendeskClient
|
||||
from notifications_utils.clients.statsd.statsd_client import StatsdClient
|
||||
from notifications_utils.clients.redis.redis_client import RedisClient
|
||||
from notifications_utils.clients.encryption.encryption_client import Encryption
|
||||
from notifications_utils import logging, request_helper
|
||||
from werkzeug.exceptions import HTTPException as WerkzeugHTTPException
|
||||
from werkzeug.local import LocalProxy
|
||||
@@ -22,7 +23,6 @@ from app.clients.email.aws_ses import AwsSesClient
|
||||
from app.clients.sms.firetext import FiretextClient
|
||||
from app.clients.sms.mmg import MMGClient
|
||||
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
|
||||
from app.encryption import Encryption
|
||||
|
||||
DATETIME_FORMAT_NO_TIMEZONE = "%Y-%m-%d %H:%M:%S.%f"
|
||||
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
from flask_bcrypt import generate_password_hash, check_password_hash
|
||||
|
||||
from itsdangerous import URLSafeSerializer
|
||||
|
||||
|
||||
class Encryption:
|
||||
def init_app(self, app):
|
||||
self.serializer = URLSafeSerializer(app.config.get('SECRET_KEY'))
|
||||
self.salt = app.config.get('DANGEROUS_SALT')
|
||||
|
||||
def encrypt(self, thing_to_encrypt):
|
||||
return self.serializer.dumps(thing_to_encrypt, salt=self.salt)
|
||||
|
||||
def decrypt(self, thing_to_decrypt):
|
||||
return self.serializer.loads(thing_to_decrypt, salt=self.salt)
|
||||
|
||||
|
||||
def hashpw(password):
|
||||
return generate_password_hash(password.encode('UTF-8'), 10).decode('utf-8')
|
||||
|
||||
|
||||
def check_hash(password, hashed_password):
|
||||
# If salt is invalid throws a 500 should add try/catch here
|
||||
return check_password_hash(hashed_password, password)
|
||||
10
app/hashing.py
Normal file
10
app/hashing.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from flask_bcrypt import generate_password_hash, check_password_hash
|
||||
|
||||
|
||||
def hashpw(password):
|
||||
return generate_password_hash(password.encode('UTF-8'), 10).decode('utf-8')
|
||||
|
||||
|
||||
def check_hash(password, hashed_password):
|
||||
# If salt is invalid throws a 500 should add try/catch here
|
||||
return check_password_hash(hashed_password, password)
|
||||
@@ -29,7 +29,7 @@ from notifications_utils.template import (
|
||||
)
|
||||
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
|
||||
|
||||
from app.encryption import (
|
||||
from app.hashing import (
|
||||
hashpw,
|
||||
check_hash
|
||||
)
|
||||
|
||||
@@ -11,7 +11,7 @@ from datetime import datetime
|
||||
|
||||
from alembic import op
|
||||
|
||||
from app.encryption import hashpw
|
||||
from app.hashing import hashpw
|
||||
import uuid
|
||||
revision = '0025_notify_service_data'
|
||||
down_revision = '0024_add_research_mode_defaults'
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
from app.encryption import Encryption
|
||||
|
||||
encryption = Encryption()
|
||||
|
||||
|
||||
def test_should_encrypt_content(notify_api):
|
||||
encryption.init_app(notify_api)
|
||||
assert encryption.encrypt("this") != "this"
|
||||
|
||||
|
||||
def test_should_decrypt_content(notify_api):
|
||||
encryption.init_app(notify_api)
|
||||
encrypted = encryption.encrypt("this")
|
||||
assert encryption.decrypt(encrypted) == "this"
|
||||
|
||||
|
||||
def test_should_encrypt_json(notify_api):
|
||||
encryption.init_app(notify_api)
|
||||
encrypted = encryption.encrypt({"this": "that"})
|
||||
assert encryption.decrypt(encrypted) == {"this": "that"}
|
||||
Reference in New Issue
Block a user