|
| 1 | +import json |
| 2 | +from src.authentication.authentication_exception import AuthenticationException |
| 3 | +from src.authentication.jwt_authenticator import JWTAuthenticator |
| 4 | +from src.configuration import config |
| 5 | + |
| 6 | + |
| 7 | +class ClientAuthenticationRequest: |
| 8 | + """ |
| 9 | + Class that represents a single authentication request. |
| 10 | + """ |
| 11 | + |
| 12 | + GRANT_TYPE_PASSWORD = "password" |
| 13 | + GRANT_TYPE_REFRESH_TOKEN = "refresh_token" |
| 14 | + GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials" |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + grant_type=None, |
| 19 | + client_id=None, |
| 20 | + client_secret=None, |
| 21 | + username=None, |
| 22 | + password=None, |
| 23 | + refresh_token=None, |
| 24 | + scope=None): |
| 25 | + """ |
| 26 | + Constructor of the instance. |
| 27 | +
|
| 28 | + :param grant_type: Type of authentication used during the procedure. |
| 29 | + :type grant_type: str |
| 30 | + :param client_id: Identifier of the authenticating client. |
| 31 | + :type client_id: str |
| 32 | + :param client_secret: Secret phrase used when authenticating with grant type "client-secret". |
| 33 | + :type client_secret: str |
| 34 | + :param username: Username used when authenticating with grant type "password". |
| 35 | + :type username: str |
| 36 | + :param password: Password used when authenticating with grant type "password". |
| 37 | + :type password: str |
| 38 | + :param refresh_token: Refresh token used when authenticating with grant type "refresh_token". |
| 39 | + :type refresh_token: str |
| 40 | + :param scope: Scope used by the client, e.g. Aruba. |
| 41 | + :type scope: str |
| 42 | + """ |
| 43 | + self.grant_type = grant_type |
| 44 | + self.client_id = client_id |
| 45 | + self.client_secret = client_secret |
| 46 | + self.username = username |
| 47 | + self.password = password |
| 48 | + self.refresh_token = refresh_token |
| 49 | + self.scope = scope |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def parse_from_json(json_string: str): |
| 53 | + """ |
| 54 | + Function that parses an instance from a JSON string. |
| 55 | +
|
| 56 | + :param json_string: JSON string the instance should be parsed from. |
| 57 | + :type json_string: str |
| 58 | +
|
| 59 | + :return: Returns the created instance. |
| 60 | + """ |
| 61 | + loads = json.loads(json_string) |
| 62 | + return ClientAuthenticationRequest(**loads) |
| 63 | + |
| 64 | + def validate(self): |
| 65 | + """ |
| 66 | + Method that validates the parsed authentication data. |
| 67 | + Depending on the grant type, the authentication requires additional field: |
| 68 | + password: requires the fields username and password. |
| 69 | + refresh_token: requires the field refresh_token. |
| 70 | + client_credentials: requires the field client_secret. |
| 71 | +
|
| 72 | + The remaining fields are optional: client_id, scope. |
| 73 | + """ |
| 74 | + if not self.grant_type: |
| 75 | + raise AuthenticationException("Parameter 'grant_type' missing") |
| 76 | + |
| 77 | + if self.grant_type == self.GRANT_TYPE_PASSWORD: |
| 78 | + if not self.username or not self.password: |
| 79 | + raise AuthenticationException("Parameter 'username' or 'password' missing") |
| 80 | + elif self.grant_type == self.GRANT_TYPE_REFRESH_TOKEN: |
| 81 | + if not self.refresh_token: |
| 82 | + raise AuthenticationException("Parameter 'refresh_token' missing") |
| 83 | + elif self.grant_type == self.GRANT_TYPE_CLIENT_CREDENTIALS: |
| 84 | + if not self.client_secret: |
| 85 | + raise AuthenticationException("Parameter 'client_secret' missing") |
| 86 | + else: |
| 87 | + raise AuthenticationException(f"Unknown grant type '{self.grant_type}'") |
| 88 | + |
| 89 | + def verify(self) -> bool: |
| 90 | + """ |
| 91 | + Function that verifies the authentication credentials. |
| 92 | + Depending on the authentication type, the credentials are verified. |
| 93 | +
|
| 94 | + :return: Returns whether the provided credentials are valid. |
| 95 | + """ |
| 96 | + if self.grant_type == self.GRANT_TYPE_PASSWORD: |
| 97 | + return self.username == config.auth_username and self.password == config.auth_password |
| 98 | + elif self.grant_type == self.GRANT_TYPE_REFRESH_TOKEN: |
| 99 | + return JWTAuthenticator.validate_refresh_token(self.refresh_token) |
| 100 | + elif self.grant_type == self.GRANT_TYPE_CLIENT_CREDENTIALS: |
| 101 | + return self.client_secret == config.auth_secret |
| 102 | + else: |
| 103 | + raise AuthenticationException(f"Unknown grant type '{self.grant_type}'") |
0 commit comments