-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVSA_Auth.py
187 lines (160 loc) · 6.3 KB
/
VSA_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
import configparser
import requests
import os
import logging
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from http.server import HTTPServer, BaseHTTPRequestHandler
from time import sleep
import re
from imaplib import IMAP4_SSL
import email
from platform import system
try:
from . import Auth
except(ImportError):
from VSA import Auth
def doInitialAuth(code, config):
vsa_uri = config['VSA']['vsa_uri']
client_id = config['VSA']['client_id']
client_secret = config['VSA']['client_secret']
authendpoint = vsa_uri + "/api/v1.0/authorize"
redirect_uri = config['Listener']['redirect_uri']
if(system() == "Windows"):
fullpath = os.getcwd() + "\\PythonVSA\\config.ini"
else:
fullpath = os.getcwd() + "/PythonVSA/config.ini"
if(not os.path.exists(fullpath)):
print("Defaulting to config.ini in current directory.")
fullpath = 'config.ini'
r = requests.post(authendpoint, json={
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri,
"client_id": client_id,
"client_secret": client_secret})
print("First refresh token:")
print(r.text)
if(r.status_code == 400):
print("An error has occurred")
print(r.text)
exit()
refreshtoken = r.json()['refresh_token']
print("Got token:")
print(refreshtoken)
print("Response code:")
print(str(r.status_code))
config['Auth'] = r.json()
config['Auth']['refreshed_at'] = datetime.datetime.now().strftime("%Y%m%d%H%M")
with open(fullpath, 'w') as configfile:
config.write(configfile)
Auth.doRefresh(refreshtoken)
def startauth():
#Dev var
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
logging.getLogger().setLevel(logging.DEBUG)
# Init config
config = configparser.ConfigParser()
if(system() == "Windows"):
fullpath = os.getcwd() + "\\PythonVSA\\config.ini"
else:
fullpath = os.getcwd() + "/PythonVSA/config.ini"
readfiles = config.read(fullpath, encoding='utf-8')
if(not readfiles):
print("Defaulting to config.ini in current directory.")
readfiles = config.read('config.ini', encoding='utf-8')
if(not readfiles):
print("We weren't able to read config.ini.")
exit()
try:
client_id = config['VSA']['client_id']
client_secret = config['VSA']['client_secret']
vsa_uri = config['VSA']['vsa_uri']
redirect_uri = config['Listener']['redirect_uri']
#listen_port = config['Listener']['listen_port']
smtp_username = config['Email']['smtp_username']
smtp_password = config['Email']['smtp_password']
smtp_emailfrom = config['Email']['smtp_emailfrom']
smtp_emailto = config['Email']['smtp_emailto']
smtp_server = config['Email']['smtp_server']
smtp_port = int(config['Email']['smtp_port'])
imap_username = config['Email']['imap_username']
imap_password = config['Email']['imap_password']
imap_email = config['Email']['imap_email']
imap_server = config['Email']['imap_server']
imap_port = int(config['Email']['imap_port'])
imap_refresh_interval = int(config['Email']['imap_refresh_interval'])
except(KeyError):
print("A required variable is missing from the configuration. Please check config.ini. ")
pass
urlforuser = vsa_uri + "/vsapres/web20/core/login.aspx?response_type=code&redirect_uri=" + redirect_uri + "&client_id=" + client_id
print("Attempting initial auth")
print("Please visit this link and copy the entire resulting URL.")
print(urlforuser)
msg = MIMEMultipart('mixed')
msg['Subject'] = "PythonVSA Authentication"
msg['From'] = smtp_emailfrom
msg['To'] = smtp_emailto
text = f"""\
Please follow this link to authenticate your new integration: {urlforuser}
Once you have authorized you will be redirected to a page that doesn't load/resolve. Copy the address from your address bar and reply to this email with it."""
msg.attach(MIMEText(text))
smtp_server = smtplib.SMTP(smtp_server, smtp_port)
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.ehlo()
smtp_server.login(smtp_username, smtp_password)
smtp_server.sendmail(smtp_emailfrom, smtp_emailto, msg.as_string())
smtp_server.close()
print(f"Waiting {imap_refresh_interval} seconds to give a chance to respond.")
sleep(imap_refresh_interval)
connection = IMAP4_SSL(imap_server, imap_port)
connection.login(imap_username, imap_password)
typ, data = connection.select('INBOX')
typ, data = connection.search(None, '(UNSEEN)')
if(data == [b'']):
print(f"No emails found. Checking again every {imap_refresh_interval} seconds.")
i = 0
while i < 5:
i = i + 1
print(f"Checking {5 - i} more times.")
sleep(imap_refresh_interval)
typ, data = connection.search(None, '(UNSEEN)')
if(data[0] != b''):
break
for num in data[0].split():
typ, data = connection.fetch(num, '(RFC822)')
try:
msg = email.message_from_bytes(data[1][1])
except(IndexError):
msg = email.message_from_bytes(data[0][1])
# Mark as read and delete email
typ, data = connection.store(num, '+FLAGS', '\\Seen')
connection.store(num, "+FLAGS", "\\Deleted")
connection.expunge()
# Parse response
pattern = r'https://.*\/\?code(=[\w\d]{34})'
pattern1 = r'https://.*\/\?code(=[\w\d]{32})'
try:
match = re.match(pattern, msg._payload)
matchraw = re.match(pattern1, msg._payload)
except(TypeError):
print("It appears we have a message with a format we can't understand. Deleting.")
continue
if(match):
code = match.group(1)
code = code.replace("=3D", "")
connection.close()
connection.logout()
doInitialAuth(code, config)
elif(matchraw):
connection.close()
connection.logout()
doInitialAuth(code, config)
else:
print("Didn't find URL. Deleting.")
print("All done.")
if __name__ == "__main__":
startauth()