Skip to content

JWT Cookie Authentication #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 12, 2019
Merged

JWT Cookie Authentication #75

merged 18 commits into from
Feb 12, 2019

Conversation

mongkok
Copy link
Member

@mongkok mongkok commented Jan 29, 2019

Resolve #55

When a token is requested and jwt_cookie decorator is set, the response will set the given cookie with the token string:

from django.urls import path

from graphene_django.views import GraphQLView
from graphql_jwt.decoratos import jwt_cookie

urlpatterns = [
    path('graphql/', jwt_cookie(GraphQLView.as_view())),
]

Update: Used graphene_django.views instead of graphql_extensions.views.

@codecov-io
Copy link

codecov-io commented Jan 29, 2019

Codecov Report

Merging #75 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #75   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          17     17           
  Lines         280    280           
  Branches       31     31           
=====================================
  Hits          280    280
Impacted Files Coverage Δ
graphql_jwt/middleware.py 100% <100%> (ø) ⬆️
graphql_jwt/backends.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34844d6...dd94984. Read the comment docs.

@mongkok mongkok changed the title JWT Cookie Authentication #55 JWT Cookie Authentication Jan 29, 2019
from django.urls import path

from graphql_extensions.views import GraphQLView
from graphql_jwt.decoratos import jwt_cookie

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in from graphql_jwt.decorators import jwt_cookie

@@ -62,14 +64,14 @@ def check_perms(user):

def token_auth(f):
@wraps(f)
@setup_jwt_cookie
def wrapper(cls, root, info, password, **kwargs):
def on_resolve(values):
user, payload = values
payload.token = get_token(user, info.context)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to get the token from cookie in get_token?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here, this is to request a new token.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was talking about adding the logic inside get_token function. Though it seems like not the right place. I did it like that: mtszsobczak@928fc4c looks like it is working ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that should work :)

@@ -0,0 +1,13 @@
Cookie authentication

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am really glad that you are working on this! Thanks :). I did some testing and it seems that when token is only in cookie request.user is an AnonymousUser. Cookie can be seen on request.cookies though. I am not very familiar with the code base so I am not sure whether the middleware or just get_token need extension (or none :) ). I am happy to help with this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mtszsobczak !,
I forgot to read the token from the cookie 😅, I'll update the PR soon.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np :) happy to help out

response = view_func(request, *args, **kwargs)

if hasattr(request, 'jwt'):
expiration = datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Cookie authentication
=====================

When a token is requested and ``jwt_cookie`` decorator is set, the response will set the given cookie with the token string::

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about mentioning here (or somewhere else) that CSRF token should be used when using cookie as transport mechanism for jwt token?

if hasattr(request, 'jwt'):
expiration = datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA

response.set_cookie(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making the cookie secure only as an opt-in? Similar to https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-SESSION_COOKIE_SECURE

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks!

@mtszsobczak
Copy link

Tested and it seems to be working. Great work!

@mongkok mongkok merged commit 0ab4c8a into master Feb 12, 2019
@mongkok mongkok deleted the feature/jwt-cookie branch February 12, 2019 21:25
tim-schilling pushed a commit to tim-schilling/django-graphql-jwt that referenced this pull request Jan 10, 2022
Bumps [strawberry-graphql](https://github.com/strawberry-graphql/strawberry) from 0.76.0 to 0.76.1.
- [Release notes](https://github.com/strawberry-graphql/strawberry/releases)
- [Changelog](https://github.com/strawberry-graphql/strawberry/blob/main/CHANGELOG.md)
- [Commits](strawberry-graphql/strawberry@0.76.0...0.76.1)

---
updated-dependencies:
- dependency-name: strawberry-graphql
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants