Skip to content

Commit

Permalink
Merge dev
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed May 15, 2024
2 parents 5f0ebbb + a79203e commit c329220
Show file tree
Hide file tree
Showing 28 changed files with 1,626 additions and 621 deletions.
4 changes: 3 additions & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"qwtel.sqlite-viewer",
"njpwerner.autodocstring",
"tamasfe.even-better-toml",
"github.vscode-github-actions"
"github.vscode-github-actions",
"codecov.codecov",
"ritwickdey.liveserver"
]
}
},
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
name: Main

on:
pull_request:
push:
paths-ignore:
- "**/*.md"
- "**/.*"
pull_request:
workflow_dispatch:

env:
Expand All @@ -18,6 +15,7 @@ env:
jobs:
test-backend:
uses: ocadotechnology/codeforlife-workspace/.github/workflows/test-python-code.yaml@main
secrets: inherit
with:
working-directory: backend

Expand Down
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"type": "debugpy"
},
{
"env": {
"PYTEST_ADDOPTS": "--no-cov"
},
"justMyCode": false,
"name": "Pytest",
"presentation": {
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"python.testing.cwd": "${workspaceFolder}/backend",
"python.testing.pytestArgs": [
"-n=auto",
"--cov=.",
"--cov-report=html",
"-c=pyproject.toml",
"."
],
Expand Down
6 changes: 4 additions & 2 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

## ℹ️ HOW-TO: Make the python-package editable.
#
# 1. Comment out the git-codeforlife package under [packages].
Expand All @@ -22,7 +23,7 @@ name = "pypi"
# 5. Run `pipenv install --dev` in your terminal.

[packages]
codeforlife = {ref = "v0.16.6", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
codeforlife = {ref = "v0.16.8", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
# 🚫 Don't add [packages] below that are inhertited from the CFL package.
# TODO: check if we need the below packages
whitenoise = "==6.5.0"
Expand All @@ -45,9 +46,10 @@ google-cloud-logging = "==1.*"
google-auth = "==2.*"
google-cloud-container = "==2.3.0"
# "django-anymail[amazon_ses]" = "==7.0.*"
pyjwt = "==2.6.0" # TODO: upgrade to latest version

[dev-packages]
codeforlife = {ref = "v0.16.6", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
codeforlife = {ref = "v0.16.8", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
# codeforlife = {file = "../../codeforlife-package-python", editable = true, extras = ["dev"]}
# 🚫 Don't add [dev-packages] below that are inhertited from the CFL package.
# TODO: check if we need the below packages
Expand Down
115 changes: 55 additions & 60 deletions backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions backend/api/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
© Ocado Group
Created on 10/05/2024 at 14:37:11(+01:00).
"""

from .token_generators import (
email_verification_token_generator,
password_reset_token_generator,
)
82 changes: 82 additions & 0 deletions backend/api/auth/token_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
© Ocado Group
Created on 10/05/2024 at 14:37:36(+01:00).
"""

import typing as t
from datetime import timedelta

import jwt
from codeforlife.user.models import User
from django.conf import settings
from django.contrib.auth.tokens import (
PasswordResetTokenGenerator,
default_token_generator,
)
from django.utils import timezone

# NOTE: type hint to help Intellisense.
password_reset_token_generator: PasswordResetTokenGenerator = (
default_token_generator
)


class EmailVerificationTokenGenerator:
"""Custom token generator used to verify a user's email address."""

def _get_audience(self, user_or_pk: t.Union[User, t.Any]):
pk = user_or_pk.pk if isinstance(user_or_pk, User) else user_or_pk
return f"user:{pk}"

def make_token(self, user_or_pk: t.Union[User, t.Any]):
"""Generate a token used to verify user's email address.
https://pyjwt.readthedocs.io/en/stable/usage.html
Args:
user: The user to generate a token for.
Returns:
A token used to verify user's email address.
"""
return jwt.encode(
payload={
"exp": (
timezone.now()
+ timedelta(seconds=settings.EMAIL_VERIFICATION_TIMEOUT)
),
"aud": [self._get_audience(user_or_pk)],
},
key=settings.SECRET_KEY,
algorithm="HS256",
)

def check_token(self, user_or_pk: t.Union[User, t.Any], token: str):
"""Check the token belongs to the user and has not expired.
Args:
user: The user to check.
token: The token to check.
Returns:
A flag designating whether the token belongs to the user and has not
expired.
"""
try:
jwt.decode(
jwt=token,
key=settings.SECRET_KEY,
audience=self._get_audience(user_or_pk),
algorithms=["HS256"],
)
except (
jwt.DecodeError,
jwt.ExpiredSignatureError,
jwt.InvalidAudienceError,
):
return False

return True


email_verification_token_generator = EmailVerificationTokenGenerator()
18 changes: 16 additions & 2 deletions backend/api/fixtures/school_1_teacher_invitations.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,22 @@
"from_teacher": 7,
"invited_teacher_first_name": "Invited",
"invited_teacher_last_name": "Teacher",
"invited_teacher_email": "[email protected]",
"invited_teacher_is_admin": false,
"invited_teacher_email": "[email protected]",
"invited_teacher_is_admin": true,
"expiry": "9999-02-09 20:26:08.298402+00:00"
}
},
{
"model": "common.schoolteacherinvitation",
"pk": 4,
"fields": {
"token": "pbkdf2_sha256$260000$hbsAadmrRo744BTM6NofUb$ePs/7vi6sSzOPpiWxNhXMZnNnE7aXOpzIhxrAa/rdiU=",
"school": 2,
"from_teacher": 7,
"invited_teacher_first_name": "Invited",
"invited_teacher_last_name": "Independent",
"invited_teacher_email": "[email protected]",
"invited_teacher_is_admin": true,
"expiry": "9999-02-09 20:26:08.298402+00:00"
}
}
Expand Down
Loading

0 comments on commit c329220

Please sign in to comment.