Saves the session to the response in a secure cookie.
(
self,
app: Quart,
session: SessionMixin,
response: Response | WerkzeugResponse | None,
)
| 179 | return self.session_class() |
| 180 | |
| 181 | async def save_session( |
| 182 | self, |
| 183 | app: Quart, |
| 184 | session: SessionMixin, |
| 185 | response: Response | WerkzeugResponse | None, |
| 186 | ) -> None: |
| 187 | """Saves the session to the response in a secure cookie.""" |
| 188 | if response is None: |
| 189 | if session.modified: |
| 190 | app.logger.exception( |
| 191 | "Secure Cookie Session modified during websocket handling. " |
| 192 | "These modifications will be lost as a cookie cannot be set." |
| 193 | ) |
| 194 | return |
| 195 | |
| 196 | name = self.get_cookie_name(app) |
| 197 | domain = self.get_cookie_domain(app) |
| 198 | path = self.get_cookie_path(app) |
| 199 | secure = self.get_cookie_secure(app) |
| 200 | samesite = self.get_cookie_samesite(app) |
| 201 | httponly = self.get_cookie_httponly(app) |
| 202 | |
| 203 | # Add a "Vary: Cookie" header if the session was accessed at all. |
| 204 | if session.accessed: |
| 205 | response.vary.add("Cookie") |
| 206 | |
| 207 | # If the session is modified to be empty, remove the cookie. |
| 208 | # If the session is empty, return without setting the cookie. |
| 209 | if not session: |
| 210 | if session.modified: |
| 211 | response.delete_cookie( |
| 212 | name, |
| 213 | domain=domain, |
| 214 | path=path, |
| 215 | secure=secure, |
| 216 | samesite=samesite, |
| 217 | httponly=httponly, |
| 218 | ) |
| 219 | response.vary.add("Cookie") |
| 220 | |
| 221 | return |
| 222 | |
| 223 | if not self.should_set_cookie(app, session): |
| 224 | return |
| 225 | |
| 226 | expires = self.get_expiration_time(app, session) |
| 227 | val = self.get_signing_serializer(app).dumps(dict(session)) |
| 228 | response.set_cookie( |
| 229 | name, |
| 230 | val, |
| 231 | expires=expires, |
| 232 | httponly=httponly, |
| 233 | domain=domain, |
| 234 | path=path, |
| 235 | secure=secure, |
| 236 | samesite=samesite, |
| 237 | ) |
| 238 | response.vary.add("Cookie") |