Created
          March 1, 2017 09:11 
        
      - 
      
- 
        Save tiberiuichim/43f5046ac7e453b3ffb41db665cdb72b to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from datetime import datetime | |
| from kinto.core import Service | |
| import requests | |
| def fb_authenticate(user_id, access_token): | |
| url = "https://graph.facebook.com/v2.8/me" | |
| data = { | |
| 'fields': 'id,name', | |
| 'access_token': access_token | |
| } | |
| resp = requests.get(url, params=data) | |
| fbdata = resp.json() | |
| if fbdata['id'] == user_id: | |
| return user_id | |
| token_exchange = Service( | |
| name='token_exchange', | |
| path='/token-exchange', | |
| description='FB to JWT token exchange and login.', | |
| ) | |
| @token_exchange.post() | |
| def exchange(request): | |
| d = request.json_body | |
| user_id = d['user_id'] | |
| access_token = d['access_token'] | |
| user_id = fb_authenticate(user_id, access_token) | |
| if user_id: | |
| token = request.create_jwt_token(user_id) | |
| delta = int(request.registry.settings['jwt.expiration']) | |
| expires = int(datetime.now().timestamp()) + delta | |
| res = { | |
| 'result': 'ok', | |
| 'token': token, | |
| 'expires': expires | |
| } | |
| return res | |
| return { | |
| 'result': 'error' | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment