From 5b52119e89cbee7f6f3d06356e495eceff7d8202 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 28 Jan 2022 16:35:25 +0100 Subject: [PATCH] #31 API further credential * adds Kspuser as another expected header data to resolve the api user * adds/updates translations --- api/models/token.py | 6 ++++-- api/settings.py | 3 ++- api/tests/v1/share/test_api_sharing.py | 1 + api/views/views.py | 6 ++++-- locale/de/LC_MESSAGES/django.mo | Bin 36201 -> 36202 bytes locale/de/LC_MESSAGES/django.po | 17 +++++++++-------- 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/api/models/token.py b/api/models/token.py index 81c0373e..e0ad6646 100644 --- a/api/models/token.py +++ b/api/models/token.py @@ -25,11 +25,12 @@ class APIUserToken(models.Model): return self.token @staticmethod - def get_user_from_token(token: str): + def get_user_from_token(token: str, username: str): """ Getter for the related user object Args: token (str): The used token + username (str): The username Returns: user (User): Otherwise None @@ -38,11 +39,12 @@ class APIUserToken(models.Model): try: token_obj = APIUserToken.objects.get( token=token, + user__username=username ) if not token_obj.is_active: raise PermissionError("Token unverified") if token_obj.valid_until is not None and token_obj.valid_until < _today: raise PermissionError("Token validity expired") except ObjectDoesNotExist: - raise PermissionError("Token invalid") + raise PermissionError("Credentials invalid") return token_obj.user diff --git a/api/settings.py b/api/settings.py index ad7afef1..27cd8e51 100644 --- a/api/settings.py +++ b/api/settings.py @@ -5,4 +5,5 @@ Contact: michel.peltriaux@sgdnord.rlp.de Created on: 21.01.22 """ -KSP_TOKEN_HEADER_IDENTIFIER = "Ksptoken" \ No newline at end of file +KSP_TOKEN_HEADER_IDENTIFIER = "Ksptoken" +KSP_USER_HEADER_IDENTIFIER = "Kspuser" \ No newline at end of file diff --git a/api/tests/v1/share/test_api_sharing.py b/api/tests/v1/share/test_api_sharing.py index d5c8b057..9e7c9eec 100644 --- a/api/tests/v1/share/test_api_sharing.py +++ b/api/tests/v1/share/test_api_sharing.py @@ -20,6 +20,7 @@ class BaseAPIV1TestCase(BaseTestCase): cls.header_data = { "HTTP_ksptoken": cls.superuser.api_token.token, + "HTTP_kspuser": cls.superuser.username, } diff --git a/api/views/views.py b/api/views/views.py index 4e0899f2..fb4f6df1 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -13,7 +13,7 @@ from django.views import View from django.views.decorators.csrf import csrf_exempt from api.models import APIUserToken -from api.settings import KSP_TOKEN_HEADER_IDENTIFIER +from api.settings import KSP_TOKEN_HEADER_IDENTIFIER, KSP_USER_HEADER_IDENTIFIER from compensation.models import EcoAccount from ema.models import Ema from intervention.models import Intervention @@ -39,7 +39,9 @@ class AbstractAPIView(View): def dispatch(self, request, *args, **kwargs): try: # Fetch the proper user from the given request header token - self.user = APIUserToken.get_user_from_token(request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None)) + ksp_token = request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None) + ksp_user = request.headers.get(KSP_USER_HEADER_IDENTIFIER, None) + self.user = APIUserToken.get_user_from_token(ksp_token, ksp_user) if not self.user.is_default_user(): raise PermissionError("Default permissions required") except PermissionError as e: diff --git a/locale/de/LC_MESSAGES/django.mo b/locale/de/LC_MESSAGES/django.mo index 714249f64471928803f5bb65d070024d03973544..cb58ae7f8b439c804a3b076f7d39b7817f299f24 100644 GIT binary patch delta 2453 zcmXZde@xX?7{~Dg7vXmO1(zR|#J!gxghs|yS_XqyL@E|6Z6d?TX2T^SA_gU*Ur9m} z$_-sJbh?l>wz5_Jq0UWdv9{b=mNipL&5A~&S=w-`Eqi}B=O13@Ih^l#&Uwx`FxDUY zdw=Xl<0;-LW6Y6_#ypJGO~yQgyHN4_I0G-CA4f3}Z($0WO~%|0shEcI@jfhfaTO*L zufrs4clKh0_{b*XO*YQa5l_brjKklY<2Z%*FI0lN7{nRP)=boQ1*k+tn1EH7h}G!B zmrxsMbp5TUdAplE+vs%z`*141c-svaKviG}r{PKG2+kz_8t36pI3K5NHYOH}Pzx(T zZKxbosS51F7cm_tJQ`YIu*D`yLzOlgm7vh|S6~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-1J*+#z`xDONg#Q`^92$P9N@E$zw9K(Bwzru&`M|=bmTaEEyF=}C@ zs122)DpiRC_%zPNNsoqB=-+A+%|ewn3zeYA^;e>wxCUq9W=z6%_xm0f4`3nv@1Qnv z0i*Z}&c?;h*~+ZKGGece#xxp37{Fmn!4s&Re~v2scUXZpF%=8jtSc~`cs*)-Cu-q+ zsG}G}-HF4f1s%bM@grPBelt!(r3ukUb z;%zt|52B7{)b*dkFNklWD)C9DJ>oOY%jl_8w`incKFd{t3RL21X9H@1oi5&os?ad% zGQN*Wa2)61C}!gisCj%l?1qA<%N|C(hO8abpG#m$=}@T~QMbC=4cLp?(Fp2UokS%* zg*vj&P$fT$N_-xb@Gn$?*e-kKA=J?pVLGlrZD>;$_1A}8bcFCtR4Gs5GQ5Ub(1VQA z2N6_)<){g3P|v6VRly^ek7KC2F^P|1Y`2}K995|*)ZdeJ9u0lC2}7tj6A!ugQ=ChD z4YkmJQRC*kWG%v}g`g_ffJ)GXs@!f=qCwO=BdCpxx!Ai%Lq{-)8ko7$8bM81irR4% z>QdIDcGiMn>_&Y*gg!irI-+yVOUT7HKcSxSI-bBm+>5*t&*Z#pOcNceF&9Uj6Bs7; z_u79(1*iqop~h{-$8kTd#P6{jb9Wi@EHBD=pWS1e0}!&1ZOhF(jP#5KN~eZ-No5BlXyWN_17g^;(k$y zQ#(a1XpQTyMNL?TN?7mu8&Tt$QJ1g{^}pmWM(}GaLSMg)OK|E4QO~lqpZZtPI7)~9 zV8py;pT|556BpnE_#|dxyYmf9BtC`Ncn0;)>krJpj6L=*=~C3VsEccH8gYOhdIjFG Or$Qe;aV)T\n" "Language-Team: LANGUAGE \n" @@ -1950,15 +1950,16 @@ msgstr "Hallo Support" msgid "you need to verify the API token for user" msgstr "Sie müssen einen API Token für folgenden Nutzer freischalten" -#: templates/email/api/verify_token.html:13 +#: templates/email/api/verify_token.html:15 msgid "" "If unsure, please contact the user. The API token can not be used until you " "activated it in the admin backend." msgstr "" -"Falls Sie sich unsicher sind, kontaktieren Sie den Nutzer vorher. Der API Token kann so lange nicht verwendet werden, " -"wie er noch nicht von Ihnen im Admin Backend aktiviert worden ist." +"Falls Sie sich unsicher sind, kontaktieren Sie den Nutzer vorher. Der API " +"Token kann so lange nicht verwendet werden, wie er noch nicht von Ihnen im " +"Admin Backend aktiviert worden ist." -#: templates/email/api/verify_token.html:16 +#: templates/email/api/verify_token.html:18 #: templates/email/checking/shared_data_checked.html:17 #: templates/email/deleting/shared_data_deleted.html:17 #: templates/email/recording/shared_data_recorded.html:17 @@ -2354,15 +2355,15 @@ msgstr "Aktueller Token" msgid "Authenticated by admins" msgstr "Von Admin freigeschaltet" -#: user/templates/user/token.html:16 +#: user/templates/user/token.html:18 msgid "Token has been verified and can be used" msgstr "Token wurde freigeschaltet und kann verwendet werden" -#: user/templates/user/token.html:18 +#: user/templates/user/token.html:20 msgid "Token waiting for verification" msgstr "Token noch nicht freigeschaltet" -#: user/templates/user/token.html:22 +#: user/templates/user/token.html:24 msgid "Valid until" msgstr "Läuft ab am"