696cd06c4e6cefa583098e264cdb0ab21322b496
[gssweb.git] / json_gssapi / src / datamodel / GSSOIDSet.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 "GSSOIDSet.h"
9 #include "../GSSException.h"
10
11 #include <cstring>
12
13 GSSOIDSet::GSSOIDSet( )
14 {
15   init();
16 }
17
18 void GSSOIDSet::init()
19 {
20   /* Variables */
21   OM_uint32 major, minor;
22   
23   /* Error checking */
24   /* Setup */
25   /* Main */
26   major = gss_create_empty_oid_set(&minor, &(this->set));
27   
28   // How the heck would this happen, anyway?
29   if (GSS_ERROR(major))
30     throw GSSException("Could not create an empty OID set.", major, minor);
31   
32   /* Cleanup */
33   /* return */
34 }
35
36 GSSOIDSet::~GSSOIDSet()
37 {
38   this->releaseOIDSet();
39 }
40
41 void GSSOIDSet::releaseOIDSet()
42 {
43   /* Variables */
44   OM_uint32 major, minor;
45   
46   /* Error checking */
47   /* Setup */
48   /* Main */
49   major = gss_release_oid_set(&minor, &(this->set));
50   if (GSS_ERROR(major))
51     throw GSSException("Error in releasing memory for an OID set", major, minor);
52   /* Cleanup */
53   /* return */
54 }
55
56 GSSOIDSet& GSSOIDSet::operator= ( const gss_OID_set other )
57 {
58   /* Variables */
59   OM_uint32 i;
60   gss_OID   element;
61   
62   /* Error checking */
63   /* Setup */
64   /* Main */
65   this->releaseOIDSet();
66   this->init();
67   for(i = 0; i < other->count; i++)
68   {
69     element = other->elements + i;
70     this->addOID(element);
71   }
72   
73   /* Cleanup */
74   /* return */
75   return(*this);
76 }
77
78 void GSSOIDSet::addOID ( const GSSOID other )
79 {
80   this->addOID(other.toGss());
81 }
82
83 void GSSOIDSet::addOID( const gss_OID other )
84 {
85   /* Variables */
86   OM_uint32 major, minor;
87   
88   /* Error checking */
89   /* Setup */
90   major = gss_add_oid_set_member(&minor, other, &(this->set) );
91   if (GSS_ERROR(major))
92     throw GSSException("Error encountered while trying to add another OID to a set", major, minor);
93
94   /* Cleanup */
95   /* return */
96 }
97
98
99 bool GSSOIDSet::includes ( const gss_OID oid ) const
100 {
101   /* Variables */
102   OM_uint32 major, minor;
103   int present;
104   
105   /* Error checking */
106   /* Setup */
107   /* Main */
108   major = gss_test_oid_set_member(&minor, oid, this->set, &present);
109   if (GSS_ERROR(major))
110     /* How? */
111     throw GSSException("Cannot check if an OID is included in a set.", major, minor);
112   
113   /* Cleanup */
114   /* return */
115   return (present == 1);
116 }
117
118 bool GSSOIDSet::includes ( const GSSOID &oid ) const
119 {
120   return(this->includes( oid.toGss() ) );
121 }
122
123 /*
124  * Desired JSON Output:
125  * 
126  * [
127  *   "{ 1 2 3 4 }",
128  *   "{ 5 6 7 8 }"
129  * ]
130  * 
131  */
132 JSONObject* GSSOIDSet::toJSONValue() const
133 {
134   /* Variables */
135   JSONObject  jsonArray = JSONObject::array();
136   JSONObject *ret, *temp;
137   OM_uint32   index;
138   /* Error checking */
139   /* Setup */
140   /* Main */
141
142   for (index = 0; index < set->count; index++)
143   {
144     GSSOID m( set->elements + index );
145     temp = m.toJSONValue();
146     jsonArray.append( *temp );
147   }
148   
149   ret = new JSONObject(jsonArray);
150   
151   /* Cleanup */
152   /* return */
153   return(ret);
154 }
155
156
157   /* Variables */
158   /* Error checking */
159   /* Setup */
160   /* Main */
161   /* Cleanup */
162   /* return */
163