Updated code for gss_init_sec_context. First round trip now works, but input token is
[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
11 #include "commands/GSSAcquireCred.h"
12 #include "commands/GSSInitSecContext.h"
13 #include "commands/GSSImportName.h"
14 #include "GSSRequest.h"
15
16 using std::bad_alloc;
17
18 GSSRequest::GSSRequest ( string jsonString )
19 {
20   /* Local variables */
21   /* Error checking */
22   /* Setup */
23   /* Main processing */
24   response = JSONObject();
25   cmd = NULL;
26   requestString = jsonString;
27   
28   /* Cleanup */
29   /* Return */
30 }
31
32 void GSSRequest::execute()
33 {
34   /* variables */
35   /* Error checking */
36   /* Setup */
37   parseJSON();
38   getCommand();
39   
40   /* Main processing */
41   if (NULL != cmd)
42     cmd->execute();
43   
44   /* Cleanup */
45   /* Return */
46 }
47
48
49
50 void GSSRequest::parseJSON()
51 {
52   /* variables */
53   json_error_t jsonErr;
54   
55   try {
56     JSONObject cookies;
57     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
58     cookies = request.get("cookies");
59     response.set("cookies", cookies );
60     response.set("method", request.get("method").string());
61   }
62   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
63   catch ( bad_alloc& e ) 
64   {
65     // Top-level response
66     response.set("error_message", "Could not parse the input JSON.");
67     response.set("original_message", requestString.c_str());
68   }
69 }
70
71
72 void GSSRequest::getCommand()
73 {
74   string method;
75   JSONObject arguments = request.get("arguments");
76   
77   /* Error checking */
78   /* Setup */
79   if (request.get("method").isNull() )
80     method = "";
81   else
82     method = string( request.get("method").string() );
83   
84   if ( "gss_import_name" == method ) 
85   {
86     cmd = new GSSImportName ( &arguments );
87   } 
88   else if ( "gss_init_sec_context" == method ) 
89   {
90     cmd = new GSSInitSecContext ( &arguments );
91   } 
92   else if ( "gss_acquire_cred" == method ) 
93   {
94     cmd = new GSSAcquireCred ( &arguments );
95   } 
96   else 
97   {
98     string error_message = string("Unrecognized command: ") + method;
99     response.set("error_message", error_message.c_str() );
100     response.set("original_message", requestString.c_str());
101     cmd = NULL;
102   }
103 }
104
105 string GSSRequest::getResponse()
106 {
107   /* Variables */
108   JSONObject *return_values;
109   string gssResponse;
110   
111   /* Main processing */
112   // Put the return values into the response, assuming that the command
113   // was actually executed.
114   if (NULL != cmd)
115   {
116     return_values = cmd->toJSON();
117     response.set("return_values", *return_values);
118   }
119   
120   // Convert the response into a string to return.
121   gssResponse = string( response.dump() );
122   
123   /* Return */
124   return(gssResponse);
125 }