3c193ca98c1d22cd1f17ddb2146d55fd57e89fd2
[gssweb.git] / json_gssapi / src / GSSRequest.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 <cstddef>
9 #include <stdexcept>
10 #include <string.h>
11
12 #include "commands/GSSAcquireCred.h"
13 #include "commands/GSSInitSecContext.h"
14 #include "commands/GSSImportName.h"
15 #include "commands/GSSDisplayName.h"
16 #include "GSSRequest.h"
17 #include "GSSException.h"
18
19 using std::bad_alloc;
20
21 GSSRequest::GSSRequest ( string jsonString )
22 {
23   /* Local variables */
24   /* Error checking */
25   /* Setup */
26   /* Main processing */
27   response = JSONObject();
28   cmd = NULL;
29   requestString = jsonString;
30   
31   /* Cleanup */
32   /* Return */
33 }
34
35 void GSSRequest::execute()
36 {
37   try {
38     /* variables */
39     /* Error checking */
40     /* Setup */
41     parseJSON();
42     getCommand();
43     
44     /* Main processing */
45     if (NULL != cmd)
46       cmd->execute();
47     
48     /* Cleanup */
49     /* Return */
50   }
51   catch (GSSException e)
52   {
53     delete(cmd);
54     cmd = NULL;
55     JSONObject return_values;
56     return_values.set("major_status", e.getMajor());
57     return_values.set("minor_status", e.getMinor());
58     return_values.set("what", e.what());
59     response.set("return_values", return_values);
60   }
61 }
62
63
64
65 void GSSRequest::parseJSON()
66 {
67   /* variables */
68   json_error_t jsonErr;
69   
70   try {
71     JSONObject cookies;
72     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
73     cookies = request.get("cookies");
74     response.set("cookies", cookies );
75     response.set("method", request.get("method").string());
76   }
77   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
78   catch ( bad_alloc& ) 
79   {
80     // Top-level response
81     response.set("error_message", "Could not parse the input JSON.");
82     response.set("original_message", requestString.c_str());
83   }
84 }
85
86
87 void GSSRequest::getCommand()
88 {
89   string method;
90   JSONObject arguments = request.get("arguments");
91   
92   /* Error checking */
93   /* Setup */
94   if (request.get("method").isNull() )
95     method = "";
96   else
97     method = string( request.get("method").string() );
98   
99   if ( "gss_import_name" == method ) 
100   {
101     cmd = new GSSImportName ( &arguments );
102   } 
103   else if ( "gss_init_sec_context" == method ) 
104   {
105     cmd = new GSSInitSecContext ( &arguments );
106   } 
107   else if ( "gss_acquire_cred" == method ) 
108   {
109     cmd = new GSSAcquireCred ( &arguments );
110   } 
111   else if ( "gss_display_name" == method )
112   {
113     cmd = new GSSDisplayName ( &arguments );
114   }
115   else 
116   {
117     string error_message = string("Unrecognized command: ") + method;
118     response.set("error_message", error_message.c_str() );
119     response.set("original_message", requestString.c_str());
120     cmd = NULL;
121   }
122 }
123
124 string GSSRequest::getResponse()
125 {
126   /* Variables */
127   JSONObject *return_values;
128   string gssResponse;
129   
130   /* Main processing */
131   // Put the return values into the response, assuming that the command
132   // was actually executed.
133   if (NULL != cmd)
134   {
135     return_values = cmd->toJSON();
136     response.set("return_values", *return_values);
137   }
138   
139   // Convert the response into a string to return.
140   gssResponse = string( response.dump() );
141   
142   /* Return */
143   return(gssResponse);
144 }
145
146
147
148 char *gss_request(char *json_string)
149 {
150   /* Variables */
151   GSSRequest *req = new GSSRequest(string(json_string));
152   
153   /* Error checking */
154   // An empty json_string could be an error, but GSSRequest does
155   // a good job of handling it.
156   
157   /* Setup */
158   /* Main processing */
159   req->execute();
160   return strdup(req->getResponse().c_str());
161 }
162
163 void deallocate_reply(char *reply)
164 {
165   delete(reply);
166 }