Cleanup provider immediately if initialisation fails
[mech_eap.orig] / util_saml.cpp
index c4cdd98..115824f 100644 (file)
@@ -52,6 +52,17 @@ using namespace std;
  * gss_eap_saml_assertion_provider is for retrieving the underlying
  * assertion.
  */
+gss_eap_saml_assertion_provider::gss_eap_saml_assertion_provider(void)
+{
+    m_assertion = NULL;
+    m_authenticated = false;
+}
+
+gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
+{
+    delete m_assertion;
+}
+
 bool
 gss_eap_saml_assertion_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
                                                          const gss_eap_attr_provider *ctx)
@@ -99,11 +110,6 @@ gss_eap_saml_assertion_provider::initFromGssContext(const gss_eap_attr_ctx *mana
     return true;
 }
 
-gss_eap_saml_assertion_provider::~gss_eap_saml_assertion_provider(void)
-{
-    delete m_assertion;
-}
-
 void
 gss_eap_saml_assertion_provider::setAssertion(const saml2::Assertion *assertion,
                                               bool authenticated)
@@ -148,8 +154,15 @@ bool
 gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
                                                    void *data) const
 {
+    bool ret;
+
     /* just add the prefix */
-    return addAttribute(this, GSS_C_NO_BUFFER, data);
+    if (m_assertion != NULL)
+        ret = addAttribute(this, GSS_C_NO_BUFFER, data);
+    else
+        ret = true;
+
+    return ret;
 }
 
 void
@@ -170,6 +183,23 @@ gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
     m_authenticated = false;
 }
 
+time_t
+gss_eap_saml_assertion_provider::getExpiryTime(void) const
+{
+    saml2::Conditions *conditions;
+    time_t expiryTime = 0;
+
+    if (m_assertion == NULL)
+        return 0;
+
+    conditions = m_assertion->getConditions();
+
+    if (conditions != NULL && conditions->getNotOnOrAfter() != NULL)
+        expiryTime = conditions->getNotOnOrAfter()->getEpoch();
+
+    return expiryTime;
+}
+
 bool
 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
                                               int *authenticated,
@@ -189,8 +219,10 @@ gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
     if (*more != -1)
         return false;
 
-    *authenticated = m_authenticated;
-    *complete = false;
+    if (authenticated != NULL)
+        *authenticated = m_authenticated;
+    if (complete != NULL)
+        *complete = true;
 
     XMLHelper::serialize(m_assertion->marshall((DOMDocument *)NULL), str);
 
@@ -280,23 +312,22 @@ gss_eap_saml_attr_provider::getAssertion(int *authenticated,
 {
     const gss_eap_saml_assertion_provider *saml;
 
-    *authenticated = false;
-    *pAssertion = NULL;
+    if (authenticated != NULL)
+        *authenticated = false;
+    if (pAssertion != NULL)
+        *pAssertion = NULL;
 
     saml = static_cast<const gss_eap_saml_assertion_provider *>
         (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
     if (saml == NULL)
         return false;
 
-    *authenticated = saml->authenticated();
-    *pAssertion = saml->getAssertion();
-
-    return (*pAssertion != NULL);
-}
+    if (authenticated != NULL)
+        *authenticated = saml->authenticated();
+    if (pAssertion != NULL)
+        *pAssertion = saml->getAssertion();
 
-gss_eap_saml_attr_provider::~gss_eap_saml_attr_provider(void)
-{
-    /* Nothing to do, we're just a wrapper around the assertion provider. */
+    return (saml->getAssertion() != NULL);
 }
 
 bool
@@ -389,8 +420,10 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
 {
     const saml2::Assertion *assertion;
 
-    *authenticated = false;
-    *complete = true;
+    if (authenticated != NULL)
+        *authenticated = false;
+    if (complete != NULL)
+        *complete = true;
     *pAttribute = NULL;
 
     if (!getAssertion(authenticated, &assertion) ||
@@ -523,3 +556,21 @@ gss_eap_saml_attr_provider::createAttrContext(void)
 {
     return new gss_eap_saml_attr_provider;
 }
+
+OM_uint32
+gssEapSamlAttrProvidersInit(OM_uint32 *minor)
+{
+    if (gss_eap_saml_assertion_provider::init() &&
+        gss_eap_saml_attr_provider::init())
+        return GSS_S_COMPLETE;
+
+    return GSS_S_FAILURE;
+}
+
+OM_uint32
+gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
+{
+    gss_eap_saml_attr_provider::finalize();
+    gss_eap_saml_assertion_provider::finalize();
+    return GSS_S_COMPLETE;
+}