2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "GSSAcquireCred.h"
9 #include "GSSException.h"
13 GSSAcquireCred::GSSAcquireCred(gss_acq_cred_type fn) : function(fn)
18 GSSAcquireCred::GSSAcquireCred ( const GSSAcquireCred& other )
23 GSSAcquireCred::~GSSAcquireCred()
28 GSSAcquireCred::GSSAcquireCred (
31 ) : GSSCommand ( params )
37 loadParameters(params);
44 bool GSSAcquireCred::loadParameters(JSONObject *params)
47 std::string sCredUsage;
55 // Easy stuff(*params)
56 this->time_req = (*params)["arguments"]["time_req"].integer();
58 if ( ! params->get("arguments").get("cred_usage").isNull() )
60 if (params->get("arguments").get("cred_usage").isString())
62 sCredUsage = params->get("arguments").get("cred_usage").string();
63 if (sCredUsage == "GSS_C_BOTH")
64 this->cred_usage = GSS_C_BOTH;
65 else if (sCredUsage == "GSS_C_INITIATE")
66 this->cred_usage = GSS_C_INITIATE;
67 else if (sCredUsage == "GSS_C_ACCEPT")
68 this->cred_usage = GSS_C_ACCEPT;
70 throw std::invalid_argument( std::string("Invalid cred_usage type given: ") + sCredUsage );
71 } else if (params->get("arguments").get("cred_usage").isInteger())
72 this->cred_usage = (gss_cred_usage_t)( params->get("arguments").get("cred_usage").integer() );
74 throw std::invalid_argument( "Unrecognized argument type for cred_usage." );
77 if ( params->get("arguments").get("desired_mechs").isArray() )
79 for (nDesiredMechs = 0;
80 nDesiredMechs < params->get("arguments").get("desired_mechs").size();
83 std::string mechStr = params->get("arguments").get("desired_mechs")[nDesiredMechs].string();
84 desiredMechs.addOID( GSSOID(mechStr).toGss() );
87 throw std::invalid_argument("Unrecognized desired_mechs array.");
96 void GSSAcquireCred::execute()
99 gss_cred_id_t output_cred_handle;
100 gss_OID_set actual_mechs;
105 this->retVal = function(
107 this->desired_name.toGss(),
109 this->desiredMechs.toGss(),
116 if (GSS_ERROR(this->retVal) )
118 std::string err("Error acquiring credential for user '");
119 err += desired_name.toString();
121 throw GSSException(err, this->retVal, this->minor_status);
124 this->cred.setValue(output_cred_handle);
125 this->actualMechs = actual_mechs;
131 /* Desired JSON output:
134 * "command": "gss_acquire_cred",
138 * "cred_handle": "###########",
147 JSONObject *GSSAcquireCred::toJSON()
150 JSONObject *ret = new JSONObject();
151 JSONObject *values = new JSONObject();
152 // JSONObject mechs = JSONObject::array();
162 values->set("major_status", this->retVal);
163 values->set("minor_status", this->minor_status);
164 values->set("time_rec", (int)this->time_rec );
166 // Objects that generate their own JSONObject
167 temp = this->cred.toJSONValue();
168 values->set("output_cred_handle", *temp );
170 temp = this->actualMechs.toJSONValue();
171 values->set("actual_mechs", *temp);
173 // Put it all together.
174 ret->set("command", "gss_acquire_cred");
175 ret->set("return_values", *values);