Skip to content

Commit

Permalink
Use string for identity in JWT
Browse files Browse the repository at this point in the history
This is required because of vimalloc/flask-jwt-extended#556
  • Loading branch information
jslvtr committed Nov 27, 2024
1 parent 37e6ff6 commit 0625b93
Show file tree
Hide file tree
Showing 35 changed files with 56 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class UserLogin(MethodView):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
```

Here you can see the when we call `create_access_token(identity=user.id)` we pass in the user's `id`. This is what gets stored (among other things) inside the JWT, so when the client sends the JWT back on every request, we can tell who the JWT belongs to.
Here you can see the when we call `create_access_token(identity=str(user.id))` we pass in the user's `id`. This is what gets stored (among other things) inside the JWT, so when the client sends the JWT back on every request, we can tell who the JWT belongs to.

**Update Nov 2024**: Before now, we used `identity=user.id`, but now we have to convert it to a string first.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ class UserLogin(MethodView):

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
# highlight-start
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200
# highlight-end

abort(401, message="Invalid credentials.")
```

**Update Nov 2024**: Before now, we used `identity=user.id`, but now we have to convert it to a string first.

## Writing the token refresh endpoint

When a user logs in, they will now have the access token and the refresh token.
Expand All @@ -91,6 +93,8 @@ class TokenRefresh(MethodView):
return {"access_token": new_token}, 200
```

**Note**: here we don't need to call `str(current_user)` because `get_jwt_identity()` returns what we previously stored, which is a string.

Note that above, we've told Flask-JWT-Extended that a refresh token is required with `@jwt_required(refresh=True)`. We'll do something similar for requiring fresh tokens in a second!

## Requiring a fresh token for certain endpoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id)
access_token = create_access_token(identity=str(user.id))
return {"access_token": access_token}, 200

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def post(self, user_data):
).first()

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
refresh_token = create_refresh_token(identity=user.id)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(identity=str(user.id))
return {"access_token": access_token, "refresh_token": refresh_token}

abort(401, message="Invalid credentials.")
Expand Down
2 changes: 1 addition & 1 deletion project/using-flask-restful/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def post(self):
user = UserModel.find_by_username(data["username"])

if user and pbkdf2_sha256.verify(data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
2 changes: 1 addition & 1 deletion project/using-flask-restx/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def post(self):
user = UserModel.find_by_username(user_data["username"])

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down
2 changes: 1 addition & 1 deletion project/using-flask-smorest/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def post(self, user_data):
user = UserModel.find_by_username(user_data["username"])

if user and pbkdf2_sha256.verify(user_data["password"], user.password):
access_token = create_access_token(identity=user.id, fresh=True)
access_token = create_access_token(identity=str(user.id), fresh=True)
refresh_token = create_refresh_token(user.id)
return {"access_token": access_token, "refresh_token": refresh_token}, 200

Expand Down

0 comments on commit 0625b93

Please sign in to comment.