forked from clickyotomy/twitter-message-bus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
executable file
·196 lines (163 loc) · 6.54 KB
/
auth.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /usr/bin/env python2.7
'''
Wrapper around the Keybase client; check the status of the client; lookup a
user on Keybase; sign, verify, encrypt, decrypt text documents.
'''
import re
import json
import distutils.spawn
from subprocess import Popen, PIPE
def clean(text):
'''
Remove ANSI escape sequences.
'''
escape = re.compile(r'\x1b[^m]*m', re.UNICODE)
return escape.sub('', text)
def status(debug=False):
'''
Check the status of the client.
'''
keybase = distutils.spawn.find_executable('keybase')
if keybase is not None:
keybase_status = [keybase, 'status', '--json']
execute = Popen(keybase_status, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout, stderr = execute.communicate()
if execute.returncode != 0:
if debug:
print '[stderr] status()'
print '{0}'.format(stderr.strip())
print '{0} (exit code: {1})'.format(' '.join(keybase_status),
execute.returncode)
else:
try:
payload = json.loads(stdout.strip())
if debug:
print '[stdout] status()'
print json.dumps(payload, indent=4, sort_keys=True)
return payload['LoggedIn'] and payload['Service']['Running']
except (KeyError, ValueError):
if debug:
print 'Unable to parse response from the Keybase Client.'
def lookup(username, debug=False):
'''
Check if a user exists on Keybase.
'''
keybase = distutils.spawn.find_executable('keybase')
if keybase is not None:
keybase_lookup = [keybase, 'id', username.strip()]
execute = Popen(keybase_lookup, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout, stderr = execute.communicate()
if execute.returncode != 0:
if debug:
print '[stderr] lookup()'
print '{0}'.format(stderr.strip())
print '{0} (exit code: {1})'.format(' '.join(keybase_lookup),
execute.returncode)
if re.search(r'.*Not found.*', clean(stderr.strip())):
return False
else:
if debug:
print '[stdout] lookup()'
print stdout.strip()
return True
def encrypt(plaintext, recipient, debug=False):
'''
Encrypt the plain-text (into keybase-saltpack).
'''
keybase = distutils.spawn.find_executable('keybase')
if keybase is not None:
keybase_encrypt = [keybase, 'encrypt', '-m', plaintext, recipient]
execute = Popen(keybase_encrypt, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout, stderr = execute.communicate()
if execute.returncode != 0:
if debug:
print '[stderr] encrypt()'
print '{0}'.format(stderr.strip())
print '{0} (exit code: {1})'.format(' '.join(keybase_encrypt),
execute.returncode)
else:
if debug:
print '[stdout] encrypt()'
print stdout.strip()
return stdout.strip()
def sign(plaintext, debug=False):
'''
Sign the plain-text (into keybase-saltpack).
'''
keybase = distutils.spawn.find_executable('keybase')
if keybase is not None:
keybase_sign = [keybase, 'sign', '-m', plaintext]
execute = Popen(keybase_sign, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout, stderr = execute.communicate()
if execute.returncode != 0:
if debug:
print '[stderr] sign()'
print '{0}'.format(stderr.strip())
print '{0} (exit code: {1})'.format(' '.join(keybase_sign),
execute.returncode)
else:
if debug:
print '[stdout] sign()'
print stdout.strip()
return stdout.strip()
def verify(signed_saltpack, debug=False):
'''
Verify the signed-text (from keybase-saltpack).
'''
keybase = distutils.spawn.find_executable('keybase')
flag, username, text = None, None, None
if keybase is not None:
keybase_verify = [keybase, 'verify', '-m', signed_saltpack]
execute = Popen(keybase_verify, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout, stderr = execute.communicate()
signed = re.compile(r'.*Signed\sby\s(?P<username>\w+).*')
username = signed.search(clean(stderr.strip()))
who = None if username is None else username.group('username').strip()
if execute.returncode != 0:
if debug:
print '[stderr] verify()'
print '{0}'.format(stderr.strip())
print '{0} (exit code: {1})'.format(' '.join(keybase_verify),
execute.returncode)
if re.search(r'.*bad signature.*', clean(stderr.strip())):
flag = False
else:
if debug:
print '[stdout] verify()'
print stderr.strip()
print stdout.strip()
flag = True
text = stdout.strip()
return flag, who, text
def decrypt(encrypted_saltpack, debug=False):
'''
Decrypt the encrypted message (from keybase-saltpack).
'''
keybase = distutils.spawn.find_executable('keybase')
who, plaintext = None, None
if keybase is not None:
keybase_encrypt = [keybase, 'decrypt', '-m', encrypted_saltpack]
execute = Popen(keybase_encrypt, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout, stderr = execute.communicate()
authored = re.compile(r'.*authored\sby\s(?P<username>\w+).*')
username = authored.search(clean(stderr.strip()))
who = None if username is None else username.group('username').strip()
if execute.returncode != 0:
if debug:
print '[stderr] decrypt()'
print '{0}'.format(stderr.strip())
print '{0} (exit code: {1})'.format(' '.join(keybase_encrypt),
execute.returncode)
else:
if debug:
print '[stdout] verify()'
print stderr.strip()
print stdout.strip()
plaintext = stdout.strip()
return who, plaintext