示例#1
0
from fastapi import FastAPI, Security
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
from fastapi.testclient import TestClient

app = FastAPI()

security = HTTPBase(scheme="Other", description="Other Security Scheme")


@app.get("/users/me")
def read_current_user(
        credentials: HTTPAuthorizationCredentials = Security(security)):
    return {
        "scheme": credentials.scheme,
        "credentials": credentials.credentials
    }


client = TestClient(app)

openapi_schema = {
    "openapi": "3.0.2",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/users/me": {
            "get": {
                "responses": {
                    "200": {
示例#2
0
from typing import Optional

from fastapi import FastAPI, Security
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
from starlette.testclient import TestClient

app = FastAPI()

security = HTTPBase(scheme="Other", auto_error=False)


@app.get("/users/me")
def read_current_user(
    credentials: Optional[HTTPAuthorizationCredentials] = Security(
        security), ):
    if credentials is None:
        return {"msg": "Create an account first"}
    return {
        "scheme": credentials.scheme,
        "credentials": credentials.credentials
    }


client = TestClient(app)

openapi_schema = {
    "openapi": "3.0.2",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
示例#3
0
from fastapi import FastAPI, Security
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
from starlette.testclient import TestClient

app = FastAPI()

security = HTTPBase(scheme="Other")


@app.get("/users/me")
def read_current_user(
        credentials: HTTPAuthorizationCredentials = Security(security)):
    return {
        "scheme": credentials.scheme,
        "credentials": credentials.credentials
    }


client = TestClient(app)

openapi_schema = {
    "openapi": "3.0.2",
    "info": {
        "title": "Fast API",
        "version": "0.1.0"
    },
    "paths": {
        "/users/me": {
            "get": {
                "responses": {
                    "200": {
示例#4
0
文件: admin.py 项目: marco79423/paji
import fastapi
from fastapi import Depends, Security, BackgroundTasks, HTTPException
from fastapi.security import HTTPAuthorizationCredentials
from fastapi.security.http import HTTPBase
from sqlalchemy.orm import Session

from paji import utils
from paji.service.jessigod import schemas, core
from paji.service.jessigod.dependencies import get_db, get_jessigod_config

router = fastapi.APIRouter()

admin_security = HTTPBase(scheme='Jessigod')


@router.post('/api/jessigod/preach', response_model=schemas.TaskOut)
async def create_propagation_task(
        task_in: schemas.TaskIn,
        background_tasks: BackgroundTasks,
        credentials: HTTPAuthorizationCredentials = Security(admin_security),
        db: Session = Depends(get_db),
        jessigod_config=fastapi.Depends(get_jessigod_config),
):

    if credentials.credentials != jessigod_config.admin_token:
        raise HTTPException(status_code=403, detail='你沒有管理員權限')

    task_id = utils.generate_id()
    background_tasks.add_task(core.handle_propagation_task, task_id, jessigod_config, task_in, db)

    return schemas.TaskOut(
示例#5
0
from typing import Optional

import fastapi
from fastapi import Depends, Query, Security
from fastapi.security import HTTPAuthorizationCredentials
from fastapi.security.http import HTTPBase
from sqlalchemy.orm import Session

from paji.service.jessigod import schemas, core
from paji.service.jessigod.dependencies import get_db
from secrets import compare_digest

router = fastapi.APIRouter()

security = HTTPBase(scheme='Jessi')


@router.get('/api/jessigod/origins', response_model=schemas.OriginsOut)
async def get_origins(db: Session = Depends(get_db)):
    origins = core.get_origins(db)
    return schemas.OriginsOut(data=[
        schemas.Origin(
            id=origin.id,
            name=origin.name,
        ) for origin in origins
    ], )


@router.get('/api/jessigod/sayings', response_model=schemas.SayingsOut)
async def get_sayings(
        page_index: int = Query(
示例#6
0
def authenticate_user(token: str = Depends(HTTPBase(scheme='Bearer'))):
    payload = decode_token(token.credentials)
    return payload