-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '3137-new-token-pair-endpoint' into develop
- Loading branch information
Showing
16 changed files
with
372 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
monkey/monkey_island/cc/services/authentication_service/flask_resources/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
from .register import Register | ||
from .registration_status import RegistrationStatus | ||
from .login import Login | ||
from .logout import Logout | ||
from .register_resources import register_resources | ||
from .agent_otp import AgentOTP | ||
from .agent_otp_login import AgentOTPLogin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...island/cc/services/authentication_service/flask_resources/refresh_authentication_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
from http import HTTPStatus | ||
|
||
from flask import make_response, request | ||
|
||
from monkey_island.cc.flask_utils import AbstractResource, responses | ||
from monkey_island.cc.services.authentication_service.token import TokenValidationError | ||
|
||
from ..authentication_facade import AuthenticationFacade | ||
from .utils import ACCESS_TOKEN_KEY_NAME, REFRESH_TOKEN_KEY_NAME | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RefreshAuthenticationToken(AbstractResource): | ||
""" | ||
A resource for refreshing tokens | ||
""" | ||
|
||
urls = ["/api/refresh-authentication-token"] | ||
|
||
def __init__(self, authentication_facade: AuthenticationFacade): | ||
self._authentication_facade = authentication_facade | ||
|
||
def post(self): | ||
""" | ||
Accepts a refresh token and returns a new token pair | ||
:return: Response with new token pair or an invalid request response | ||
""" | ||
try: | ||
old_refresh_token = request.json[REFRESH_TOKEN_KEY_NAME] | ||
access_token, refresh_token = self._authentication_facade.generate_new_token_pair( | ||
old_refresh_token | ||
) | ||
response = { | ||
"response": { | ||
"user": { | ||
ACCESS_TOKEN_KEY_NAME: access_token, | ||
REFRESH_TOKEN_KEY_NAME: refresh_token, | ||
} | ||
} | ||
} | ||
return response, HTTPStatus.OK | ||
except TokenValidationError: | ||
return make_response({}, HTTPStatus.UNAUTHORIZED) | ||
except Exception: | ||
return responses.make_response_to_invalid_request() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
monkey/monkey_island/cc/services/authentication_service/token/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .token_generator import TokenGenerator | ||
from .token_validator import TokenValidator | ||
from .token_parser import TokenParser, ParsedToken, TokenValidationError | ||
from .types import Token |
67 changes: 67 additions & 0 deletions
67
monkey/monkey_island/cc/services/authentication_service/token/token_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from flask_security import Security | ||
from itsdangerous import BadSignature, Serializer, SignatureExpired | ||
from pydantic import PrivateAttr | ||
|
||
from common.base_models import InfectionMonkeyBaseModel | ||
|
||
from .types import Token | ||
|
||
|
||
class TokenValidationError(Exception): | ||
"""Raise when an invalid token is encountered""" | ||
|
||
|
||
class InvalidTokenSignatureError(TokenValidationError): | ||
"""Raise when a token's signature is invalid""" | ||
|
||
|
||
class ExpiredTokenError(TokenValidationError): | ||
"""Raise when a token has expired""" | ||
|
||
|
||
class ParsedToken(InfectionMonkeyBaseModel): | ||
raw_token: Token | ||
user_uniquifier: str | ||
expiration_time: int | ||
_token_serializer: Serializer = PrivateAttr() | ||
|
||
def __init__(self, token_serializer: Serializer, *, raw_token: Token, expiration_time: int): | ||
self._token_serializer = token_serializer | ||
|
||
user_uniquifier = self._token_serializer.loads(raw_token, max_age=expiration_time) | ||
super().__init__( | ||
raw_token=raw_token, user_uniquifier=user_uniquifier, expiration_time=expiration_time | ||
) | ||
|
||
def is_expired(self) -> bool: | ||
try: | ||
self._token_serializer.loads(self.raw_token, max_age=self.expiration_time) | ||
return False | ||
except SignatureExpired: | ||
return True | ||
|
||
|
||
class TokenParser: | ||
def __init__(self, security: Security, token_expiration: int): | ||
self._token_serializer = security.remember_token_serializer | ||
self._token_expiration = token_expiration # in seconds | ||
|
||
def parse(self, token: Token) -> ParsedToken: | ||
""" | ||
Parses a token and returns a data structure with its components | ||
:param token: The token to parse | ||
:return: The parsed token | ||
:raises TokenValidationError: If the token could not be parsed | ||
""" | ||
try: | ||
return ParsedToken( | ||
token_serializer=self._token_serializer, | ||
raw_token=token, | ||
expiration_time=self._token_expiration, | ||
) | ||
except SignatureExpired: | ||
# NOTE: SignatureExpired is a subclass of BadSignature; this clause must come first. | ||
raise ExpiredTokenError("Token has expired") | ||
except BadSignature: | ||
raise InvalidTokenSignatureError("Invalid token signature") |
18 changes: 0 additions & 18 deletions
18
monkey/monkey_island/cc/services/authentication_service/token/token_validator.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.