Commit an overly-large chunk of work.
[gssweb.git] / json_gssapi / src / commands / GSSImportName.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 "GSSImportName.h"
9 #include "GSSException.h"
10 #include <cache/GSSNameCache.h>
11
12
13 typedef OM_uint32 (*gss_imp_name_type)(
14     OM_uint32 *,        /* minor_status */
15     gss_buffer_t,       /* input_name_buffer */
16     gss_OID,            /* input_name_type(used to be const) */
17     gss_name_t *);      /* output_name */
18
19
20
21 void GSSImportName::execute()
22 {
23   /* Variables */
24   gss_name_t name;
25   std::string key;
26
27   /* Error checking */
28   /* Setup */
29
30   /* Main */
31   retVal = function(&minor_status, inputName.toGss(), inputNameType.toGss(), &name);
32   if ( GSS_ERROR(this->retVal) )
33   {
34     std::string errMsg;
35     errMsg += "Cannot import name: ";
36     errMsg += inputName.toString();
37     throw GSSException(errMsg.c_str(), this->retVal, this->minor_status, inputNameType.toGss());
38   }
39   this->outputName.setValue(name);
40   key = GSSNameCache::instance()->store(this->outputName);
41 //   std::cout << "Storing key: " << key << std::endl;
42   this->outputName.setKey(key);
43   /* Cleanup */
44   /* Return */
45   
46 }
47
48 /* Example output:
49  * 
50  * {
51  *   "command": "gss_import_name",
52  *   "return_values":
53  *   {
54  *     "major_status": 0,
55  *     "minor_status": 0,
56  *     "gss_name": "base64_encoded_string"
57  *   }
58  * }
59  * 
60  */
61 JSONObject *GSSImportName::toJSON()
62 {
63   /* Variables */
64   JSONObject *ret = new JSONObject();
65   JSONObject *values = new JSONObject();
66   
67   /* Error checking */
68   
69   /* Setup */
70   
71   /* Main */
72   values->set("major_status", this->retVal);
73   values->set("minor_status", this->minor_status);
74   values->set("gss_name", this->outputName.getKey().c_str() );
75   ret->set("command", "gss_import_name");
76   ret->set("return_values", *values);
77   
78   /* Cleanup */
79   
80   /* Return */
81   return(ret);
82 }
83
84 GSSImportName::GSSImportName ( gss_imp_name_type fn )
85 {
86   // defaults
87   function = fn;
88   inputName = "";
89   inputNameType.setValue(GSSBuffer("{ 1 2 840 113554 1 2 1 4 }"));
90 }
91
92 GSSImportName::GSSImportName(JSONObject *params, gss_imp_name_type fn) : GSSCommand(params)
93 {
94   /* Variables */
95   /* Error checking */
96   /* Setup */
97   /* Main */
98   loadParameters(params);
99   function = fn;
100   /* Cleanup */
101   /* Return */
102   
103 }
104
105 bool GSSImportName::loadParameters(JSONObject *params)
106 {
107   /* Variables */
108   
109   /* Error checking */
110   /* Setup */
111   // Should I zeroOut?
112   
113   /* Main processing */
114   // Easy stuff(*params)
115   if ( params->get("arguments").isNull() )
116     return true;
117   
118   if ( !params->get("arguments").get("input_name").isNull() )
119   {
120     if ( params->get("arguments").get("input_name").isString() )
121     {
122       std::string input_name = params->get("arguments").get("input_name").string();
123       this->inputName.setValue( input_name );
124     }
125   }
126   
127   if ( !params->get("arguments").get("input_name_type").isNull() )
128   {
129     if ( params->get("arguments").get("input_name_type").isString() )
130     {
131       std::string input_name_type = params->get("arguments").get("input_name_type").string();
132       this->inputNameType.setValue( input_name_type );
133     }
134   }
135   
136   /* Cleanup */
137   /* Return */
138   return true;
139 }
140
141
142 //   /***********
143 //    * QOP_REQ *
144 //    ***********/
145 //   if ( ! params->get("arguments").get("qop_req").isNull() )
146 //   {
147 //     if (params->get("arguments").get("qop_req").isString())
148 //     {
149 //       sQopReq = params->get("arguments").get("qop_req").string();
150 //       if (sQopReq == "GSS_C_QOP_DEFAULT")
151 //         this->qop_req = GSS_C_QOP_DEFAULT;
152 //       else
153 //         throw std::invalid_argument( std::string("Invalid QOP type given: ") + sQopReq );
154 //     } else if (params->get("arguments").get("qop_req").isInteger())
155 //       this->qop_req = (gss_cred_usage_t)( params->get("arguments").get("qop_req").integer() );
156 //     else
157 //       throw std::invalid_argument( "Unrecognized argument type for qop_req." );
158 //   }
159 //   
160 //   /*****************
161 //    * input_message *
162 //    *****************/
163 //   if ( ! params->get("arguments").get("input_message").isNull() )
164 //   {
165 //     sInputMessage = params->get("arguments").get("input_message").string();
166 //     this->inputMessage.setValue(sInputMessage);
167 //   }
168
169
170 /* Variables */
171 /* Error checking */
172 /* Setup */
173 /* Main */
174 /* Cleanup */
175 /* Return */