def test_jwt_expired_token(app): """Test jwt creation.""" with app.app_context(): # Extra parameters extra = dict(exp=datetime(1970, 1, 1), ) # Create token token = jwt_create_token(user_id=1, additional_data=extra) # Decode with pytest.raises(JWTExpiredToken): jwt_decode_token(token) # Random token with pytest.raises(JWTDecodeError): jwt_decode_token('Roadster SV')
def test_jwt_expired_token(app): """Test jwt creation.""" with app.app_context(): # Extra parameters extra = dict( exp=datetime(1970, 1, 1), ) # Create token token = jwt_create_token(user_id=1, additional_data=extra) # Decode with pytest.raises(JWTExpiredToken): jwt_decode_token(token) # Random token with pytest.raises(JWTDecodeError): jwt_decode_token('Roadster SV')
def jwt_verify_token(headers): """Verify the JWT token. :param dict headers: The request headers. :returns: The token data. :rtype: dict """ # Get the token from headers token = headers.get(current_app.config['OAUTH2SERVER_JWT_AUTH_HEADER']) if token is None: raise JWTInvalidHeaderError # Get authentication type authentication_type = \ current_app.config['OAUTH2SERVER_JWT_AUTH_HEADER_TYPE'] # Check if the type should be checked if authentication_type is not None: # Get the prefix and the token prefix, token = token.split() # Check if the type matches if prefix != authentication_type: raise JWTInvalidHeaderError try: # Get the token data decode = jwt_decode_token(token) # Check the integrity of the user if current_user.get_id() != decode.get('sub'): raise JWTInvalidIssuer return decode except _JWTDecodeError as exc: raise_from(JWTDecodeError(), exc) except _JWTExpiredToken as exc: raise_from(JWTExpiredToken(), exc)
def test_jwt_token(app): """Test jwt creation.""" with app.app_context(): # Extra parameters extra = dict(defenders=['jessica', 'luke', 'danny', 'matt']) # Create token normally token = jwt_create_token(user_id=22, additional_data=extra) decode = jwt_decode_token(token) # Decode assert 'jessica' in decode.get('defenders') assert 22 == decode.get('sub')
def test_jwt_token(app): """Test jwt creation.""" with app.app_context(): # Extra parameters extra = dict(defenders=["jessica", "luke", "danny", "matt"]) # Create token normally token = jwt_create_token(user_id=22, additional_data=extra) decode = jwt_decode_token(token) # Decode assert "jessica" in decode.get("defenders") assert 22 == decode.get("sub")
def test_jwt_token(app): """Test jwt creation.""" with app.app_context(): # Extra parameters extra = dict( defenders=['jessica', 'luke', 'danny', 'matt'] ) # Create token normally token = jwt_create_token(user_id=22, additional_data=extra) decode = jwt_decode_token(token) # Decode assert 'jessica' in decode.get('defenders') assert 22 == decode.get('sub')