dj-jwt-auth 1.8.0__py3-none-any.whl → 1.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dj-jwt-auth
3
- Version: 1.8.0
3
+ Version: 1.9.0
4
4
  Summary: A Django package for JSON Web Token validation and verification. Using PyJWT.
5
5
  Home-page: https://www.example.com/
6
6
  Author: Konstantin Seleznev
@@ -137,4 +137,3 @@ Login URL will be available at `/admin/oidc/`.
137
137
 
138
138
  ### Testing:
139
139
  Run command `python runtests.py` to run tests.
140
- To run specific test use `python runtests.py <test_name>`, like `python runtests.py "tests.test.OIDCHandlerTest.test_new_email_exists"`.
@@ -8,14 +8,14 @@ django_jwt/settings.py,sha256=pXQ8WUU4LGBe6PQxCLTLM_2_b1CCSgehqim3yJDqZdw,1922
8
8
  django_jwt/urls.py,sha256=ZhcnRcQ1MBRh-bS7fTa-Vkz8yuWUhv-G_uRXKLnKAs0,320
9
9
  django_jwt/user.py,sha256=v2oCoAThstFg5x5cOA9nSKlYWZzCEqhFbz63WGhE_NM,6382
10
10
  django_jwt/utils.py,sha256=uIFw1JMldE_blC7YkaZbY7rpcawP2E6zsdLyuZRwJoA,1645
11
- django_jwt/views.py,sha256=jQW3YrW-Oed54KqxjDMaBOQ-dxEnqTAcBEXlQSK-zCE,4231
11
+ django_jwt/views.py,sha256=GzfMlVHbbVduJltWhEdEZU8_LyOFvlczxAakUh2KA7w,4626
12
12
  django_jwt/templates/django-jwt-index.html,sha256=y8f0v2WbRAFxnIU799I_MZCVsjn1sbdh7bypjdWB0lA,1353
13
13
  django_jwt/templates/admin/login.html,sha256=Nihyu0IGvDDZVvQDITXozwlj6XCQ0B8gqlyHLqVNyJc,275
14
14
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
15
  tests/models.py,sha256=jhoJcCEsx5B9AibmoLQLDD3cEsyYzYy6dMdYhRnBVFo,338
16
16
  tests/test.py,sha256=HEy5DsEYrPmtWSdeDhgdeV2tZTYlepaWt82VoTBrta8,11828
17
17
  tests/urls.py,sha256=D5FhDSVAudurkrpkCZZPnDvgXSgifwFVB3nAlYBg7uQ,212
18
- dj_jwt_auth-1.8.0.dist-info/METADATA,sha256=bSdoEA3Rd_Ug7C7B4ZaPCnBs4WOnLUjGhVNkMjDGxMU,4553
19
- dj_jwt_auth-1.8.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
20
- dj_jwt_auth-1.8.0.dist-info/top_level.txt,sha256=58O7TdK-yECZcbmPc52KNlBFpjIUlENuZubCxaSOxus,17
21
- dj_jwt_auth-1.8.0.dist-info/RECORD,,
18
+ dj_jwt_auth-1.9.0.dist-info/METADATA,sha256=9wT1rkV7h9f5Xi7wYvqbWjuJwolLxF4pjIqef1kRBI4,4416
19
+ dj_jwt_auth-1.9.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
20
+ dj_jwt_auth-1.9.0.dist-info/top_level.txt,sha256=58O7TdK-yECZcbmPc52KNlBFpjIUlENuZubCxaSOxus,17
21
+ dj_jwt_auth-1.9.0.dist-info/RECORD,,
django_jwt/views.py CHANGED
@@ -39,68 +39,84 @@ def index_response(request, msg, status=400):
39
39
  )
40
40
 
41
41
 
42
- class AbsView(View):
43
- def dispatch(self, request, *args, **kwargs):
44
- try:
45
- return super().dispatch(request, *args, **kwargs)
46
- except HTTPError as exc:
47
- log.warning(f"OIDC Admin HTTPError: {exc}")
48
- return index_response(request=request, msg=exc.response.text, status=exc.response.status_code)
49
- except ConfigException as exc:
50
- return HttpResponse(content=str(exc), status=500)
51
- except BadRequestException as exc:
52
- return index_response(request=request, msg=str(exc))
53
- except Exception as exc:
54
- return index_response(request=request, msg=str(exc))
42
+ class InitiateView(View):
43
+ callback_view_name = "receive_redirect_view"
44
+ client_id = None
45
+ scope = "openid"
55
46
 
56
-
57
- class StartOIDCAuthView(AbsView):
58
47
  def get(self, request):
59
48
  pkce_secret = PKCESecret()
60
- redirect_uri = jwt_settings.OIDC_ADMIN_REDIRECT_URI
61
- if not redirect_uri:
62
- redirect_uri = request.build_absolute_uri(reverse("receive_redirect_view"))
49
+ redirect_uri = request.build_absolute_uri(reverse(self.callback_view_name))
63
50
  authorization_endpoint = config.admin().get("authorization_endpoint")
64
51
  state = base64.urlsafe_b64encode(get_random_string().encode()).decode()
65
52
  params = {
66
- "client_id": jwt_settings.OIDC_ADMIN_CLIENT_ID,
53
+ "client_id": self.client_id,
67
54
  "redirect_uri": redirect_uri,
68
55
  "response_type": "code",
69
56
  "state": state,
70
- "scope": jwt_settings.OIDC_ADMIN_SCOPE,
57
+ "scope": self.scope,
71
58
  "code_challenge": pkce_secret.challenge,
72
59
  "code_challenge_method": pkce_secret.challenge_method,
73
60
  "ui_locales": "en",
74
61
  "nonce": get_random_string(),
75
62
  }
76
63
  cache.set(state, str(pkce_secret), timeout=600)
77
- log.info(f"OIDC Admin login: {authorization_endpoint}?{urlencode(params)}")
64
+ log.info(f"OIDC Initiate: {authorization_endpoint}?{urlencode(params)}")
78
65
  return redirect(f"{authorization_endpoint}?{urlencode(params)}")
79
66
 
80
67
 
81
- class ReceiveRedirectView(AbsView):
82
- def get(self, request):
68
+ class CallbackView(View):
69
+ callback_view_name = "receive_redirect_view"
70
+ user = None
71
+ payload = None
72
+
73
+ def fail(self, request, msg):
74
+ raise BadRequestException(msg)
75
+
76
+ def dispatch(self, request, *args, **kwargs):
83
77
  code = request.GET.get("code")
84
78
  state = request.GET.get("state")
85
79
  if not code or not state:
86
- log.warning(f"No code or state in the request {request.GET}")
87
- raise BadRequestException("No code or state in the request")
80
+ log.warning(f"OIDC No code or state in the request {request.GET}")
81
+ return self.fail(request, "No code or state in the request")
88
82
 
89
- redirect_uri = request.build_absolute_uri(reverse("receive_redirect_view"))
83
+ redirect_uri = request.build_absolute_uri(self.callback_view_name)
90
84
  if state := cache.get(state):
91
85
  token = get_access_token(code, redirect_uri, state)
92
- data = oidc_handler.decode_token(token)
93
- user = UserHandler(data, request, token).get_user()
94
- log.info(f"OIDC Admin login: {user}", extra={"data": data})
95
- roles = role_handler.apply(user, data)
96
- if not user.is_staff:
97
- raise BadRequestException(f"User {user.email} is not staff\nRoles: {roles}")
98
- login(request, user, backend=jwt_settings.OIDC_AUTHORIZATION_BACKEND)
99
- return redirect("admin:index")
86
+ self.payload = oidc_handler.decode_token(token)
87
+ self.user = UserHandler(self.payload, request, token).get_user()
88
+ return super().dispatch(request, *args, **kwargs)
89
+ return self.fail(request, "No PKCE secret found in cache")
100
90
 
101
- raise BadRequestException("No PKCE secret found in cache")
91
+
92
+ class StartOIDCAuthView(InitiateView):
93
+ client_id = jwt_settings.OIDC_ADMIN_CLIENT_ID
94
+ scope = jwt_settings.OIDC_ADMIN_SCOPE
95
+
96
+
97
+ class ReceiveRedirectView(CallbackView):
98
+ def dispatch(self, request, *args, **kwargs):
99
+ try:
100
+ return super().dispatch(request, *args, **kwargs)
101
+ except HTTPError as exc:
102
+ log.warning(f"OIDC Admin HTTPError: {exc}")
103
+ return index_response(request=request, msg=exc.response.text, status=exc.response.status_code)
104
+ except ConfigException as exc:
105
+ return HttpResponse(content=str(exc), status=500)
106
+ except BadRequestException as exc:
107
+ return index_response(request=request, msg=str(exc))
108
+ except Exception as exc:
109
+ return index_response(request=request, msg=str(exc))
110
+
111
+ def get(self, request):
112
+ log.info(f"OIDC Admin login: {self.user}", extra={"data": self.payload})
113
+ roles = role_handler.apply(self.user, self.payload)
114
+ if not self.user.is_staff:
115
+ raise BadRequestException(f"User {self.user.email} is not staff\nRoles: {roles}")
116
+ login(request, self.user, backend=jwt_settings.OIDC_AUTHORIZATION_BACKEND)
117
+ return redirect("admin:index")
102
118
 
103
119
 
104
- class LogoutView(AbsView):
120
+ class LogoutView(View):
105
121
  def get(self, request):
106
122
  return index_response(request, "Logged out", status=401)