def post(self, request: HttpRequest) -> HttpResponse: """Handle a HTTP POST request.""" try: preset = PRESETS[int(request.POST.get('Request', ''))] except (ValueError, KeyError): return HttpResponseBadRequest() light_request = LightRequest( id=create_xml_uuid(), issuer=CONNECTOR_SETTINGS.service_provider['request_issuer'], level_of_assurance=LevelOfAssurance.LOW, provider_name="Demo Service Provider", sp_type=ServiceProviderType.PUBLIC, relay_state=request.POST.get('RelayState') or None, origin_country_code='EU', citizen_country_code=request.POST.get('Country'), name_id_format=preset.id_format, requested_attributes={name: [] for name in preset.attributes} ) if not light_request.citizen_country_code: # Use a placeholder to get through light request validation. light_request.citizen_country_code = COUNTRY_PLACEHOLDER self.saml_request = SAMLRequest.from_light_request(light_request, '/dest', datetime.utcnow()) signature_options = CONNECTOR_SETTINGS.service_provider['response_signature'] if signature_options and signature_options.get('key_file') and signature_options.get('cert_file'): self.saml_request.sign_request(**signature_options) return self.get(request)
def create_light_token(self, issuer: str, hash_algorithm: str, secret: str) -> Tuple[LightToken, str]: """ Create and encode a light token according to token settings. :param issuer: Token issuer. :param hash_algorithm: A hashlib hash algorithm. :param secret: A secret shared between communication parties. :return: A tuple of the token and its encoded form. """ token = LightToken(id=create_xml_uuid(TOKEN_ID_PREFIX), created=datetime.now(), issuer=issuer) LOGGER.info('[#%r] Created light token: id=%r, issuer=%r', self.log_id, token.id, token.issuer) encoded_token = token.encode(hash_algorithm, secret).decode('ascii') LOGGER.info('[#%r] Encoded light token: %r', self.log_id, encoded_token) return token, encoded_token
def test_create_xml_uuid_valid_prefix(self, _uuid_mock: Mock): self.assertEqual(create_xml_uuid('T'), 'T0uuid4')
def test_create_xml_uuid_default_prefix(self, _uuid_mock: Mock): self.assertEqual(create_xml_uuid(), '_0uuid4')