# Copyright (C) 2011 Yaco Sistemas (http://www.yaco.es)
# Copyright (C) 2010 Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#            http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from saml2 import saml, class_name
from saml2.config import IdPConfig
from saml2.assertion import Assertion
from saml2.s_utils import sid
from saml2.s_utils import success_status_factory
from saml2.sigver import signed_instance_factory, pre_signature_part
from saml2.sigver import security_context, response_factory

BASEDIR = os.path.dirname(os.path.abspath(__file__))


def auth_response(identity, in_response_to, sp_conf):
    """Generates a fresh signed authentication response"""
    sp_entity_id = sp_conf.entityid
    idp_entity_id = sp_conf.idps().keys()[0]
    acs = sp_conf.endpoint('assertion_consumer_service')[0]
    issuer = saml.Issuer(text=idp_entity_id, format=saml.NAMEID_FORMAT_ENTITY)
    response = response_factory(issuer=issuer,
                                in_response_to=in_response_to,
                                destination=acs,
                                status=success_status_factory())
    idp_conf = IdPConfig()
    name_form = "urn:oasis:names:tc:SAML:2.0:attrname-format:uri"
    idp_conf.load({
            'entityid': idp_entity_id,
            'xmlsec_binary': sp_conf.xmlsec_binary,
            'attribute_map_dir': os.path.join(BASEDIR, 'attribute-maps'),
            'service': {
                'idp': {
                    'endpoints': tuple(),
                    'policy':  {
                        'default': {
                            "lifetime": {"minutes": 15},
                            "attribute_restrictions": None,
                            "name_form": name_form,
                            }
                        }
                    },
                },
            'key_file': os.path.join(BASEDIR, 'idpcert.key'),
            'cert_file': os.path.join(BASEDIR, 'idpcert.pem'),
            'metadata': '',
            })

    ast = Assertion(identity)
    ast.apply_policy(sp_entity_id, idp_conf.policy, {})
    name_id = saml.NameID(format=saml.NAMEID_FORMAT_TRANSIENT,
                          text=sid())

    authn_class = saml.AUTHN_PASSWORD
    authn_authn = 'http://idp.example.com/login/'
    assertion = ast.construct(sp_entity_id, in_response_to, acs,
                              name_id, sp_conf.attribute_converters,
                              idp_conf.policy,
                              issuer=issuer,
                              authn_class=authn_class,
                              authn_auth=authn_authn)

    sec = security_context(idp_conf)

    assertion.signature = pre_signature_part(assertion.id, sec.my_cert, 1)
    to_sign = [(class_name(assertion), assertion.id)]
    response.assertion = assertion
    return signed_instance_factory(response, sec, to_sign)

@@ -713,20 +713,8 @@
 saml
-, class_name
 %0Afro
@@ -749,76 +749,8 @@
 fig%0A
-from saml2.assertion import Assertion%0Afrom saml2.s_utils import sid%0A
 from
@@ -798,34 +798,34 @@
 ory%0Afrom saml2.s
-ig
+er
 ver import signe
@@ -823,51 +823,26 @@
 ort 
-signed_instance_factory, pre_signature_part
+Identifier, Server
 %0Afro
@@ -866,26 +866,8 @@
 port
- security_context,
  res
@@ -936,16 +936,72 @@
 e__))%0A%0A%0A
+class FakeDb(dict):%0A%0A    def sync(self):%0A        pass%0A%0A%0A
 def auth
@@ -2428,327 +2428,154 @@
  %7D)%0A
-%0A
     
-ast = Assertion(identity)%0A    ast.apply_policy(sp_entity_id, idp_conf.policy, %7B%7D)%0A    name_id = saml.NameID(format=saml.NAMEID_FORMAT_TRANSIENT,%0A                          text=sid())%0A%0A    authn_class = saml.AUTHN_PASSWORD%0A    authn_authn = 'http://idp.example.com/login/'%0A    assertion = ast.construct(sp_
+server = Server(%22%22, idp_conf)%0A    server.ident = Identifier(FakeDb())%0A%0A    userid = 'irrelevant'%0A    response = server.authn_response(id
 entity
-_id
 , in
@@ -2627,506 +2627,70 @@
     
-name_id, sp_conf.attribute_converters,%0A                              idp_conf.policy,%0A                              issuer=issuer,%0A                              authn_class=authn_class,%0A                              authn_auth=authn_authn)%0A%0A    sec = security_context(idp_conf)%0A%0A    assertion.signature = pre_signature_part(assertion.id, sec.my_cert, 1)%0A    to_sign = %5B(class_name(assertion), assertion.id)%5D%0A    response.assertion = assertion%0A    return signed_instance_factory(response, sec, to_sign
+       sp_entity_id, None, userid)%0A    return '%5Cn'.join(response
 )%0A

Simplify the auth_response function reusing as much code as possible from pysaml2 Server class