Commit 6e4d55b
committed
Refactor: OAuth2 token refresh — instance-var caching + heartbeat-driven refresh
- OAuth2Handler.__init__ reads _access_token, _refresh_token, _expires_at
from AddonSettings once at construction; no per-call settings I/O
- _store_tokens() updates both settings and instance vars atomically
- _do_token_refresh(): unconditional internal refresh (single _ so subclass
can override); base uses refresh_token grant
- refresh_access_token() -> bool: no-op when > 300s from expiry (_REFRESH_MARGIN),
calls _do_token_refresh() otherwise; returns True/False instead of raising
- get_valid_token(): pure getter returning self._access_token — no side-effects;
callers that need a fresh token must call refresh_access_token() first
- active_authentication(): calls refresh_access_token() before get_valid_token()
- _clear_token_settings(): clears both settings and instance vars
NLZIETOAuth2Handler:
- __init__ caches _id_token from settings
- _do_token_refresh() override: silent re-auth via id_token (web flow) or
refresh_token grant (device flow); calls super()._do_token_refresh() for latter
- 401 fallback in list_profiles() calls _do_token_refresh() directly
(force-refresh, bypasses expiry check)
- _store_tokens() and _clear_token_settings() maintain _id_token in sync
chn_nlziet:
- __set_auth_headers(): refresh_access_token() then get_valid_token()
- create_iptv_streams() + create_iptv_epg(): refresh token on every heartbeat
cycle so long IPTV Manager sessions (4+ hours) never hit stale tokens
Tests (all 55 pass, 18 skipped = live-credential tests):
- Updated test_06, test_16: set _expires_at on instance (not just settings),
call refresh_access_token() explicitly
- Updated test_refresh_no_tokens: assertFalse() instead of assertRaises(ValueError)
- New test_refresh_access_token_noop_when_valid: monkey-patch verifies
_do_token_refresh is NOT called when token is fresh
- New test_get_valid_token_is_pure_getter: verifies no refresh side-effect
- New test_instance_vars_loaded_from_settings_at_init: verifies all three
instance vars populated from settings at construction
Co-authored-by: Claude Sonnet 4.6
Signed-off-by: Oliver
Signed-off-by: Olliver Schinagl <oliver@schinagl.nl>1 parent e6751b2 commit 6e4d55b
File tree
4 files changed
+138
-40
lines changed- channels/channel.nlziet/nlziet
- resources/lib/authentication
- tests/authentication
4 files changed
+138
-40
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
273 | 274 | | |
274 | 275 | | |
275 | 276 | | |
| |||
1420 | 1421 | | |
1421 | 1422 | | |
1422 | 1423 | | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
1423 | 1427 | | |
1424 | 1428 | | |
1425 | 1429 | | |
| |||
1494 | 1498 | | |
1495 | 1499 | | |
1496 | 1500 | | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
1497 | 1504 | | |
1498 | 1505 | | |
1499 | 1506 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| |||
177 | 179 | | |
178 | 180 | | |
179 | 181 | | |
180 | | - | |
| 182 | + | |
| 183 | + | |
181 | 184 | | |
182 | 185 | | |
183 | 186 | | |
| |||
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
198 | 200 | | |
199 | | - | |
| 201 | + | |
200 | 202 | | |
201 | 203 | | |
202 | | - | |
203 | | - | |
| 204 | + | |
204 | 205 | | |
205 | 206 | | |
206 | 207 | | |
| |||
219 | 220 | | |
220 | 221 | | |
221 | 222 | | |
222 | | - | |
| 223 | + | |
223 | 224 | | |
224 | 225 | | |
225 | 226 | | |
| |||
245 | 246 | | |
246 | 247 | | |
247 | 248 | | |
| 249 | + | |
| 250 | + | |
248 | 251 | | |
249 | 252 | | |
250 | 253 | | |
| |||
282 | 285 | | |
283 | 286 | | |
284 | 287 | | |
285 | | - | |
286 | | - | |
| 288 | + | |
| 289 | + | |
287 | 290 | | |
288 | 291 | | |
289 | 292 | | |
| |||
299 | 302 | | |
300 | 303 | | |
301 | 304 | | |
302 | | - | |
303 | | - | |
304 | | - | |
| 305 | + | |
305 | 306 | | |
306 | 307 | | |
307 | 308 | | |
| |||
745 | 746 | | |
746 | 747 | | |
747 | 748 | | |
| 749 | + | |
748 | 750 | | |
749 | 751 | | |
750 | 752 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
50 | 54 | | |
51 | 55 | | |
52 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
53 | 62 | | |
54 | 63 | | |
55 | 64 | | |
| |||
105 | 114 | | |
106 | 115 | | |
107 | 116 | | |
108 | | - | |
109 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
110 | 120 | | |
111 | | - | |
| 121 | + | |
| 122 | + | |
112 | 123 | | |
113 | 124 | | |
114 | | - | |
| 125 | + | |
| 126 | + | |
115 | 127 | | |
116 | 128 | | |
117 | 129 | | |
| |||
135 | 147 | | |
136 | 148 | | |
137 | 149 | | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
142 | 157 | | |
143 | 158 | | |
144 | 159 | | |
145 | 160 | | |
146 | 161 | | |
147 | 162 | | |
148 | | - | |
| 163 | + | |
149 | 164 | | |
150 | 165 | | |
151 | 166 | | |
152 | | - | |
153 | | - | |
154 | | - | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
155 | 186 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
| 187 | + | |
| 188 | + | |
164 | 189 | | |
165 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
166 | 194 | | |
167 | 195 | | |
168 | 196 | | |
| |||
180 | 208 | | |
181 | 209 | | |
182 | 210 | | |
| 211 | + | |
183 | 212 | | |
184 | 213 | | |
185 | 214 | | |
| |||
195 | 224 | | |
196 | 225 | | |
197 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
198 | 230 | | |
199 | 231 | | |
200 | 232 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
219 | | - | |
| 219 | + | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
222 | 223 | | |
223 | | - | |
| 224 | + | |
| 225 | + | |
224 | 226 | | |
225 | 227 | | |
226 | 228 | | |
| |||
612 | 614 | | |
613 | 615 | | |
614 | 616 | | |
| 617 | + | |
615 | 618 | | |
| 619 | + | |
616 | 620 | | |
617 | 621 | | |
618 | 622 | | |
| |||
755 | 759 | | |
756 | 760 | | |
757 | 761 | | |
758 | | - | |
| 762 | + | |
759 | 763 | | |
760 | 764 | | |
761 | | - | |
762 | | - | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
763 | 820 | | |
764 | 821 | | |
765 | 822 | | |
| |||
0 commit comments