#include "GSSAcquireCred.h"
#include "GSSException.h"
+#include <stdexcept>
+
GSSAcquireCred::GSSAcquireCred(gss_acq_cred_type fn) : function(fn)
{
}
-GSSAcquireCred& GSSAcquireCred::operator= ( const GSSAcquireCred& other )
+GSSAcquireCred::GSSAcquireCred (
+ JSONObject *params,
+ gss_acq_cred_type fn
+) : GSSCommand ( params )
{
- return(*this);
+ /* Variables */
+ /* Error checking */
+ /* Setup */
+ /* Main */
+ loadParameters(params);
+ function = fn;
+ /* Cleanup */
+ /* Return */
}
-GSSAcquireCred::GSSAcquireCred ( JSONObject params ) : GSSCommand ( params )
+
+bool GSSAcquireCred::loadParameters(JSONObject *params)
{
+ /* Variables */
+ std::string sCredUsage;
+ size_t nDesiredMechs;
+
+ /* Error checking */
+ /* Setup */
+ // Should I zeroOut?
+
+ /* Main processing */
+ // Easy stuff(*params)
+ this->time_req = (*params)["arguments"]["time_req"].integer();
+ if ( ! params->get("arguments").get("cred_usage").isNull() )
+ {
+ if (params->get("arguments").get("cred_usage").isString())
+ {
+ sCredUsage = params->get("arguments").get("cred_usage").string();
+ if (sCredUsage == "GSS_C_BOTH")
+ this->cred_usage = GSS_C_BOTH;
+ else if (sCredUsage == "GSS_C_INITIATE")
+ this->cred_usage = GSS_C_INITIATE;
+ else if (sCredUsage == "GSS_C_ACCEPT")
+ this->cred_usage = GSS_C_ACCEPT;
+ else
+ throw std::invalid_argument( std::string("Invalid cred_usage type given: ") + sCredUsage );
+ } else if (params->get("arguments").get("cred_usage").isInteger())
+ this->cred_usage = (gss_cred_usage_t)( params->get("arguments").get("cred_usage").integer() );
+ else
+ throw std::invalid_argument( "Unrecognized argument type for cred_usage." );
+ }
+
+ if ( params->get("arguments").get("desired_mechs").isArray() )
+ {
+ for (nDesiredMechs = 0;
+ nDesiredMechs < params->get("arguments").get("desired_mechs").size();
+ nDesiredMechs++)
+ {
+ std::string mechStr = params->get("arguments").get("desired_mechs")[nDesiredMechs].string();
+ desiredMechs.addOID( GSSOID(mechStr).toGss() );
+ }
+ } else
+ throw std::invalid_argument("Unrecognized desired_mechs array.");
+
+ /* Cleanup */
+ /* Return */
+ return true;
}
+
void GSSAcquireCred::execute()
{
/* Variables */
/* Return */
}
+/* Desired JSON output:
+ *
+ * {
+ * "command": "gss_acquire_cred",
+ * "return_values": {
+ * "major_status": 0,
+ * "minor_status": 0,
+ * "cred_handle": "###########",
+ * "actual_mechs": [
+ * "{ 1 2 3 4 }",
+ * "{ 5 6 7 8 }"
+ * ],
+ * "time_rec": 0
+ * }
+ * }
+ */
JSONObject *GSSAcquireCred::toJSON()
{
- return new JSONObject();
+ /* Variables */
+ JSONObject *ret = new JSONObject();
+ JSONObject *values = new JSONObject();
+ JSONObject mechs = JSONObject::array();
+ JSONObject *temp;
+ gss_OID_set gssMechs;
+ OM_uint32 i;
+
+ /* Error checking */
+
+ /* Setup */
+
+ /* Main */
+ gssMechs = actualMechs.toGss();
+ for (i = 0; i < gssMechs->count; i++)
+ {
+ GSSOID m( gssMechs->elements + i );
+ temp = m.toJSONValue();
+ std::cout << "\nMech value: " << temp->string();
+ mechs.append( *temp );
+ }
+ values->set("major_status", this->retVal);
+ values->set("minor_status", this->minor_status);
+ temp = this->cred.toJSONValue();
+ values->set("output_cred_handle", *temp );
+ values->set("actual_mechs", mechs);
+ values->set("time_rec", (int)this->time_rec );
+ ret->set("command", "gss_acquire_cred");
+ ret->set("return_values", *values);
+
+ /* Cleanup */
+
+ /* Return */
+ return(ret);
}
public:
GSSAcquireCred( gss_acq_cred_type fn = &gss_acquire_cred );
GSSAcquireCred ( const GSSAcquireCred& other );
- GSSAcquireCred ( JSONObject params );
+ GSSAcquireCred ( JSONObject *params, gss_acq_cred_type fn = &gss_acquire_cred );
~GSSAcquireCred();
GSSAcquireCred& operator= ( const GSSAcquireCred& other );
virtual void execute();
virtual JSONObject* toJSON();
+ bool loadParameters(JSONObject *params);
gss_acq_cred_type getGSSFunction() { return function; };
#ifndef GSSCREDENTIAL_H
#define GSSCREDENTIAL_H
+#include "../util_json.h"
#include <gssapi/gssapi.h>
+#include <string>
typedef OM_uint32 (*gss_acq_cred_type)(
OM_uint32 *, /* minor_status */
gss_cred_id_t toGss() const { return(credential); }
void setValue(gss_cred_id_t cred) { this->credential = cred; }
+ JSONObject *toJSONValue() { return( new JSONObject("not now") ); }
private:
gss_cred_id_t credential;
/* Return */
}
-std::string GSSOID::toString()
+std::string GSSOID::toString() const
{
/* Variables */
OM_uint32 major, minor;
return(ret);
}
+JSONObject *GSSOID::toJSONValue() const
+{
+ std::string val = this->toString();
+ JSONObject *ret = new JSONObject( val.c_str() );
+ return(ret);
+}
+
bool GSSOID::setValue ( GSSBuffer buf )
{
init(buf);
#include <string>
#include "gssapi.h"
+#include "../util_json.h"
#include "GSSBuffer.h"
~GSSOID();
gss_OID toGss() const { return(oid); };
- std::string toString();
+ std::string toString() const;
bool setValue(GSSBuffer buf);
bool setValue(gss_OID gssOID);
+ JSONObject *toJSONValue() const;
private:
gss_OID oid;
bool gssInternal;
}
+bool GSSOIDSet::includes ( const gss_OID oid ) const
+{
+ /* Variables */
+ OM_uint32 major, minor;
+ int present;
+
+ /* Error checking */
+ /* Setup */
+ /* Main */
+ major = gss_test_oid_set_member(&minor, oid, this->set, &present);
+ if (GSS_ERROR(major))
+ /* How? */
+ throw GSSException("Cannot check if an OID is included in a set.", major, minor);
+
+ /* Cleanup */
+ /* return */
+ return (present == 1);
+}
+
+bool GSSOIDSet::includes ( const GSSOID &oid ) const
+{
+ return(this->includes( oid.toGss() ) );
+}
+
+// JSONObject* GSSOIDSet::toJSONValue() const
+// {
+// /* Variables */
+// JSONObject x("hello");
+// /* Error checking */
+// /* Setup */
+// /* Main */
+// /* Cleanup */
+// /* return */
+// }
+
/* Variables */
/* Error checking */
void addOID (const gss_OID other );
gss_OID_set toGss() const { return(set); }
+ bool includes(const gss_OID oid) const;
+ bool includes(const GSSOID &oid) const;
+
+ JSONObject *toJSONValue() const;
+
private:
gss_OID_set set;
#include "command_mocks/MockAcquireCred.h"
#include <datamodel/GSSName.h>
#include <gssapi.h>
+#include <stdexcept>
CPPUNIT_TEST_SUITE_REGISTRATION( GSSAcquireCredTest );
void GSSAcquireCredTest::testConstructorWithJSONObject()
{
+ /* Variables */
+ // To feed into the JSON
+ OM_uint32 time_req = rand();
+ gss_cred_usage_t cred_usage = 2;
+ GSSOID mech( (char *)"{ 1 2 840 113554 1 2 1 4 }" );
+ std::stringstream input;
+ // The JSON string
+ input <<
+ "{\"method\" : \"gss_acquire_cred\", \
+ \"arguments\": \
+ {\"desired_name\" : \"#################\", \
+ \"time_req\" : " << time_req << ", \
+ \"cred_usage\" : " << cred_usage << ", \
+ \"desired_mechs\": [\"" << mech.toString() << "\"] \
+ } \
+ }";
+
+ // The JSON itself
+ json_error_t jsonErr;
+ JSONObject json = JSONObject::load(input.str().c_str(), 0, &jsonErr);
+
+ GSSOIDSet desiredMechs;
+
+
+ GSSAcquireCred cmd = GSSAcquireCred(&json, &mock_acquire_cred);
+
+ /* Error checking */
+ /* Setup */
+ /* Main */
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "GSSAcquireCred's time_req was not loaded correctly",
+ time_req,
+ cmd.getTimeReq()
+ );
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "GSSAcquireCred's cred_usage was not loaded correctly",
+ cred_usage,
+ cmd.getCredUsage()
+ );
+
+ json["arguments"].set( "cred_usage", "GSS_C_INITIATE" );
+ cmd.loadParameters(&json);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "GSSAcquireCred's cred_usage was not loaded correctly",
+ GSS_C_INITIATE,
+ (int)cmd.getCredUsage()
+ );
+
+ // CPPUNIT_ASSERT_THROW(expression, exception);
+ json["arguments"].set( "cred_usage", "GSS_C_INITIATOR" );
+ CPPUNIT_ASSERT_THROW_MESSAGE(
+ "GSSAcquireCred's JSON parsing is admitting invalid strings.",
+ cmd.loadParameters(&json),
+ std::invalid_argument
+ );
+
+ desiredMechs = cmd.getDesiredMechs();
+ CPPUNIT_ASSERT_MESSAGE(
+ "The desired mechanisms were not set properly.",
+ desiredMechs.includes(mech)
+ );
+
+ /* Cleanup */
+ /* Return */
}
void GSSAcquireCredTest::testEmptyCall()
cmd.getTimeRec()
);
-
/* Cleanup */
/* Return */
}
+
+/* Desired JSON output:
+ *
+ * {
+ * "command": "gss_acquire_cred",
+ * "return_values": {
+ * "major_status": 0,
+ * "minor_status": 0,
+ * "cred_handle": "###########",
+ * "actual_mechs": [
+ * "{ 1 2 3 4 }",
+ * "{ 5 6 7 8 }"
+ * ],
+ * "time_rec": 0
+ * }
+ * }
+ */
void GSSAcquireCredTest::testJSONMarshal()
{
-
+ /* Variables */
+ GSSOIDSet actualMechs;
+ JSONObject *result;
+ GSSAcquireCred cmd = GSSAcquireCred(&mock_acquire_cred);
+
+ /* Error checking */
+ /* Setup */
+ actualMechs.addOID( GSSOID( (char *)"{ 1 2 3 4 }" ) );
+ actualMechs.addOID( GSSOID( (char *)"{ 5 6 7 8 }" ) );
+ MockAcquireCred::reset();
+ MockAcquireCred::retVal = 0;
+ MockAcquireCred::minor_status = 0;
+ MockAcquireCred::output_cred_handle = GSS_C_NO_CREDENTIAL;
+ MockAcquireCred::actual_mechs = actualMechs.toGss();
+ MockAcquireCred::time_rec = 0;
+
+ /* Main */
+ cmd.execute();
+ result = cmd.toJSON();
+
+ std::cout << "\n" << result->dump() << "\n";
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "The command name is incorrect",
+ std::string("gss_acquire_cred"),
+ std::string( (*result)["command"].string() )
+ );
+
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "The return value was reported incorrectly",
+ (int)MockAcquireCred::retVal,
+ (int)( (*result)["return_values"]["major_status"].integer() )
+ );
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "The minor_status value was reported incorrectly",
+ (int)MockAcquireCred::minor_status,
+ (int)( (*result)["return_values"]["minor_status"].integer() )
+ );
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "The gss_name was reported incorrectly",
+ std::string("{ 1 2 3 4 }"),
+ std::string( (*result)["return_values"]["actual_mechs"][(size_t)0].string() )
+ );
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "The gss_name was reported incorrectly",
+ std::string("{ 5 6 7 8 }"),
+ std::string( (*result)["return_values"]["actual_mechs"][(size_t)1].string() )
+ );
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "The minor_status value was reported incorrectly",
+ (int)MockAcquireCred::time_rec,
+ (int)( (*result)["return_values"]["time_rec"].integer() )
+ );
+
+
+ /* Cleanup */
+ /* Return */
}
+