forked from favoritemedium/sso-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sso.py
50 lines (39 loc) · 1.17 KB
/
sso.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
from __future__ import print_function
import re
import time
import datetime
import json
import Cookie
import boto3
from boto3.dynamodb.conditions import Key, Attr
def sso(event, context):
User = boto3.resource('dynamodb').Table('FmSsoUsers')
params = event['params']
atoken_query = params['querystring'].get('atoken', None)
cookie_str = params['header'].get('Cookie', None)
found = False
atoken = None
if atoken_query is not None:
atoken = atoken_query
elif cookie_str is not None:
cookie = Cookie.SimpleCookie()
cookie.load(cookie_str.encode('utf-8'))
if 'atoken' in cookie:
atoken = cookie['atoken'].value
if atoken is not None:
found, user = find_user_by_atoken(User, atoken)
if (not found) or (atoken is None):
raise Exception('Unauthorized')
if user:
# TODO: Refresh token
pass
return {
'access_token': user['atoken'],
'refresh_token': user['rtoken'],
}
def find_user_by_atoken(User, atoken):
user = User.scan(
FilterExpression=Attr('atoken').eq(atoken)
)
found = (user['Count'] != 0)
return found, user['Items'][0]