Coverage for src/django_device_bound_sessions/conf.py: 100%

59 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-06 02:43 -0600

1"""Settings for django-device-bound-sessions. 

2 

3Configure via a ``DBSC`` dict in your Django settings, e.g.:: 

4 

5 DBSC = { 

6 "BOUND_COOKIE_AGE": 300, 

7 "ENFORCEMENT": "anonymous", 

8 } 

9 

10Values are read on every access so ``override_settings`` works in tests. 

11""" 

12 

13from typing import Any, Literal, NotRequired, TypedDict 

14 

15from django.conf import settings 

16 

17 

18class ScopeRule(TypedDict): 

19 """One entry of ``scope.scope_specification``.""" 

20 

21 type: Literal["include", "exclude"] 

22 domain: NotRequired[str] 

23 path: NotRequired[str] 

24 

25 

26class DBSCConfig(TypedDict, total=False): 

27 """Shape of the ``DBSC`` setting. Annotate yours to have it checked:: 

28 

29 from django_device_bound_sessions.conf import DBSCConfig 

30 

31 DBSC: DBSCConfig = {"BOUND_COOKIE_AGE": 300} 

32 """ 

33 

34 BOUND_COOKIE_NAME: str 

35 BOUND_COOKIE_AGE: int 

36 BOUND_COOKIE_GRACE: int 

37 BOUND_COOKIE_DOMAIN: str | None 

38 BOUND_COOKIE_PATH: str 

39 BOUND_COOKIE_SECURE: bool 

40 BOUND_COOKIE_HTTPONLY: bool 

41 BOUND_COOKIE_SAMESITE: Literal["Lax", "Strict", "None"] | None 

42 ALGORITHMS: list[Literal["ES256", "RS256"]] 

43 CHALLENGE_MAX_AGE: int 

44 CACHE_ALIAS: str 

45 REGISTRATION_PATH: str | None 

46 REFRESH_PATH: str | None 

47 SCOPE_ORIGIN: str | None 

48 SCOPE_INCLUDE_SITE: bool 

49 SCOPE_SPECIFICATION: list[ScopeRule] 

50 ALLOWED_REFRESH_INITIATORS: list[str] 

51 OFFER_ON_LOGIN: bool 

52 OFFER_MAX_AGE: int 

53 ENFORCEMENT: Literal["anonymous", "logout", "off"] 

54 

55 

56DEFAULTS: DBSCConfig = { 

57 # --- Bound (short-lived) cookie -------------------------------------- 

58 "BOUND_COOKIE_NAME": "dbsc_bound", 

59 # Lifetime in seconds. When it expires, the browser refreshes it by 

60 # proving possession of the device-bound key. 

61 "BOUND_COOKIE_AGE": 600, 

62 # Extra seconds the server accepts past BOUND_COOKIE_AGE (clock skew, 

63 # requests already in flight when the cookie expired). 

64 "BOUND_COOKIE_GRACE": 30, 

65 "BOUND_COOKIE_DOMAIN": None, 

66 "BOUND_COOKIE_PATH": "/", 

67 "BOUND_COOKIE_SECURE": True, 

68 "BOUND_COOKIE_HTTPONLY": True, 

69 "BOUND_COOKIE_SAMESITE": "Lax", 

70 # --- Protocol --------------------------------------------------------- 

71 # Algorithms offered in Secure-Session-Registration, in preference order. 

72 # "none" is intentionally not supported. 

73 "ALGORITHMS": ["ES256", "RS256"], 

74 # How long (seconds) a registration challenge stays valid, counted from 

75 # when the offer is sent. The browser answers within seconds; keep it short, 

76 # as the session isn't protected until it's bound. (Refresh challenges last 

77 # as long as a bound cookie: AGE + GRACE.) 

78 "CHALLENGE_MAX_AGE": 60, 

79 # Cache alias used to make challenges single-use (replay protection). 

80 "CACHE_ALIAS": "default", 

81 # --- Session configuration sent to the browser ----------------------- 

82 # Override the endpoint URLs (otherwise resolved from django_device_bound_sessions.urls). 

83 "REGISTRATION_PATH": None, 

84 "REFRESH_PATH": None, 

85 # scope.origin; None lets the browser default it to the serving origin. 

86 "SCOPE_ORIGIN": None, 

87 # scope.include_site: True = whole site, False = this origin only. 

88 "SCOPE_INCLUDE_SITE": False, 

89 # scope.scope_specification, e.g. 

90 # [{"type": "exclude", "domain": "*", "path": "/static"}] 

91 "SCOPE_SPECIFICATION": [], 

92 "ALLOWED_REFRESH_INITIATORS": [], 

93 # --- Behaviour -------------------------------------------------------- 

94 # Automatically offer DBSC registration after a successful login(). 

95 "OFFER_ON_LOGIN": True, 

96 # Seconds a pending offer stays usable. Until a session is bound, anyone 

97 # holding its cookie could receive the offer, so keep this short: the 

98 # offer is meant for the page load right after login. 

99 "OFFER_MAX_AGE": 60, 

100 # What happens when a bound session arrives without a valid bound cookie: 

101 # "anonymous" - treat this request as unauthenticated (default) 

102 # "logout" - log the session out entirely (also ends the DBSC session) 

103 # "off" - do nothing; inspect request.dbsc yourself 

104 "ENFORCEMENT": "anonymous", 

105} 

106 

107 

108# DEFAULTS is a TypedDict so its keys and values are checked; lookups by a 

109# runtime setting name go through this plain-dict copy instead. 

110_DEFAULTS_BY_NAME: dict[str, object] = dict(DEFAULTS) 

111 

112 

113class DBSCSettings: 

114 """Typed accessors for the ``DBSC`` settings dict, with defaults applied.""" 

115 

116 BOUND_COOKIE_NAME: str 

117 BOUND_COOKIE_AGE: int 

118 BOUND_COOKIE_GRACE: int 

119 BOUND_COOKIE_DOMAIN: str | None 

120 BOUND_COOKIE_PATH: str 

121 BOUND_COOKIE_SECURE: bool 

122 BOUND_COOKIE_HTTPONLY: bool 

123 BOUND_COOKIE_SAMESITE: Literal["Lax", "Strict", "None"] | None 

124 ALGORITHMS: list[str] 

125 CHALLENGE_MAX_AGE: int 

126 CACHE_ALIAS: str 

127 REGISTRATION_PATH: str | None 

128 REFRESH_PATH: str | None 

129 SCOPE_ORIGIN: str | None 

130 SCOPE_INCLUDE_SITE: bool 

131 SCOPE_SPECIFICATION: list[ScopeRule] 

132 ALLOWED_REFRESH_INITIATORS: list[str] 

133 OFFER_ON_LOGIN: bool 

134 OFFER_MAX_AGE: int 

135 ENFORCEMENT: Literal["anonymous", "logout", "off"] 

136 

137 def __getattr__(self, name: str) -> Any: 

138 if name not in _DEFAULTS_BY_NAME: 

139 raise AttributeError(f"Unknown DBSC setting: {name}") 

140 configured: dict[str, Any] = getattr(settings, "DBSC", {}) 

141 return configured.get(name, _DEFAULTS_BY_NAME[name]) 

142 

143 

144dbsc_settings = DBSCSettings() 

145 

146# Keys stored in request.session. 

147BINDING_KEY = "_dbsc" 

148OFFER_KEY = "_dbsc_offer" 

149REGISTRATION_NONCE_KEY = "_dbsc_registration_nonce"