-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathverify_with_own_code.py
More file actions
63 lines (51 loc) · 2.46 KB
/
verify_with_own_code.py
File metadata and controls
63 lines (51 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from requests import Request, Session
import os
from random import SystemRandom
import sys
sys.path.append('../shared/')
import ts_auth
# Generate a random number n digits in length using a system random.
def random_with_n_digits(n):
return "".join(SystemRandom().choice('123456789') for _ in range(n))
# Replace the defaults below with your Telesign authentication credentials
customer_id = os.getenv('CUSTOMER_ID', 'FFFFFFFF-EEEE-DDDD-1234-AB1234567890')
api_key = os.getenv('API_KEY', 'ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==')
# Set the REST API URL
url = "https://rest-ww.telesign.com/v1/verify/sms"
# Set the request inputs.
# Set the default below to your test phone number. In your production code, update the phone number dynamically for each transaction.
phone_number = os.getenv('PHONE_NUMBER', '11234567890')
verify_code = random_with_n_digits(5)
# Add all headers except auth headers
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Date': ts_auth.format_current_date()
}
# Create the payload
payload = f"phone_number={phone_number}&verify_code={verify_code}"
# Create session and prepped request
s = Session()
req = Request('POST', url, data=payload, headers=headers)
prepped_request = req.prepare()
# Add auth to prepped request
request_properties = {
"method": prepped_request.method,
"headers": prepped_request.headers,
"body": prepped_request.body,
"url": prepped_request.url
}
# prepped_request.headers = ts_auth.add_basic(request_properties, customer_id, api_key)
prepped_request.headers = ts_auth.add_digest(request_properties, customer_id, api_key)
# Make the request and capture the response.
# If Telesign Verify Plus is enabled for SMS Verify for your account, Telesign checks the risk score of the phone number before sending the SMS.
response = s.send(prepped_request)
# Display the request and response in the console for debugging purposes. In your production code, you would likely remove this.
ts_auth.pretty_print_request(prepped_request)
print(f"Response:\n{response.text}\n")
# Display prompt to enter verification code in the console.
# In your production code, you would instead collect the potential verification code from the end-user in your platform's interface.
user_entered_verify_code = input("Please enter the verification code you were sent: ")
if verify_code == user_entered_verify_code.strip():
print("Your code is correct.")
else:
print("Your code is incorrect.")