Skip to content

Commit a52ff39

Browse files
author
IndominusByte
committed
add howt to access & extract token in readme
1 parent 475257b commit a52ff39

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,75 @@ INFO: Waiting for application startup.
7272
INFO: Application startup complete.
7373
INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
7474
```
75+
### Access it
76+
To access a jwt_required protected url, all we have to do is send in the JWT with the request. By default, this is done with an authorization header that looks like:
77+
```bash
78+
Authorization: Bearer <access_token>
79+
```
80+
We can see this in action using CURL:
81+
```console
82+
$ curl http://localhost:5000/protected
83+
84+
{"detail":"Missing Authorization Header"}
85+
86+
$ curl -H "Content-Type: application/json" -X POST \
87+
-d '{"username":"test","password":"test"}' http://localhost:5000/login
88+
89+
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTczMzMxMzMsIm5iZiI6MTU5NzMzMzEzMywianRpIjoiNDczY2ExM2ItOWI1My00NDczLWJjZTctMWZiOWMzNTlmZmI0IiwiZXhwIjoxNTk3MzM0MDMzLCJpZGVudGl0eSI6InRlc3QiLCJ0eXBlIjoiYWNjZXNzIiwiZnJlc2giOmZhbHNlfQ.42CusQo6nsLxOk6bBUP1vnVX-REx4ZYBYYIjYChWf0c"
90+
91+
$ export TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTczMzMxMzMsIm5iZiI6MTU5NzMzMzEzMywianRpIjoiNDczY2ExM2ItOWI1My00NDczLWJjZTctMWZiOWMzNTlmZmI0IiwiZXhwIjoxNTk3MzM0MDMzLCJpZGVudGl0eSI6InRlc3QiLCJ0eXBlIjoiYWNjZXNzIiwiZnJlc2giOmZhbHNlfQ.42CusQo6nsLxOk6bBUP1vnVX-REx4ZYBYYIjYChWf0c
92+
93+
$ curl -H "Authorization: Bearer $TOKEN" http://localhost:5000/protected
94+
95+
{"logged_in_as":"test"}
96+
```
97+
## Extract Token
98+
Access all URL to see what the result
99+
```python
100+
from pydantic import BaseModel, Field
101+
from fastapi import FastAPI, Depends, HTTPException
102+
from fastapi_jwt_auth import AuthJWT
103+
104+
app = FastAPI()
105+
106+
class User(BaseModel):
107+
username: str = Field(...,min_length=1)
108+
password: str = Field(...,min_length=1)
109+
110+
@app.post('/login',status_code=200)
111+
def login(user: User):
112+
if user.username != 'test' or user.password != 'test':
113+
raise HTTPException(status_code=401,detail='Bad username or password')
114+
115+
access_token = AuthJWT.create_access_token(identity=user.username)
116+
return access_token
117+
118+
# Returns the JTI (unique identifier) of an encoded JWT
119+
@app.get('/get-jti',status_code=200)
120+
def get_jti():
121+
access_token = AuthJWT.create_access_token(identity='test')
122+
return AuthJWT.get_jti(encoded_token=access_token)
123+
124+
# this will return the identity of the JWT that is accessing this endpoint.
125+
# If no JWT is present, `None` is returned instead.
126+
@app.get('/get-jwt-identity',status_code=200)
127+
def get_jwt_identity(Authorize: AuthJWT = Depends()):
128+
Authorize.jwt_optional()
129+
130+
current_user = Authorize.get_jwt_identity()
131+
return {"logged_in_as": current_user}
132+
133+
# this will return the python dictionary which has all
134+
# of the claims of the JWT that is accessing the endpoint.
135+
# If no JWT is currently present, return None instead
136+
@app.get('/get-raw-jwt',status_code=200)
137+
def get_raw_jwt(Authorize: AuthJWT = Depends()):
138+
Authorize.jwt_optional()
139+
140+
token = Authorize.get_raw_jwt()
141+
return {"token": token}
142+
```
143+
75144
## Configuration Options
76145
- `AUTHJWT_ACCESS_TOKEN_EXPIRES`<br/>
77146
How long an access token should live before it expires. If you not define in env variable

0 commit comments

Comments
 (0)