Skip to content

Commit 8ffc957

Browse files
added security api delegate class
1 parent 0232ea9 commit 8ffc957

19 files changed

Lines changed: 2278 additions & 49 deletions

File tree

docs/generators/python-fastapi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2424
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
2525
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
2626
|fastapiImplementationPackage|python package name for the implementation code (convention: snake_case).| |impl|
27+
|isLibrary|whether to generate minimal python code to be published as a separate library or not| |false|
2728
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
28-
|libraryMode|whether to generate minimal python code to be published as a separate library| |false|
2929
|packageName|python package name (convention: snake_case).| |openapi_server|
3030
|packageVersion|python package version.| |1.0.0|
3131
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

modules/openapi-generator/src/main/resources/python-fastapi/security_api.mustache

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22

33
from typing import List
4+
{{#isLibrary}}from abc import ABC, abstractmethod{{/isLibrary}}
45

56
from fastapi import Depends, Security # noqa: F401
67
from fastapi.openapi.models import OAuthFlowImplicit, OAuthFlows # noqa: F401
@@ -56,10 +57,22 @@ oauth2_implicit = OAuth2(
5657
)
5758
)
5859
{{/isImplicit}}
60+
{{#isLibrary}}
5961

62+
{{>security_base_cls_init}}
6063

64+
@abstractmethod
65+
def get_token(self, security_scopes: SecurityScopes, token: str) -> TokenModel:
66+
...
67+
68+
@abstractmethod
69+
def validate_scope(self, required_scopes: SecurityScopes, token_scopes: List[str]) -> bool:
70+
...
71+
72+
{{/isLibrary}}
6173
def get_token_{{name}}(
62-
security_scopes: SecurityScopes, token: str = Depends(oauth2_{{#isPassword}}password{{/isPassword}}{{#isCode}}code{{/isCode}}{{#isImplicit}}implicit{{/isImplicit}})
74+
security_scopes: SecurityScopes,
75+
token: str = Depends(oauth2_{{#isPassword}}password{{/isPassword}}{{#isCode}}code{{/isCode}}{{#isImplicit}}implicit{{/isImplicit}})
6376
) -> TokenModel:
6477
"""
6578
Validate and decode token.
@@ -69,9 +82,8 @@ def get_token_{{name}}(
6982
:return: Decoded token information or None if token is invalid
7083
:rtype: TokenModel | None
7184
"""
72-
73-
...
74-
85+
{{^isLibrary}}...{{/isLibrary}}
86+
{{#isLibrary}}return Base{{name}}.subclasses[0]().get_token(security_scopes, token){{/isLibrary}}
7587

7688
def validate_scope_{{name}}(
7789
required_scopes: SecurityScopes, token_scopes: List[str]
@@ -86,12 +98,24 @@ def validate_scope_{{name}}(
8698
:return: True if access to called API is allowed
8799
:rtype: bool
88100
"""
89-
90-
return False
91-
101+
{{^isLibrary}}return False{{/isLibrary}}
102+
{{#isLibrary}}return Base{{name}}.subclasses[0]().validate_scope(required_scopes, token_scopes){{/isLibrary}}
92103
{{/isOAuth}}
93104
{{#isApiKey}}
105+
{{#isLibrary}}
106+
107+
{{>security_base_cls_init}}
108+
109+
@abstractmethod
110+
def get_token(
111+
self,{{#isKeyInHeader}}
112+
token_api_key_header: str,{{/isKeyInHeader}}{{#isKeyInCookie}}
113+
token_api_key_cookie: str,{{/isKeyInCookie}}{{#isKeyInQuery}}
114+
token_api_key_query: str,{{/isKeyInQuery}}
115+
) -> TokenModel:
116+
...
94117

118+
{{/isLibrary}}
95119
def get_token_{{name}}(
96120
{{#isKeyInHeader}}token_api_key_header: str = Security(
97121
APIKeyHeader(name="{{keyParamName}}", auto_error=False)
@@ -113,34 +137,46 @@ def get_token_{{name}}(
113137
:return: Information attached to provided api_key or None if api_key is invalid or does not allow access to called API
114138
:rtype: TokenModel | None
115139
"""
116-
117-
...
118-
140+
{{^isLibrary}}...{{/isLibrary}}
141+
{{#isLibrary}}return Base{{name}}.subclasses[0]().get_token(
142+
{{#isKeyInHeader}}token_api_key_header,{{/isKeyInHeader}}{{#isKeyInCookie}}
143+
token_api_key_cookie,{{/isKeyInCookie}}{{#isKeyInQuery}}
144+
token_api_key_query,{{/isKeyInQuery}}
145+
){{/isLibrary}}
119146
{{/isApiKey}}
120147
{{#isBasicBasic}}
121-
122148
basic_auth = HTTPBasic()
149+
{{#isLibrary}}
123150

151+
{{>security_base_cls_init}}
124152

125-
def get_token_{{name}}(
126-
credentials: HTTPBasicCredentials = Depends(basic_auth)
127-
) -> TokenModel:
153+
@abstractmethod
154+
def get_token(self, credentials: HTTPBasicCredentials) -> TokenModel:
155+
...
156+
157+
{{/isLibrary}}
158+
def get_token_{{name}}(credentials: HTTPBasicCredentials = Depends(basic_auth)) -> TokenModel:
128159
"""
129160
Check and retrieve authentication information from basic auth.
130161

131162
:param credentials Credentials provided by Authorization header
132163
:type credentials: HTTPBasicCredentials
133164
:rtype: TokenModel | None
134165
"""
135-
136-
...
137-
166+
{{^isLibrary}}...{{/isLibrary}}
167+
{{#isLibrary}}return Base{{name}}.subclasses[0]().get_token(credentials){{/isLibrary}}
138168
{{/isBasicBasic}}
139169
{{#isBasicBearer}}
140-
141170
bearer_auth = HTTPBearer()
171+
{{#isLibrary}}
172+
173+
{{>security_base_cls_init}}
142174

175+
@abstractmethod
176+
def get_token(self, credentials: HTTPAuthorizationCredentials) -> TokenModel:
177+
...
143178

179+
{{/isLibrary}}
144180
def get_token_{{name}}(credentials: HTTPAuthorizationCredentials = Depends(bearer_auth)) -> TokenModel:
145181
"""
146182
Check and retrieve authentication information from custom bearer token.
@@ -150,8 +186,7 @@ def get_token_{{name}}(credentials: HTTPAuthorizationCredentials = Depends(beare
150186
:return: Decoded token information or None if token is invalid
151187
:rtype: TokenModel | None
152188
"""
153-
154-
...
155-
189+
{{^isLibrary}}...{{/isLibrary}}
190+
{{#isLibrary}}return Base{{name}}.subclasses[0]().get_token(credentials){{/isLibrary}}
156191
{{/isBasicBearer}}
157192
{{/authMethods}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Base{{name}}(ABC):
2+
subclasses: ClassVar[Tuple] = ()
3+
4+
def __init_subclass__(cls, **kwargs):
5+
super().__init_subclass__(**kwargs)
6+
Base{{name}}.subclasses = Base{{name}}.subclasses + (cls,)

samples/client/others/typescript/encode-decode/build/.openapi-generator/FILES

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/others/typescript/encode-decode/build/index.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/others/typescript/encode-decode/build/models/ObjectSerializer.ts

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/others/typescript/encode-decode/build/models/all.ts

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)