-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_mgmt.py
213 lines (182 loc) · 7.66 KB
/
github_mgmt.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import re
import json
import sys
import time
import requests
import pyotp
from bs4 import BeautifulSoup
class Github_mgmt(object):
def __init__(self, **kwargs):
self.url = 'https://github.com'
self.api_url = 'https://api.github.com'
self.github_username = kwargs['github_username']
self.github_password = kwargs['github_password']
self.github_otp_secret = kwargs['github_otp_secret']
def get_otp(self, secret):
return pyotp.TOTP(secret).now()
def get_authorizations(self):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
return s.get(self.api_url + '/authorizations').json()
except Exception as e:
raise Exception('Unable to retrieve authorizations')
def get_authorization_by_id(self, authorization_id):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
return s.get(self.api_url + '/authorizations/' +
str(authorization_id)).json()
except Exception as e:
raise Exception('Unable to retrieve authorization id: {0}'.format(
authorization_id))
def create_authorization(self, payload):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
return s.post(self.api_url + '/authorizations',
json=payload).json()
except Exception as e:
raise Exception('Unable to create authorization')
def delete_authorization(self, authorization_id):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
s.delete(self.api_url + '/authorizations/' + str(authorization_id))
except Exception as e:
raise Exception('Unable to remove authorization id: {0}'.format(
authorization_id))
def get_repo_invitations(self):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
return s.get(self.api_url + '/user/repository_invitations').json()
except Exception as e:
raise Exception('Unable to retrieve user repository invitations')
def accept_repo_invitation(self, invitation_id):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
return s.patch(self.api_url + '/user/repository_invitations/' +
str(invitation_id))
except Exception as e:
raise Exception(
'Unable to accept user repository invitation id: '.format(
invitation_id))
def decline_repo_invitation(self, invitation_id):
try:
s = requests.Session()
s.auth = (self.github_username, self.github_password)
headers = {
'X-GitHub-OTP': self.get_otp(self.github_otp_secret),
'Content-type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
s.headers.update(headers)
return s.delete(self.api_url + '/user/repository_invitations/' +
str(invitation_id))
except Exception as e:
raise Exception(
'Unable to decline user repository invitation id: '.format(
invitation_id))
def rotate_password(self, new_password):
try:
s = requests.Session()
response = s.get(
self.url + '/login?' +
'return_to=https%3A%2F%2Fgithub.com%2Fsettings%2Fadmin'
).content
soup = BeautifulSoup(response, features='html.parser')
token = soup.find('input', {
'name': 'authenticity_token'
}).get('value')
headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Accept': 'text/html',
'Connection': 'keep-alive'
}
s.headers.update(headers)
data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': token,
'login': self.github_username,
'password': self.github_password
}
login_response = s.post(self.url + '/session', data=data).content
soup = BeautifulSoup(login_response, features='html.parser')
token = soup.find('input', {
'name': 'authenticity_token'
}).get('value')
data = {
'utf8': '✓',
'authenticity_token': token,
'otp': self.get_otp(self.github_otp_secret)
}
otp_response = s.post(self.url + '/sessions/two-factor',
data=data).content
soup = BeautifulSoup(otp_response, features='html.parser')
pwd_form = soup.find('form', {'class': 'edit_user'})
token = pwd_form.find('input', {
'name': 'authenticity_token'
}).get('value')
data = {
'utf8': '✓',
'_method': 'put',
'authenticity_token': token,
'session_revoked': 'false',
'user[old_password]': self.github_password,
'user[password]': new_password,
'user[password_confirmation]': new_password
}
change_pwd_response = s.post(self.url + '/account',
data=data).content
soup = BeautifulSoup(change_pwd_response, features='html.parser')
except:
pass
try:
soup = BeautifulSoup(change_pwd_response, features='html.parser')
except NameError:
raise Exception(
'Failed to change password - Possibly wrong password')
try:
if not re.search('Password changed successfully', soup.getText()):
raise Exception('Failed to change password - Unknown error')
except Exception as e:
raise Exception(e)