17
17
from hashlib import sha256
18
18
from http import HTTPStatus
19
19
from os import path
20
- from typing import Dict , List
20
+ from typing import TYPE_CHECKING , Any , Dict , List
21
21
22
22
import jinja2
23
23
from jinja2 import TemplateNotFound
24
24
25
+ from twisted .web .server import Request
26
+
25
27
from synapse .api .errors import NotFoundError , StoreError , SynapseError
26
28
from synapse .config import ConfigError
27
29
from synapse .http .server import DirectServeHtmlResource , respond_with_html
28
30
from synapse .http .servlet import parse_bytes_from_args , parse_string
29
31
from synapse .types import UserID
30
32
33
+ if TYPE_CHECKING :
34
+ from synapse .server import HomeServer
35
+
31
36
# language to use for the templates. TODO: figure this out from Accept-Language
32
37
TEMPLATE_LANGUAGE = "en"
33
38
@@ -69,11 +74,7 @@ class ConsentResource(DirectServeHtmlResource):
69
74
against the user.
70
75
"""
71
76
72
- def __init__ (self , hs ):
73
- """
74
- Args:
75
- hs (synapse.server.HomeServer): homeserver
76
- """
77
+ def __init__ (self , hs : "HomeServer" ):
77
78
super ().__init__ ()
78
79
79
80
self .hs = hs
@@ -106,18 +107,14 @@ def __init__(self, hs):
106
107
107
108
self ._hmac_secret = hs .config .form_secret .encode ("utf-8" )
108
109
109
- async def _async_render_GET (self , request ):
110
- """
111
- Args:
112
- request (twisted.web.http.Request):
113
- """
110
+ async def _async_render_GET (self , request : Request ) -> None :
114
111
version = parse_string (request , "v" , default = self ._default_consent_version )
115
112
username = parse_string (request , "u" , default = "" )
116
113
userhmac = None
117
114
has_consented = False
118
115
public_version = username == ""
119
116
if not public_version :
120
- args : Dict [bytes , List [bytes ]] = request .args
117
+ args : Dict [bytes , List [bytes ]] = request .args # type: ignore
121
118
userhmac_bytes = parse_bytes_from_args (args , "h" , required = True )
122
119
123
120
self ._check_hash (username , userhmac_bytes )
@@ -147,14 +144,10 @@ async def _async_render_GET(self, request):
147
144
except TemplateNotFound :
148
145
raise NotFoundError ("Unknown policy version" )
149
146
150
- async def _async_render_POST (self , request ):
151
- """
152
- Args:
153
- request (twisted.web.http.Request):
154
- """
147
+ async def _async_render_POST (self , request : Request ) -> None :
155
148
version = parse_string (request , "v" , required = True )
156
149
username = parse_string (request , "u" , required = True )
157
- args : Dict [bytes , List [bytes ]] = request .args
150
+ args : Dict [bytes , List [bytes ]] = request .args # type: ignore
158
151
userhmac = parse_bytes_from_args (args , "h" , required = True )
159
152
160
153
self ._check_hash (username , userhmac )
@@ -177,7 +170,9 @@ async def _async_render_POST(self, request):
177
170
except TemplateNotFound :
178
171
raise NotFoundError ("success.html not found" )
179
172
180
- def _render_template (self , request , template_name , ** template_args ):
173
+ def _render_template (
174
+ self , request : Request , template_name : str , ** template_args : Any
175
+ ) -> None :
181
176
# get_template checks for ".." so we don't need to worry too much
182
177
# about path traversal here.
183
178
template_html = self ._jinja_env .get_template (
@@ -186,11 +181,11 @@ def _render_template(self, request, template_name, **template_args):
186
181
html = template_html .render (** template_args )
187
182
respond_with_html (request , 200 , html )
188
183
189
- def _check_hash (self , userid , userhmac ) :
184
+ def _check_hash (self , userid : str , userhmac : bytes ) -> None :
190
185
"""
191
186
Args:
192
- userid (unicode) :
193
- userhmac (bytes) :
187
+ userid:
188
+ userhmac:
194
189
195
190
Raises:
196
191
SynapseError if the hash doesn't match
0 commit comments