Skip to content

Commit f204ad8

Browse files
committed
docs: update to reflect changes in interface
1 parent fa891ac commit f204ad8

File tree

7 files changed

+51
-94
lines changed

7 files changed

+51
-94
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ pip install fastapi-oidc
5050

5151
## Usage
5252

53+
See this example for how to use `docker-compose` to set up authentication with
54+
fastapi-oidc + keycloak.
55+
56+
### Standard usage
57+
5358
```python3
5459
from typing import Optional
5560

@@ -72,7 +77,12 @@ auth = Auth(
7277
app = FastAPI(
7378
title="Example",
7479
version="dev",
75-
dependencies=[Depends(auth.implicit_scheme)], # multiple schemes available
80+
dependencies=[Depends(auth.implicit_scheme)],
81+
# multiple available schemes:
82+
# - oidc_scheme (displays all schemes supported by the auth server in docs)
83+
# - password_scheme
84+
# - implicit_scheme
85+
# - authcode_scheme
7686
)
7787

7888
@app.get("/protected")
@@ -90,12 +100,8 @@ class MyAuthenticatedUser(IDToken):
90100
custom_field: str
91101
custom_default: float = 3.14
92102

93-
94-
app = FastAPI()
95-
96-
authenticate_user = get_auth(...)
97-
98-
@app.get("/protected")
99-
def protected(user: MyAuthenticatedUser = Depends(authenticate_user)):
100-
return {"Hello": "World", "custom_field": user.custom_field}
103+
auth = Auth(
104+
...,
105+
idtoken_model=MyAuthenticatedUser,
106+
)
101107
```

docs/index.rst

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,38 @@ Basic configuration for verifying OIDC tokens.
4646

4747
.. code-block:: python3
4848
49+
from typing import Optional
50+
4951
from fastapi import Depends
5052
from fastapi import FastAPI
53+
from fastapi import Security
54+
from fastapi import status
55+
56+
from fastapi_oidc import Auth
57+
from fastapi_oidc import KeycloakIDToken
58+
59+
auth = Auth(
60+
openid_connect_url="http://localhost:8080/auth/realms/my-realm/.well-known/openid-configuration",
61+
issuer="http://localhost:8080/auth/realms/my-realm", # optional, verification only
62+
client_id="my-client", # optional, verification only
63+
scopes=["email"], # optional, verification only
64+
idtoken_model=KeycloakIDToken, # optional, verification only
65+
)
5166
52-
from fastapi_oidc import IDToken
53-
from fastapi_oidc import get_auth
54-
55-
56-
app = FastAPI()
57-
58-
authenticate_user = get_auth(
59-
openid_connect_url="https://dev-123456.okta.com/.well-known/openid-configuration",
60-
issuer="dev-126594.okta.com", # optional, verification only
61-
audience="https://yourapi.url.com/api", # optional, verification only
62-
signature_cache_ttl=3600, # optional
67+
app = FastAPI(
68+
title="Example",
69+
version="dev",
70+
dependencies=[Depends(auth.implicit_scheme)],
71+
# multiple available schemes:
72+
# - oidc_scheme (displays all schemes supported by the auth server in docs)
73+
# - password_scheme
74+
# - implicit_scheme
75+
# - authcode_scheme
6376
)
6477
6578
@app.get("/protected")
66-
def protected(id_token: IDToken = Depends(authenticate_user)):
67-
return {"Hello": "World", "user_email": id_token.email}
79+
def protected(id_token: KeycloakIDToken = Security(auth.required)):
80+
return dict(message=f"You are {id_token.email}")
6881
6982
7083
API Reference

example/__init__.py

Whitespace-only changes.

example/main.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

fastapi_oidc/auth.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
Module for validating Open ID Connect ID Tokens.
3+
Module for validating Open ID Connect tokens.
44
55
Usage
66
=====
77
88
.. code-block:: python3
99
1010
# This assumes you've already configured get_auth in your_app.py
11-
from your_app.auth import authenticate_user
11+
from your_app.auth import auth
1212
1313
@app.get("/auth")
14-
def test_auth(authenticated_user: AuthenticatedUser = Depends(authenticate_user)):
15-
name = authenticated_user.preferred_username
16-
return f"Hello {name}"
14+
def test_auth(authenticated_user: IDToken = Depends(auth.required)):
15+
return f"Hello {authenticated_user.preferred_username}"
1716
"""
1817

1918
from typing import List
@@ -65,8 +64,8 @@ def __init__(
6564
issuer (URL): (Optional) The issuer URL from your auth server.
6665
client_id (str): (Optional) The client_id configured by your auth server.
6766
scopes (Dict[str, str]): (Optional) A dictionary of scopes and their descriptions.
68-
signature_cache_ttl (int): How many seconds your app should cache the
69-
authorization server's public signatures.
67+
signature_cache_ttl (int): (Optional) How many seconds your app should
68+
cache the authorization server's public signatures.
7069
idtoken_model (Type): (Optional) The model to use for validating the ID Token.
7170
7271
Raises:
@@ -123,7 +122,7 @@ def required(
123122
HTTPBearer()
124123
),
125124
) -> IDToken:
126-
"""Validate and parse OIDC ID token against issuer in config.
125+
"""Validate and parse OIDC ID token against configuration.
127126
Note this function caches the signatures and algorithms of the issuing
128127
server for signature_cache_ttl seconds.
129128
@@ -157,7 +156,7 @@ def optional(
157156
HTTPBearer(auto_error=False)
158157
),
159158
) -> Optional[IDToken]:
160-
"""Optionally validate and parse OIDC ID token against issuer in config.
159+
"""Optionally validate and parse OIDC ID token against configuration.
161160
Will not raise if the user is not authenticated. Note this function
162161
caches the signatures and algorithms of the issuing server for
163162
signature_cache_ttl seconds.
@@ -186,7 +185,7 @@ def authenticate_user(
186185
authorization_credentials: Optional[HTTPAuthorizationCredentials],
187186
auto_error: bool,
188187
) -> Optional[IDToken]:
189-
"""Validate and parse OIDC ID token against issuer in config.
188+
"""Validate and parse OIDC ID token against against configuration.
190189
Note this function caches the signatures and algorithms of the issuing server
191190
for signature_cache_ttl seconds.
192191

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ build-backend = "poetry.masonry.api"
3737
profile = "black"
3838
force_single_line = "True"
3939
known_first_party = []
40-
known_third_party = ["cachetools", "cryptography", "fastapi", "jose", "jwt", "pydantic", "pytest", "requests", "starlette", "uvicorn"]
40+
known_third_party = ["cachetools", "cryptography", "fastapi", "jose", "jwt", "pydantic", "pytest", "requests"]

tests/test_auth.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test__authenticate_user(
2929
assert id_token.aud == config_w_aud["client_id"]
3030

3131

32-
# Ensure that when no audience is supplied, that the audience defaults to client ID
3332
def test__authenticate_user_no_aud(
3433
monkeypatch,
3534
mock_discovery,

0 commit comments

Comments
 (0)