-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from koxudaxi/support_security
Support security
- Loading branch information
Showing
14 changed files
with
410 additions
and
15 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
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,47 @@ | ||
|
||
from __future__ import annotations | ||
|
||
from typing import List, Optional | ||
|
||
from fastapi import Depends, FastAPI, HTTPException, Query | ||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | ||
from pydantic import BaseModel | ||
from starlette import status | ||
|
||
{{ imports }} | ||
|
||
app = FastAPI() | ||
|
||
|
||
DUMMY_CREDENTIALS = 'abcdefg' | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
email: str | ||
|
||
|
||
def get_dummy_user(token: str) -> User: | ||
return User(username=token, email='[email protected]') | ||
|
||
|
||
async def valid_token(auth: HTTPAuthorizationCredentials = Depends(HTTPBearer())) -> str: | ||
if auth.credentials == DUMMY_CREDENTIALS: | ||
return 'dummy' | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid authentication credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
|
||
async def valid_current_user(token: str = Depends(valid_token)) -> User: | ||
return get_dummy_user(token) | ||
|
||
|
||
|
||
{% for operation in operations %} | ||
@app.{{operation.type}}('{{operation.snake_case_path}}', response_model={{operation.response}}) | ||
def {{operation.function_name}}({{operation.snake_case_arguments}}{%- if operation.security -%}{%- if operation.snake_case_arguments -%}, {%- endif -%}user: User = Depends(valid_current_user){%- endif -%}) -> {{operation.response}}: | ||
pass | ||
{% endfor %} |
80 changes: 80 additions & 0 deletions
80
tests/data/expected/openapi/custom_template_security/custom_security/main.py
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,80 @@ | ||
# generated by fastapi-codegen: | ||
# filename: custom_security.yaml | ||
# timestamp: 2020-06-19T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from fastapi import Depends, FastAPI, HTTPException, Query | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
from starlette import status | ||
|
||
from .models import Pet, PetForm | ||
|
||
app = FastAPI() | ||
|
||
|
||
DUMMY_CREDENTIALS = 'abcdefg' | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
email: str | ||
|
||
|
||
def get_dummy_user(token: str) -> User: | ||
return User(username=token, email='[email protected]') | ||
|
||
|
||
async def valid_token( | ||
auth: HTTPAuthorizationCredentials = Depends(HTTPBearer()), | ||
) -> str: | ||
if auth.credentials == DUMMY_CREDENTIALS: | ||
return 'dummy' | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid authentication credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
|
||
async def valid_current_user(token: str = Depends(valid_token)) -> User: | ||
return get_dummy_user(token) | ||
|
||
|
||
@app.get('/food/{food_id}', response_model=None) | ||
def show_food_by_id(food_id: str, user: User = Depends(valid_current_user)) -> None: | ||
pass | ||
|
||
|
||
@app.get('/pets', response_model=List[Pet]) | ||
def list_pets( | ||
limit: Optional[int] = 0, | ||
home_address: Optional[str] = Query('Unknown', alias='HomeAddress'), | ||
kind: Optional[str] = 'dog', | ||
) -> List[Pet]: | ||
pass | ||
|
||
|
||
@app.post('/pets', response_model=None) | ||
def post_pets(body: PetForm, user: User = Depends(valid_current_user)) -> None: | ||
pass | ||
|
||
|
||
@app.get('/pets/{pet_id}', response_model=Pet) | ||
def show_pet_by_id( | ||
pet_id: str = Query(..., alias='petId'), user: User = Depends(valid_current_user) | ||
) -> Pet: | ||
pass | ||
|
||
|
||
@app.put('/pets/{pet_id}', response_model=None) | ||
def put_pets_pet_id( | ||
pet_id: str = Query(..., alias='petId'), | ||
body: PetForm = None, | ||
user: User = Depends(valid_current_user), | ||
) -> None: | ||
pass |
23 changes: 23 additions & 0 deletions
23
tests/data/expected/openapi/custom_template_security/custom_security/models.py
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,23 @@ | ||
# generated by datamodel-codegen: | ||
# filename: custom_security.yaml | ||
# timestamp: 2020-06-19T00:00:00+00:00 | ||
|
||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Pet(BaseModel): | ||
id: int | ||
name: str | ||
tag: Optional[str] = None | ||
|
||
|
||
class Error(BaseModel): | ||
code: int | ||
message: str | ||
|
||
|
||
class PetForm(BaseModel): | ||
name: Optional[str] = None | ||
age: Optional[int] = None |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.