summaryrefslogtreecommitdiffstats
path: root/lasso/xml/saml-2.0/samlp2_logout_request.c
blob: d57f0789a57e8d26df9c18804b7552e220a84915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* $Id$
 *
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004-2007 Entr'ouvert
 * http://lasso.entrouvert.org
 *
 * Authors: See AUTHORS file in top-level directory.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "../private.h"
#include "../../utils.h"
#include "samlp2_logout_request.h"
#include <libxml/tree.h>
#include <xmlsec/xmltree.h>

/**
 * SECTION:samlp2_logout_request
 * @short_description: &lt;samlp2:LogoutRequest&gt;
 *
 * <figure><title>Schema fragment for samlp2:LogoutRequest</title>
 * <programlisting><![CDATA[
 *
 * <complexType name="LogoutRequestType">
 *   <complexContent>
 *     <extension base="samlp:RequestAbstractType">
 *       <sequence>
 *         <choice>
 *           <element ref="saml:BaseID"/>
 *           <element ref="saml:NameID"/>
 *           <element ref="saml:EncryptedID"/>
 *         </choice>
 *         <element ref="samlp:SessionIndex" minOccurs="0" maxOccurs="unbounded"/>
 *       </sequence>
 *       <attribute name="Reason" type="string" use="optional"/>
 *       <attribute name="NotOnOrAfter" type="dateTime" use="optional"/>
 *     </extension>
 *   </complexContent>
 * </complexType>
 * ]]></programlisting>
 * </figure>
 */

typedef struct _LassoSamlp2LogoutRequestPrivate LassoSamlp2LogoutRequestPrivate;

struct _LassoSamlp2LogoutRequestPrivate {
	GList *SessionIndex;
};

/*****************************************************************************/
/* private methods                                                           */
/*****************************************************************************/


static struct XmlSnippet schema_snippets[] = {
	{ "BaseID", SNIPPET_NODE | SNIPPET_JUMP_ON_MATCH | SNIPPET_JUMP_3, G_STRUCT_OFFSET(LassoSamlp2LogoutRequest, BaseID), NULL,
		LASSO_SAML2_ASSERTION_PREFIX, LASSO_SAML2_ASSERTION_HREF},
	{ "NameID", SNIPPET_NODE | SNIPPET_JUMP_ON_MATCH | SNIPPET_JUMP_2, G_STRUCT_OFFSET(LassoSamlp2LogoutRequest, NameID), NULL,
		LASSO_SAML2_ASSERTION_PREFIX, LASSO_SAML2_ASSERTION_HREF},
	{ "EncryptedID", SNIPPET_NODE, G_STRUCT_OFFSET(LassoSamlp2LogoutRequest, EncryptedID), NULL,
		LASSO_SAML2_ASSERTION_PREFIX, LASSO_SAML2_ASSERTION_HREF},
	{ "SessionIndex", SNIPPET_CONTENT,
		G_STRUCT_OFFSET(LassoSamlp2LogoutRequest, SessionIndex), NULL, NULL, NULL},
	{ "SessionIndex", SNIPPET_LIST_NODES,
		0, NULL, NULL, NULL},
	{ "Reason", SNIPPET_ATTRIBUTE,
		G_STRUCT_OFFSET(LassoSamlp2LogoutRequest, Reason), NULL, NULL, NULL},
	{ "NotOnOrAfter", SNIPPET_ATTRIBUTE,
		G_STRUCT_OFFSET(LassoSamlp2LogoutRequest, NotOnOrAfter), NULL, NULL, NULL},
	{NULL, 0, 0, NULL, NULL, NULL}
};

static LassoNodeClass *parent_class = NULL;

#define SESSION_INDEX "SessionIndex"

#define GET_PRIVATE(x) G_TYPE_INSTANCE_GET_PRIVATE(x, \
		LASSO_TYPE_SAMLP2_LOGOUT_REQUEST, LassoSamlp2LogoutRequestPrivate)

static void
dispose(GObject *object)
{
	LassoSamlp2LogoutRequest *logout_request = LASSO_SAMLP2_LOGOUT_REQUEST(object);
	LassoSamlp2LogoutRequestPrivate *pv;

	pv = GET_PRIVATE(logout_request);
	lasso_release_list_of_strings(pv->SessionIndex);
	G_OBJECT_CLASS(parent_class)->dispose(G_OBJECT(logout_request));
}

/*****************************************************************************/
/* instance and class init functions                                         */
/*****************************************************************************/

/* GROSS HACK: in order to support multiple session index elements, we use a private field that is
 * directly parsed or serialized through the overloaded get_xmlNode and init_from_xml virtual
 * methods.  The structure of LassoSamlp2LogoutRequest is part, until the next major release, of our
 * public ABI, so we cound not do otherwise.
 *
 * The last parsed element is kept in the legacy field logout_request->SessionIndex. At parsing and
 * serializing time it is separated from other elements. This should keep the old behaviour intact.
 */
static xmlNode*
get_xmlNode(LassoNode *node, gboolean lasso_dump)
{
	xmlNode *xmlnode;
	GList *other_session_index, *it;
	char *keep_session_index;

	other_session_index = lasso_samlp2_logout_request_get_session_indexes((LassoSamlp2LogoutRequest*)node);
	/* save SessionIndex simple field, and nullify it */
	keep_session_index = ((LassoSamlp2LogoutRequest*)node)->SessionIndex;
	((LassoSamlp2LogoutRequest*)node)->SessionIndex = NULL;
	xmlnode = parent_class->get_xmlNode(node, lasso_dump);
	lasso_foreach(it, other_session_index) {
		xmlNode *child = xmlSecAddChild(xmlnode, BAD_CAST SESSION_INDEX,
				BAD_CAST LASSO_SAML2_PROTOCOL_HREF);
#if (XMLSEC_MAJOR > 1) || (XMLSEC_MAJOR == 1 && XMLSEC_MINOR > 2) || (XMLSEC_MAJOR == 1 && XMLSEC_MINOR == 2 && XMLSEC_SUBMINOR > 12)
		xmlSecNodeEncodeAndSetContent(child, BAD_CAST it->data);
#else
		xmlChar *content;
		content = xmlEncodeSpecialChars(child->doc, BAD_CAST it->data);
		xmlNodeSetContent(child, content);
		xmlFree(content);
#endif
	}
	((LassoSamlp2LogoutRequest*)node)->SessionIndex = keep_session_index;
	lasso_release_list_of_strings(other_session_index);

	return xmlnode;
}

static int
init_from_xml(LassoNode *node, xmlNode *xmlnode)
{
	int rc = 0;
	xmlNode *child = NULL;
	LassoSamlp2LogoutRequestPrivate *pv = NULL;
	LassoSamlp2LogoutRequest *logout_request = (LassoSamlp2LogoutRequest*)node;

	rc = parent_class->init_from_xml(node, xmlnode);
	if ((logout_request->BaseID != 0) +
	    (logout_request->NameID != 0) +
	    (logout_request->EncryptedID != 0) != 1) {
		error("samlp2:LogoutRequest needs one of BaseID, NameID or EncryptedID");
		rc = 1;
	}

	if (rc == 0) {

		pv = GET_PRIVATE(node);
		child = xmlSecFindChild(xmlnode, BAD_CAST SESSION_INDEX,
				BAD_CAST LASSO_SAML2_PROTOCOL_HREF);
		while (child && xmlSecCheckNodeName(child, BAD_CAST SESSION_INDEX,
					BAD_CAST LASSO_SAML2_PROTOCOL_HREF)) {
			xmlChar *content = xmlNodeGetContent(child);
			lasso_list_add_string(pv->SessionIndex, (char*) content);
			lasso_release_xml_string(content);
			child = xmlSecGetNextElementNode(child->next);
		}
		/* remove the first one, since it is also stored in node->SessionIndex */
		if (pv->SessionIndex) {
			lasso_release_string(pv->SessionIndex->data);
			pv->SessionIndex = g_list_delete_link(pv->SessionIndex, pv->SessionIndex);
		}
	}

	return rc;
}

static void
class_init(LassoSamlp2LogoutRequestClass *klass)
{
	LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);

	parent_class = g_type_class_peek_parent(klass);
	nclass->node_data = g_new0(LassoNodeClassData, 1);
	klass->parent.parent.init_from_xml = init_from_xml;
	klass->parent.parent.get_xmlNode = get_xmlNode;
	lasso_node_class_set_nodename(nclass, "LogoutRequest");
	lasso_node_class_set_ns(nclass, LASSO_SAML2_PROTOCOL_HREF, LASSO_SAML2_PROTOCOL_PREFIX);
	lasso_node_class_add_snippets(nclass, schema_snippets);
	g_type_class_add_private(G_OBJECT_CLASS(klass), sizeof(LassoSamlp2LogoutRequestPrivate));
	G_OBJECT_CLASS(klass)->dispose = dispose;
}

GType
lasso_samlp2_logout_request_get_type()
{
	static GType this_type = 0;

	if (!this_type) {
		static const GTypeInfo this_info = {
			sizeof (LassoSamlp2LogoutRequestClass),
			NULL,
			NULL,
			(GClassInitFunc) class_init,
			NULL,
			NULL,
			sizeof(LassoSamlp2LogoutRequest),
			0,
			NULL,
			NULL
		};

		this_type = g_type_register_static(LASSO_TYPE_SAMLP2_REQUEST_ABSTRACT,
				"LassoSamlp2LogoutRequest", &this_info, 0);
	}
	return this_type;
}

/**
 * lasso_samlp2_logout_request_get_session_indexes:
 * @logout_request: a #LogoutRequest object
 *
 * If the logout request contains more than one SessionIndex element, this method must be used to
 * retrieve due to historical circonstances. It will a return a list of the content of the
 * SessionIndex elements.
 *
 * Return value:(element-type utf8)(transfer full):  a #GList of sessions index.
 */
GList*
lasso_samlp2_logout_request_get_session_indexes(LassoSamlp2LogoutRequest *logout_request)
{
	GList *ret = NULL;
	LassoSamlp2LogoutRequestPrivate *pv = NULL;
	g_return_val_if_fail(LASSO_IS_SAMLP2_LOGOUT_REQUEST(logout_request), NULL);

	/* Return concatenation of old field + new private field */
	pv = GET_PRIVATE(logout_request);
	lasso_assign_list_of_strings(ret, pv->SessionIndex);
	if (logout_request->SessionIndex) {
		ret = g_list_prepend(ret, g_strdup(logout_request->SessionIndex));
	}
	return ret;
}

/**
 * lasso_samlp2_logout_request_set_session_indexes:
 * @logout_request: a #LogoutRequest object
 * @session_index:(element-type utf8): a list of session index
 *
 * If you want to set more than one SessionIndex on a LogoutRequest, use this method. Beware that
 * the public field named SessionIndex corresponds to the last element in this list. This is an
 * symptom of the way elements are parsed by Lasso.
 *
 */
void
lasso_samlp2_logout_request_set_session_indexes(LassoSamlp2LogoutRequest *logout_request,
		GList *session_index)
{
	LassoSamlp2LogoutRequestPrivate *pv;

	g_return_if_fail(LASSO_IS_SAMLP2_LOGOUT_REQUEST(logout_request));

	/* assign rest of the list to the new private field */
	pv = GET_PRIVATE(logout_request);
	lasso_assign_list_of_strings(pv->SessionIndex, session_index);
	/* extract last element and assign it to old field */
	if (pv->SessionIndex && pv->SessionIndex->next) {
		GList *last = g_list_last(pv->SessionIndex);
		lasso_assign_new_string(logout_request->SessionIndex, (char*) last->data);
		pv->SessionIndex = g_list_delete_link(pv->SessionIndex, last);
	}
}

/**
 * lasso_samlp2_logout_request_new:
 *
 * Creates a new #LassoSamlp2LogoutRequest object.
 *
 * Return value: a newly created #LassoSamlp2LogoutRequest object
 **/
LassoNode*
lasso_samlp2_logout_request_new()
{
	return g_object_new(LASSO_TYPE_SAMLP2_LOGOUT_REQUEST, NULL);
}