Ejemplo n.º 1
0
def test_authenticate_valid(rf, monkeypatch, cognito_well_known_keys,
                            jwk_private_key_one):
    token = create_jwt_token(
        jwk_private_key_one,
        {
            "iss": "https://cognito-idp.eu-central-1.amazonaws.com/bla",
            "aud": settings.COGNITO_AUDIENCE,
            "sub": "username",
        },
    )

    def func(payload):
        return USER_MODEL(username=payload["sub"])

    monkeypatch.setattr(USER_MODEL.objects,
                        "get_or_create_for_cognito",
                        func,
                        raising=False)

    request = rf.get("/", HTTP_AUTHORIZATION=b"bearer %s" % token)
    auth = backend.JSONWebTokenAuthentication()
    user, auth_token = auth.authenticate(request)
    assert user
    assert user.username == "username"
    assert auth_token == token
Ejemplo n.º 2
0
def test_authenticate_invalid(rf, cognito_well_known_keys,
                              jwk_private_key_two):
    token = create_jwt_token(
        jwk_private_key_two, {
            'iss': 'https://cognito-idp.eu-central-1.amazonaws.com/bla',
            'aud': settings.COGNITO_AUDIENCE,
            'sub': 'username',
        })

    request = rf.get('/', HTTP_AUTHORIZATION=b'bearer %s' % token)
    auth = backend.JSONWebTokenAuthentication()

    with pytest.raises(AuthenticationFailed):
        auth.authenticate(request)
Ejemplo n.º 3
0
def test_authenticate_invalid(rf, cognito_well_known_keys,
                              jwk_private_key_two):
    token = create_jwt_token(
        jwk_private_key_two,
        {
            "iss": "https://cognito-idp.eu-central-1.amazonaws.com/bla",
            "aud": settings.COGNITO_AUDIENCE,
            "sub": "username",
        },
    )

    request = rf.get("/", HTTP_AUTHORIZATION=b"bearer %s" % token)
    auth = backend.JSONWebTokenAuthentication()

    with pytest.raises(AuthenticationFailed):
        auth.authenticate(request)
Ejemplo n.º 4
0
def test_authenticate_valid(rf, monkeypatch, cognito_well_known_keys, jwk_private_key_one):
    token = create_jwt_token(
        jwk_private_key_one,
        {
            'iss': 'https://cognito-idp.eu-central-1.amazonaws.com/bla',
            'aud': settings.COGNITO_AUDIENCE,
            'sub': 'username',
        })

    def func(payload):
        return USER_MODEL(
            username=payload['sub'])

    monkeypatch.setattr(
        USER_MODEL.objects, 'get_or_create_for_cognito', func, raising=False)

    request = rf.get('/', HTTP_AUTHORIZATION=b'bearer %s' % token)
    auth = backend.JSONWebTokenAuthentication()
    user, auth_token = auth.authenticate(request)
    assert user
    assert user.username == 'username'
    assert auth_token == token
Ejemplo n.º 5
0
def test_authenticate_error_spaces(rf):
    request = rf.get('/', HTTP_AUTHORIZATION=b'bearer random iets')
    auth = backend.JSONWebTokenAuthentication()

    with pytest.raises(AuthenticationFailed):
        auth.authenticate(request)
Ejemplo n.º 6
0
def test_authenticate_error_invalid_header(rf):
    request = rf.get('/', HTTP_AUTHORIZATION=b'bearer')
    auth = backend.JSONWebTokenAuthentication()

    with pytest.raises(AuthenticationFailed):
        auth.authenticate(request)
Ejemplo n.º 7
0
def test_authenticate_no_token(rf):
    request = rf.get('/')
    auth = backend.JSONWebTokenAuthentication()
    assert auth.authenticate(request) is None