Skip to content

Commit d97c86f

Browse files
committed
doc: third-party auth
1 parent 19fc447 commit d97c86f

File tree

13 files changed

+394
-30
lines changed

13 files changed

+394
-30
lines changed

api/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ThirdPartyAuthForm(forms.Form):
2323
)
2424
client_id = forms.IntegerField(widget=HiddenInput())
2525
third_party_app = forms.CharField(widget=HiddenInput())
26-
cgu_link = forms.URLField(widget=HiddenInput())
26+
privacy_link = forms.URLField(widget=HiddenInput())
2727
username = forms.CharField(widget=HiddenInput())
2828
callback_url = forms.URLField(widget=HiddenInput())
2929
signature = forms.CharField(widget=HiddenInput())

api/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ def has_perms(self, perm_list: Iterable[str]) -> bool:
6666
return all(self.has_perm(perm) for perm in perm_list)
6767

6868
def reset_hmac(self, *, commit: bool = True) -> str:
69-
"""Reset and return the HMAC key for this client."""
69+
"""Reset and return the HMAC key for this client.
70+
71+
Args:
72+
commit: if True (the default), persist the new hmac in db.
73+
"""
7074
self.hmac_key = get_hmac_key()
7175
if commit:
7276
self.save()

api/schemas.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from ninja import ModelSchema
2-
from pydantic import Field
1+
from ninja import ModelSchema, Schema
2+
from pydantic import Field, HttpUrl
33

44
from api.models import ApiClient
55
from core.schemas import SimpleUserSchema
@@ -12,3 +12,12 @@ class Meta:
1212

1313
owner: SimpleUserSchema
1414
permissions: list[str] = Field(alias="all_permissions")
15+
16+
17+
class ThirdPartyAuthParamsSchema(Schema):
18+
client_id: int
19+
third_party_app: str
20+
privacy_link: HttpUrl
21+
username: str
22+
callback_url: HttpUrl
23+
signature: str

api/templates/api/third_party/auth.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
{% endtrans %}
1515
</p>
1616
<p class="margin-bottom">
17-
{% trans trimmed app=third_party_app, cgu_link=third_party_cgu, sith_cgu_link=sith_cgu %}
18-
The privacy policies of <a href="{{ cgu_link }}">{{ app }}</a>
17+
{% trans trimmed app=third_party_app, privacy_link=third_party_cgu, sith_cgu_link=sith_cgu %}
18+
The privacy policies of <a href="{{ privacy_link }}">{{ app }}</a>
1919
and of <a href="{{ sith_cgu_link }}">the Students' Association</a>
2020
applies as soon as the form is submitted.
2121
{% endtrans %}

api/tests/test_third_party_auth.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from api.models import ApiClient, get_hmac_key
1111
from core.baker_recipes import subscriber_user
12+
from core.schemas import UserProfileSchema
1213
from core.utils import hmac_hexdigest
1314

1415

@@ -34,14 +35,16 @@ def setUp(self):
3435
self.query = {
3536
"client_id": self.api_client.id,
3637
"third_party_app": "app",
37-
"cgu_link": "https://foobar.fr/",
38+
"privacy_link": "https://foobar.fr/",
3839
"username": "bibou",
3940
"callback_url": "https://callback.fr/",
4041
}
4142
self.query["signature"] = hmac_hexdigest(self.api_client.hmac_key, self.query)
42-
self.callback_data = {"user_id": self.user.id}
43+
self.callback_data = {
44+
"user": UserProfileSchema.from_orm(self.user).model_dump()
45+
}
4346
self.callback_data["signature"] = hmac_hexdigest(
44-
self.api_client.hmac_key, self.callback_data
47+
self.api_client.hmac_key, self.callback_data["user"]
4548
)
4649

4750
def test_auth_ok(self):

api/views.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,16 @@
1010
from django.urls import reverse, reverse_lazy
1111
from django.utils.translation import gettext as _
1212
from django.views.generic import FormView, TemplateView
13-
from ninja import Schema
1413
from ninja_extra.shortcuts import get_object_or_none
15-
from pydantic import HttpUrl
1614

1715
from api.forms import ThirdPartyAuthForm
1816
from api.models import ApiClient
17+
from api.schemas import ThirdPartyAuthParamsSchema
1918
from core.models import SithFile
2019
from core.schemas import UserProfileSchema
2120
from core.utils import hmac_hexdigest
2221

2322

24-
class ThirdPartyAuthParamsSchema(Schema):
25-
client_id: int
26-
third_party_app: str
27-
cgu_link: HttpUrl
28-
username: str
29-
callback_url: HttpUrl
30-
signature: str
31-
32-
3323
class ThirdPartyAuthView(LoginRequiredMixin, FormView):
3424
form_class = ThirdPartyAuthForm
3525
template_name = "api/third_party/auth.jinja"
@@ -93,7 +83,7 @@ def form_valid(self, form):
9383
def get_context_data(self, **kwargs):
9484
return super().get_context_data(**kwargs) | {
9585
"third_party_app": self.params.third_party_app,
96-
"third_party_cgu": self.params.cgu_link,
86+
"third_party_cgu": self.params.privacy_link,
9787
"sith_cgu": SithFile.objects.get(id=settings.SITH_CGU_FILE_ID),
9888
}
9989

core/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ def get_client_ip(request: HttpRequest) -> str | None:
210210
def hmac_hexdigest(
211211
key: str | bytes,
212212
data: Mapping[str, Any] | Sequence[tuple[str, Any]],
213-
digest: str | Callable[[Buffer], HASH] = "sha256",
213+
digest: str | Callable[[Buffer], HASH] = "sha512",
214214
) -> str:
215215
"""Return the hexdigest of the signature of the given data.
216216
217217
Args:
218218
key: the HMAC key used for the signature
219219
data: the data to sign
220-
digest: a PEP247 hashing algorithm
220+
digest: a PEP247 hashing algorithm (by default, sha512)
221221
222222
Examples:
223223
```python
@@ -226,7 +226,7 @@ def hmac_hexdigest(
226226
"bar": "somevalue",
227227
}
228228
hmac_key = secrets.token_hex(64)
229-
signature = hmac_hexdigest(hmac_key, data, "sha512")
229+
signature = hmac_hexdigest(hmac_key, data, "sha256")
230230
```
231231
"""
232232
if isinstance(key, str):

docs/reference/api/schemas.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: api.schemas

docs/reference/api/views.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: api.views

0 commit comments

Comments
 (0)