1717from hashlib import sha256
1818from http import HTTPStatus
1919from os import path
20- from typing import Dict , List
20+ from typing import TYPE_CHECKING , Any , Dict , List
2121
2222import jinja2
2323from jinja2 import TemplateNotFound
2424
25+ from twisted .web .server import Request
26+
2527from synapse .api .errors import NotFoundError , StoreError , SynapseError
2628from synapse .config import ConfigError
2729from synapse .http .server import DirectServeHtmlResource , respond_with_html
2830from synapse .http .servlet import parse_bytes_from_args , parse_string
2931from synapse .types import UserID
3032
33+ if TYPE_CHECKING :
34+ from synapse .server import HomeServer
35+
3136# language to use for the templates. TODO: figure this out from Accept-Language
3237TEMPLATE_LANGUAGE = "en"
3338
@@ -69,11 +74,7 @@ class ConsentResource(DirectServeHtmlResource):
6974 against the user.
7075 """
7176
72- def __init__ (self , hs ):
73- """
74- Args:
75- hs (synapse.server.HomeServer): homeserver
76- """
77+ def __init__ (self , hs : "HomeServer" ):
7778 super ().__init__ ()
7879
7980 self .hs = hs
@@ -106,18 +107,14 @@ def __init__(self, hs):
106107
107108 self ._hmac_secret = hs .config .form_secret .encode ("utf-8" )
108109
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 :
114111 version = parse_string (request , "v" , default = self ._default_consent_version )
115112 username = parse_string (request , "u" , default = "" )
116113 userhmac = None
117114 has_consented = False
118115 public_version = username == ""
119116 if not public_version :
120- args : Dict [bytes , List [bytes ]] = request .args
117+ args : Dict [bytes , List [bytes ]] = request .args # type: ignore
121118 userhmac_bytes = parse_bytes_from_args (args , "h" , required = True )
122119
123120 self ._check_hash (username , userhmac_bytes )
@@ -147,14 +144,10 @@ async def _async_render_GET(self, request):
147144 except TemplateNotFound :
148145 raise NotFoundError ("Unknown policy version" )
149146
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 :
155148 version = parse_string (request , "v" , required = True )
156149 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
158151 userhmac = parse_bytes_from_args (args , "h" , required = True )
159152
160153 self ._check_hash (username , userhmac )
@@ -177,7 +170,9 @@ async def _async_render_POST(self, request):
177170 except TemplateNotFound :
178171 raise NotFoundError ("success.html not found" )
179172
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 :
181176 # get_template checks for ".." so we don't need to worry too much
182177 # about path traversal here.
183178 template_html = self ._jinja_env .get_template (
@@ -186,11 +181,11 @@ def _render_template(self, request, template_name, **template_args):
186181 html = template_html .render (** template_args )
187182 respond_with_html (request , 200 , html )
188183
189- def _check_hash (self , userid , userhmac ) :
184+ def _check_hash (self , userid : str , userhmac : bytes ) -> None :
190185 """
191186 Args:
192- userid (unicode) :
193- userhmac (bytes) :
187+ userid:
188+ userhmac:
194189
195190 Raises:
196191 SynapseError if the hash doesn't match
0 commit comments