-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservices_torp.py
More file actions
189 lines (146 loc) · 6.66 KB
/
services_torp.py
File metadata and controls
189 lines (146 loc) · 6.66 KB
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
from flask import render_template, Blueprint, url_for, redirect, request, session, flash
from flask.ext.basicauth import BasicAuth
from multiprocessing import Pool, Process
from preston.esi import Preston as ESIPreston
from urllib.parse import urlparse
import requests
import uuid
from services_util import *
from services_slack import *
names_url = 'https://esi.tech.ccp.is/latest/universe/names/?datasource=tranquility'
services_torp= Blueprint('services_torp', __name__)
config = yaml.load(open('services.conf', 'r'))
crest_config = yaml.load(open('crest_config.conf', 'r'))
create_crest_config = crest_config['torp_create']
join_crest_config = crest_config['torp_join']
create_preston = ESIPreston(
user_agent=create_crest_config['EVE_OAUTH_USER_AGENT'],
client_id=create_crest_config['EVE_OAUTH_CLIENT_ID'],
client_secret=create_crest_config['EVE_OAUTH_SECRET'],
callback_url=create_crest_config['EVE_OAUTH_CALLBACK'],
scope=create_crest_config['EVE_OAUTH_SCOPE']
)
join_preston = ESIPreston(
user_agent=join_crest_config['EVE_OAUTH_USER_AGENT'],
client_id=join_crest_config['EVE_OAUTH_CLIENT_ID'],
client_secret=join_crest_config['EVE_OAUTH_SECRET'],
callback_url=join_crest_config['EVE_OAUTH_CALLBACK'],
scope=join_crest_config['EVE_OAUTH_SCOPE']
)
SLACK_NPSI_TOKEN = config['SLACK_NPSI_TOKEN']
@services_torp.route('/npsi', methods = ['GET', 'POST'])
@services_torp.route('/npsi/', methods = ['GET', 'POST'])
def send_to_torpfleet():
return redirect(url_for('npsi'))
@services_torp.route('/torpfleet', methods = ['GET', 'POST'])
@services_torp.route('/torpfleet/', methods = ['GET', 'POST'])
def npsi():
"""
Form for sending Slack invites for the WDS BLOPS/NPSI community
"""
if request.method == 'GET':
return render_template('services-npsi-landing.html')
elif request.method == 'POST':
email = request.form['email']
name = request.form['name']
slack_invite = invite_to_slack(email=email, name=name, token=SLACK_NPSI_TOKEN)
return render_template('services-npsi-success.html')
@services_torp.route('/torp/createfleet/', methods = ['GET','POST'])
def create_fleet():
if request.method == 'GET':
return render_template('services-npsi-createfleet.html', showcrest=True, crest_url=create_preston.get_authorize_url())
elif request.method == 'POST':
#grab form data
fleeturl = request.form['fleeturl']
session_id = session['id']
if fleeturl == None or session_id == None:
return redirect(url_for('services_torp.create_fleet'))
#parse out the fleetid from fleeturl
fleetid = urlparse(fleeturl).path.split("/")[2]
#update the db record
update_query = insert_db('UPDATE fleets set fleetid=? where sessionid=? ', [fleetid, session_id])
#grab the fleetid
fid = dict(query_db('SELECT id FROM fleets where sessionid=?', [session_id], True))['id']
if fid == None:
return redirect(url_for('services_torp.create_fleet'))
return render_template('services-npsi-createfleet.html', showcrest=False, fid=fid )
@services_torp.route('/torp/createfleet/callback')
def eve_create_oauth_callback():
if 'error' in request.path:
flash('There was an error in EVE\'s response', 'error')
return redirect(url_for('services_torp.create_fleet'))
try:
auth = create_preston.authenticate(request.args['code'])
except Exception as e:
print('SSO callback exception: ' + str(e))
flash('There was an authentication error signing you in.', 'error')
return redirect(url_for('services_torp.create_fleet'))
pilot_info = auth.whoami()
pilot_name = pilot_info['CharacterName']
refresh_token = auth.refresh_token
#do some sessionid bullshit
session_id = uuid.uuid4().urn[9:]
session['id'] = session_id
#check that the pilot is in our list of FC's
#create a DB entry for this FC
insert_query = insert_db('INSERT INTO fleets '
'(name, token, fleetid, dateadded, sessionid) '
'VALUES (?, ?, "0", datetime(), ?)',
[pilot_name, refresh_token, session_id])
return render_template('services-npsi-createfleet.html', showcrest=False)
@services_torp.route('/torp/joinfleet', methods = ['GET'])
@services_torp.route('/torp/joinfleet/', methods = ['GET'])
@services_torp.route('/torp/joinfleet/<int:fid>', methods = ['GET'])
def join_fleet(fid):
if fid == None:
flash('No fleet specified.', 'error')
return render_template('services-npsi-error.html')
session['id'] = fid
#lookup the fleetid, make sure it's valid
fleet_query = query_db('select fleetid, token from fleets where id=?', [fid], True)
if len(fleet_query) == 0:
flash('There is no active fleet with the specified ID.', 'error')
return render_template('services-npsi-error.html')
fleet = dict(fleet_query)
#grab the fleetinfo from ESI
refresh_token = fleet['token']
fleetid = fleet['fleetid']
if fleetid == 0:
flash('There is no active fleet with the specified ID.', 'error')
return render_template('services-npsi-error.html')
#check that the fleet is still good
fcauth = create_preston.use_refresh_token(refresh_token)
result = fcauth.fleets[fleetid]
#if result.get('error') is not None:
# print(4)
# flash('This fleet is no longer available.', 'error')
# return render_template('services-npsi-error.html')
return render_template('services-npsi-joinfleet.html', showcrest=True, crest_url=join_preston.get_authorize_url())
@services_torp.route('/torp/joinfleet/callback')
def eve_join_oauth_callback():
if 'error' in request.path:
flash('There was an error in EVE\'s response', 'error')
return url_for('services_torp.fleet_landing')
try:
auth = join_preston.authenticate(request.args['code'])
except Exception as e:
print('SSO callback exception: ' + str(e))
flash('There was an authentication error signing you in.', 'error')
return redirect(url_for('services_torp.fleet_landing'))
pilot_info = auth.whoami()
pilot_name = pilot_info['CharacterName']
pilot_id = pilot_info['CharacterID']
fid = session['id']
#lookup the fleetid in db
fleet = dict(query_db('select fleetid, token from fleets where id=?', [fid], True))
fleetid = fleet['fleetid']
refresh_token = fleet['token']
#grab the FC's token
fcauth = create_preston.use_refresh_token(refresh_token)
access_token = fcauth.access_token
#send an invite
data = { "character_id": pilot_id, "role": "squad_member" }
uri = 'https://esi.tech.ccp.is/latest/fleets/{fid}/members/?token={token}'.format(fid = fleetid, token=access_token)
r = requests.post(uri,json=data)
print(r.text)
return render_template('services-npsi-joinfleet.html', showcrest=False)