Skip to content

Commit 1d01c41

Browse files
committed
Merge branch '3078-make-token-secretstr' into develop
Issue #3078 PR #3211
2 parents 6ad62ce + 6f44913 commit 1d01c41

File tree

7 files changed

+13
-14
lines changed

7 files changed

+13
-14
lines changed

monkey/common/types/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
from .networking import NetworkService, NetworkPort, PortStatus, SocketAddress, NetworkProtocol
66
from .plugin_types import PluginName
77
from .plugin_types import PluginVersion
8-
from .otp import OTP
8+
from .secrets import OTP, Token

monkey/common/types/otp.py monkey/common/types/secrets.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from pydantic import SecretStr
44

55
OTP: TypeAlias = SecretStr
6+
Token: TypeAlias = SecretStr

monkey/monkey_island/cc/services/authentication_service/authentication_facade.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from flask_security import UserDatastore
77

8-
from common.types import OTP
8+
from common.types import OTP, Token
99
from common.utils.code_utils import secure_generate_random_string
1010
from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic
1111
from monkey_island.cc.models import IslandMode
@@ -14,7 +14,6 @@
1414

1515
from . import AccountRole
1616
from .i_otp_repository import IOTPRepository
17-
from .types import Token
1817
from .user import User
1918

2019
OTP_EXPIRATION_TIME = 2 * 60 # 2 minutes
@@ -104,7 +103,7 @@ def refresh_user_token(self, user: User) -> Tuple[Token, int]:
104103
"""
105104
self.revoke_all_tokens_for_user(user)
106105

107-
return str(user.get_auth_token()), self._token_ttl_sec
106+
return Token(user.get_auth_token()), self._token_ttl_sec
108107

109108
def authorize_otp(self, otp: OTP) -> bool:
110109
# SECURITY: This method must not run concurrently, otherwise there could be TOCTOU errors,

monkey/monkey_island/cc/services/authentication_service/flask_resources/refresh_authentication_token.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def post(self):
3434
response = {
3535
"response": {
3636
"user": {
37-
ACCESS_TOKEN_KEY_NAME: new_token,
37+
ACCESS_TOKEN_KEY_NAME: new_token.get_secret_value(),
3838
TOKEN_TTL_KEY_NAME: token_ttl_sec,
3939
}
4040
}

monkey/monkey_island/cc/services/authentication_service/types.py

-3
This file was deleted.

monkey/tests/unit_tests/monkey_island/cc/services/authentication_service/flask_resources/test_refresh_authentication_token.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
import pytest
44

55
from common.common_consts.token_keys import ACCESS_TOKEN_KEY_NAME, TOKEN_TTL_KEY_NAME
6+
from common.types import Token
67
from monkey_island.cc.services.authentication_service.authentication_facade import (
78
AuthenticationFacade,
89
)
910
from monkey_island.cc.services.authentication_service.flask_resources.refresh_authentication_token import ( # noqa: E501
1011
RefreshAuthenticationToken,
1112
)
1213

13-
REQUEST_AUTHENTICATION_TOKEN = "my_authentication_token"
14-
15-
NEW_AUTHENTICATION_TOKEN = "new_authentication_token"
14+
NEW_AUTHENTICATION_TOKEN = Token("new_authentication_token")
1615
TOKEN_TTL_SEC = 123
1716

1817

@@ -37,7 +36,10 @@ def test_token__provides_refreshed_token(
3736
response = request_token()
3837

3938
assert response.status_code == HTTPStatus.OK
40-
assert response.json["response"]["user"][ACCESS_TOKEN_KEY_NAME] == NEW_AUTHENTICATION_TOKEN
39+
assert (
40+
response.json["response"]["user"][ACCESS_TOKEN_KEY_NAME]
41+
== NEW_AUTHENTICATION_TOKEN.get_secret_value()
42+
)
4143
assert response.json["response"]["user"][TOKEN_TTL_KEY_NAME] == TOKEN_TTL_SEC
4244

4345

monkey/tests/unit_tests/monkey_island/cc/services/authentication_service/test_authentication_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ def reset_uniquifier(user: User):
133133
mock_user_datastore.set_uniquifier.side_effect = reset_uniquifier
134134
user = User(username=USERNAME, password=PASSWORD, fs_uniquifier="a")
135135

136-
original_access_token = user.get_auth_token()
136+
original_access_token = str(user.get_auth_token())
137137
new_access_token, token_ttl_sec = authentication_facade.refresh_user_token(user)
138138

139139
mock_user_datastore.set_uniquifier.assert_called_once()
140140
assert mock_user_datastore.set_uniquifier.call_args[0][0].username == user.username
141-
assert new_access_token != original_access_token
141+
assert new_access_token.get_secret_value() != original_access_token
142142
assert token_ttl_sec == TOKEN_TTL_SEC
143143

144144

0 commit comments

Comments
 (0)