Example #1
0
 async def extract_token(self):
     request = self.request
     if 'ws_token' in request.query:
         jwt_token = request.query['ws_token'].encode('utf-8')
         try:
             jwetoken = jwe.JWE()
             jwetoken.deserialize(jwt_token.decode('utf-8'))
             jwetoken.decrypt(get_jwk_key())
             payload = jwetoken.payload
         except jwe.InvalidJWEOperation:
             logger.warn(f'Invalid operation', exc_info=True)
             return
         except jwe.InvalidJWEData:
             logger.warn(f'Error decrypting JWT token', exc_info=True)
             return
         json_payload = json.loads(payload)
         if json_payload['exp'] <= int(time.time()):
             logger.warn(f'Expired token {jwt_token}', exc_info=True)
             return
         data = {
             'type': 'wstoken',
             'token': json_payload['token']
         }
         if 'id' in json_payload:
             data['id'] = json_payload['id']
         return data
Example #2
0
async def generate_validation_token(data, ttl=3660):
    data = data or {}
    claims = {
        "iat": int(time.time()),
        "exp": int(time.time() + ttl),
    }
    claims.update(data)
    payload = orjson.dumps(claims)
    jwetoken = jwe.JWE(payload,
                       json_encode({
                           "alg": "A256KW",
                           "enc": "A256CBC-HS512"
                       }))
    jwetoken.add_recipient(get_jwk_key())
    token = jwetoken.serialize(compact=True)

    last_time = time.time() + ttl
    datetime_format = app_settings.get("datetime_format")
    default_timezone = app_settings.get("default_timezone", "UTC")
    tz = pytz.timezone(default_timezone)

    if datetime_format is None:
        last_date = datetime.fromtimestamp(last_time, tz=tz).isoformat()
    else:
        last_date = datetime.fromtimestamp(last_time,
                                           tz=tz).strftime(datetime_format)
    return token, last_date
Example #3
0
 def generate_websocket_token(self, real_token, data=None):
     data = data or {}
     claims = {
         "iat": int(time.time()),
         "exp": int(time.time() + self._websockets_ttl),
         "token": real_token,
     }
     claims.update(data)
     payload = ujson.dumps(claims)
     jwetoken = jwe.JWE(payload.encode("utf-8"), json_encode({"alg": "A256KW", "enc": "A256CBC-HS512"}))
     jwetoken.add_recipient(get_jwk_key())
     token = jwetoken.serialize(compact=True)
     return token
Example #4
0
 def generate_websocket_token(self, real_token, data={}):
     claims = {
         'iat': int(time.time()),
         'exp': int(time.time() + self._websockets_ttl),
         'token': real_token
     }
     claims.update(data)
     payload = ujson.dumps(claims)
     jwetoken = jwe.JWE(
         payload.encode('utf-8'),
         json_encode({
             "alg": "A256KW",
             "enc": "A256CBC-HS512"}))
     jwetoken.add_recipient(get_jwk_key())
     token = jwetoken.serialize(compact=True)
     return token
Example #5
0
 def generate_websocket_token(self, real_token, data={}):
     claims = {
         'iat': int(time.time()),
         'exp': int(time.time() + self._websockets_ttl),
         'token': real_token
     }
     claims.update(data)
     payload = ujson.dumps(claims)
     jwetoken = jwe.JWE(
         payload.encode('utf-8'),
         json_encode({
             "alg": "A256KW",
             "enc": "A256CBC-HS512"
         }))
     jwetoken.add_recipient(get_jwk_key())
     token = jwetoken.serialize(compact=True)
     return token
Example #6
0
async def extract_validation_token(jwt_token):
    try:
        jwetoken = jwe.JWE()
        jwetoken.deserialize(jwt_token)
        jwetoken.decrypt(get_jwk_key())
        payload = jwetoken.payload
    except jwe.InvalidJWEOperation:
        logger.warn(f"Invalid operation", exc_info=True)
        return
    except jwe.InvalidJWEData:
        logger.warn(f"Error decrypting JWT token", exc_info=True)
        return
    json_payload = ujson.loads(payload)
    if json_payload["exp"] <= int(time.time()):
        logger.warning(f"Expired token {jwt_token}", exc_info=True)
        return
    return json_payload
Example #7
0
async def generate_validation_token(data, ttl=3660):
    data = data or {}
    claims = {
        "iat": int(time.time()),
        "exp": int(time.time() + ttl),
    }
    claims.update(data)
    payload = ujson.dumps(claims)
    jwetoken = jwe.JWE(payload.encode("utf-8"),
                       json_encode({
                           "alg": "A256KW",
                           "enc": "A256CBC-HS512"
                       }))
    jwetoken.add_recipient(get_jwk_key())
    token = jwetoken.serialize(compact=True)

    last_time = time.time() + ttl
    last_date = datetime.fromtimestamp(last_time).isoformat()
    return token, last_date
Example #8
0
 async def extract_token(self):
     request = self.request
     if "ws_token" in request.query:
         jwt_token = request.query["ws_token"].encode("utf-8")
         try:
             jwetoken = jwe.JWE()
             jwetoken.deserialize(jwt_token.decode("utf-8"))
             jwetoken.decrypt(get_jwk_key())
             payload = jwetoken.payload
         except jwe.InvalidJWEOperation:
             logger.warn(f"Invalid operation", exc_info=True)
             return
         except jwe.InvalidJWEData:
             logger.warn(f"Error decrypting JWT token", exc_info=True)
             return
         json_payload = json.loads(payload)
         if json_payload["exp"] <= int(time.time()):
             logger.warning(f"Expired token {jwt_token}", exc_info=True)
             return
         data = {"type": "wstoken", "token": json_payload["token"]}
         if "id" in json_payload:
             data["id"] = json_payload["id"]
         return data
Example #9
0
 async def extract_token(self):
     request = self.request
     if 'ws_token' in request.query:
         jwt_token = request.query['ws_token'].encode('utf-8')
         try:
             jwetoken = jwe.JWE()
             jwetoken.deserialize(jwt_token.decode('utf-8'))
             jwetoken.decrypt(get_jwk_key())
             payload = jwetoken.payload
         except jwe.InvalidJWEOperation:
             logger.warn(f'Invalid operation', exc_info=True)
             return
         except jwe.InvalidJWEData:
             logger.warn(f'Error decrypting JWT token', exc_info=True)
             return
         json_payload = json.loads(payload)
         if json_payload['exp'] <= int(time.time()):
             logger.warn(f'Expired token {jwt_token}', exc_info=True)
             return
         data = {'type': 'wstoken', 'token': json_payload['token']}
         if 'id' in json_payload:
             data['id'] = json_payload['id']
         return data
Example #10
0
def test_warn_about_jwk_secret(loop, caplog):
    with caplog.at_level(logging.WARNING, logger="guillotina"):
        utils.get_jwk_key(settings={"debug": False})
        assert len(caplog.records) == 1
        assert "has been dynamically generated" in caplog.records[0].message