Skip to content

Commit

Permalink
Update project for new package versions
Browse files Browse the repository at this point in the history
Add app_name to files. New django versions demand this pattern.
Fix credential authentication in backends.
Change gunicorn conf file extension to .py and references in Dockerfile.
  • Loading branch information
sebastian-correa committed Dec 3, 2021
1 parent 40bc1c5 commit 28b016e
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/conduit/apps/articles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
router = DefaultRouter()
router.register(r'articles', ArticleViewSet)

app_name = "articles"
urlpatterns = [
url(r'^articles/feed/?$', ArticlesFeedAPIView.as_view()),

Expand Down
6 changes: 4 additions & 2 deletions backend/conduit/apps/authentication/backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jwt

from django.conf import settings
from django.utils.encoding import force_str

from rest_framework import authentication, exceptions

Expand Down Expand Up @@ -74,9 +75,10 @@ def _authenticate_credentials(self, request, token):
Try to authenticate the given credentials. If authentication is
successful, return the user and token. If not, throw an error.
"""
sec = force_str(settings.SECRET_KEY, encoding="utf-8")
try:
payload = jwt.decode(token, settings.SECRET_KEY)
except:
payload = jwt.decode(token, sec, algorithms=['HS256'])
except jwt.exceptions.DecodeError:
msg = 'Invalid authentication. Could not decode token.'
raise exceptions.AuthenticationFailed(msg)

Expand Down
7 changes: 5 additions & 2 deletions backend/conduit/apps/authentication/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils import encoding
import jwt

from datetime import datetime, timedelta
Expand All @@ -7,6 +8,7 @@
AbstractBaseUser, BaseUserManager, PermissionsMixin
)
from django.db import models
from django.utils.encoding import force_str

from conduit.apps.core.models import TimestampedModel

Expand Down Expand Up @@ -131,9 +133,10 @@ def _generate_jwt_token(self):
"""
dt = datetime.now() + timedelta(days=60)

sec = force_str(settings.SECRET_KEY, encoding="utf-8")
token = jwt.encode({
'id': self.pk,
'exp': int(dt.strftime('%s'))
}, settings.SECRET_KEY, algorithm='HS256')
}, sec, algorithm='HS256')

return token.decode('utf-8')
return token
1 change: 1 addition & 0 deletions backend/conduit/apps/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView
)

app_name = "authentication"
urlpatterns = [
url(r'^user/?$', UserRetrieveUpdateAPIView.as_view()),
url(r'^users/?$', RegistrationAPIView.as_view()),
Expand Down
1 change: 1 addition & 0 deletions backend/conduit/apps/profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .views import ProfileRetrieveAPIView, ProfileFollowAPIView

app_name = "profiles"
urlpatterns = [
url(r'^profiles/(?P<username>\w+)/?$', ProfileRetrieveAPIView.as_view()),
url(r'^profiles/(?P<username>\w+)/follow/?$',
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/aws/codedeploy/start_server
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export AWS_DEFAULT_REGION
DJANGO_SETTINGS_MODULE='conduit.settings.ec2'
export DJANGO_SETTINGS_MODULE

pipenv run gunicorn --config ../infrastructure/aws/codedeploy/gunicorn.ec2.conf conduit.wsgi
pipenv run gunicorn --config ../infrastructure/aws/codedeploy/gunicorn.ec2.conf.py conduit.wsgi
6 changes: 3 additions & 3 deletions infrastructure/docker/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ COPY backend/Pipfile.lock /app

RUN pipenv sync

COPY infrastructure/docker/api/gunicorn.docker.conf /conf/gunicorn.conf
COPY infrastructure/docker/api/gunicorn.docker.conf.py /conf/gunicorn.conf.py

COPY backend /app

Expand All @@ -26,6 +26,6 @@ CMD [ \
"pipenv", \
"run", \
"gunicorn", \
"--config", "/conf/gunicorn.conf", \
"--config", "/conf/gunicorn.conf.py", \
"conduit.wsgi" \
]
]

0 comments on commit 28b016e

Please sign in to comment.