|
| 1 | +from pydantic import BaseModel, ValidationError, validator, conint, constr |
| 2 | +from typing import Optional, List |
| 3 | + |
| 4 | +import copy |
| 5 | +import re |
| 6 | + |
| 7 | +from .errors import Verify2Error |
| 8 | + |
| 9 | + |
| 10 | +class Verify2: |
| 11 | + valid_channels = [ |
| 12 | + 'sms', |
| 13 | + 'whatsapp', |
| 14 | + 'whatsapp_interactive', |
| 15 | + 'voice', |
| 16 | + 'email', |
| 17 | + 'silent_auth', |
| 18 | + ] |
| 19 | + |
| 20 | + def __init__(self, client): |
| 21 | + self._client = client |
| 22 | + self._auth_type = 'jwt' |
| 23 | + |
| 24 | + def new_request(self, params: dict): |
| 25 | + try: |
| 26 | + params_to_verify = copy.deepcopy(params) |
| 27 | + Verify2.VerifyRequest.parse_obj(params_to_verify) |
| 28 | + except (ValidationError, Verify2Error) as err: |
| 29 | + raise err |
| 30 | + |
| 31 | + if not hasattr(self._client, '_application_id'): |
| 32 | + self._auth_type = 'header' |
| 33 | + |
| 34 | + return self._client.post( |
| 35 | + self._client.api_host(), |
| 36 | + '/v2/verify', |
| 37 | + params, |
| 38 | + auth_type=self._auth_type, |
| 39 | + ) |
| 40 | + |
| 41 | + def check_code(self, request_id: str, code: str): |
| 42 | + params = {'code': str(code)} |
| 43 | + |
| 44 | + if not hasattr(self._client, '_application_id'): |
| 45 | + self._auth_type = 'header' |
| 46 | + |
| 47 | + return self._client.post( |
| 48 | + self._client.api_host(), |
| 49 | + f'/v2/verify/{request_id}', |
| 50 | + params, |
| 51 | + auth_type=self._auth_type, |
| 52 | + ) |
| 53 | + |
| 54 | + def cancel_verification(self, request_id: str): |
| 55 | + if not hasattr(self._client, '_application_id'): |
| 56 | + self._auth_type = 'header' |
| 57 | + |
| 58 | + return self._client.delete( |
| 59 | + self._client.api_host(), |
| 60 | + f'/v2/verify/{request_id}', |
| 61 | + auth_type=self._auth_type, |
| 62 | + ) |
| 63 | + |
| 64 | + class VerifyRequest(BaseModel): |
| 65 | + brand: str |
| 66 | + workflow: List[dict] |
| 67 | + locale: Optional[str] |
| 68 | + channel_timeout: Optional[conint(ge=60, le=900)] |
| 69 | + client_ref: Optional[str] |
| 70 | + code_length: Optional[conint(ge=4, le=10)] |
| 71 | + fraud_check: Optional[bool] |
| 72 | + code: Optional[constr(min_length=4, max_length=10, regex='^(?=[a-zA-Z0-9]{4,10}$)[a-zA-Z0-9]*$')] |
| 73 | + |
| 74 | + @validator('workflow') |
| 75 | + def check_valid_workflow(cls, v): |
| 76 | + for workflow in v: |
| 77 | + Verify2._check_valid_channel(workflow) |
| 78 | + Verify2._check_valid_recipient(workflow) |
| 79 | + Verify2._check_app_hash(workflow) |
| 80 | + if workflow['channel'] == 'whatsapp' and 'from' in workflow: |
| 81 | + Verify2._check_whatsapp_sender(workflow) |
| 82 | + |
| 83 | + def _check_valid_channel(workflow): |
| 84 | + if 'channel' not in workflow or workflow['channel'] not in Verify2.valid_channels: |
| 85 | + raise Verify2Error( |
| 86 | + f'You must specify a valid verify channel inside the "workflow" object, one of: "{Verify2.valid_channels}"' |
| 87 | + ) |
| 88 | + |
| 89 | + def _check_valid_recipient(workflow): |
| 90 | + if 'to' not in workflow or ( |
| 91 | + workflow['channel'] != 'email' and not re.search(r'^[1-9]\d{6,14}$', workflow['to']) |
| 92 | + ): |
| 93 | + raise Verify2Error(f'You must specify a valid "to" value for channel "{workflow["channel"]}"') |
| 94 | + |
| 95 | + def _check_app_hash(workflow): |
| 96 | + if workflow['channel'] == 'sms' and 'app_hash' in workflow: |
| 97 | + if type(workflow['app_hash']) != str or len(workflow['app_hash']) != 11: |
| 98 | + raise Verify2Error( |
| 99 | + 'Invalid "app_hash" specified. If specifying app_hash, \ |
| 100 | + it must be passed as a string and contain exactly 11 characters.' |
| 101 | + ) |
| 102 | + elif workflow['channel'] != 'sms' and 'app_hash' in workflow: |
| 103 | + raise Verify2Error('Cannot specify a value for "app_hash" unless using SMS for authentication.') |
| 104 | + |
| 105 | + def _check_whatsapp_sender(workflow): |
| 106 | + if not re.search(r'^[1-9]\d{6,14}$', workflow['from']): |
| 107 | + raise Verify2Error(f'You must specify a valid "from" value if included.') |
0 commit comments