@@ -18,6 +18,7 @@ def test_auth(authenticated_user: AuthenticatedUser = Depends(authenticate_user)
18
18
19
19
from typing import Callable
20
20
from typing import Optional
21
+ from typing import Type
21
22
22
23
from fastapi import Depends
23
24
from fastapi import HTTPException
@@ -28,6 +29,7 @@ def test_auth(authenticated_user: AuthenticatedUser = Depends(authenticate_user)
28
29
from jose .exceptions import JWTClaimsError
29
30
30
31
from fastapi_oidc import discovery
32
+ from fastapi_oidc .exceptions import TokenSpecificationError
31
33
from fastapi_oidc .types import IDToken
32
34
33
35
@@ -38,31 +40,43 @@ def get_auth(
38
40
base_authorization_server_uri : str ,
39
41
issuer : str ,
40
42
signature_cache_ttl : int ,
43
+ token_type : Type [IDToken ] = IDToken ,
41
44
) -> Callable [[str ], IDToken ]:
42
45
"""Take configurations and return the authenticate_user function.
43
46
44
47
This function should only be invoked once at the beggining of your
45
48
server code. The function it returns should be used to check user credentials.
46
49
47
50
Args:
48
- client_id (str): This string is provided when you register with your resource server.
51
+ client_id (str): This string is provided when you register with your resource
52
+ server.
53
+ base_authorization_server_uri(URL): Everything before /.wellknow in your auth
54
+ server URL. I.E. https://dev-123456.okta.com
55
+ issuer (URL): Same as base_authorization. This is used to generating OpenAPI3.0
56
+ docs which is broken (in OpenAPI/FastAPI) right now.
57
+ signature_cache_ttl (int): How many seconds your app should cache the
58
+ authorization server's public signatures.
49
59
audience (str): (Optional) The audience string configured by your auth server.
50
60
If not set defaults to client_id
51
- base_authorization_server_uri(URL): Everything before /.wellknow in your auth server URL.
52
- I.E. https://dev-123456.okta.com
53
- issuer (URL): Same as base_authorization. This is used to generating OpenAPI3.0 docs which
54
- is broken (in OpenAPI/FastAPI) right now.
55
- signature_cache_ttl (int): How many seconds your app should cache the authorization
56
- server's public signatures.
61
+ token_type (IDToken or subclass): (Optional) An optional class to be returned by
62
+ the authenticate_user function.
57
63
58
64
59
65
Returns:
60
- func: authenticate_user(auth_header: str)
66
+ func: authenticate_user(auth_header: str) -> IDToken (or token_type)
61
67
62
68
Raises:
63
69
Nothing intentional
64
70
"""
65
- # As far as I can tell this does two things.
71
+
72
+ if not issubclass (token_type , IDToken ):
73
+ raise TokenSpecificationError (
74
+ "Invalid argument for token_type. "
75
+ "Token type must be a subclass of fastapi_oidc.type.IDToken. "
76
+ f"Received { token_type = } "
77
+ )
78
+
79
+ # As far as I can tell the oauth2_scheme does two things.
66
80
# 1. Extracts and returns the Authorization header.
67
81
# 2. Integrates with the OpenAPI3.0 doc generation in FastAPI.
68
82
# This integration doesn't matter much now since OpenAPI
@@ -79,8 +93,8 @@ def authenticate_user(auth_header: str = Depends(oauth2_scheme)) -> IDToken:
79
93
for signature_cache_ttl seconds.
80
94
81
95
Args:
82
- auth_header (str): Base64 encoded OIDC Token. This is invoked behind the scenes
83
- by Depends.
96
+ auth_header (str): Base64 encoded OIDC Token. This is invoked behind the
97
+ scenes by Depends.
84
98
85
99
Return:
86
100
IDToken (types.IDToken):
@@ -103,27 +117,9 @@ def authenticate_user(auth_header: str = Depends(oauth2_scheme)) -> IDToken:
103
117
# Disabled at_hash check since we aren't using the access token
104
118
options = {"verify_at_hash" : False },
105
119
)
106
- return IDToken .parse_obj (token )
120
+ return token_type .parse_obj (token )
107
121
108
122
except (ExpiredSignatureError , JWTError , JWTClaimsError ) as err :
109
123
raise HTTPException (status_code = 401 , detail = f"Unauthorized: { err } " )
110
124
111
125
return authenticate_user
112
-
113
-
114
- # This is a dummy method for sphinx docs. DO NOT User.
115
- # TODO Find a way to doc higher order functions w/ sphinx.
116
- def authenticate_user (auth_header : str ) -> IDToken : # type: ignore
117
- """
118
- Validate and parse OIDC ID token against issuer in config.
119
- Note this function caches the signatures and algorithms of the issuing server
120
- for signature_cache_ttl seconds.
121
-
122
- Args:
123
- auth_header (str): Base64 encoded OIDC Token. This is invoked behind the scenes
124
- by Depends.
125
-
126
- Return:
127
- IDToken (types.IDToken):
128
- """
129
- pass
0 commit comments