|
5 | 5 |
|
6 | 6 | from importlib import import_module |
7 | 7 | from pathlib import Path |
8 | | -from typing import ( |
9 | | - TYPE_CHECKING, |
10 | | - Any, |
11 | | - Callable, |
12 | | - Dict, |
13 | | - Optional, |
14 | | - Type, |
15 | | - Union, |
16 | | - cast, |
17 | | -) |
| 8 | +from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union, cast |
18 | 9 |
|
19 | | -from sanic.http.tls.creators import CertCreator, MkcertCreator, TrustmeCreator |
| 10 | +from sanic.http.tls.context import process_to_context |
| 11 | +from sanic.http.tls.creators import MkcertCreator, TrustmeCreator |
20 | 12 |
|
21 | 13 |
|
22 | 14 | if TYPE_CHECKING: |
@@ -106,21 +98,30 @@ def load(self) -> SanicApp: |
106 | 98 |
|
107 | 99 |
|
108 | 100 | class CertLoader: |
109 | | - _creator_class: Type[CertCreator] |
| 101 | + _creators = { |
| 102 | + "mkcert": MkcertCreator, |
| 103 | + "trustme": TrustmeCreator, |
| 104 | + } |
110 | 105 |
|
111 | 106 | def __init__(self, ssl_data: Dict[str, Union[str, os.PathLike]]): |
112 | | - creator_name = ssl_data.get("creator") |
113 | | - if creator_name not in ("mkcert", "trustme"): |
| 107 | + self._ssl_data = ssl_data |
| 108 | + |
| 109 | + creator_name = cast(str, ssl_data.get("creator")) |
| 110 | + |
| 111 | + self._creator_class = self._creators.get(creator_name) |
| 112 | + if not creator_name: |
| 113 | + return |
| 114 | + |
| 115 | + if not self._creator_class: |
114 | 116 | raise RuntimeError(f"Unknown certificate creator: {creator_name}") |
115 | | - elif creator_name == "mkcert": |
116 | | - self._creator_class = MkcertCreator |
117 | | - elif creator_name == "trustme": |
118 | | - self._creator_class = TrustmeCreator |
119 | 117 |
|
120 | 118 | self._key = ssl_data["key"] |
121 | 119 | self._cert = ssl_data["cert"] |
122 | 120 | self._localhost = cast(str, ssl_data["localhost"]) |
123 | 121 |
|
124 | 122 | def load(self, app: SanicApp): |
| 123 | + if not self._creator_class: |
| 124 | + return process_to_context(self._ssl_data) |
| 125 | + |
125 | 126 | creator = self._creator_class(app, self._key, self._cert) |
126 | 127 | return creator.generate_cert(self._localhost) |
0 commit comments