-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwizard.py
183 lines (154 loc) · 5.03 KB
/
wizard.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
import os
import webbrowser
from login import GoogleLogin
import time
import base64
import json
import dill as pickle
from mailcleaner import GmailClient
# configs
client_id = None
client_secret = None
clean_interval = 5 * 60 # 5 Min
# user
auth_client = None
gmail_client = None
user_email = None
user_token = None
# from file
google_client_config = "google_config.json"
login_credentials_file = "login_credential.json"
def load_credentials():
# try:
login_creds = open(login_credentials_file, 'r').read()
login_creds = json.loads(login_creds)
global user_email
global user_token
global client_id
global client_secret
global auth_client
global clean_interval
user_email = login_creds["user_email"]
user_token = pickle.loads(base64.b64decode(login_creds["user_token"]))
auth_client = pickle.loads(base64.b64decode(login_creds["auth_client"]))
client_id = (base64.b64decode(login_creds["client_id"])).decode()
client_secret = (base64.b64decode(login_creds["client_secret"])).decode()
clean_interval = login_creds["clean_interval"]
print("[+] Credentials Loaded for email: " + user_email)
return True
# except:
# return False
def login_with_google():
# If Config file is present use the config file
global auth_client, client_id, client_secret, user_token
# Create and login
try:
if os.path.exists(google_client_config):
gauth = GoogleLogin(client_id, client_secret)
gauth.load_from_file(google_client_config)
else:
print("Google OAuth Client Config file was not found.")
print("Please ensure that \'" + google_client_config + "\' file exists in the present directory\n")
print("To Obtain your client_id and client_secret go to: https://console.cloud.google.com/apis/credentials\n")
while not client_id:
print("[MANUAL INPUT]")
client_id = input("> Please Enter Google Oauth Client ID: ")
while not client_secret:
print("[MANUAL INPUT]")
client_secret = input("> Please Enter Google Oauth Client Secret: ")
gauth = GoogleLogin(client_id, client_secret)
# Get a Auth URL
auth_url = gauth.create_auth_url()
# Start Localhost bottle server
#
# Spawn Browser
print(auth_url)
webbrowser.open(auth_url)
try:
os.system("termux-open-url " + auth_url)
except:
pass
# Print Instructions
print("[!]\tPlease Login with your google account and give email access.")
print("\tThis is a one time login and wouldnt be required after logging in.\n")
print("[Note]: If ater login you get a blank page starting with 'localhost' please copy the url and paste below.\n\n")
auth_url = input("> Enter url here after being redirected: ")
while auth_url==None:
auth_url = input("> Enter url here: ")
# Prevent HTTPS Issues
if "http:" in auth_url:
auth_url = auth_url.replace("http:", "https:")
# Try get token
user_token = gauth.create_token(auth_url)
if not user_token:
print("[!] Error Occured in Logging in. Please run the script again.")
wait_and_exit()
# Store the gauth
auth_client = gauth
client_id = auth_client.client_id
client_secret = auth_client.client_secret
return user_token
print("[!] Unknown Error Occured...")
wait_and_exit()
except:
wait_and_exit()
def setup_email_client():
"""
Setup
"""
global auth_client
global user_email
if not user_email:
user_email = auth_client.get_user_email()
if not user_email:
print("[!] There was an error in obtaining user email.")
user_email = input("> Please manually enter your account email here: ")
global gmail_client
gmail_client = GmailClient()
if not gmail_client.login_mail_client(user_email, user_token):
print("[!] There was an error logging in to imap servers. please try again")
wait_and_exit()
return True
def save_credentials():
"""
Save Login credentials for later use
"""
try:
global client_id, client_secret, user_email, user_token, auth_client, clean_interval
user_token_pickle = (base64.b64encode(pickle.dumps(user_token,0))).decode()
auth_client_pickle = (base64.b64encode(pickle.dumps(auth_client,0))).decode()
credentials = { "client_id" : base64.b64encode(client_id.encode()).decode(), "client_secret" : base64.b64encode(client_secret.encode()).decode(), "clean_interval" : clean_interval, "user_email": user_email, "user_token" : str(user_token_pickle), "auth_client" : str(auth_client_pickle) }
credentials = json.dumps(credentials,indent=4)
with open(login_credentials_file, 'w') as file:
file.write(credentials)
file.close()
except:
print("There was an error in saving the credentials to a file")
return True
def main():
"""
Main Method
"""
global gmail_client
global auth_client
global user_token
if os.path.exists(login_credentials_file):
print("[+] Login Credentials Found...")
load_credentials()
if not user_token and not auth_client:
login_with_google()
if not gmail_client:
print("[+] Creating New GmailClient...")
setup_email_client()
if gmail_client:
save_credentials()
print("[+] You can run the script now...")
#
def wait_and_exit():
"""
Wait then exit
"""
input("\n[Press any key to exit...]")
exit()
if __name__ == '__main__':
main()