Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

fastapi oauth2

from typing import Union

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
    username: str
    email: Union[str, None] = None
    full_name: Union[str, None] = None
    disabled: Union[bool, None] = None


def fake_decode_token(token):
    return User(
        username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
    )


async def get_current_user(token: str = Depends(oauth2_scheme)):
    user = fake_decode_token(token)
    return user


@app.get("/users/me")
async def read_users_me(current_user: User = Depends(get_current_user)):
    return current_user
Comment

PREVIOUS NEXT
Code Example
Python :: python sys.argv exception 
Python :: python datetime move forward one day 
Python :: distance matrix gogle map python 
Python :: load static 
Python :: array of objects in python 
Python :: np.random.exponential 
Python :: how to install package offline 
Python :: python dataframe to excel 
Python :: raising custom exception python 
Python :: Filter Pandas rows by specific string elements 
Python :: how to return a missing element in python 
Python :: example of ternary operator in python 
Python :: python input string 
Python :: Reverse an string Using Stack in Python 
Python :: private instance attribute python 
Python :: concat dataframe pandas 
Python :: créer fonction python 
Python :: how to hide ticks marks in matplotlib 
Python :: deep clone 2d list python 
Python :: Group based sort pandas 
Python :: ubuntu 20.10 python 3.8 
Python :: gensim show_topics get topic 
Python :: addition of array in python with input 
Python :: how to fix def multiply(a ,b): a*b 
Python :: if a list has a string remove 
Python :: python for in for in 
Python :: How to Get the Union of Sets in Python 
Python :: django sign up 
Python :: format date string python 
Python :: how to change value of categorical variable in python 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =