from functools import wraps
from flask import request, abort
import sys
import jwt
from api import log


def requires_auth(func):
    @wraps(func)
    def func_wrapper(*args, **kwargs):

        # decode the session cookie
        try:
            # look for the session token on the cookies or a header
            session_id = request.cookies.get('session_id') or request.headers.get('sessionid')

            log.info(session_id)

            # TODO: read secret from configuration
            session_id = jwt.decode(session_id, 'secret', algorithms=['HS256'])
            # TODO: Validate the claims and token expiration
        except:
            log.error('unable to decode session')
            log.error(sys.exc_info()[0])
            abort(401)

        return func(*args, **kwargs)
    return func_wrapper

@@ -53,16 +53,19 @@
 t, abort
+, g
 %0Aimport 
@@ -561,16 +561,110 @@
 S256'%5D)%0A
+%0A            # add it to the request app context%0A            g.userid = session_id%5B%22userid%22%5D%0A%0A
         

add userid to the request app context