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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/* $Id$
*
* Lasso - A free implementation of the Liberty Alliance specifications.
*
* Copyright (C) 2004, 2005 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <lasso/saml-2.0/providerprivate.h>
#include <lasso/saml-2.0/profileprivate.h>
#include <lasso/id-ff/providerprivate.h>
#include <lasso/id-ff/identityprivate.h>
#include <lasso/id-ff/serverprivate.h>
#include <lasso/saml-2.0/ecpprivate.h>
#include <lasso/saml-2.0/ecp.h>
/*****************************************************************************/
/* public methods */
/*****************************************************************************/
/**
* lasso_ecp_destroy:
* @ecp: a #LassoEcp
*
* Destroys a #LassoEcp object
*
**/
void
lasso_ecp_destroy(LassoEcp *ecp)
{
g_object_unref(G_OBJECT(ecp));
}
/*****************************************************************************/
/* private methods */
/*****************************************************************************/
static LassoNodeClass *parent_class = NULL;
/*****************************************************************************/
/* overridden parent class methods */
/*****************************************************************************/
static void
dispose(GObject *object)
{
LassoEcp *ecp = LASSO_ECP(object);
if (ecp->private_data->messageID) {
xmlFree(ecp->private_data->messageID);
ecp->private_data->messageID = NULL;
}
if (ecp->private_data->relay_state) {
xmlFree(ecp->private_data->relay_state);
ecp->private_data->relay_state = NULL;
}
G_OBJECT_CLASS(parent_class)->dispose(G_OBJECT(ecp));
}
static void
finalize(GObject *object)
{
LassoEcp *ecp = LASSO_ECP(object);
g_free(ecp->private_data);
ecp->private_data = NULL;
G_OBJECT_CLASS(parent_class)->finalize(object);
}
/*****************************************************************************/
/* instance and class init functions */
/*****************************************************************************/
static void
instance_init(LassoEcp *ecp)
{
ecp->private_data = g_new(LassoEcpPrivate, 1);
ecp->private_data->messageID = NULL;
ecp->private_data->relay_state = NULL;
ecp->assertionConsumerURL = NULL;
}
static void
class_init(LassoEcpClass *klass)
{
parent_class = g_type_class_peek_parent(klass);
G_OBJECT_CLASS(klass)->dispose = dispose;
G_OBJECT_CLASS(klass)->finalize = finalize;
}
int
lasso_ecp_process_authn_request_msg(LassoEcp *ecp, const char *authn_request_msg)
{
xmlDoc *doc;
xmlXPathContext *xpathCtx;
xmlXPathObject *xpathObj;
xmlNode *xmlnode;
xmlOutputBuffer *buf;
xmlCharEncodingHandler *handler;
LassoProfile *profile;
LassoProvider *remote_provider;
profile = LASSO_PROFILE(ecp);
if (authn_request_msg == NULL) {
return critical_error(LASSO_PROFILE_ERROR_MISSING_REQUEST);
}
doc = xmlParseMemory(authn_request_msg, strlen(authn_request_msg));
xpathCtx = xmlXPathNewContext(doc);
xmlXPathRegisterNs(xpathCtx, (xmlChar*)"ecp", (xmlChar*)LASSO_ECP_HREF);
xpathObj = xmlXPathEvalExpression((xmlChar*)"//ecp:RelayState", xpathCtx);
if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr) {
xmlnode = xpathObj->nodesetval->nodeTab[0];
ecp->private_data->relay_state = xmlNodeGetContent(xmlnode);
}
xmlXPathRegisterNs(xpathCtx, (xmlChar*)"paos", (xmlChar*)LASSO_PAOS_HREF);
xpathObj = xmlXPathEvalExpression((xmlChar*)"//paos:Request", xpathCtx);
if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr) {
ecp->private_data->messageID = xmlGetProp(
xpathObj->nodesetval->nodeTab[0], (xmlChar*)"messageID");
}
xmlXPathRegisterNs(xpathCtx, (xmlChar*)"s", (xmlChar*)LASSO_SOAP_ENV_HREF);
xpathObj = xmlXPathEvalExpression((xmlChar*)"//s:Header", xpathCtx);
if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr) {
xmlnode = xpathObj->nodesetval->nodeTab[0];
xmlUnlinkNode(xmlnode);
}
xmlnode = xmlDocGetRootElement(doc);
handler = xmlFindCharEncodingHandler("utf-8");
buf = xmlAllocOutputBuffer(handler);
xmlNodeDumpOutput(buf, NULL, xmlnode, 0, 0, "utf-8");
xmlOutputBufferFlush(buf);
LASSO_PROFILE(ecp)->msg_body = g_strdup(
(char*)(buf->conv ? buf->conv->content : buf->buffer->content));
xmlOutputBufferClose(buf);
profile->remote_providerID = lasso_server_get_first_providerID(profile->server);
if (profile->remote_providerID == NULL) {
return critical_error(LASSO_SERVER_ERROR_PROVIDER_NOT_FOUND);
}
remote_provider = g_hash_table_lookup(profile->server->providers,
profile->remote_providerID);
if (LASSO_IS_PROVIDER(remote_provider) == FALSE)
return critical_error(LASSO_SERVER_ERROR_PROVIDER_NOT_FOUND);
profile->msg_url = lasso_provider_get_metadata_one(remote_provider,
"SingleSignOnService SOAP");
if (profile->msg_url == NULL) {
return critical_error(LASSO_PROFILE_ERROR_UNKNOWN_PROFILE_URL);
}
return 0;
}
int
lasso_ecp_process_response_msg(LassoEcp *ecp, const char *response_msg)
{
xmlDoc *doc;
xmlXPathContext *xpathCtx;
xmlXPathObject *xpathObj;
xmlNode *new_envelope, *header, *paos_response, *ecp_relay_state;
xmlNode *body = NULL;
xmlOutputBuffer *buf;
xmlCharEncodingHandler *handler;
xmlNs *soap_env_ns, *ecp_ns;
doc = xmlParseMemory(response_msg, strlen(response_msg));
xpathCtx = xmlXPathNewContext(doc);
xmlXPathRegisterNs(xpathCtx, (xmlChar*)"s", (xmlChar*)LASSO_SOAP_ENV_HREF);
xpathObj = xmlXPathEvalExpression((xmlChar*)"//s:Body", xpathCtx);
if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr) {
body = xpathObj->nodesetval->nodeTab[0];
}
xmlXPathRegisterNs(xpathCtx, (xmlChar*)"ecp", (xmlChar*)LASSO_ECP_HREF);
xpathObj = xmlXPathEvalExpression((xmlChar*)"//ecp:Response", xpathCtx);
if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr) {
ecp->assertionConsumerURL = (char*)xmlGetProp(
xpathObj->nodesetval->nodeTab[0], (xmlChar*)"AssertionConsumerURL");
}
xmlXPathFreeContext(xpathCtx);
xmlXPathFreeObject(xpathObj);
new_envelope = xmlNewNode(NULL, (xmlChar*)"Envelope");
xmlSetNs(new_envelope, xmlNewNs(new_envelope,
(xmlChar*)LASSO_SOAP_ENV_HREF, (xmlChar*)LASSO_SOAP_ENV_PREFIX));
xmlNewNs(new_envelope,
(xmlChar*)LASSO_SAML_ASSERTION_HREF, (xmlChar*)LASSO_SAML_ASSERTION_PREFIX);
header = xmlNewTextChild(new_envelope, NULL, (xmlChar*)"Header", NULL);
/* PAOS request header block */
soap_env_ns = xmlNewNs(new_envelope,
(xmlChar*)LASSO_SOAP_ENV_HREF, (xmlChar*)LASSO_SOAP_ENV_PREFIX);
paos_response = xmlNewNode(NULL, (xmlChar*)"Response");
xmlSetNs(paos_response, xmlNewNs(paos_response,
(xmlChar*)LASSO_PAOS_HREF, (xmlChar*)LASSO_PAOS_PREFIX));
xmlSetNsProp(paos_response, soap_env_ns, (xmlChar*)"mustUnderstand", (xmlChar*)"1");
xmlSetNsProp(paos_response, soap_env_ns, (xmlChar*)"actor",
(xmlChar*)LASSO_SOAP_ENV_ACTOR);
if (ecp->private_data->messageID) {
xmlSetNsProp(paos_response, soap_env_ns, (xmlChar*)"refToMessageID",
(xmlChar*)ecp->private_data->messageID);
}
xmlAddChild(header, paos_response);
/* ECP relay state block */
if (ecp->private_data->relay_state) {
ecp_relay_state = xmlNewNode(NULL, (xmlChar*)"RelayState");
xmlNodeSetContent(ecp_relay_state, (xmlChar*)ecp->private_data->relay_state);
ecp_ns = xmlNewNs(ecp_relay_state, (xmlChar*)LASSO_ECP_HREF,
(xmlChar*)LASSO_ECP_PREFIX);
xmlSetNs(ecp_relay_state, ecp_ns);
xmlSetNsProp(ecp_relay_state, soap_env_ns, (xmlChar*)"mustUnderstand",
(xmlChar*)"1");
xmlSetNsProp(ecp_relay_state, soap_env_ns, (xmlChar*)"actor",
(xmlChar*)LASSO_SOAP_ENV_ACTOR);
xmlAddChild(header, ecp_relay_state);
}
xmlAddChild(new_envelope, xmlCopyNode(body, 1));
handler = xmlFindCharEncodingHandler("utf-8");
buf = xmlAllocOutputBuffer(handler);
xmlNodeDumpOutput(buf, NULL, new_envelope, 0, 0, "utf-8");
xmlOutputBufferFlush(buf);
LASSO_PROFILE(ecp)->msg_body = g_strdup(
(char*)(buf->conv ? buf->conv->content : buf->buffer->content));
xmlOutputBufferClose(buf);
xmlFreeDoc(doc);
return 0;
}
GType
lasso_ecp_get_type()
{
static GType this_type = 0;
if (!this_type) {
static const GTypeInfo this_info = {
sizeof (LassoEcpClass),
NULL,
NULL,
(GClassInitFunc) class_init,
NULL,
NULL,
sizeof(LassoEcp),
0,
(GInstanceInitFunc) instance_init,
};
this_type = g_type_register_static(LASSO_TYPE_PROFILE,
"LassoEcp", &this_info, 0);
}
return this_type;
}
/**
* lasso_ecp_new
*
* Creates a new #LassoEcp.
*
* Return value: a newly created #LassoEcp object; or NULL if an error
* occured
**/
LassoEcp*
lasso_ecp_new(LassoServer *server)
{
LassoEcp *ecp;
ecp = g_object_new(LASSO_TYPE_ECP, NULL);
LASSO_PROFILE(ecp)->server = g_object_ref(server);
return ecp;
}
|