This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo_bells_and_whistles.py
executable file
·139 lines (113 loc) · 5.38 KB
/
demo_bells_and_whistles.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import sys
import uuid
import toopher
from toopher import ToopherApiError
import argparse
DEFAULT_USERNAME = 'demo@toopher.com'
DEFAULT_TERMINAL_NAME = 'my computer'
def print_sep(char='-'):
print char*72
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Toopher Library Usage Demo')
parser.add_argument('--sms', dest='sms_authentication', action='store_true',
help='Use SMS Authentication mode (SMS messages instead of Smartphone push)')
parser.add_argument('--sms-inband-reply', dest='sms_inband_reply', action='store_true',
help='Send a OTP to user over SMS for entry through the website (instead of prompting them to reply via SMS). This option only makes sense if --sms is also supplied.')
args = parser.parse_args()
print_sep('=')
print 'Library Usage Demo'
print_sep('=')
print
key = os.environ.get('TOOPHER_CONSUMER_KEY')
secret = os.environ.get('TOOPHER_CONSUMER_SECRET')
if not (key or secret):
print 'Setup Credentials (set environment variables to prevent prompting)'
print_sep()
print 'Enter your requester credential details (from https://dev.toopher.com)'
while not key:
key = raw_input('TOOPHER_CONSUMER_KEY=')
while not secret:
secret = raw_input('TOOPHER_CONSUMER_SECRET=')
api = toopher.ToopherApi(key, secret, os.environ.get('TOOPHER_BASE_URL'))
while True:
print 'Step 1: Pair requester with phone'
print_sep('-')
if args.sms_authentication:
pair_func = api.pair_sms
pair_help = 'Non-US numbers should start with a "+" and include the country code.'
pair_type = 'mobile number'
else:
pair_func = api.pair
pair_help = 'Pairing phrases are generated on the mobile app'
pair_type = 'pairing phrase'
print pair_help
pairing_key = raw_input('Enter {0}: '.format(pair_type))
while not pairing_key:
print 'Please enter a {0} to continue'.format(pair_type)
pairing_key = raw_input('Enter {0}: '.format(pair_type))
user_name = raw_input('Enter a username for this pairing [%s]: ' % DEFAULT_USERNAME)
if not user_name:
user_name = DEFAULT_USERNAME
print 'Sending pairing request...'
try:
pairing_status = pair_func(pairing_key, user_name)
pairing_id = pairing_status.id
break
except ToopherApiError, e:
print 'The pairing phrase was not accepted (reason: %s)' % e
while True:
raw_input('Authorize pairing on phone and then press return to continue.')
print 'Checking status of pairing request...'
try:
pairing_status = api.get_pairing_status(pairing_id)
if pairing_status.enabled:
print 'Pairing complete'
print
break
else:
print 'The pairing has not been authorized by the phone yet'
except ToopherApiError, e:
raise
print 'Could not check pairing status (reason: %s)' % e
terminal_extras = {}
while True:
print 'Step 2: Authenticate log in'
print_sep()
terminal_name = raw_input('Enter a terminal name for this authentication request [%s]: ' % DEFAULT_TERMINAL_NAME)
if not terminal_name:
terminal_name = DEFAULT_TERMINAL_NAME
if terminal_name in terminal_extras:
terminal_extra = terminal_extras[terminal_name]
else:
terminal_extra = terminal_extras[terminal_name] = uuid.uuid4()
print 'Sending authentication request...'
try:
request_status = api.authenticate(pairing_id, terminal_name, terminal_name_extra=terminal_extra, use_sms_inband_reply=args.sms_inband_reply)
request_id = request_status.id
except ToopherApiError, e:
print 'Error initiating authentication (reason: %s)' % e
continue
while True:
if args.sms_inband_reply:
otp = raw_input('Enter the One-Time-Password that was sent to your phone to continue: ')
else:
otp = None
raw_input('Response to authentication request on phone (if prompted) and then press return to continue.')
print 'Checking status of authentication request...'
try:
if otp:
request_status = api.authenticate_with_otp(request_id, otp)
else:
request_status = api.get_authentication_status(request_id)
except ToopherApiError, e:
print 'Could not check authentication status (reason: %s)' % e
continue
if request_status.pending:
print 'The authentication request has not received a response from the phone yet.'
else:
automation = 'automatically ' if request_status.automated else ''
result = 'granted' if request_status.granted else 'denied'
print 'The request was ' + automation + result + "!"
break
raw_input('Press return to authenticate again, or Ctrl-C to exit')