-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
289 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
from enum import Enum | ||
|
||
|
||
class SecuritySchemeType(str, Enum): | ||
apiKey = "apiKey" | ||
http = "http" | ||
oauth2 = "oauth2" | ||
openIdConnect = "openIdConnect" | ||
|
||
class BaseEnum(str, Enum): | ||
def __str__(self) -> str: | ||
return self.value | ||
return self.value # type: ignore | ||
|
||
def __repr__(self) -> str: | ||
return str(self) | ||
|
||
|
||
class APIKeyIn(str, Enum): | ||
class SecuritySchemeType(BaseEnum): | ||
apiKey = "apiKey" | ||
http = "http" | ||
oauth2 = "oauth2" | ||
mutualTLS = "mutualTLS" | ||
openIdConnect = "openIdConnect" | ||
|
||
|
||
class APIKeyIn(BaseEnum): | ||
query = "query" | ||
header = "header" | ||
cookie = "cookie" | ||
|
||
def __str__(self) -> str: | ||
return self.value | ||
|
||
def __repr__(self) -> str: | ||
return str(self) | ||
class Header(BaseEnum): | ||
authorization = "Authorization" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .base import APIKeyInCookie, APIKeyInHeader, APIKeyInQuery | ||
|
||
__all__ = [ | ||
"APIKeyInCookie", | ||
"APIKeyInHeader", | ||
"APIKeyInQuery", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import Any, Literal, Optional | ||
|
||
from esmerald.openapi.enums import APIKeyIn, SecuritySchemeType | ||
from esmerald.openapi.security.base import HTTPBase | ||
|
||
|
||
class APIKeyInQuery(HTTPBase): | ||
def __init__( | ||
self, | ||
*, | ||
type_: Literal[ | ||
"apiKey", "http", "mutualTLS", "oauth2", "openIdConnect" | ||
] = SecuritySchemeType.apiKey.value, | ||
scheme_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
in_: Optional[Literal["query", "header", "cookie"]] = APIKeyIn.query.value, | ||
name: Optional[str] = None, | ||
**kwargs: Any, | ||
): | ||
super().__init__( | ||
type_=type_, | ||
description=description, | ||
name=name, | ||
in_=in_, | ||
scheme_name=scheme_name or self.__class__.__name__, | ||
**kwargs, | ||
) | ||
|
||
|
||
class APIKeyInHeader(HTTPBase): | ||
def __init__( | ||
self, | ||
*, | ||
type_: Literal[ | ||
"apiKey", "http", "mutualTLS", "oauth2", "openIdConnect" | ||
] = SecuritySchemeType.apiKey.value, | ||
scheme_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
in_: Optional[Literal["query", "header", "cookie"]] = APIKeyIn.header.value, | ||
name: Optional[str] = None, | ||
**kwargs: Any, | ||
): | ||
super().__init__( | ||
type_=type_, | ||
description=description, | ||
name=name, | ||
in_=in_, | ||
scheme_name=scheme_name or self.__class__.__name__, | ||
**kwargs, | ||
) | ||
|
||
|
||
class APIKeyInCookie(HTTPBase): | ||
def __init__( | ||
self, | ||
*, | ||
type_: Literal[ | ||
"apiKey", "http", "mutualTLS", "oauth2", "openIdConnect" | ||
] = SecuritySchemeType.apiKey.value, | ||
scheme_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
in_: Optional[Literal["query", "header", "cookie"]] = APIKeyIn.cookie.value, | ||
name: Optional[str] = None, | ||
**kwargs: Any, | ||
): | ||
super().__init__( | ||
type_=type_, | ||
description=description, | ||
name=name, | ||
in_=in_, | ||
scheme_name=scheme_name or self.__class__.__name__, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Any, Literal, Optional, Union | ||
|
||
from pydantic import AnyUrl, BaseModel, ConfigDict | ||
|
||
from esmerald.openapi.models import SecurityScheme | ||
|
||
|
||
class HTTPAuthorizationCredentials(BaseModel): | ||
scheme: str | ||
credentials: str | ||
|
||
|
||
class HTTPBase(SecurityScheme): | ||
""" | ||
Base for all HTTP security headers. | ||
""" | ||
|
||
model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True) | ||
|
||
def __init__( | ||
self, | ||
*, | ||
type_: Optional[Literal["apiKey", "http", "mutualTLS", "oauth2", "openIdConnect"]] = None, | ||
bearerFormat: Optional[str] = None, | ||
scheme_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
in_: Optional[Literal["query", "header", "cookie"]] = None, | ||
name: Optional[str] = None, | ||
scheme: Optional[str] = None, | ||
openIdConnectUrl: Optional[Union[AnyUrl, str]] = None, | ||
**kwargs: Any, | ||
): | ||
super().__init__( # type: ignore | ||
type=type_, | ||
bearerFormat=bearerFormat, | ||
description=description, | ||
name=name, | ||
security_scheme_in=in_, | ||
scheme_name=scheme_name, | ||
scheme=scheme, | ||
openIdConnectUrl=openIdConnectUrl, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# from .base import HTTPBasic as HTTPBasic | ||
# from .base import HTTPAuthorizationCredentials as HTTPAuthorizationCredentials | ||
# from .base import HTTPBasicCredentials as HTTPBasicCredentials | ||
from .base import Bearer as Bearer | ||
from .base import Basic, Bearer, Digest | ||
|
||
__all__ = [ | ||
"Basic", | ||
"Bearer", | ||
"Digest", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .base import OAuth2 | ||
|
||
__all__ = [ | ||
"OAuth2", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Any, Dict, Literal, Optional, Union | ||
|
||
from esmerald.openapi.enums import SecuritySchemeType | ||
from esmerald.openapi.models import OAuthFlows | ||
from esmerald.openapi.security.base import HTTPBase | ||
|
||
|
||
class OAuth2(HTTPBase): | ||
""" | ||
The OAuth2 scheme. | ||
For every parameter of the OAuthFlows, expects a OAuthFlow object type. | ||
Example: | ||
implicit: Optional[OAuthFlow] = OAuthFlow() | ||
password: Optional[OAuthFlow] = OAuthFlow() | ||
clientCredentials: Optional[OAuthFlow] = OAuthFlow() | ||
authorizationCode: Optional[OAuthFlow] = OAuthFlow() | ||
flows: OAuthFlows( | ||
implicit=implicit, | ||
password=password, | ||
clientCredentials=clientCredentials, | ||
authorizationCode=authorizationCode, | ||
) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
type_: Literal[ | ||
"apiKey", "http", "mutualTLS", "oauth2", "openIdConnect" | ||
] = SecuritySchemeType.oauth2.value, | ||
scheme_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
name: Optional[str] = None, | ||
flows: Union[OAuthFlows, Dict[str, Dict[str, Any]]] = OAuthFlows(), | ||
**kwargs: Any, | ||
): | ||
extra: Dict[Any, Any] = {} | ||
extra["flows"] = flows | ||
extra.update(kwargs) | ||
super().__init__( | ||
type_=type_, | ||
description=description, | ||
name=name, | ||
scheme_name=scheme_name or self.__class__.__name__, | ||
**extra, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .base import OpenIdConnect | ||
|
||
__all__ = ["OpenIdConnect"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Any, Literal, Optional, Union | ||
|
||
from pydantic import AnyUrl | ||
|
||
from esmerald.openapi.enums import SecuritySchemeType | ||
from esmerald.openapi.security.base import HTTPBase | ||
|
||
|
||
class OpenIdConnect(HTTPBase): | ||
def __init__( | ||
self, | ||
*, | ||
type_: Literal[ | ||
"apiKey", "http", "mutualTLS", "oauth2", "openIdConnect" | ||
] = SecuritySchemeType.openIdConnect.value, | ||
openIdConnectUrl: Optional[Union[AnyUrl, str]] = None, | ||
scheme_name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
**kwargs: Any, | ||
): | ||
super().__init__( | ||
type_=type_, | ||
description=description, | ||
scheme_name=scheme_name or self.__class__.__name__, | ||
openIdConnectUrl=openIdConnectUrl, | ||
**kwargs, | ||
) |