-
Notifications
You must be signed in to change notification settings - Fork 1
/
LWAauth.py
44 lines (34 loc) · 1.28 KB
/
LWAauth.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
# Copyright 2018 Calum Loudon
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not
# use this file except in compliance with the License. A copy of the License
# is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# This file talks to Amazon LWA (the AWS OAuth2 service) to extract the user
# corresponding to an access token.
import os
import json
import pprint
import requests
import urllib
from logutilities import log_info, log_debug, log_error
LWA_PROFILE_URL = 'https://api.amazon.com/user/profile?'
def get_user_from_token(token):
log_debug("Token is %s", token)
url = LWA_PROFILE_URL + urllib.parse.urlencode({ 'access_token' : token })
r = requests.get(url=url)
if r.status_code == 200:
log_debug("Amazon profile returned is:")
log_debug(json.dumps(r.json(), indent=4))
body = r.json()
user = body['user_id']
else:
log_error("Amazon look up returned an error %d", r.status_code)
log_error(json.dumps(r.json(), indent=4))
user = "<unknown>"
return user