Exemple #1
0
def get_api_access(key: str, ip: str) -> Tuple[str, str]:
    """
    Cached API key data

    :param key: API Key value
    :param ip: Client IP
    :return:
    """
    return APIKey.get_name_and_access_str(key, ip)
Exemple #2
0
def test_apikey_access_inactive():
    secret = "test_apikey_access_inactive"
    # Prepare key
    key = APIKey(
        name="key1",
        description="Non-expiring key, Inactive",
        is_active=False,
        key=secret,
        access=[
            APIAccess(api="api1", role="*"),
            APIAccess(api="api2", role="r1"),
            APIAccess(api="api2", role="r2"),
        ],
    )
    key.save()
    # Check
    assert APIKey.get_access(secret) == []
    assert APIKey.get_access_str(secret) == ""
    # Cleanup
    key.delete()
Exemple #3
0
def test_apikey_access_expired():
    secret = "test_apikey_access_non_expired"
    # Prepare key
    key = APIKey(
        name="key1",
        description="Expiring key",
        expires=datetime.datetime.now(),
        is_active=True,
        key=secret,
        access=[
            APIAccess(api="api1", role="*"),
            APIAccess(api="api2", role="r1"),
            APIAccess(api="api2", role="r2"),
        ],
    )
    key.save()
    # Check
    assert APIKey.get_access(secret) == []
    assert APIKey.get_access_str(secret) == ""
    # Cleanup
    key.delete()
Exemple #4
0
def test_apikey_access_non_expired():
    secret = "test_apikey_access_non_expired"
    # Prepare key
    key = APIKey(
        name="key1",
        description="Expiring key",
        expires=datetime.datetime(year=2050, month=1, day=1),
        is_active=True,
        key=secret,
        access=[
            APIAccess(api="api1", role="*"),
            APIAccess(api="api2", role="r1"),
            APIAccess(api="api2", role="r2"),
        ],
    )
    key.save()
    # Check
    assert APIKey.get_access(secret) == [("api1", "*"), ("api2", "r1"), ("api2", "r2")]
    assert APIKey.get_access_str(secret) == "api1:*,api2:r1,api2:r2"
    # Cleanup
    key.delete()
Exemple #5
0
def test_apikey_invalid():
    secret = "no such key"
    assert APIKey.get_access(secret) == []
    assert APIKey.get_access_str(secret) == ""
Exemple #6
0
 def get_api_access(cls, key, ip):
     return APIKey.get_name_and_access_str(key, ip)