Update loadParameters to check for JSON value type, NULL
[gssweb.git] / json_gssapi / src / GSSAcquireCred.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  *
4  * For license details, see the LICENSE file in the root of this project.
5  *
6  */
7
8 #include "GSSAcquireCred.h"
9 #include "GSSException.h"
10
11 #include <stdexcept>
12
13 GSSAcquireCred::GSSAcquireCred(gss_acq_cred_type fn) : function(fn)
14 {
15
16 }
17
18 GSSAcquireCred::GSSAcquireCred ( const GSSAcquireCred& other )
19 {
20
21 }
22
23 GSSAcquireCred::~GSSAcquireCred()
24 {
25
26 }
27
28 GSSAcquireCred::GSSAcquireCred ( 
29     JSONObject *params, 
30     gss_acq_cred_type fn
31 ) : GSSCommand ( params )
32 {
33   /* Variables */
34   /* Error checking */
35   /* Setup */
36   /* Main */
37   loadParameters(params);
38   function = fn;
39   /* Cleanup */
40   /* Return */
41 }
42
43
44 bool GSSAcquireCred::loadParameters(JSONObject *params)
45 {
46   /* Variables */
47   std::string sCredUsage;
48   size_t nDesiredMechs;
49   
50   /* Error checking */
51   /* Setup */
52   // Should I zeroOut?
53   
54   /* Main processing */
55   // Easy stuff(*params)
56   this->time_req = (*params)["arguments"]["time_req"].integer();
57
58   if ( ! params->get("arguments").get("cred_usage").isNull() )
59   {
60     if (params->get("arguments").get("cred_usage").isString())
61     {
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;
69       else
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() );
73     else
74       throw std::invalid_argument( "Unrecognized argument type for cred_usage." );
75       }
76   
77   if ( params->get("arguments").get("desired_mechs").isArray() )
78   {
79     for (nDesiredMechs = 0; 
80          nDesiredMechs < params->get("arguments").get("desired_mechs").size();
81          nDesiredMechs++)
82     {
83       std::string mechStr = params->get("arguments").get("desired_mechs")[nDesiredMechs].string();
84       desiredMechs.addOID( GSSOID(mechStr).toGss() );
85     }
86   } else
87     throw std::invalid_argument("Unrecognized desired_mechs array.");
88   
89   /* Cleanup */
90   /* Return */
91   return true;
92 }
93
94
95
96 void GSSAcquireCred::execute()
97 {
98   /* Variables */
99   gss_cred_id_t output_cred_handle;
100   gss_OID_set   actual_mechs;
101   
102   /* Error checking */
103   /* Setup */
104   /* Main */
105   this->retVal = function(
106     &this->minor_status,
107     this->desired_name.toGss(),
108     this->time_req,
109     this->desiredMechs.toGss(),
110     this->cred_usage,
111     &output_cred_handle,
112     &actual_mechs,
113     &this->time_rec
114   );
115   
116   if (GSS_ERROR(this->retVal) )
117   {
118     std::string err("Error acquiring credential for user '");
119     err += desired_name.toString();
120     err += "'.";
121     throw GSSException(err, this->retVal, this->minor_status);
122   }
123   
124   this->cred.setValue(output_cred_handle);
125   this->actualMechs = actual_mechs;
126   
127   /* Cleanup */
128   /* Return */
129 }
130
131 /* Desired JSON output:
132  * 
133  * {
134  *   "command": "gss_acquire_cred",
135  *   "return_values": {
136  *     "major_status": 0,
137  *     "minor_status": 0,
138  *     "cred_handle": "###########",
139  *     "actual_mechs": [
140  *       "{ 1 2 3 4 }",
141  *       "{ 5 6 7 8 }"
142  *     ],
143  *     "time_rec": 0
144  *   }
145  * }
146  */
147 JSONObject *GSSAcquireCred::toJSON()
148 {
149   /* Variables */
150   JSONObject *ret = new JSONObject();
151   JSONObject *values = new JSONObject();
152 //   JSONObject mechs = JSONObject::array();
153   JSONObject *temp;
154   
155   /* Error checking */
156   
157   /* Setup */
158   
159   /* Main */
160   // Return Values
161   // Easy stuff
162   values->set("major_status", this->retVal);
163   values->set("minor_status", this->minor_status);
164   values->set("time_rec", (int)this->time_rec );
165   
166   // Objects that generate their own JSONObject
167   temp = this->cred.toJSONValue();
168   values->set("output_cred_handle", *temp );
169   
170   temp = this->actualMechs.toJSONValue();
171   values->set("actual_mechs", *temp);
172   
173   // Put it all together.
174   ret->set("command", "gss_acquire_cred");
175   ret->set("return_values", *values);
176   
177   /* Cleanup */
178   
179   /* Return */
180   return(ret);
181 }