Esempio n. 1
0
 def wrapper(*args, **kwargs):
     guard = current_guard()
     token = guard.read_token_from_header()
     jwt_data = guard.extract_jwt_token(token)
     add_jwt_data_to_app_context(jwt_data)
     retval = method(*args, **kwargs)
     remove_jwt_data_from_app_context()
     return retval
Esempio n. 2
0
def _verify_and_add_jwt():
    """
    This helper method just checks and adds jwt data to the app context. Will
    not add jwt data if it is already present. Only use in this module
    """
    if not app_context_has_jwt_data():
        guard = current_guard()
        token = guard.read_token_from_header()
        jwt_data = guard.extract_jwt_token(token)
        add_jwt_data_to_app_context(jwt_data)
Esempio n. 3
0
 def wrapper(*args, **kwargs):
     # TODO put the x-api-key and such in the constants and check if is also found as a query parameter!
     token_store_token_id = request.headers.get('x-api-key', "")
     print(token_store_token_id)
     if token_store_token_id:
         print("found da x-api-key")
         token_store_token = current_token(token_store_token_id)
         print("hydrating a JWT for this api_token")
         #token = {"id":1, "token_name":"my_api", "roles":"admin"}
         encoded_jwt = current_guard().encode_jwt_token(token_store_token,
                                                        is_api=True)
         decoded_jwt = current_guard().extract_jwt_token(encoded_jwt)
         # TODO have the model check for it's enabled, not expired and such using the underlying Token_store model
         add_jwt_data_to_app_context(decoded_jwt)
         try:
             return method(*args, **kwargs)
         finally:
             remove_jwt_data_from_app_context()
     else:
         _verify_and_add_jwt(optional=True)
         try:
             return method(*args, **kwargs)
         finally:
             remove_jwt_data_from_app_context()
Esempio n. 4
0
 def wrapper(*args, **kwargs):
     PraetorianError.require_condition(
         not current_guard().roles_disabled,
         "This feature is not available because roles are disabled",
     )
     role_set = set([str(n) for n in required_rolenames])
     _verify_and_add_jwt()
     try:
         MissingRoleError.require_condition(
             current_rolenames().issuperset(role_set),
             "This endpoint requires all the following roles: "
             "{}".format([", ".join(role_set)]),
         )
         return method(*args, **kwargs)
     finally:
         remove_jwt_data_from_app_context()
Esempio n. 5
0
def _verify_and_add_jwt(optional=False):
    """
    This helper method just checks and adds jwt data to the app context.
    If optional is False and the header is missing the token, just returns.

    Will not add jwt data if it is already present.

    Only use in this module
    """
    if not app_context_has_jwt_data():
        guard = current_guard()
        try:
            token = guard.read_token_from_header()
        except MissingToken as err:
            if optional:
                return
            raise err
        jwt_data = guard.extract_jwt_token(token)
        add_jwt_data_to_app_context(jwt_data)