This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.py
169 lines (138 loc) · 5.47 KB
/
authentication.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
"""Handles IMAP operations for tMail, the terminal Gmail client"""
import os
import time
from os import path
from simplecrypt import decrypt, encrypt
import imaplib
from requests_oauthlib import OAuth2Session
CLIENT_ID = '47637480825-5d3ndp33q8m6eojt015p9th1q5cig3bm.apps.googleusercontent.com'
CLIENT_SECRET = 'xjFxdgVhJZjypUUoW7sC8R4Y'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
SCOPE = 'https://mail.google.com/'
SETTINGS_DIR = '.tmail'
SETTINGS_FILE = 'settings.txt'
REFRESH_KEY_FILE = '.refresh_key'
ACCESS_KEY_FILE = '.access_key'
def _generate_auth_string(user, token):
"""Generates the string to use when authenticating within IMAP"""
return 'user=%s\1auth=Bearer %s\1\1' % (user, token)
def get_expiration():
"""Retrieves the access token expiration from the settings file"""
with open(path.join(
path.expanduser('~'),
SETTINGS_DIR,
SETTINGS_FILE)) as settings_file:
for line in settings_file:
if line.startswith('expiration'):
tokens = line.split('=')
return float(tokens[1])
raise IOError('No expiration in settings')
def refresh_access_token():
"""Refreshes the access token"""
with open(path.join(
path.expanduser('~'),
SETTINGS_DIR,
ACCESS_KEY_FILE), 'rb') as key_file:
refresh_token = key_file.read()
refresh_token = decrypt(CLIENT_SECRET, refresh_token).decode('utf-8')
google = OAuth2Session(client_id=CLIENT_ID)
token = google.refresh_token(
'https://accounts.google.com/o/oauth2/token',
refresh_token=refresh_token,
client_secret=CLIENT_SECRET,
client_id=CLIENT_ID)
return token['access_token'], token['expires_in']
def save_expiration(expiration):
"""Saves over any existing expiration in the settings file"""
with open(
path.join(path.expanduser('~'), SETTINGS_DIR, SETTINGS_FILE),
'r+') as settings_file:
lines = settings_file.readlines()
with open(
path.join(path.expanduser('~'), SETTINGS_DIR, SETTINGS_FILE),
'w+') as settings_file:
for line in lines:
if not line.startswith('expiration'):
settings_file.write(line)
with open(
path.join(path.expanduser('~'), SETTINGS_DIR, SETTINGS_FILE),
'a+') as settings_file:
settings_file.write('expiration=' + str(time.time() + expiration))
def get_access_token():
"""Retrieves the access token from the keys file"""
try:
expiration = get_expiration()
assert expiration > time.time()
except AssertionError:
access_token, expiration = refresh_access_token()
save_expiration(expiration)
return access_token
with open(path.join(
path.expanduser('~'),
SETTINGS_DIR,
ACCESS_KEY_FILE), 'rb') as key_file:
access_token = key_file.read()
return decrypt(CLIENT_SECRET, access_token).decode('utf-8')
def get_username():
"""Retrieves the username from the settings file"""
with open(path.join(
path.expanduser('~'),
SETTINGS_DIR,
SETTINGS_FILE)) as settings_file:
for line in settings_file:
if line.startswith('username'):
tokens = line.split('=')
return tokens[1]
raise IOError('No username in settings')
def oauth_process():
"""Goes through the OAuth2 process for Gmail"""
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
google = OAuth2Session(
client_id=CLIENT_ID,
scope=SCOPE,
redirect_uri=REDIRECT_URI)
authorization_url = google.authorization_url(
'https://accounts.google.com/o/oauth2/auth')
print('Please visit this url to authenticate: ')
print(authorization_url)
authorization_response = input('Please enter the authorization code: ')
token = google.fetch_token(
'https://accounts.google.com/o/oauth2/token',
client_secret=CLIENT_SECRET,
code=authorization_response)
return token['refresh_token'], token['access_token'], float(token['expires_in'])
def save_username():
username = input('Please enter your Gmail address: ')
with open(
path.join(path.expanduser('~'), SETTINGS_DIR, SETTINGS_FILE),
'a+') as settings_file:
settings_file.write('username=' + username + '\n')
return username
def authenticate():
"""Authenticates the current user using OAuth2"""
username = None
try:
access_token = get_access_token()
except IOError:
os.makedirs(path.join(path.expanduser('~'), SETTINGS_DIR), exist_ok=True)
username = save_username()
refresh_token, access_token, expiration = oauth_process()
save_expiration(expiration)
with open(
path.join(path.expanduser('~'), SETTINGS_DIR, REFRESH_KEY_FILE),
'wb+') as refresh_key_file:
refresh_key_file.write(encrypt(CLIENT_SECRET, refresh_token))
with open(
path.join(path.expanduser('~'), SETTINGS_DIR, ACCESS_KEY_FILE),
'wb+') as access_key_file:
access_key_file.write(encrypt(CLIENT_SECRET, access_token))
if username is None:
try:
username = get_username()
except IOError:
username = save_username()
auth_string = _generate_auth_string(username, access_token)
imap_conn = imaplib.IMAP4_SSL('imap.gmail.com')
imap_conn.authenticate('XOAUTH2', lambda x: auth_string.encode('ascii'))
return imap_conn