forked from SwiftLaTeX/SwiftLaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_auth.py
62 lines (54 loc) · 2.23 KB
/
github_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
import config
import requests
import logging
import utils
import abstract_auth
import traceback
class GithubOAuth(abstract_auth.Auth):
@staticmethod
def get_authorization_url():
random_token = utils.random_string(16)
authorization_base_url = "https://github.com/login/oauth/authorize?client_id=%s&scope=user public_repo&redirect_uri=%s&state=%s" % (
config.GITHUB_CLIENT_ID, config.OAUTH_REDIRECT_URI, random_token)
return authorization_base_url, random_token
@staticmethod
def get_access_token(code):
token_url = "https://github.com/login/oauth/access_token"
headers = {'Accept': 'application/json'}
data = {'code': code, 'grant_type': 'authorization_code', 'client_id': config.GITHUB_CLIENT_ID,
'client_secret': config.GITHUB_CLIENT_SECRET, 'redirect_uri': config.OAUTH_REDIRECT_URI}
try:
r = requests.post(token_url, headers=headers, data=data, timeout=20)
idata = r.json()
return [idata['access_token'], idata['access_token'], idata['access_token']]
except:
traceback.print_exc()
logging.warning("Unable to fetch access token for github!")
return None
@staticmethod
def get_user_login_info(token, userid):
token_url = "https://api.github.com/user"
headers = {'Authorization': 'bearer %s' % token}
userinfo = {}
try:
r = requests.get(token_url, headers=headers, timeout=20)
idata = r.json()
userinfo['name'] = idata['name']
userinfo['email'] = idata['email']
except:
logging.warning("Unable to fetch name for github!")
return None
if not userinfo['email']:
email_url = "https://api.github.com/user/emails"
headers = {'Authorization': 'bearer %s' % token}
try:
r = requests.get(email_url, headers=headers, timeout=20)
idata = r.json()
userinfo['email'] = idata[0]['email']
except:
logging.warning("Unable to fetch email for github!")
return None
return userinfo
@staticmethod
def refresh_token(token):
return token