/**
 * Licensed to the University Corporation for Advanced Internet
 * Development, Inc. (UCAID) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * UCAID licenses this file to you 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.
 */

/**
 * @file shibsp/attribute/AttributeDecoder.h
 *
 * Decodes SAML NameID/Attribute objects into resolved Attributes.
 */

#ifndef __shibsp_attrdecoder_h__
#define __shibsp_attrdecoder_h__

#include <shibsp/base.h>

#include <string>
#include <vector>

namespace xmltooling {
    class XMLTOOL_API GenericRequest;
    class XMLTOOL_API QName;
    class XMLTOOL_API XMLObject;
};

namespace shibsp {

    class SHIBSP_API Attribute;

    /**
     * Decodes XML objects into resolved Attributes.
     */
    class SHIBSP_API AttributeDecoder
    {
        MAKE_NONCOPYABLE(AttributeDecoder);
    protected:
        /**
         * Constructor.
         *
         * @param e root of DOM to configure the decoder
         */
        AttributeDecoder(const xercesc::DOMElement* e);

        /** Flag for case sensitivity of decoded attributes. */
        bool m_caseSensitive;

        /** Flag for hiding attributes from CGI export. */
        bool m_internal;

        /** Flag for language aware decoding. */
        bool m_langAware;

        /** Hash algorithm to apply to decoded values. */
        std::string m_hashAlg;

        /**
         * Helper method to handle base class decoding housekeeping.
         *
         * @param attr  the new Attribute object being created
         * @return  the attr parameter
         */
        virtual Attribute* _decode(Attribute* attr) const;

        /**
         * Helper method that returns a range of objects the decoder should operate on,
         * based on the language settings of the decoder and the client request.
         *
         * @param request   the client request, if any
         * @param objects   the objects to examine
         * @return  a pair of iterators representing the range of objects to examine
         */
        virtual std::pair<std::vector<xmltooling::XMLObject*>::const_iterator,std::vector<xmltooling::XMLObject*>::const_iterator> valueRange(
            const xmltooling::GenericRequest* request, const std::vector<xmltooling::XMLObject*>& objects
            ) const;

    public:
        virtual ~AttributeDecoder();

        /**
         * Decodes an XMLObject into a resolved Attribute.
         *
         * @param request           request triggering the decode, if any
         * @param ids               array containing primary identifier in first position, followed by any aliases
         * @param xmlObject         XMLObject to decode
         * @param assertingParty    name of the party asserting the attribute
         * @param relyingParty      name of the party relying on the attribute
         * @return a resolved Attribute, or nullptr
         */
        virtual Attribute* decode(
            const xmltooling::GenericRequest* request,
            const std::vector<std::string>& ids,
            const xmltooling::XMLObject* xmlObject,
            const char* assertingParty=nullptr,
            const char* relyingParty=nullptr
            ) const;

        /**
         * @deprecated
         * Decodes an XMLObject into a resolved Attribute.
         *
         * @param ids               array containing primary identifier in first position, followed by any aliases
         * @param xmlObject         XMLObject to decode
         * @param assertingParty    name of the party asserting the attribute
         * @param relyingParty      name of the party relying on the attribute
         * @return a resolved Attribute, or nullptr
         */
        virtual Attribute* decode(
            const std::vector<std::string>& ids,
            const xmltooling::XMLObject* xmlObject,
            const char* assertingParty=nullptr,
            const char* relyingParty=nullptr
            ) const;
    };


    /** Decodes into a SimpleAttribute. */
    extern SHIBSP_API xmltooling::QName StringAttributeDecoderType;

    /** Decodes scoped and NameID attributes into a ScopedAttribute. */
    extern SHIBSP_API xmltooling::QName ScopedAttributeDecoderType;

    /** Decodes NameID information into a NameIDAttribute. */
    extern SHIBSP_API xmltooling::QName NameIDAttributeDecoderType;

    /** Decodes scoped attributes into a NameIDAttribute. */
    extern SHIBSP_API xmltooling::QName NameIDFromScopedAttributeDecoderType;

    /** Decodes KeyInfo information into a SimpleAttribute. */
    extern SHIBSP_API xmltooling::QName KeyInfoAttributeDecoderType;

    /** Decodes arbitrary DOM information into an ExtensibleAttribute. */
    extern SHIBSP_API xmltooling::QName DOMAttributeDecoderType;

    /** Decodes arbitrary XML into an XMLAttribute. */
    extern SHIBSP_API xmltooling::QName XMLAttributeDecoderType;

    /** Decodes base64-encoded data into a SimpleAttribute. */
    extern SHIBSP_API xmltooling::QName Base64AttributeDecoderType;

    /** Registers built-in AttributeDecoders into the runtime. */
    void registerAttributeDecoders();
};

#endif /* __shibsp_attrdecoder_h__ */
