fastapi-oidc icon indicating copy to clipboard operation
fastapi-oidc copied to clipboard

How to implement fastapi-oidc in my app?

Open stribizhev opened this issue 3 years ago • 0 comments

Can you please provide a complete walk-through how to implement the example in an existing code?

I received the client_id, base_authorization_server_uri, issuer (must be the same as base_authorization_server_uri in my case, this is what I was told ), and I have tried using the code as shown in the readme with these values, but all I get is an empty "Available authorizations" dialog and "Not authenticated" error message in the Swagger UI.

So, what else should be added to a code in main.py like

import datetime, os, random, time
from pathlib import Path
from typing import Union, Optional
from fastapi import FastAPI, Depends, File, UploadFile, status
from fastapi.responses import FileResponse

from collections.abc import Callable
from fastapi_oidc import IDToken, get_auth

OIDC_config = {
    "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "base_authorization_server_uri": "https://url",
    "issuer": "https://url",
    "signature_cache_ttl": 3600,
}
swagger_ui_init_oauth = {
    "clientId": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "appName": "name",
    "usePkceWithAuthorizationCodeGrant": "true",
    ###"clientSecret": "secret",   ### Told not to use it if I use PKCE
    "oauth2RedirectUrl": "https://redirURL",
    "scopeSeparator": " ",
    "scopes": "scopes...",
}

authenticate_user: Callable = get_auth(**OIDC_config)

app = FastAPI(swagger_ui_init_oauth=swagger_ui_init_oauth)

@app.post("/highlight_file/", tags=['highlight_file'])
async def highlight_file(file: UploadFile = File(...), id_token: IDToken = Depends(authenticate_user)):
    print(f'ID Token: {id_token}')
    return await highlight_file_function(file)

async def highlight_file_function(file):
    # process file code

stribizhev avatar Jul 28 '22 11:07 stribizhev