summaryrefslogtreecommitdiffstats
path: root/lasso/id-ff/session.c
blob: 6e4db4dea3258073527956c4056a54a4dda8f8e8 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* $Id$
 *
 * Lasso - A free implementation of the Liberty Alliance specifications.
 *
 * Copyright (C) 2004 Entr'ouvert
 * http://lasso.entrouvert.org
 * 
 * Authors: Nicolas Clapies <nclapies@entrouvert.com>
 *          Valery Febvre <vfebvre@easter-eggs.com>
 *
 * 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 <lasso/environs/session.h>

#define LASSO_SESSION_NODE                   "LassoSession"
#define LASSO_SESSION_ASSERTIONS_NODE        "LassoAssertions"
#define LASSO_SESSION_ASSERTION_NODE         "LassoAssertion"
#define LASSO_SESSION_REMOTE_PROVIDERID_ATTR "RemoteProviderID"

struct _LassoSessionPrivate
{
  gboolean dispose_has_run;
};

static GObjectClass *parent_class = NULL;

/*****************************************************************************/
/* private functions                                                         */
/*****************************************************************************/

static void
lasso_session_copy_assertion(gpointer key,
			     gpointer value,
			     gpointer assertions)
{
  g_hash_table_insert((GHashTable *)assertions, g_strdup((gchar *)key),
		      lasso_node_copy(LASSO_NODE(value)));
}

static void
lasso_session_dump_assertion(gpointer   key,
			     gpointer   value,
			     LassoNode *assertions)
{
  LassoNode      *assertion_node, *assertion_copy;
  LassoNodeClass *assertion_class, *assertions_class;

  /* new lasso assertion node */
  assertion_node = lasso_node_new();
  assertion_class = LASSO_NODE_GET_CLASS(assertion_node);
  assertion_class->set_name(assertion_node, LASSO_SESSION_ASSERTION_NODE);

  /* set the remote provider id */
  assertion_class->set_prop(assertion_node, LASSO_SESSION_REMOTE_PROVIDERID_ATTR, key);
  
  /* set assertion node */
  assertion_copy = lasso_node_copy(LASSO_NODE(value));
  assertion_class->add_child(assertion_node, assertion_copy, FALSE);
  lasso_node_destroy(assertion_copy);

  /* add lasso assertion node to lasso assertions node */
  assertions_class = LASSO_NODE_GET_CLASS(assertions);
  assertions_class->add_child(assertions, assertion_node, TRUE);
  lasso_node_destroy(assertion_node);
}

/*****************************************************************************/
/* public methods                                                            */
/*****************************************************************************/

gint
lasso_session_add_assertion(LassoSession *session,
			    gchar        *remote_providerID,
			    LassoNode    *assertion)
{
  int i;
  gboolean found;

  g_return_val_if_fail(session!=NULL, -1);
  g_return_val_if_fail(remote_providerID!=NULL, -2);
  g_return_val_if_fail(assertion!=NULL, -3);

  /* add the remote provider id */
  found = FALSE;
  for(i = 0; i<session->providerIDs->len; i++) {
    if(xmlStrEqual(remote_providerID, g_ptr_array_index(session->providerIDs, i))) {
      found = TRUE;
    }
  }
  if(found == TRUE) {
    message(G_LOG_LEVEL_ERROR, "A provider id already exists\n");
    return(-4);
  }

  g_ptr_array_add(session->providerIDs, g_strdup(remote_providerID));

  /* add the assertion */
  g_hash_table_insert(session->assertions, g_strdup(remote_providerID),
		      lasso_node_copy(assertion));

  return(0);
}

LassoSession*
lasso_session_copy(LassoSession *session)
{
  LassoSession *copy;
  guint i;

  if (session == NULL) {
    return(NULL);
  }

  copy = LASSO_SESSION(g_object_new(LASSO_TYPE_SESSION, NULL));

  copy->providerIDs = g_ptr_array_new();
  for(i=0; i<session->providerIDs->len; i++) {
    g_ptr_array_add(copy->providerIDs,
		    g_strdup(g_ptr_array_index(session->providerIDs, i)));
  }
  copy->assertions = g_hash_table_new_full(g_str_hash, g_str_equal,
					   (GDestroyNotify)g_free,
					   (GDestroyNotify)lasso_node_destroy);
  g_hash_table_foreach(copy->assertions, (GHFunc)lasso_session_copy_assertion,
		       (gpointer)copy->assertions);

  return(copy);
}

void
lasso_session_destroy(LassoSession *session)
{
  if (LASSO_IS_SESSION(session)) {
    g_object_unref(G_OBJECT(session));
  }
}

gchar*
lasso_session_dump(LassoSession *session)
{
  LassoNode      *session_node, *assertions_node;
  LassoNodeClass *session_class, *assertions_class;
  int table_size;
  xmlChar *dump;

  g_return_val_if_fail(session != NULL, NULL);

  session_node = lasso_node_new();
  session_class = LASSO_NODE_GET_CLASS(session_node);
  session_class->set_name(session_node, LASSO_SESSION_NODE);

  /* dump the assertions */
  table_size = g_hash_table_size(session->assertions);
  if (table_size > 0) {
    assertions_node = lasso_node_new();
    assertions_class = LASSO_NODE_GET_CLASS(assertions_node);
    assertions_class->set_name(assertions_node, LASSO_SESSION_ASSERTIONS_NODE);
    g_hash_table_foreach(session->assertions, (GHFunc)lasso_session_dump_assertion,
			 assertions_node);
    session_class->add_child(session_node, assertions_node, FALSE);
    lasso_node_destroy(assertions_node);
  }

  dump = lasso_node_export(session_node);

  lasso_node_destroy(session_node);

  return(dump);
}

LassoNode*
lasso_session_get_assertion(LassoSession *session,
			    gchar        *remote_providerID)
{
  LassoNode *assertion;

  g_return_val_if_fail(session != NULL, NULL);
  g_return_val_if_fail(remote_providerID != NULL, NULL);

  assertion = (LassoNode *)g_hash_table_lookup(session->assertions,
					       remote_providerID);
  if (assertion == NULL) {
    return NULL;
  }

  return(lasso_node_copy(assertion));
}

gchar*
lasso_session_get_authentication_method(LassoSession *session,
					gchar        *remote_providerID)
{
  LassoNode *assertion, *as;
  gchar *providerID = remote_providerID;
  gchar *authentication_method;
  GError *err = NULL;

  if (remote_providerID == NULL) {
    providerID = lasso_session_get_next_assertion_remote_providerID(session);
  }
  assertion = lasso_session_get_assertion(session, providerID);
  if (remote_providerID == NULL) {
    g_free(providerID);
  }
  as = lasso_node_get_child(assertion, "AuthenticationStatement", NULL);
  authentication_method = lasso_node_get_attr_value(as, "AuthenticationMethod", &err);
  if (authentication_method == NULL) {
    message(G_LOG_LEVEL_ERROR, err->message);
    g_error_free(err);
    goto done;
  }

 done:
  lasso_node_destroy(as);
  lasso_node_destroy(assertion);
  return (authentication_method);
}

gchar*
lasso_session_get_next_assertion_remote_providerID(LassoSession *session)
{
  gchar *remote_providerID;

  g_return_val_if_fail(session!=NULL, NULL);

  if(session->providerIDs->len == 0) {
    return(NULL);
  }

  remote_providerID = g_strdup(g_ptr_array_index(session->providerIDs, 0));

  return(remote_providerID);
}

gint
lasso_session_remove_assertion(LassoSession *session,
			       gchar        *remote_providerID)
{
  LassoNode *assertion;
  int i;

  g_return_val_if_fail(session!=NULL, -1);

  g_return_val_if_fail(remote_providerID!=NULL, -2);

  /* remove the assertion */
  assertion = lasso_session_get_assertion(session, remote_providerID);
  if (assertion != NULL) {
    g_hash_table_remove(session->assertions, remote_providerID);
    lasso_node_destroy(assertion);
  }

  /* remove the remote provider id */
  for(i = 0; i<session->providerIDs->len; i++){
    if(xmlStrEqual(remote_providerID, g_ptr_array_index(session->providerIDs, i))){
      g_ptr_array_remove_index(session->providerIDs, i);
      break;
    }
  }

  return(0);
}

/*****************************************************************************/
/* overrided parent class methods                                            */
/*****************************************************************************/

static void
lasso_session_dispose(LassoSession *session)
{
  if (session->private->dispose_has_run == TRUE) {
    return;
  }
  session->private->dispose_has_run = TRUE;

  debug("Session object 0x%x disposed ...\n", session);

  g_hash_table_destroy(session->assertions);
  session->assertions = NULL;

  parent_class->dispose(G_OBJECT(session));
}

static void
lasso_session_finalize(LassoSession *session)
{
  gint i;

  debug("Session object 0x%x finalized ...\n", session);

  /* free allocated memory for providerIDs array */
  for (i=0; i<session->providerIDs->len; i++) {
    g_free(session->providerIDs->pdata[i]);
    session->providerIDs->pdata[i] = NULL;
  }
  g_ptr_array_free(session->providerIDs, TRUE);
  session->providerIDs = NULL;

  g_free(session->private);
  session->private = NULL;

  parent_class->finalize(G_OBJECT(session));
}

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

static void
lasso_session_instance_init(LassoSession *session)
{
  session->private = g_new (LassoSessionPrivate, 1);
  session->private->dispose_has_run = FALSE;

  session->providerIDs = g_ptr_array_new();
  session->assertions = g_hash_table_new_full(g_str_hash, g_str_equal,
					      (GDestroyNotify)g_free,
					      (GDestroyNotify)lasso_node_destroy);
}

static void
lasso_session_class_init(LassoSessionClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS(class);
  
  parent_class = g_type_class_peek_parent(class);
  /* override parent class methods */
  gobject_class->dispose  = (void *)lasso_session_dispose;
  gobject_class->finalize = (void *)lasso_session_finalize;
}

GType lasso_session_get_type() {
  static GType this_type = 0;

  if (!this_type) {
    static const GTypeInfo this_info = {
      sizeof (LassoSessionClass),
      NULL,
      NULL,
      (GClassInitFunc) lasso_session_class_init,
      NULL,
      NULL,
      sizeof(LassoSession),
      0,
      (GInstanceInitFunc) lasso_session_instance_init,
    };
    
    this_type = g_type_register_static(G_TYPE_OBJECT,
				       "LassoSession",
				       &this_info, 0);
  }
  return this_type;
}

LassoSession*
lasso_session_new()
{
  LassoSession *session;

  session = LASSO_SESSION(g_object_new(LASSO_TYPE_SESSION, NULL));

  return(session);
}

LassoSession*
lasso_session_new_from_dump(gchar *dump)
{
  LassoSession   *session;
  LassoNode      *session_node;
  LassoNode      *assertions_node, *assertion_node;
  xmlNodePtr      assertions_xmlNode, assertion_xmlNode;
  xmlChar        *remote_providerID;
  GError         *err = NULL;

  g_return_val_if_fail(dump != NULL, NULL);

  session = LASSO_SESSION(g_object_new(LASSO_TYPE_SESSION, NULL));

  /* get session */
  session_node = lasso_node_new_from_dump(dump);
  if (session_node == NULL) {
    message(G_LOG_LEVEL_WARNING, "Can't create a session from dump\n");
    return (NULL);
  }

  /* get assertions */
  assertions_node = lasso_node_get_child(session_node,
					 LASSO_SESSION_ASSERTIONS_NODE, NULL);
  if (assertions_node != NULL) {
    assertions_xmlNode = LASSO_NODE_GET_CLASS(assertions_node)->get_xmlNode(assertions_node);
    assertion_xmlNode = assertions_xmlNode->children;

    while (assertion_xmlNode != NULL) {
      /* assertion xmlNode  */
      if (assertion_xmlNode->type == XML_ELEMENT_NODE && \
	  xmlStrEqual(assertion_xmlNode->name, LASSO_SESSION_ASSERTION_NODE)) {
	/* assertion node */
	assertion_node = lasso_node_new_from_xmlNode(assertion_xmlNode);
	remote_providerID = lasso_node_get_attr_value(assertion_node,
						      LASSO_SESSION_REMOTE_PROVIDERID_ATTR,
						      &err);
	if (remote_providerID == NULL) {
	  message(G_LOG_LEVEL_CRITICAL, err->message);
	  g_error_free(err);
	  continue;
	}
	lasso_session_add_assertion(session, remote_providerID, assertion_node);
	g_free(remote_providerID);
	lasso_node_destroy(assertion_node);
      }
      assertion_xmlNode = assertion_xmlNode->next;
    }
  }
  lasso_node_destroy(assertions_node);
  lasso_node_destroy(session_node);

  return (session);
}