|
| 1 | +## How to use with BadgerDoc |
| 2 | + |
| 3 | +Import and create an instance of tenant dependency. |
| 4 | +To make this dependency work with BadgerDoc you need to provide an url to keycloak and set 'algorithm' arg to `RS256`. |
| 5 | + |
| 6 | +```Python |
| 7 | +from tenant_dependency import TenantData, get_tenant_info |
| 8 | + |
| 9 | +# inner url would look like this "http://badgerdoc-keycloak" (http://{keycloak_pod_name}) |
| 10 | +# outer url would look like this "http://dev1.gcov.ru" |
| 11 | + |
| 12 | +tenant = get_tenant_info(url="http://dev1.gcov.ru", algorithm="RS256", debug=True) |
| 13 | +``` |
| 14 | +## Note: |
| 15 | +#### If you're using algorithm RS256 (Current BadgerDoc algorithm) you don't need arg "key", use only "url". |
| 16 | +#### Otherwise, if you're using algorithm HS256, you need "key" arg and don't need "url". Check `usage_example/main.py`. |
| 17 | +Param `debug` is needed to render `Authorize` button in `/openapi` docs. |
| 18 | + |
| 19 | +Now you can add this dependency to your FastAPI endpoint: |
| 20 | + |
| 21 | +```Python |
| 22 | +from fastapi import Depends, FastAPI |
| 23 | +from tenant_dependency import TenantData, get_tenant_info |
| 24 | + |
| 25 | +app = FastAPI() |
| 26 | +tenant = get_tenant_info(url="http://dev1.gcov.ru", algorithm="RS256", debug=True) |
| 27 | + |
| 28 | + |
| 29 | +@app.post("/test") |
| 30 | +async def get_nums(token: TenantData = Depends(tenant)): |
| 31 | + return token.dict() |
| 32 | +``` |
| 33 | +Default values for `algorithm` and `debug` is `RS256` and `True`, |
| 34 | +so you can create an instance like that: |
| 35 | +```Python |
| 36 | +tenant = get_tenant_info(url="http://dev1.gcov.ru") |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +Go to docs and check how it works. |
| 41 | + |
| 42 | +1) Click `Authorize` and submit token. |
| 43 | +2) Try to use endpoint. |
| 44 | + |
| 45 | +Without valid jwt this endpoint `/test` will raise 401 Error. |
| 46 | + |
| 47 | + |
| 48 | +```Python |
| 49 | +@app.post("/test") |
| 50 | +async def get_nums(token: TenantData = Depends(tenant)): |
| 51 | + return token.dict() |
| 52 | +``` |
| 53 | +This dependency will: |
| 54 | +1) Check if incoming request came with header `Authorization`, if that header wasn't provided Error 401 will be raised. |
| 55 | +2) If header `Authorization` exists, dependency will try to validate it with signature key or url those you put as args to dependency `tenant = get_tenant_info(key="SECRET") / tenant = get_tenant_info(url="http://bagerdoc-keycloack")`. If is key invalid or token is expired 401 Error will be raised. |
| 56 | +3) It also checks header for arg "X-Current-Tenant", so if that arg isn't provided 401 will be raised. In case "X-Current-Tenant" provided dependency will check "X-Current-Tenant" containing in `tenants` array. Raises 401 if check wasn't successful. |
| 57 | +4) If token is valid dependency will parse token's body and get data from it. Token must contain data about `tenants`, `user_id` and `roles`, otherwise 403 error will be raised. |
| 58 | +5) If previous steps were successful, your endpoint can do anything further, and you can work with `token` arg that is actually is a pydantic model, so you can work with it like you work with other pydantic models. |
0 commit comments