-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* starting verify implementation * using Literal from typing-extensions for 3.7 compatibility * add new_request and check_code verify2 methods, use new class structure, add tests * adding more test cases * adding custom verification code validation and testing * moving custom code validation from workflow object to the main request body * adding fraud_check parameter to verify v2 request * adding verify v2 to readme, adding verify2.cancel_verification * removing build on PR as we already build on push * fixing typo in link
- Loading branch information
Showing
21 changed files
with
740 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: Build | ||
on: [push, pull_request] | ||
on: [push] | ||
jobs: | ||
test: | ||
name: Test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from pydantic import BaseModel, ValidationError, validator, conint, constr | ||
from typing import Optional, List | ||
|
||
import copy | ||
import re | ||
|
||
from .errors import Verify2Error | ||
|
||
|
||
class Verify2: | ||
valid_channels = [ | ||
'sms', | ||
'whatsapp', | ||
'whatsapp_interactive', | ||
'voice', | ||
'email', | ||
'silent_auth', | ||
] | ||
|
||
def __init__(self, client): | ||
self._client = client | ||
self._auth_type = 'jwt' | ||
|
||
def new_request(self, params: dict): | ||
try: | ||
params_to_verify = copy.deepcopy(params) | ||
Verify2.VerifyRequest.parse_obj(params_to_verify) | ||
except (ValidationError, Verify2Error) as err: | ||
raise err | ||
|
||
if not hasattr(self._client, '_application_id'): | ||
self._auth_type = 'header' | ||
|
||
return self._client.post( | ||
self._client.api_host(), | ||
'/v2/verify', | ||
params, | ||
auth_type=self._auth_type, | ||
) | ||
|
||
def check_code(self, request_id: str, code: str): | ||
params = {'code': str(code)} | ||
|
||
if not hasattr(self._client, '_application_id'): | ||
self._auth_type = 'header' | ||
|
||
return self._client.post( | ||
self._client.api_host(), | ||
f'/v2/verify/{request_id}', | ||
params, | ||
auth_type=self._auth_type, | ||
) | ||
|
||
def cancel_verification(self, request_id: str): | ||
if not hasattr(self._client, '_application_id'): | ||
self._auth_type = 'header' | ||
|
||
return self._client.delete( | ||
self._client.api_host(), | ||
f'/v2/verify/{request_id}', | ||
auth_type=self._auth_type, | ||
) | ||
|
||
class VerifyRequest(BaseModel): | ||
brand: str | ||
workflow: List[dict] | ||
locale: Optional[str] | ||
channel_timeout: Optional[conint(ge=60, le=900)] | ||
client_ref: Optional[str] | ||
code_length: Optional[conint(ge=4, le=10)] | ||
fraud_check: Optional[bool] | ||
code: Optional[constr(min_length=4, max_length=10, regex='^(?=[a-zA-Z0-9]{4,10}$)[a-zA-Z0-9]*$')] | ||
|
||
@validator('workflow') | ||
def check_valid_workflow(cls, v): | ||
for workflow in v: | ||
Verify2._check_valid_channel(workflow) | ||
Verify2._check_valid_recipient(workflow) | ||
Verify2._check_app_hash(workflow) | ||
if workflow['channel'] == 'whatsapp' and 'from' in workflow: | ||
Verify2._check_whatsapp_sender(workflow) | ||
|
||
def _check_valid_channel(workflow): | ||
if 'channel' not in workflow or workflow['channel'] not in Verify2.valid_channels: | ||
raise Verify2Error( | ||
f'You must specify a valid verify channel inside the "workflow" object, one of: "{Verify2.valid_channels}"' | ||
) | ||
|
||
def _check_valid_recipient(workflow): | ||
if 'to' not in workflow or ( | ||
workflow['channel'] != 'email' and not re.search(r'^[1-9]\d{6,14}$', workflow['to']) | ||
): | ||
raise Verify2Error(f'You must specify a valid "to" value for channel "{workflow["channel"]}"') | ||
|
||
def _check_app_hash(workflow): | ||
if workflow['channel'] == 'sms' and 'app_hash' in workflow: | ||
if type(workflow['app_hash']) != str or len(workflow['app_hash']) != 11: | ||
raise Verify2Error( | ||
'Invalid "app_hash" specified. If specifying app_hash, \ | ||
it must be passed as a string and contain exactly 11 characters.' | ||
) | ||
elif workflow['channel'] != 'sms' and 'app_hash' in workflow: | ||
raise Verify2Error('Cannot specify a value for "app_hash" unless using SMS for authentication.') | ||
|
||
def _check_whatsapp_sender(workflow): | ||
if not re.search(r'^[1-9]\d{6,14}$', workflow['from']): | ||
raise Verify2Error(f'You must specify a valid "from" value if included.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "https://developer.nexmo.com/api-errors#not-found", | ||
"title": "Not Found", | ||
"detail": "Request '5fcc26ef-1e54-48a6-83ab-c47546a19824' was not found or it has been verified already.", | ||
"instance": "02cabfcc-2e09-4b5d-b098-1fa7ccef4607" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"request_id": "e043d872-459b-4750-a20c-d33f91d6959f", | ||
"status": "completed" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"title": "Conflict", | ||
"detail": "The current Verify workflow step does not support a code.", | ||
"instance": "690c48de-c5d1-49f2-8712-b3b0a840f911", | ||
"type": "https://developer.nexmo.com/api-errors#conflict" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"request_id": "c11236f4-00bf-4b89-84ba-88b25df97315" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"title": "Conflict", | ||
"type": "https://www.developer.vonage.com/api-errors/verify#conflict", | ||
"detail": "Concurrent verifications to the same number are not allowed.", | ||
"instance": "738f9313-418a-4259-9b0d-6670f06fa82d", | ||
"request_id": "575a2054-aaaf-4405-994e-290be7b9a91f" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "https://developer.nexmo.com/api-errors#forbidden", | ||
"title": "Forbidden", | ||
"detail": "Your account does not have permission to perform this action.", | ||
"instance": "1995bc0d-c850-4bf0-aa1e-6c40da43d3bf" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "https://developer.nexmo.com/api-errors#bad-request", | ||
"title": "Invalid Code", | ||
"detail": "The code you provided does not match the expected value.", | ||
"instance": "16d6bca6-c0dc-4add-94b2-0dbc12cba83b" | ||
} |
Oops, something went wrong.