-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
339 lines (280 loc) · 13.1 KB
/
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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from datetime import datetime, timedelta
import os
import jwt
import json
import requests
from functools import wraps
from flask import g, request, jsonify
from jwt import DecodeError, ExpiredSignature
from settings import SLACK_SECRET, TOKEN_SECRET
from db import session
import classes as cls
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
authHeader = request.headers.get('x-access-token')
appname = request.headers.get('appname')
print 'appname' + str(appname)
print 'authHeader' + str(authHeader)
if not appname :
print 'comes here'
if not authHeader:
response = jsonify(message='Missing authorization header:' + str(authHeader))
response.status_code = 401
return response
try:
print "Inside login_required"
payload = parse_token(request)
except DecodeError:
response = jsonify(message='Token is invalid')
response.status_code = 401
return response
except ExpiredSignature:
response = jsonify(message='Token has expired')
response.status_code = 401
return response
g.user_id = payload['sub']
g.slackTeamId = payload['slackTeamId']
g.slackTeamName = payload['slackTeamName']
g.slackAccessToken = payload['slackAccessToken'];
g.slackUserId = payload['slackUserId']
return f(*args, **kwargs)
else :
print 'comes here1'
if appname != 'stig' :
print 'comes here2'
response = jsonify(message='App Name is not valid:' + str(appname))
response.status_code = 401
return response
else:
if not authHeader:
print 'comes here3'
response = jsonify(message='Missing authorization header:')
response.status_code = 401
return response
elif authHeader != 'stigToken$123' :
print 'comes here4'
response = jsonify(message='Token is invalid')
response.status_code = 401
return response
else :
print 'comes here5'
return f(*args, **kwargs)
return decorated_function
# API - ME :
def me():
if(not g.user_id):
print 'User Not Logged In.', 404
return 'User Not Logged In.', 404
user = session.query(cls.User).filter(cls.User.id == g.user_id).first()
if(not user):
print 'User Not Logged In.', 404
return 'User Not Logged In.', 404
return jsonify(dict(imgUrl=user.imgUrl, displayName=user.name, user_realname=user.real_name, userId=user.id, slackTeamId=g.slackTeamId, slackTeamName=g.slackTeamName, slackAccessToken=g.slackAccessToken, slackUserId=g.slackUserId))
def create_token(user, slackTeamId, slackTeamName, slackAccessToken, slackUserId):
payload = {
'sub': user.id,
'slackTeamId' : slackTeamId,
'slackTeamName' : slackTeamName,
'slackAccessToken' : slackAccessToken,
'slackUserId' : slackUserId,
# 'iat': datetime.now(),
'exp': datetime.now() + timedelta(days=14)
}
token = jwt.encode(payload, TOKEN_SECRET)
return token.decode('unicode_escape')
def parse_token(req):
authHeader = req.headers.get('x-access-token')
print 'parse_token: authHeader:' + str(authHeader)
token = authHeader.split()[1]
return jwt.decode(token, TOKEN_SECRET)
def login():
user = User.query.filter_by(email=request.json['email']).first()
if not user or not user.check_password(request.json['password']):
response = jsonify(message='Wrong Email or Password')
response.status_code = 401
return response
token = create_token(user)
return jsonify(token=token)
def signup():
user = User(email=request.json['email'], password=request.json['password'])
db.session.add(user)
db.session.commit()
token = create_token(user)
return jsonify(token=token)
# Services Auth Routes:
def ext_login():
users_api_url = 'https://slack.com/api/auth.test'
params = {
'slackAccessToken': request.form['token'],
}
slackAccessToken = params["slackAccessToken"]
headers = {'User-Agent': 'DEAP'}
print 'slackAccessToken:' + str(slackAccessToken)
# Step 2. Retrieve information about the current user.
r = requests.get(users_api_url, params={'token':slackAccessToken}, headers=headers)
profile = json.loads(r.text)
print 'slack profile:' + str(profile)
# Step 3. (optional) Link accounts.
if request.headers.get('x-access-token'):
# user = User.query.filter_by(slack=profile['user_id']).first()
user = session.query(cls.User).filter(cls.User.name == profile['user']).first()
if user:
response = jsonify(message='There is already a Slack account that belongs to you')
response.status_code = 409
return response
payload = parse_token(request)
# user = User.query.filter_by(id=payload['sub']).first()
user = session.query(cls.User).filter(cls.User.id == payload['sub']).first()
if not user:
response = jsonify(message='User not found')
response.status_code = 400
return response
u = User(name=profile['user'])
org = session.query(cls.Organization).filter(cls.Organization.slack_teamid == profile['team_id']).first()
orgexists = "true"
if not org:
orgexists = "false"
else:
userOrg = session.query(cls.UserOrganization).filter(cls.UserOrganization.org_id == org.id).filter(cls.UserOrganization.user_id == user.id).first()
if not userOrg:
# associate user and org
jsonStr = {"user_id":user.id,"organization_id": org.id,"org_tokens":0,"org_reputation":0}
userOrg = cls.UserOrganization(jsonStr, session)
session.add(userOrg)
session.commit()
session.add(u)
session.commit()
token = create_token(u, profile['team_id'], profile['team'], orgexists)
return jsonify(token=token)
user = session.query(cls.User).filter(cls.User.slackId == profile['user_id']).first()
orgs = session.query(cls.Organization).filter(cls.Organization.slack_teamid == profile['team_id']).all()
orgChannelId = ''
count = 1
# getting all orgs from current slack team
for org in orgs:
# sync the extension db with all the members currently exists in slack team
syncUsers(org.id, slackAccessToken)
if count == 1:
orgChannelId = org.channelId
else:
orgChannelId = orgChannelId + ',' + org.channelId
count = count + 1;
print 'orgChannelId is' + str(orgChannelId);
if user:
token = create_token(user, profile['team_id'], profile['team'], slackAccessToken, profile['user_id'])
return jsonify(token=token, orgChannelId=orgChannelId)
# getting additional info about user
userInfo = requests.get('https://slack.com/api/users.info', params={'token':slackAccessToken, 'user':profile['user_id']}, headers=headers)
userData = json.loads(userInfo.text)['user']
# create user in DB if this is the first time login into slack extension
jsonStr = {"slackId":profile['user_id'], "name":profile['user'], "real_name":userData['profile']['real_name'], "imgUrl":userData['profile']['image_48'], "imgUrl72":userData['profile']['image_72']}
u = cls.User(jsonStr, session)
session.add(u)
session.commit()
token = create_token(u, profile['team_id'], profile['team'], slackAccessToken, profile['user_id'])
return jsonify(token=token, orgChannelId=orgChannelId)
# Services Auth Routes:
def slack():
access_token_url = 'https://slack.com/api/oauth.access'
users_api_url = 'https://slack.com/api/auth.test'
params = {
'client_id': request.json['clientId'],
'redirect_uri': request.json['redirectUri'],
'client_secret': SLACK_SECRET,
'code': request.json['code']
}
# Step 1. Exchange authorization code for access token.
r = requests.get(access_token_url, params=params)
print 'r.text:' + str(r.text)
response = json.loads(r.text)
print str(response)
slackAccessToken = response["slackAccessToken"]
headers = {'User-Agent': 'DEAP'}
print 'slackAccessToken:' + str(slackAccessToken)
# Step 2. Retrieve information about the current user.
r = requests.get(users_api_url, params={'token':slackAccessToken}, headers=headers)
profile = json.loads(r.text)
print 'slack profile:' + str(profile)
# Step 3. (optional) Link accounts.
if request.headers.get('x-access-token'):
user = session.query(cls.User).filter(cls.User.name == profile['user']).first()
if user:
response = jsonify(message='There is already a Slack account that belongs to you')
response.status_code = 409
return response
payload = parse_token(request)
# user = User.query.filter_by(id=payload['sub']).first()
user = session.query(cls.User).filter(cls.User.id == payload['sub']).first()
if not user:
response = jsonify(message='User not found')
response.status_code = 400
return response
u = User(name=profile['user'])
org = session.query(cls.Organization).filter(cls.Organization.slack_teamid == profile['team_id']).first()
orgexists = "true"
if not org:
orgexists = "false"
else:
userOrg = session.query(cls.UserOrganization).filter(cls.UserOrganization.org_id == org.id).filter(cls.UserOrganization.user_id == user.id).first()
if not userOrg:
# associate user and org
jsonStr = {"user_id":user.id,"organization_id": org.id,"org_tokens":0,"org_reputation":0}
userOrg = cls.UserOrganization(jsonStr, session)
session.add(userOrg)
session.commit()
session.add(u)
session.commit()
token = create_token(u, profile['team_id'], profile['team'], orgexists)
return jsonify(token=token)
user = session.query(cls.User).filter(cls.User.slackId == profile['user_id']).first()
orgs = session.query(cls.Organization).filter(cls.Organization.slack_teamid == profile['team_id']).all()
for org in orgs:
syncUsers(org.id, slackAccessToken)
if user:
token = create_token(user, profile['team_id'], profile['team'], slackAccessToken, profile['user_id'])
return jsonify(token=token)
jsonStr = {"slackId":profile['user_id'], "name":profile['user']}
u = cls.User(jsonStr, session)
session.add(u)
session.commit()
token = create_token(u, profile['team_id'], profile['team'], slackAccessToken, profile['user_id'])
return jsonify(token=token)
def syncUsers(orgId, slackAccessToken):
# get all the db users
usersInSystem = session.query(cls.User).all()
usersInSystem.sort(key=lambda x: x.id, reverse=True)
# get al user organizations in this org
userOrgs = session.query(cls.UserOrganization).filter(cls.UserOrganization.organization_id == orgId)
usersOrgDic = {}
for userOrg in userOrgs:
usersOrgDic[userOrg.user_id] = userOrg.id
# get all slack users
team_users_api_url = 'https://slack.com/api/users.list'
headers = {'User-Agent': 'DEAP'}
r = requests.get(team_users_api_url, params={'token':slackAccessToken}, headers=headers)
slackUsers = json.loads(r.text)['members']
usersDic = {}
usersDic1 = {}
for user in usersInSystem:
usersDic[user.slackId] = user.id
for slackUser in slackUsers :
if slackUser['deleted'] == True :
continue
if slackUser['is_bot'] == True :
continue
try:
userId = usersDic[slackUser['id']]
except KeyError:
jsonStr = {"slackId":slackUser['id'], "name":slackUser['name'], "imgUrl":slackUser['profile']['image_48'], "imgUrl72":slackUser['profile']['image_72'], "real_name":slackUser['profile']['real_name']}
u = cls.User(jsonStr, session)
session.add(u)
session.flush()
userId = u.id
userOrg = session.query(cls.UserOrganization).filter(cls.UserOrganization.organization_id == orgId).filter(cls.UserOrganization.user_id == userId).first()
# associate user and org if there are not associated earlier
if not userOrg:
jsonStr = {"user_id":userId,"organization_id": orgId,"org_tokens":0,"org_reputation":0}
userOrg = cls.UserOrganization(jsonStr, session)
session.add(userOrg)
session.commit()