Skip to content

Commit

Permalink
fix secure
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaopeuko committed Apr 27, 2024
1 parent 55ba0a3 commit dcd5869
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
![PyPI - Downloads](https://img.shields.io/pypi/dm/secured)
![PyPI](https://img.shields.io/pypi/v/secured)
![PyPI - Wheel](https://img.shields.io/pypi/wheel/secured)
![PyPI - License](https://img.shields.io/pypi/l/secured)

# Secured

- [Secured](#secured)
Expand Down
1 change: 1 addition & 0 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
ad['password'] = 'my_secret'
print((ad.password))
print((ad['password']))
print(ad.password == "my_secret")
56 changes: 17 additions & 39 deletions secured/secure.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
from typing import Union

class Secure(str):
"""
A class for securing sensitive data.
This class is designed to add a thin layer of protection by obscuring sensitive data, such as database URLs or API keys,
in order to prevent accidental exposure in logs or debug output. The representation of the secured data can be customized
with a specific message.
Attributes:
message: Custom message to represent the secured data when printed or logged.

Example:
>>> DATABASE_URL = "your_actual_database_url"
>>> sensitive_data = Secure(DATABASE_URL, "<Data Hidden>")
>>> print(sensitive_data)
'<Data Hidden>'
"""

def __new__(cls, original: str, message: str = "<Sensitive data secured>"): # type: ignore
class Secure(str):
def __new__(cls, original: str, message: str = "<Sensitive data secured>"):
"""
Create a new Secure instance that appears as a custom message.
Expand All @@ -28,21 +13,14 @@ def __new__(cls, original: str, message: str = "<Sensitive data secured>"): # ty
Returns:
Secure: A new Secure instance displaying the placeholder message.
"""
# Initialize the Secure instance with the message instead of the original content.
return super(Secure, cls).__new__(cls, message)
instance = super(Secure, cls).__new__(cls, original)
instance._original = original
instance._message = message
return instance

def __init__(self, original: str, message: str = "<Sensitive data secured>"):
"""
Initializes a Secure object. The initialization logic is handled by __new__; __init__ does not
need to handle the data directly.
Args:
original: The original data to secure.
message: A custom message to use for representing the secured data.
"""
super().__init__()
self.original = original
self.message = message
# Initialization handled in __new__, nothing required here
pass

def __repr__(self) -> str:
"""
Expand All @@ -51,7 +29,7 @@ def __repr__(self) -> str:
Returns:
str: The custom message representing the secured data.
"""
return self.message
return self._message

def __str__(self) -> str:
"""
Expand All @@ -60,28 +38,28 @@ def __str__(self) -> str:
Returns:
str: The custom message representing the secured data.
"""
return self.__repr__()
return self._message

def to_int(self) -> int | str:
def to_int(self) -> Union[int, str]:
"""
Try converting the original secured data to an integer.
Returns:
Union[int, str]: The integer value of the original data, or the custom message if conversion fails.
"""
try:
return int(self.original)
return int(self._original)
except ValueError:
return self.message
return self._message

def to_float(self) -> float | str:
def to_float(self) -> Union[float, str]:
"""
Try converting the original secured data to a float.
Returns:
Union[float, str]: The float value of the original data, or the custom message if conversion fails.
"""
try:
return float(self.original)
return float(self._original)
except ValueError:
return self.message
return self._message
41 changes: 41 additions & 0 deletions tests/test_secure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from secured.secure import Secure

def test_secure_initialization():
"""Test the initialization and representation of Secure objects."""
secure = Secure("sensitive_data", "<Data Hidden>")
assert str(secure) == "<Data Hidden>"
assert repr(secure) == "<Data Hidden>"

def test_secure_default_message():
"""Test the default message used when no custom message is provided."""
secure = Secure("sensitive_data")
assert str(secure) == "<Sensitive data secured>"

def test_to_int_success():
"""Test converting secured data to an integer successfully."""
secure = Secure("123")
assert secure.to_int() == 123

def test_to_int_failure():
"""Test failing to convert secured data to an integer."""
secure = Secure("sensitive_data")
assert secure.to_int() == "<Sensitive data secured>"

def test_to_float_success():
"""Test converting secured data to a float successfully."""
secure = Secure("123.45")
assert secure.to_float() == 123.45

def test_to_float_failure():
"""Test failing to convert secured data to a float."""
secure = Secure("sensitive_data")
assert secure.to_float() == "<Sensitive data secured>"


def test_key_success():
"""Test failing to convert secured data to a float."""
secure = Secure("sensitive_data")
assert secure == "sensitive_data"
secure = Secure("12345")
assert secure == "12345"
assert secure._original == "12345"

0 comments on commit dcd5869

Please sign in to comment.