polices: add helper to remove None-value keys from dict for policies
This commit is contained in:
		| @ -1,12 +1,12 @@ | |||||||
| <img src="passbook/static/static/passbook/logo.svg" height="50" alt="passbook logo"><img src="passbook/static/static/passbook/brand_inverted.svg" height="50" alt="passbook"> | <img src="passbook/static/static/passbook/logo.svg" height="50" alt="passbook logo"><img src="passbook/static/static/passbook/brand_inverted.svg" height="50" alt="passbook"> | ||||||
|  |  | ||||||
| ======= |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| ## What is passbook? | ## What is passbook? | ||||||
|  |  | ||||||
|  | |||||||
| @ -21,7 +21,6 @@ def save_ip_reputation(): | |||||||
|     for key in keys: |     for key in keys: | ||||||
|         score = cache.get(key) |         score = cache.get(key) | ||||||
|         remote_ip = key.replace(CACHE_KEY_IP_PREFIX, "") |         remote_ip = key.replace(CACHE_KEY_IP_PREFIX, "") | ||||||
|         print(remote_ip) |  | ||||||
|         rep, _ = IPReputation.objects.get_or_create(ip=remote_ip) |         rep, _ = IPReputation.objects.get_or_create(ip=remote_ip) | ||||||
|         rep.score = score |         rep.score = score | ||||||
|         objects_to_update.append(rep) |         objects_to_update.append(rep) | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								passbook/policies/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								passbook/policies/utils.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | """Policy Utils""" | ||||||
|  | from typing import Any, Dict | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def delete_none_keys(dict_: Dict[Any, Any]) -> Dict[Any, Any]: | ||||||
|  |     """Remove any keys from `dict_` that are None.""" | ||||||
|  |     new_dict = {} | ||||||
|  |     for key, value in dict_.items(): | ||||||
|  |         if value is not None: | ||||||
|  |             new_dict[key] = value | ||||||
|  |     return new_dict | ||||||
| @ -21,10 +21,12 @@ from passbook.flows.planner import ( | |||||||
| ) | ) | ||||||
| from passbook.flows.views import SESSION_KEY_PLAN | from passbook.flows.views import SESSION_KEY_PLAN | ||||||
| from passbook.lib.utils.urls import redirect_with_qs | from passbook.lib.utils.urls import redirect_with_qs | ||||||
|  | from passbook.policies.utils import delete_none_keys | ||||||
| from passbook.sources.oauth.auth import AuthorizedServiceBackend | from passbook.sources.oauth.auth import AuthorizedServiceBackend | ||||||
| from passbook.sources.oauth.clients import BaseOAuthClient, get_client | from passbook.sources.oauth.clients import BaseOAuthClient, get_client | ||||||
| from passbook.sources.oauth.models import OAuthSource, UserOAuthSourceConnection | from passbook.sources.oauth.models import OAuthSource, UserOAuthSourceConnection | ||||||
| from passbook.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND | from passbook.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND | ||||||
|  | from passbook.stages.prompt.stage import PLAN_CONTEXT_PROMPT | ||||||
|  |  | ||||||
| LOGGER = get_logger() | LOGGER = get_logger() | ||||||
|  |  | ||||||
| @ -175,7 +177,6 @@ class OAuthCallback(OAuthClientMixin, View): | |||||||
|         """Prepare Authentication Plan, redirect user FlowExecutor""" |         """Prepare Authentication Plan, redirect user FlowExecutor""" | ||||||
|         kwargs.update( |         kwargs.update( | ||||||
|             { |             { | ||||||
|                 # PLAN_CONTEXT_PENDING_USER: user, |  | ||||||
|                 # Since we authenticate the user by their token, they have no backend set |                 # Since we authenticate the user by their token, they have no backend set | ||||||
|                 PLAN_CONTEXT_AUTHENTICATION_BACKEND: "django.contrib.auth.backends.ModelBackend", |                 PLAN_CONTEXT_AUTHENTICATION_BACKEND: "django.contrib.auth.backends.ModelBackend", | ||||||
|                 PLAN_CONTEXT_SSO: True, |                 PLAN_CONTEXT_SSO: True, | ||||||
| @ -249,7 +250,13 @@ class OAuthCallback(OAuthClientMixin, View): | |||||||
|                 % {"source": self.source.name} |                 % {"source": self.source.name} | ||||||
|             ), |             ), | ||||||
|         ) |         ) | ||||||
|         context = self.get_user_enroll_context(source, access, info) |         # Trim out all keys that have a value of None, | ||||||
|  |         # so we use `"key" in ` checks in policies | ||||||
|  |         context = { | ||||||
|  |             PLAN_CONTEXT_PROMPT: delete_none_keys( | ||||||
|  |                 self.get_user_enroll_context(source, access, info) | ||||||
|  |             ) | ||||||
|  |         } | ||||||
|         return self.handle_login_flow(source.enrollment_flow, **context) |         return self.handle_login_flow(source.enrollment_flow, **context) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | |||||||
| @ -15,6 +15,7 @@ from passbook.flows.planner import ( | |||||||
| ) | ) | ||||||
| from passbook.flows.views import SESSION_KEY_PLAN | from passbook.flows.views import SESSION_KEY_PLAN | ||||||
| from passbook.lib.utils.urls import redirect_with_qs | from passbook.lib.utils.urls import redirect_with_qs | ||||||
|  | from passbook.policies.utils import delete_none_keys | ||||||
| from passbook.providers.saml.utils.encoding import decode_base64_and_inflate | from passbook.providers.saml.utils.encoding import decode_base64_and_inflate | ||||||
| from passbook.sources.saml.exceptions import ( | from passbook.sources.saml.exceptions import ( | ||||||
|     MissingSAMLResponse, |     MissingSAMLResponse, | ||||||
| @ -153,7 +154,7 @@ class Processor: | |||||||
|         return self._flow_response( |         return self._flow_response( | ||||||
|             request, |             request, | ||||||
|             self._source.enrollment_flow, |             self._source.enrollment_flow, | ||||||
|             **{PLAN_CONTEXT_PROMPT: name_id_filter}, |             **{PLAN_CONTEXT_PROMPT: delete_none_keys(name_id_filter)}, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|     def _flow_response( |     def _flow_response( | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer