|
| 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 |
0 commit comments