1 /* $Id$
2  *
3  * Lasso - A free implementation of the Liberty Alliance specifications.
4  *
5  * Copyright (C) 2004-2007 Entr'ouvert
6  * http://lasso.entrouvert.org
7  *
8  * Authors: See AUTHORS file in top-level directory.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "../private.h"
25 #include "samlp2_response.h"
26 #include "saml2_assertion.h"
27 #include "saml2_encrypted_element.h"
28 #include "../../utils.h"
29 
30 /**
31  * SECTION:samlp2_response
32  * @short_description: &lt;samlp2:Response&gt;
33  *
34  * <figure><title>Schema fragment for samlp2:Response</title>
35  * <programlisting><![CDATA[
36  *
37  * <complexType name="ResponseType">
38  *   <complexContent>
39  *     <extension base="samlp:StatusResponseType">
40  *       <choice minOccurs="0" maxOccurs="unbounded">
41  *         <element ref="saml:Assertion"/>
42  *         <element ref="saml:EncryptedAssertion"/>
43  *       </choice>
44  *     </extension>
45  *   </complexContent>
46  * </complexType>
47  * ]]></programlisting>
48  * </figure>
49  */
50 
51 extern LassoNode* lasso_assertion_encrypt(LassoSaml2Assertion *assertion, char *recipient);
52 
53 /*****************************************************************************/
54 /* private methods                                                           */
55 /*****************************************************************************/
56 
57 static struct XmlSnippet schema_snippets[] = {
58 	{ "Assertion", SNIPPET_LIST_NODES, G_STRUCT_OFFSET(LassoSamlp2Response, Assertion), NULL,
59 		LASSO_SAML2_ASSERTION_PREFIX, LASSO_SAML2_ASSERTION_HREF},
60 	{ "EncryptedAssertion", SNIPPET_LIST_NODES | SNIPPET_JUMP_ON_MATCH | SNIPPET_BACK_1,
61 		G_STRUCT_OFFSET(LassoSamlp2Response, EncryptedAssertion),
62 		NULL, LASSO_SAML2_ASSERTION_PREFIX,
63 	LASSO_SAML2_ASSERTION_HREF},
64 	{NULL, 0, 0, NULL, NULL, NULL}
65 };
66 
67 static LassoNodeClass *parent_class = NULL;
68 
69 static xmlNode*
get_xmlNode(LassoNode * node,gboolean lasso_dump)70 get_xmlNode(LassoNode *node, gboolean lasso_dump)
71 {
72 	LassoSamlp2Response *response = LASSO_SAMLP2_RESPONSE(node);
73 	GList *assertions = NULL;
74 	GList *Assertion_save = NULL;
75 	LassoNode *encrypted_element = NULL;
76 	xmlNode *result = NULL;
77 
78 
79 	/* Encrypt Assertions for messages but not for dumps */
80 	if (lasso_dump == FALSE) {
81 		Assertion_save = response->Assertion;
82 		response->Assertion = NULL;
83 		lasso_foreach (assertions, Assertion_save) {
84 			encrypted_element = lasso_assertion_encrypt(assertions->data, NULL);
85 			if (encrypted_element != NULL) {
86 				lasso_list_add_new_gobject(response->EncryptedAssertion, encrypted_element);
87 			} else {
88 				lasso_list_add_gobject(response->Assertion, assertions->data);
89 			}
90 		}
91 	}
92 
93 	result = parent_class->get_xmlNode(node, lasso_dump);
94 
95 	if (lasso_dump == FALSE) {
96 		lasso_release_list_of_gobjects(response->EncryptedAssertion);
97 		lasso_assign_new_list_of_gobjects(response->Assertion, Assertion_save);
98 	}
99 
100 	return result;
101 }
102 
103 /*****************************************************************************/
104 /* instance and class init functions                                         */
105 /*****************************************************************************/
106 
107 
108 static void
class_init(LassoSamlp2ResponseClass * klass,void * unused G_GNUC_UNUSED)109 class_init(LassoSamlp2ResponseClass *klass, void *unused G_GNUC_UNUSED)
110 {
111 	LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);
112 
113 	parent_class = g_type_class_peek_parent(klass);
114 	nclass->get_xmlNode = get_xmlNode;
115 	nclass->node_data = g_new0(LassoNodeClassData, 1);
116 	nclass->node_data->keep_xmlnode = TRUE;
117 	lasso_node_class_set_nodename(nclass, "Response");
118 	lasso_node_class_set_ns(nclass, LASSO_SAML2_PROTOCOL_HREF, LASSO_SAML2_PROTOCOL_PREFIX);
119 	lasso_node_class_add_snippets(nclass, schema_snippets);
120 }
121 
122 GType
lasso_samlp2_response_get_type()123 lasso_samlp2_response_get_type()
124 {
125 	static GType this_type = 0;
126 
127 	if (!this_type) {
128 		static const GTypeInfo this_info = {
129 			sizeof (LassoSamlp2ResponseClass),
130 			NULL,
131 			NULL,
132 			(GClassInitFunc) class_init,
133 			NULL,
134 			NULL,
135 			sizeof(LassoSamlp2Response),
136 			0,
137 			NULL,
138 			NULL
139 		};
140 
141 		this_type = g_type_register_static(LASSO_TYPE_SAMLP2_STATUS_RESPONSE,
142 				"LassoSamlp2Response", &this_info, 0);
143 	}
144 	return this_type;
145 }
146 
147 /**
148  * lasso_samlp2_response_new:
149  *
150  * Creates a new #LassoSamlp2Response object.
151  *
152  * Return value: a newly created #LassoSamlp2Response object
153  **/
154 LassoNode*
lasso_samlp2_response_new()155 lasso_samlp2_response_new()
156 {
157 	return g_object_new(LASSO_TYPE_SAMLP2_RESPONSE, NULL);
158 }
159