refactor unknown attribute syntax detection
[moonshot.git] / mech_eap / util_radius.cpp
index 4e2f6e0..5462acc 100644 (file)
@@ -63,12 +63,12 @@ gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
 }
 
 bool
-gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
+gss_eap_radius_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
                                                       const gss_eap_attr_provider *ctx)
 {
     const gss_eap_radius_attr_provider *radius;
 
-    if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
+    if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
         return false;
 
     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
@@ -82,11 +82,11 @@ gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *ma
 }
 
 bool
-gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
+gss_eap_radius_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
                                                  const gss_cred_id_t gssCred,
                                                  const gss_ctx_id_t gssCtx)
 {
-    if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
+    if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
         return false;
 
     if (gssCtx != GSS_C_NO_CONTEXT) {
@@ -189,8 +189,7 @@ copyAvps(const VALUE_PAIR *src)
         vpcopy = paircopyvp(vp);
         if (vpcopy == NULL) {
             pairfree(&dst);
-            throw new std::bad_alloc;
-            return NULL;
+            throw std::bad_alloc();
         }
         *pDst = vpcopy;
         pDst = &vpcopy->next;
@@ -619,18 +618,10 @@ gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
     return GSS_S_COMPLETE;
 }
 
-static json_t *
+static JSONObject
 avpToJson(const VALUE_PAIR *vp)
 {
-    json_t *obj = json_object();
-
-    if (obj == NULL) {
-        throw new std::bad_alloc;
-        return NULL;
-    }
-
-    /* FIXME check json_object_set_new return value */
-    json_object_set_new(obj, "type", json_integer(vp->attribute));
+    JSONObject obj;
 
     assert(vp->length <= MAX_STRING_LEN);
 
@@ -638,65 +629,71 @@ avpToJson(const VALUE_PAIR *vp)
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
-        json_object_set_new(obj, "value", json_integer(vp->lvalue));
+        obj.set("value", vp->lvalue);
         break;
     case PW_TYPE_STRING:
-        json_object_set_new(obj, "value", json_string(vp->vp_strvalue));
+        obj.set("value", vp->vp_strvalue);
         break;
     default: {
         char *b64;
 
-        if (base64Encode(vp->vp_octets, vp->length, &b64) < 0) {
-            json_decref(obj);
-            throw new std::bad_alloc;
-        }
+        if (base64Encode(vp->vp_octets, vp->length, &b64) < 0)
+            throw std::bad_alloc();
 
-        json_object_set_new(obj, "value", json_string(b64));
+        obj.set("value", b64);
         GSSEAP_FREE(b64);
         break;
     }
     }
 
+    obj.set("type", vp->attribute);
+
     return obj;
 }
 
 static bool
-jsonToAvp(VALUE_PAIR **pVp, json_t *obj)
+jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
 {
     VALUE_PAIR *vp = NULL;
     DICT_ATTR *da;
     uint32_t attrid;
-    json_t *type, *value;
 
-    type = json_object_get(obj, "type");
-    value = json_object_get(obj, "value");
-    if (type == NULL || value == NULL)
+    JSONObject type = obj["type"];
+    JSONObject value = obj["value"];
+
+    if (!type.isInteger())
         goto fail;
 
-    attrid = json_integer_value(type);
+    attrid = type.integer();
     da = dict_attrbyvalue(attrid);
     if (da != NULL) {
         vp = pairalloc(da);
     } else {
-        vp = paircreate(attrid, PW_TYPE_STRING);
-    }
-    if (vp == NULL) {
-        throw new std::bad_alloc;
-        goto fail;
+        int type = base64Valid(value.string()) ?
+            PW_TYPE_OCTETS : PW_TYPE_STRING;
+        vp = paircreate(attrid, type);
     }
+    if (vp == NULL)
+        throw std::bad_alloc();
 
     switch (vp->type) {
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
+        if (!value.isInteger())
+            goto fail;
+
         vp->length = 4;
-        vp->lvalue = json_integer_value(value);
+        vp->lvalue = value.integer();
         break;
     case PW_TYPE_STRING: {
-        const char *str = json_string_value(value);
-        size_t len;
+        if (!value.isString())
+            goto fail;
 
-        if (str == NULL || (len = strlen(str)) >= MAX_STRING_LEN)
+        const char *str = value.string();
+        size_t len = strlen(str);
+
+        if (len >= MAX_STRING_LEN)
             goto fail;
 
         vp->length = len;
@@ -705,12 +702,14 @@ jsonToAvp(VALUE_PAIR **pVp, json_t *obj)
     }
     case PW_TYPE_OCTETS:
     default: {
-        const char *str = json_string_value(value);
-        int len;
+        if (!value.isString())
+            goto fail;
+
+        const char *str = value.string();
+        ssize_t len = strlen(str);
 
         /* this optimization requires base64Decode only understand packed encoding */
-        if (str == NULL ||
-            strlen(str) >= BASE64_EXPAND(MAX_STRING_LEN))
+        if (len >= BASE64_EXPAND(MAX_STRING_LEN))
             goto fail;
 
         len = base64Decode(str, vp->vp_octets);
@@ -718,7 +717,6 @@ jsonToAvp(VALUE_PAIR **pVp, json_t *obj)
             goto fail;
 
         vp->length = len;
-        vp->vp_octets[len] = '\0';
         break;
     }
     }
@@ -742,19 +740,18 @@ gss_eap_radius_attr_provider::name(void) const
 
 bool
 gss_eap_radius_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
-                                                json_t *obj)
+                                                 JSONObject &obj)
 {
     VALUE_PAIR **pNext = &m_vps;
-    json_t *attrs;
-    size_t i;
 
     if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
         return false;
 
-    attrs = json_object_get(obj, "attributes");
+    JSONObject attrs = obj["attributes"];
+    size_t nelems = attrs.size();
 
-    for (i = 0; i < json_array_size(attrs); i++) {
-        json_t *attr = json_array_get(attrs, i);
+    for (size_t i = 0; i < nelems; i++) {
+        JSONObject attr = attrs[i];
         VALUE_PAIR *vp;
 
         if (!jsonToAvp(&vp, attr))
@@ -764,6 +761,8 @@ gss_eap_radius_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
         pNext = &vp->next;
     }
 
+    m_authenticated = obj["authenticated"].integer();
+
     return true;
 }
 
@@ -773,27 +772,19 @@ gss_eap_radius_attr_provider::prefix(void) const
     return "urn:ietf:params:gss-eap:radius-avp";
 }
 
-json_t *
+JSONObject
 gss_eap_radius_attr_provider::jsonRepresentation(void) const
 {
-    json_t *obj, *attrs;
-
-    attrs = json_array();
-    if (attrs == NULL)
-        throw new std::bad_alloc;
+    JSONObject obj, attrs = JSONObject::array();
 
     for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) {
-        json_t *attr = avpToJson(vp);
-        json_array_append_new(attrs, attr);
+        JSONObject attr = avpToJson(vp);
+        attrs.append(attr);
     }
 
-    obj = json_object();
-    if (obj == NULL) {
-        json_decref(attrs);
-        throw new std::bad_alloc;
-    }
+    obj.set("attributes", attrs);
 
-    json_object_set_new(obj, "attributes", attrs);
+    obj.set("authenticated", m_authenticated);
 
     return obj;
 }