Update loadParameters to check for JSON value type, NULL
[gssweb.git] / json_gssapi / src / commands / GSSGetMic.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 "GSSGetMic.h"
9
10 #include <stdexcept>
11 #include <mit-krb5/gssapi/gssapi.h>
12
13 GSSGetMic::GSSGetMic ( JSONObject* params, gss_get_mic_type fn )
14 {
15     this->function = fn;
16     loadParameters(params);
17 }
18
19 /*
20  * {"method": "gss_get_mic",
21  *  "arguments":
22  *  {
23  *       "context_handle": "#######",
24  *       "qop_req": "GSS_C_QOP_DEFAULT",
25  *       "input_message": "mary had a little lamb"
26  *  }
27  * }
28  * 
29  */
30 bool GSSGetMic::loadParameters ( JSONObject* params )
31 {
32   std::string sQopReq, sConfReq, sInputMessage;
33   
34   /* Error checking */
35   /* Setup */
36   // Should I zeroOut?
37   
38   /* Main processing */
39   
40   /***********
41    * QOP_REQ *
42    ***********/
43   if ( ! params->get("arguments").get("qop_req").isNull() )
44   {
45     if (params->get("arguments").get("qop_req").isString())
46     {
47       sQopReq = params->get("arguments").get("qop_req").string();
48       if (sQopReq == "GSS_C_QOP_DEFAULT")
49         this->qop_req = GSS_C_QOP_DEFAULT;
50       else
51         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
52     } else if (params->get("arguments").get("qop_req").isInteger())
53       this->qop_req = (gss_cred_usage_t)( params->get("arguments").get("qop_req").integer() );
54     else
55       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
56   }
57   
58   /*****************
59    * input_message *
60    *****************/
61   if ( ! params->get("arguments").get("input_message").isNull() )
62   {
63     sInputMessage = params->get("arguments").get("input_message").string();
64     this->inputMessage.setValue(sInputMessage);
65   }
66   
67   /* Cleanup */
68   /* Return */
69   return true;
70 }
71
72 void GSSGetMic::execute()
73 {
74     gss_buffer_desc output;
75     
76     this->retVal =  this->function(
77       &(this->minor_status),
78       this->context,
79       this->qop_req,
80       this->inputMessage.toGss(),
81       &output
82     );
83     
84     this->outputToken.setValue( (char *)(output.value), output.length );
85 }
86
87 JSONObject* GSSGetMic::toJSON()
88 {
89   /* Variables */
90   const char *conf_state;
91   JSONObject *ret = new JSONObject();
92   JSONObject *values = new JSONObject();
93   
94   /* Error checking */
95   
96   /* Setup */
97   
98   /* Main */
99   // Return Values
100   // Easy stuff
101   values->set("major_status", this->retVal);
102   values->set("minor_status", this->minor_status);
103   
104   values->set(
105     "output_token",
106     this->outputToken.toString().c_str()
107   );
108   
109   // Put it all together.
110   ret->set("command", "gss_get_mic");
111   ret->set("return_values", *values);
112   
113   /* Cleanup */
114   
115   /* Return */
116   return(ret);
117 }