Skip to content

Commit

Permalink
refactor: extende OAUTH2Basic authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jul 15, 2024
1 parent 88a8887 commit 15dc0d5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
18 changes: 11 additions & 7 deletions eox_nelp/api_clients/authenticators.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,24 @@ def authenticate(self, api_client):
return session


class SMSVendoAuthenticator(BasicAuthAuthenticator):
"""Authenticator for custom use of the SMS vendor particular API."""
class Oauth2BasicAuthenticator(BasicAuthAuthenticator):
"""Authenticator for custom use using basic auth to get a Oauth2 Token (Bearer or JWT).
Token_type on depends of the response used after the oauth2 token request.
Then the token is used for the next requests.
"""

def authenticate(self, api_client):
"""Authenticate the session with basic auth in order to get Bearer token.
Then the Bearer token is added to a new session Headers.
SMS vendor configuration.
"""Authenticate the session with basic auth in order to get token(Bearer or JWT).
Then the token is added to a new session Headers.
Is needed the user, password and token_path class atrributes to the get oauth2 token,
based on the client configuration.
"""
auth_session = super().authenticate(api_client)
key = f"smsvendor-{api_client.user}-{api_client.password}"
key = f"oauth2-basic-{api_client.user}-{api_client.password}"
headers = cache.get(key)

if not headers:
authenticate_url = f"{api_client.base_url}/oauth2/v1/token"
authenticate_url = f"{api_client.base_url}/{api_client.token_path}"
response = auth_session.post(
url=authenticate_url,
data={"grant_type": "client_credentials", "scope": "notification"}
Expand Down
5 changes: 3 additions & 2 deletions eox_nelp/api_clients/sms_vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings

from eox_nelp.api_clients import AbstractAPIRestClient
from eox_nelp.api_clients.authenticators import SMSVendoAuthenticator
from eox_nelp.api_clients.authenticators import Oauth2BasicAuthenticator

try:
from eox_audit_model.decorators import audit_method
Expand All @@ -18,7 +18,7 @@ def audit_method(action): # pylint: disable=unused-argument

class SMSVendorApiClient(AbstractAPIRestClient):
"""Allow to perform SMS send operations."""
authentication_class = SMSVendoAuthenticator
authentication_class = Oauth2BasicAuthenticator

@property
def base_url(self):
Expand All @@ -27,6 +27,7 @@ def base_url(self):
def __init__(self):
self.user = getattr(settings, "SMS_VENDOR_USERNAME")
self.password = getattr(settings, "SMS_VENDOR_PASSWORD")
self.token_path = getattr(settings, "SMS_VENDOR_TOKEN_PATH")

super().__init__()

Expand Down
1 change: 1 addition & 0 deletions eox_nelp/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def plugin_settings(settings): # pylint: disable=function-redefined
settings.SMS_VENDOR_URL = 'https://testing.com'
settings.SMS_VENDOR_USERNAME = 'test-user'
settings.SMS_VENDOR_PASSWORD = 'test-password'
settings.SMS_VENDOR_TOKEN_PATH = "oauth2/v1/token"
settings.SMS_VENDOR_SEND_SMS_PATH = 'sms/send'

settings.PEARSON_RTI_WSDL_URL = 'https://testing.com'
Expand Down

0 comments on commit 15dc0d5

Please sign in to comment.