|
| 1 | +from __future__ import annotations |
| 2 | +from typing import TYPE_CHECKING |
| 3 | + |
| 4 | +from .camara_auth import CamaraAuth |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from vonage import Client |
| 8 | + |
| 9 | + |
| 10 | +class SimSwap: |
| 11 | + """Class containing methods for working with the Vonage SIM Swap API.""" |
| 12 | + |
| 13 | + def __init__(self, client: Client): |
| 14 | + self._client = client |
| 15 | + self._auth_type = 'oauth2' |
| 16 | + self._camara_auth = CamaraAuth(client) |
| 17 | + |
| 18 | + def check(self, phone_number: str, max_age: int = None): |
| 19 | + """Check if a SIM swap has been performed in a given time frame. |
| 20 | +
|
| 21 | + Args: |
| 22 | + phone_number (str): The phone number to check. Use the E.164 format without a leading +. |
| 23 | + max_age (int, optional): Period in hours to be checked for SIM swap. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + The response from the API as a dict. |
| 27 | + """ |
| 28 | + oicd_response = self._camara_auth.make_oidc_request( |
| 29 | + number=phone_number, scope='dpv:FraudPreventionAndDetection#check-sim-swap' |
| 30 | + ) |
| 31 | + token = self._camara_auth.request_camara_token(oicd_response) |
| 32 | + |
| 33 | + params = {'phoneNumber': phone_number} |
| 34 | + if max_age: |
| 35 | + params['maxAge'] = max_age |
| 36 | + |
| 37 | + return self._client.post( |
| 38 | + 'api-eu.vonage.com', |
| 39 | + '/camara/sim-swap/v040/check', |
| 40 | + params=params, |
| 41 | + auth_type=self._auth_type, |
| 42 | + oauth_token=token, |
| 43 | + ) |
| 44 | + |
| 45 | + def get_last_swap_date(self, phone_number: str): |
| 46 | + """Get the last SIM swap date for a phone number. |
| 47 | +
|
| 48 | + Args: |
| 49 | + phone_number (str): The phone number to check. Use the E.164 format without a leading +. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + The response from the API as a dict. |
| 53 | + """ |
| 54 | + oicd_response = self._camara_auth.make_oidc_request( |
| 55 | + number=phone_number, |
| 56 | + scope='dpv:FraudPreventionAndDetection#retrieve-sim-swap-date', |
| 57 | + ) |
| 58 | + token = self._camara_auth.request_camara_token(oicd_response) |
| 59 | + return self._client.post( |
| 60 | + 'api-eu.vonage.com', |
| 61 | + '/camara/sim-swap/v040/retrieve-date', |
| 62 | + params={'phoneNumber': phone_number}, |
| 63 | + auth_type=self._auth_type, |
| 64 | + oauth_token=token, |
| 65 | + ) |
0 commit comments