Skip to content

Commit 3e1da22

Browse files
author
IndominusByte
committed
add api documentation
1 parent 046b2eb commit 3e1da22

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

docs/api-doc.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
In here you will find the API for everything exposed in this extension.
2+
3+
### Configuring FastAPI JWT Auth
4+
5+
**load_config**(callback)
6+
: *This decorator sets the callback function to overwrite state on AuthJWT class so
7+
when you initialize an instance in dependency injection default value will be overwritten.*
8+
9+
**Hint**: *The callback must be a function that returns a list of tuple or pydantic object.*
10+
11+
**token_in_denylist_loader**(callback)
12+
: *This decorator sets the callback function that will be called when
13+
a protected endpoint is accessed and will check if the JWT has been
14+
been revoked. By default, this callback is not used.*
15+
16+
**Hint**: *The callback must be a function that takes `one` argument, which is the decoded JWT (python dictionary),
17+
and returns `True` if the token has been revoked, or `False` otherwise.*
18+
19+
### Protected Endpoint
20+
21+
**jwt_required**()
22+
: *If you call this function, it will ensure that the requester has a valid access token before
23+
executing the code below your router. This does not check the freshness of the access token.*
24+
25+
**jwt_optional**()
26+
: *If an access token present in the request, this will call the endpoint with `get_jwt_identity()`
27+
having the identity of the access token. If no access token is present in the request, this endpoint
28+
will still be called, but `get_jwt_identity()` will return None instead.*
29+
30+
*If there is an invalid access token in the request (expired, tampered with, etc),
31+
this will still call the appropriate error handler.*
32+
33+
**jwt_refresh_token_required**()
34+
: *If you call this function, it will ensure that the requester has a valid refresh token before
35+
executing the code below your router.*
36+
37+
**fresh_jwt_required**()
38+
: *If you call this function, it will ensure that the requester has a valid and fresh access token before
39+
executing the code below your router.*
40+
41+
### Utilities
42+
43+
**create_access_token**(subject, fresh=False, algorithm=None, headers=None, expires_time=None, audience=None, user_claims={})
44+
: *Create a new access token.*
45+
46+
* Parameters:
47+
* **subject**: Identifier for who this token is for example id or username from database
48+
* **fresh**: Identify token is fresh or non-fresh
49+
* **algorithm**: Algorithm allowed to encode the token
50+
* **headers**: Valid dict for specifying additional headers in JWT header section
51+
* **expires_time**: Set the duration of the JWT
52+
* **audience**: Expected audience in the JWT
53+
* **user_claims**: Custom claims to include in this token. This data must be dictionary
54+
* Returns: An encoded access token
55+
56+
**create_refresh_token**(subject, algorithm=None, headers=None, expires_time=None, audience=None, user_claims={})
57+
: *Creates a new refresh token.*
58+
59+
* Parameters:
60+
* **subject**: Identifier for who this token is for example id or username from database
61+
* **algorithm**: Algorithm allowed to encode the token
62+
* **headers**: Valid dict for specifying additional headers in JWT header section
63+
* **expires_time**: Set the duration of the JWT
64+
* **audience**: Expected audience in the JWT
65+
* **user_claims**: Custom claims to include in this token. This data must be dictionary
66+
* Returns: An encoded refresh token
67+
68+
**set_access_cookies**(encoded_access_token, max_age=None)
69+
: *Configures the response to set access token in a cookie. This will also set the CSRF double submit values
70+
in a separate cookie.*
71+
72+
* Parameters:
73+
* **encoded_access_token**: The encoded access token to set in the cookies
74+
* **max_age**: The max age of the cookie value should be `integer` the number of seconds
75+
* Returns: None
76+
77+
**set_refresh_cookies**(encoded_refresh_token, max_age=None)
78+
: *Configures the response to set refresh token in a cookie. This will also set the CSRF double submit values
79+
in a separate cookie.*
80+
81+
* Parameters:
82+
* **encoded_refresh_token**: The encoded refresh token to set in the cookies
83+
* **max_age**: The max age of the cookie value should be `integer` the number of seconds
84+
* Returns: None
85+
86+
**unset_jwt_cookies**()
87+
: *Unset (delete) all jwt stored in a cookies.*
88+
89+
**unset_access_cookies**()
90+
: *Remove access token and access CSRF double submit from the response cookies.*
91+
92+
**unset_refresh_cookies**()
93+
: *Remove refresh token and refresh CSRF double submit from the response cookies.*
94+
95+
**get_raw_jwt**()
96+
: *This will return the python dictionary which has all of the claims of the JWT that is accessing the endpoint.
97+
If no JWT is currently present, return `None` instead.*
98+
99+
**get_jti**(encoded_token)
100+
: *Returns the JTI (unique identifier) of an encoded JWT*
101+
102+
* Parameters:
103+
* **encoded_token**: The encoded JWT from parameter
104+
* Returns: String of JTI
105+
106+
**get_jwt_subject**()
107+
: *This will return the subject of the JWT that is accessing the endpoint.
108+
If no JWT is present, `None` is returned instead.*
109+
110+
**get_unverified_jwt_headers**(encoded_token=None)
111+
: *Returns the Headers of an encoded JWT without verifying the actual signature of JWT.*
112+
113+
* Parameters:
114+
* **encoded_token**: The encoded JWT to get the Header from protected endpoint or from parameter
115+
* Returns: JWT header parameters as a dictionary

docs/usage/jwt-in-cookies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Highly recommended using JWT in cookies, if your frontend interaction with the backend, your frontend may be storing JWT in the browser localStorage or sessionStorage. There is nothing wrong with this, but if you have any sort of XSS vulnerability on your site, an attacker will be able to trivially steal your tokens. If you want some additional security on your site, you can save your JWT in an httponly cookies. Which keeps javascript cannot be able to access the cookies.
1+
Highly recommended using JWT in cookies, if your frontend interacts with the backend, your frontend may be storing JWT in the browser localStorage or sessionStorage. There is nothing wrong with this, but if you have any sort of XSS vulnerability on your site, an attacker will be able to trivially steal your tokens. If you want some additional security on your site, you can save your JWT in an httponly cookies. Which keeps javascript cannot be able to access the cookies.
22

33
Here is a basic example of how to store JWT in cookies:
44

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ nav:
4343
- Denylist Options: configuration/denylist.md
4444
- Cookies Options: configuration/cookies.md
4545
- CSRF Options: configuration/csrf.md
46+
- API Documentation: api-doc.md
4647
- Development - Contributing: contributing.md
4748
- Release Notes: release-notes.md
4849

0 commit comments

Comments
 (0)