Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove jwcrypto #624

Merged
merged 3 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions benefits/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from django.db import models
from django.urls import reverse

from jwcrypto import jwk


logger = logging.getLogger(__name__)

Expand All @@ -22,12 +20,6 @@ class PemData(models.Model):
def __str__(self):
return self.label

@property
def jwk(self):
"""jwcrypto.jwk.JWK instance from this PemData."""
pem_bytes = bytes(self.text, "utf-8")
return jwk.JWK.from_pem(pem_bytes)


class AuthProvider(models.Model):
"""An entity that provides authentication for eligibility verifiers."""
Expand Down Expand Up @@ -100,9 +92,9 @@ def __str__(self):
return self.name

@property
def public_jwk(self):
"""jwcrypto.jwk.JWK instance of this Verifier's public key"""
return self.public_key.jwk
def public_key_data(self):
"""This Verifier's public key as a string."""
return self.public_key.text

@property
def requires_authentication(self):
Expand Down Expand Up @@ -189,9 +181,9 @@ def index_url(self):
return reverse("core:agency_index", args=[self.slug])

@property
def private_jwk(self):
"""jwcrypto.jwk.JWK instance of this Agency's private key"""
return self.private_key.jwk
def private_key_data(self):
"""This Agency's private key as a string."""
return self.private_key.text

@staticmethod
def by_id(id):
Expand Down
4 changes: 2 additions & 2 deletions benefits/eligibility/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ def _verify(request, form):
issuer=settings.ALLOWED_HOSTS[0],
agency=agency.agency_id,
jws_signing_alg=agency.jws_signing_alg,
client_private_jwk=agency.private_jwk,
client_private_key=agency.private_key_data,
jwe_encryption_alg=verifier.jwe_encryption_alg,
jwe_cek_enc=verifier.jwe_cek_enc,
server_public_jwk=verifier.public_jwk,
server_public_key=verifier.public_key_data,
)

# get the eligibility type names
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Authlib==1.0.1
cryptography==37.0.2
Django==3.2.13
django-csp==3.7
git+https://github.com/cal-itp/eligibility-api#egg=eligibility_api
gunicorn==20.1.0
jwcrypto==1.3
requests==2.27.1
six==1.16.0
13 changes: 6 additions & 7 deletions tests/pytest/eligibility/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import uuid

from pathlib import Path
from jwcrypto import jwk

from benefits.core import session
from benefits.core.models import TransitAgency
Expand Down Expand Up @@ -88,10 +87,10 @@ def test_confirm_success(mocker, rf):
"eligibility": ["type1"],
},
verifier.jws_signing_alg,
_get_jwk("server.key"),
_get_key("server.key"),
verifier.jwe_encryption_alg,
verifier.jwe_cek_enc,
_get_jwk("client.pub"),
_get_key("client.pub"),
),
)

Expand All @@ -108,12 +107,12 @@ def test_confirm_success(mocker, rf):
assert response.url == reverse("enrollment:index")


def _get_jwk(filename):
def _get_key(filename):
current_path = Path(os.path.dirname(os.path.realpath(__file__)))
file_path = current_path / "keys" / filename

with file_path.open(mode="rb") as pemfile:
key = jwk.JWK.from_pem(pemfile.read())
key = str(pemfile.read(), "utf-8")

return key

Expand Down Expand Up @@ -190,10 +189,10 @@ def _tokenize_response_error_scenarios():
"eligibility": ["type1"],
},
"RS512", # signing algorithm that doesn't match verifier.jws_signing_alg
_get_jwk("server.key"),
_get_key("server.key"),
verifier.jwe_encryption_alg,
verifier.jwe_cek_enc,
_get_jwk("client.pub"),
_get_key("client.pub"),
),
id='TokenError("JWS token signature verification failed")',
),
Expand Down