polices: add helper to remove None-value keys from dict for policies

This commit is contained in:
Jens Langhammer 2020-07-08 23:03:58 +02:00
parent d786fa4b7c
commit e58ac7ae90
5 changed files with 25 additions and 7 deletions

View File

@ -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">
======= ![CI Build status](https://img.shields.io/azure-devops/build/beryjuorg/passbook/1?style=flat-square)
![CI Build status](https://img.shields.io/azure-devops/build/beryjuorg/5d94b893-6dea-4f68-a8fe-10f1674fc3a9/1?style=flat-square) ![Tests](https://img.shields.io/azure-devops/tests/beryjuorg/passbook/1?compact_message&style=flat-square)
![Code Coverage](https://img.shields.io/codecov/c/gh/beryju/passbook?style=flat-square)
![Docker pulls](https://img.shields.io/docker/pulls/beryju/passbook.svg?style=flat-square) ![Docker pulls](https://img.shields.io/docker/pulls/beryju/passbook.svg?style=flat-square)
![Docker pulls (gatekeeper)](https://img.shields.io/docker/pulls/beryju/passbook-gatekeeper.svg?style=flat-square) ![Docker pulls (gatekeeper)](https://img.shields.io/docker/pulls/beryju/passbook-gatekeeper.svg?style=flat-square)
![Latest version](https://img.shields.io/docker/v/beryju/passbook?sort=semver&style=flat-square) ![Latest version](https://img.shields.io/docker/v/beryju/passbook?sort=semver&style=flat-square)
![LGTM Grade](https://img.shields.io/lgtm/grade/python/github/BeryJu/passbook?style=flat-square) ![LGTM Grade](https://img.shields.io/lgtm/grade/python/github/BeryJu/passbook?style=flat-square)
![Code Coverage](https://img.shields.io/codecov/c/gh/beryju/passbook?style=flat-square)
## What is passbook? ## What is passbook?

View File

@ -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)

View 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

View File

@ -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)

View File

@ -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(