|
| 1 | +from pydantic import validate_call |
| 2 | +from vonage_http_client.http_client import HttpClient |
| 3 | + |
| 4 | +from .responses import OidcResponse, TokenResponse |
| 5 | + |
| 6 | + |
| 7 | +class NetworkAuth: |
| 8 | + """Class containing methods for authenticating Network APIs following Camara standards.""" |
| 9 | + |
| 10 | + def __init__(self, http_client: HttpClient): |
| 11 | + self._http_client = http_client |
| 12 | + self._host = 'api-eu.vonage.com' |
| 13 | + self._auth_type = 'jwt' |
| 14 | + self._sent_data_type = 'form' |
| 15 | + |
| 16 | + @property |
| 17 | + def http_client(self) -> HttpClient: |
| 18 | + """The HTTP client used to make requests to the Network Auth API. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + HttpClient: The HTTP client used to make requests to the Network Auth API. |
| 22 | + """ |
| 23 | + return self._http_client |
| 24 | + |
| 25 | + @validate_call |
| 26 | + def get_oauth2_user_token(self, number: str, scope: str) -> str: |
| 27 | + """Get an OAuth2 user token for a given number and scope. |
| 28 | +
|
| 29 | + Args: |
| 30 | + number (str): The phone number to authenticate. |
| 31 | + scope (str): The scope of the token. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + str: The OAuth2 user token. |
| 35 | + """ |
| 36 | + oidc_response = self.make_oidc_request(number, scope) |
| 37 | + token_response = self.request_access_token(oidc_response.auth_req_id) |
| 38 | + return token_response.access_token |
| 39 | + |
| 40 | + @validate_call |
| 41 | + def make_oidc_request(self, number: str, scope: str) -> OidcResponse: |
| 42 | + """Make an OIDC request to authenticate a user. |
| 43 | +
|
| 44 | + Args: |
| 45 | + number (str): The phone number to authenticate. |
| 46 | + scope (str): The scope of the token. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + OidcResponse: A response containing the authentication request ID. |
| 50 | + """ |
| 51 | + number = self._ensure_plus_prefix(number) |
| 52 | + params = {'login_hint': number, 'scope': scope} |
| 53 | + |
| 54 | + response = self._http_client.post( |
| 55 | + self._host, |
| 56 | + '/oauth2/bc-authorize', |
| 57 | + params, |
| 58 | + self._auth_type, |
| 59 | + self._sent_data_type, |
| 60 | + ) |
| 61 | + return OidcResponse(**response) |
| 62 | + |
| 63 | + @validate_call |
| 64 | + def request_access_token( |
| 65 | + self, auth_req_id: str, grant_type: str = 'urn:openid:params:grant-type:ciba' |
| 66 | + ) -> TokenResponse: |
| 67 | + """Request a Camara access token using an authentication request ID given as a response to |
| 68 | + an OIDC request.""" |
| 69 | + params = {'auth_req_id': auth_req_id, 'grant_type': grant_type} |
| 70 | + |
| 71 | + response = self._http_client.post( |
| 72 | + self._host, |
| 73 | + '/oauth2/token', |
| 74 | + params, |
| 75 | + self._auth_type, |
| 76 | + self._sent_data_type, |
| 77 | + ) |
| 78 | + return TokenResponse(**response) |
| 79 | + |
| 80 | + def _ensure_plus_prefix(self, number: str) -> str: |
| 81 | + """Ensure that the number has a plus prefix. |
| 82 | +
|
| 83 | + Args: |
| 84 | + number (str): The phone number to check. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + str: The phone number with a plus prefix. |
| 88 | + """ |
| 89 | + if number.startswith('+'): |
| 90 | + return number |
| 91 | + return f'+{number}' |
0 commit comments