Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/render/json/__init__.py: 0%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-16 22:16 +0000

1from .default import DefaultRender as default 

2from .user import UserRender as user 

3from viur.core import securitykey, current, errors 

4from viur.core.decorators import * 

5import json 

6 

7__all__ = [default] 

8 

9 

10@exposed 

11def skey(amount: int = 1, *args, **kwargs) -> str: 

12 """ 

13 Creates CSRF-security-keys for transactions. 

14 

15 All returned keys are associated with the session, therefore they cannot be used across sessions. 

16 The keys get a maximum lifetime of the session lifetime, afterward they become invalid. 

17 

18 :param amount: Optional amount of securitykeys to create in a batch. 

19 `amount > 1` can only be used by authenticated users, for a maximum of 100 keys. 

20 

21 See module securitykey for details. 

22 """ 

23 current.request.get().response.headers["Content-Type"] = "application/json" 

24 

25 if amount == 1: 

26 return json.dumps(securitykey.create()) 

27 

28 if not 0 < amount <= 100: 

29 raise errors.Forbidden("Invalid amount provided") 

30 

31 if not current.user.get(): 

32 raise errors.Forbidden("Batch securitykey creation is only available to authenticated users") 

33 

34 return json.dumps([securitykey.create() for _ in range(amount)]) 

35 

36 

37def _postProcessAppObj(obj): # Register our SKey function 

38 obj["skey"] = skey 

39 return obj