~ubuntu-branches/ubuntu/utopic/lasso/utopic-proposed

« back to all changes in this revision

Viewing changes to lasso/environs/session.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2004-09-13 09:26:34 UTC
  • Revision ID: james.westby@ubuntu.com-20040913092634-01vdfl8j9cp94exa
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: session.c,v 1.24 2004/09/06 17:49:19 fpeters Exp $
 
2
 *
 
3
 * Lasso - A free implementation of the Liberty Alliance specifications.
 
4
 *
 
5
 * Copyright (C) 2004 Entr'ouvert
 
6
 * http://lasso.entrouvert.org
 
7
 * 
 
8
 * Authors: Nicolas Clapies <nclapies@entrouvert.com>
 
9
 *          Valery Febvre <vfebvre@easter-eggs.com>
 
10
 *
 
11
 * This program is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2 of the License, or
 
14
 * (at your option) any later version.
 
15
 * 
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 * 
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
24
 */
 
25
 
 
26
#include <lasso/environs/session.h>
 
27
#include <lasso/lasso_config.h>
 
28
 
 
29
#define LASSO_SESSION_NODE                   "Session"
 
30
#define LASSO_SESSION_ASSERTIONS_NODE        "Assertions"
 
31
#define LASSO_SESSION_ASSERTION_NODE         "AuthnAssertion"
 
32
#define LASSO_SESSION_REMOTE_PROVIDERID_ATTR "RemoteProviderID"
 
33
 
 
34
struct _LassoSessionPrivate
 
35
{
 
36
  gboolean dispose_has_run;
 
37
};
 
38
 
 
39
static GObjectClass *parent_class = NULL;
 
40
 
 
41
/*****************************************************************************/
 
42
/* private functions                                                         */
 
43
/*****************************************************************************/
 
44
 
 
45
static void
 
46
lasso_session_copy_assertion(gpointer key,
 
47
                             gpointer value,
 
48
                             gpointer assertions)
 
49
{
 
50
  g_hash_table_insert((GHashTable *)assertions, g_strdup((gchar *)key),
 
51
                      lasso_node_copy(LASSO_NODE(value)));
 
52
}
 
53
 
 
54
static void
 
55
lasso_session_dump_assertion(gpointer   key,
 
56
                             gpointer   value,
 
57
                             LassoNode *assertions)
 
58
{
 
59
  LassoNode      *assertion_node, *assertion_copy;
 
60
  LassoNodeClass *assertion_class, *assertions_class;
 
61
 
 
62
  /* new lasso assertion node */
 
63
  assertion_node = lasso_node_new();
 
64
  assertion_class = LASSO_NODE_GET_CLASS(assertion_node);
 
65
  assertion_class->set_name(assertion_node, LASSO_SESSION_ASSERTION_NODE);
 
66
 
 
67
  /* set the remote provider id */
 
68
  assertion_class->set_prop(assertion_node, LASSO_SESSION_REMOTE_PROVIDERID_ATTR, key);
 
69
  
 
70
  /* set assertion node */
 
71
  assertion_copy = lasso_node_copy(LASSO_NODE(value));
 
72
  assertion_class->add_child(assertion_node, assertion_copy, FALSE);
 
73
  lasso_node_destroy(assertion_copy);
 
74
 
 
75
  /* add lasso assertion node to lasso assertions node */
 
76
  assertions_class = LASSO_NODE_GET_CLASS(assertions);
 
77
  assertions_class->add_child(assertions, assertion_node, TRUE);
 
78
  lasso_node_destroy(assertion_node);
 
79
}
 
80
 
 
81
/*****************************************************************************/
 
82
/* public methods                                                            */
 
83
/*****************************************************************************/
 
84
 
 
85
gint
 
86
lasso_session_add_assertion(LassoSession *session,
 
87
                            gchar        *providerID,
 
88
                            LassoNode    *assertion)
 
89
{
 
90
  int i;
 
91
  gboolean found = FALSE;
 
92
 
 
93
  g_return_val_if_fail(session != NULL, -1);
 
94
  g_return_val_if_fail(providerID != NULL, -2);
 
95
  g_return_val_if_fail(assertion != NULL, -3);
 
96
 
 
97
  /* add the remote provider id */
 
98
  for(i = 0; i<session->providerIDs->len; i++) {
 
99
    if(xmlStrEqual(providerID, g_ptr_array_index(session->providerIDs, i))) {
 
100
      found = TRUE;
 
101
      break;
 
102
    }
 
103
  }
 
104
  if(found == TRUE) {
 
105
    debug("An assertion existed already for this providerID, it was replaced by the new one.\n");
 
106
  }
 
107
  else {
 
108
    g_ptr_array_add(session->providerIDs, g_strdup(providerID));
 
109
  }
 
110
 
 
111
  /* add the assertion */
 
112
  g_hash_table_insert(session->assertions, g_strdup(providerID),
 
113
                      lasso_node_copy(assertion));
 
114
 
 
115
  session->is_dirty = TRUE;
 
116
 
 
117
  return 0;
 
118
}
 
119
 
 
120
LassoSession*
 
121
lasso_session_copy(LassoSession *session)
 
122
{
 
123
  LassoSession *copy;
 
124
  guint i;
 
125
 
 
126
  if (session == NULL) {
 
127
    return NULL;
 
128
  }
 
129
 
 
130
  copy = LASSO_SESSION(g_object_new(LASSO_TYPE_SESSION, NULL));
 
131
 
 
132
  copy->providerIDs = g_ptr_array_new();
 
133
  for(i=0; i<session->providerIDs->len; i++) {
 
134
    g_ptr_array_add(copy->providerIDs,
 
135
                    g_strdup(g_ptr_array_index(session->providerIDs, i)));
 
136
  }
 
137
  copy->assertions = g_hash_table_new_full(g_str_hash, g_str_equal,
 
138
                                           (GDestroyNotify)g_free,
 
139
                                           (GDestroyNotify)lasso_node_destroy);
 
140
  g_hash_table_foreach(session->assertions, (GHFunc)lasso_session_copy_assertion,
 
141
                       (gpointer)copy->assertions);
 
142
  copy->is_dirty = session->is_dirty;
 
143
 
 
144
  return copy;
 
145
}
 
146
 
 
147
void
 
148
lasso_session_destroy(LassoSession *session)
 
149
{
 
150
  if (LASSO_IS_SESSION(session)) {
 
151
    g_object_unref(G_OBJECT(session));
 
152
  }
 
153
}
 
154
 
 
155
gchar*
 
156
lasso_session_dump(LassoSession *session)
 
157
{
 
158
  LassoNode      *session_node, *assertions_node;
 
159
  LassoNodeClass *session_class, *assertions_class;
 
160
  int             table_size;
 
161
  gchar          *dump;
 
162
 
 
163
  g_return_val_if_fail(session != NULL, NULL);
 
164
 
 
165
  session_node = lasso_node_new();
 
166
  session_class = LASSO_NODE_GET_CLASS(session_node);
 
167
  session_class->set_name(session_node, LASSO_SESSION_NODE);
 
168
  session_class->set_ns(session_node, lassoLassoHRef, NULL);
 
169
 
 
170
  /* dump the assertions */
 
171
  table_size = g_hash_table_size(session->assertions);
 
172
  if (table_size > 0) {
 
173
    assertions_node = lasso_node_new();
 
174
    assertions_class = LASSO_NODE_GET_CLASS(assertions_node);
 
175
    assertions_class->set_name(assertions_node, LASSO_SESSION_ASSERTIONS_NODE);
 
176
    g_hash_table_foreach(session->assertions, (GHFunc)lasso_session_dump_assertion,
 
177
                         assertions_node);
 
178
    session_class->add_child(session_node, assertions_node, FALSE);
 
179
    lasso_node_destroy(assertions_node);
 
180
  }
 
181
 
 
182
  /* Add lasso version in the xml node */
 
183
  session_class->set_prop(LASSO_NODE(session_node), "version", PACKAGE_VERSION);
 
184
 
 
185
  dump = lasso_node_export(session_node);
 
186
 
 
187
  lasso_node_destroy(session_node);
 
188
 
 
189
  return dump;
 
190
}
 
191
 
 
192
LassoNode*
 
193
lasso_session_get_assertion(LassoSession *session,
 
194
                            gchar        *providerID)
 
195
{
 
196
  LassoNode *assertion;
 
197
 
 
198
  g_return_val_if_fail(session != NULL, NULL);
 
199
  g_return_val_if_fail(providerID != NULL, NULL);
 
200
 
 
201
  assertion = (LassoNode *)g_hash_table_lookup(session->assertions,
 
202
                                               providerID);
 
203
  if (assertion == NULL) {
 
204
    return NULL;
 
205
  }
 
206
 
 
207
  return lasso_node_copy(assertion);
 
208
}
 
209
 
 
210
gchar*
 
211
lasso_session_get_authentication_method(LassoSession *session,
 
212
                                        gchar        *remote_providerID)
 
213
{
 
214
  LassoNode *assertion, *as;
 
215
  gchar *providerID = remote_providerID;
 
216
  gchar *authentication_method;
 
217
  GError *err = NULL;
 
218
 
 
219
  if (providerID == NULL) {
 
220
    providerID = lasso_session_get_first_providerID(session);
 
221
  }
 
222
  assertion = lasso_session_get_assertion(session, providerID);
 
223
  if (providerID == NULL) {
 
224
    g_free(providerID);
 
225
  }
 
226
  as = lasso_node_get_child(assertion, "AuthenticationStatement", NULL, NULL);
 
227
  authentication_method = lasso_node_get_attr_value(as, "AuthenticationMethod", &err);
 
228
  if (authentication_method == NULL) {
 
229
    message(G_LOG_LEVEL_CRITICAL, err->message);
 
230
    g_error_free(err);
 
231
    goto done;
 
232
  }
 
233
 
 
234
 done:
 
235
  lasso_node_destroy(as);
 
236
  lasso_node_destroy(assertion);
 
237
  return authentication_method;
 
238
}
 
239
 
 
240
gchar*
 
241
lasso_session_get_first_providerID(LassoSession *session)
 
242
{
 
243
  gchar *providerID;
 
244
 
 
245
  g_return_val_if_fail(session != NULL, NULL);
 
246
 
 
247
  if(session->providerIDs->len == 0) {
 
248
    return NULL;
 
249
  }
 
250
 
 
251
  providerID = g_ptr_array_index(session->providerIDs, 0);
 
252
  if (providerID == NULL) {
 
253
    return NULL;
 
254
  }
 
255
 
 
256
  return g_strdup(providerID);
 
257
}
 
258
 
 
259
gchar*
 
260
lasso_session_get_provider_index(LassoSession *session,
 
261
                                  gint          index)
 
262
{
 
263
  gchar *providerID;
 
264
 
 
265
  g_return_val_if_fail(session != NULL, NULL);
 
266
 
 
267
  /* verify index is valid */
 
268
  if ( (session->providerIDs == NULL) && (session->providerIDs->len < 0) ) {
 
269
    return NULL;
 
270
  }
 
271
  if ( (index < 0) || (index >= session->providerIDs->len) ) {
 
272
    return NULL;
 
273
  }
 
274
 
 
275
  /* get the provider id */
 
276
  providerID = g_ptr_array_index(session->providerIDs, index);
 
277
  if (providerID == NULL) {
 
278
    return NULL;
 
279
  }
 
280
 
 
281
  return g_strdup(providerID);
 
282
}
 
283
 
 
284
gint
 
285
lasso_session_remove_assertion(LassoSession *session,
 
286
                               gchar        *providerID)
 
287
{
 
288
  LassoNode *assertion;
 
289
  int i;
 
290
 
 
291
  g_return_val_if_fail(session != NULL, -1);
 
292
  g_return_val_if_fail(providerID != NULL, -1);
 
293
 
 
294
  /* remove the assertion */
 
295
  assertion = lasso_session_get_assertion(session, providerID);
 
296
  if (assertion != NULL) {
 
297
    g_hash_table_remove(session->assertions, providerID);
 
298
    lasso_node_destroy(assertion);
 
299
  }
 
300
 
 
301
  /* remove the remote provider id */
 
302
  for(i = 0; i<session->providerIDs->len; i++) {
 
303
    if(xmlStrEqual(providerID, g_ptr_array_index(session->providerIDs, i))) {
 
304
      g_ptr_array_remove_index(session->providerIDs, i);
 
305
      break;
 
306
    }
 
307
  }
 
308
 
 
309
  session->is_dirty = TRUE;
 
310
 
 
311
  return 0;
 
312
}
 
313
 
 
314
/*****************************************************************************/
 
315
/* overrided parent class methods                                            */
 
316
/*****************************************************************************/
 
317
 
 
318
static void
 
319
lasso_session_dispose(LassoSession *session)
 
320
{
 
321
  if (session->private->dispose_has_run == TRUE) {
 
322
    return;
 
323
  }
 
324
  session->private->dispose_has_run = TRUE;
 
325
 
 
326
  debug("Session object 0x%x disposed ...\n", session);
 
327
 
 
328
  g_hash_table_destroy(session->assertions);
 
329
  session->assertions = NULL;
 
330
 
 
331
  parent_class->dispose(G_OBJECT(session));
 
332
}
 
333
 
 
334
static void
 
335
lasso_session_finalize(LassoSession *session)
 
336
{
 
337
  gint i;
 
338
 
 
339
  debug("Session object 0x%x finalized ...\n", session);
 
340
 
 
341
  /* free allocated memory for providerIDs array */
 
342
  for (i=0; i<session->providerIDs->len; i++) {
 
343
    g_free(session->providerIDs->pdata[i]);
 
344
    session->providerIDs->pdata[i] = NULL;
 
345
  }
 
346
  g_ptr_array_free(session->providerIDs, TRUE);
 
347
  session->providerIDs = NULL;
 
348
 
 
349
  g_free(session->private);
 
350
  session->private = NULL;
 
351
 
 
352
  parent_class->finalize(G_OBJECT(session));
 
353
}
 
354
 
 
355
/*****************************************************************************/
 
356
/* instance and class init functions                                         */
 
357
/*****************************************************************************/
 
358
 
 
359
static void
 
360
lasso_session_instance_init(LassoSession *session)
 
361
{
 
362
  session->private = g_new (LassoSessionPrivate, 1);
 
363
  session->private->dispose_has_run = FALSE;
 
364
 
 
365
  session->providerIDs = g_ptr_array_new();
 
366
  session->assertions = g_hash_table_new_full(g_str_hash, g_str_equal,
 
367
                                              (GDestroyNotify)g_free,
 
368
                                              (GDestroyNotify)lasso_node_destroy);
 
369
  session->is_dirty = TRUE;
 
370
}
 
371
 
 
372
static void
 
373
lasso_session_class_init(LassoSessionClass *class)
 
374
{
 
375
  GObjectClass *gobject_class = G_OBJECT_CLASS(class);
 
376
  
 
377
  parent_class = g_type_class_peek_parent(class);
 
378
  /* override parent class methods */
 
379
  gobject_class->dispose  = (void *)lasso_session_dispose;
 
380
  gobject_class->finalize = (void *)lasso_session_finalize;
 
381
}
 
382
 
 
383
GType lasso_session_get_type() {
 
384
  static GType this_type = 0;
 
385
 
 
386
  if (!this_type) {
 
387
    static const GTypeInfo this_info = {
 
388
      sizeof (LassoSessionClass),
 
389
      NULL,
 
390
      NULL,
 
391
      (GClassInitFunc) lasso_session_class_init,
 
392
      NULL,
 
393
      NULL,
 
394
      sizeof(LassoSession),
 
395
      0,
 
396
      (GInstanceInitFunc) lasso_session_instance_init,
 
397
    };
 
398
    
 
399
    this_type = g_type_register_static(G_TYPE_OBJECT,
 
400
                                       "LassoSession",
 
401
                                       &this_info, 0);
 
402
  }
 
403
  return this_type;
 
404
}
 
405
 
 
406
LassoSession*
 
407
lasso_session_new()
 
408
{
 
409
  LassoSession *session;
 
410
 
 
411
  session = LASSO_SESSION(g_object_new(LASSO_TYPE_SESSION, NULL));
 
412
 
 
413
  return session;
 
414
}
 
415
 
 
416
LassoSession*
 
417
lasso_session_new_from_dump(gchar *dump)
 
418
{
 
419
  LassoSession   *session;
 
420
  LassoNode      *session_node;
 
421
  LassoNode      *assertions_node, *assertion_node, *assertion;
 
422
  xmlNodePtr      assertions_xmlNode, assertion_xmlNode;
 
423
  xmlChar        *providerID;
 
424
  GError         *err = NULL;
 
425
 
 
426
  g_return_val_if_fail(dump != NULL, NULL);
 
427
 
 
428
  session = LASSO_SESSION(g_object_new(LASSO_TYPE_SESSION, NULL));
 
429
 
 
430
  /* get session */
 
431
  session_node = lasso_node_new_from_dump(dump);
 
432
  if (session_node == NULL) {
 
433
    message(G_LOG_LEVEL_WARNING, "Can't create a session from dump\n");
 
434
    return NULL;
 
435
  }
 
436
 
 
437
  /* get assertions */
 
438
  assertions_node = lasso_node_get_child(session_node,
 
439
                                         LASSO_SESSION_ASSERTIONS_NODE,
 
440
                                         lassoLassoHRef, NULL);
 
441
  if (assertions_node != NULL) {
 
442
    assertions_xmlNode = LASSO_NODE_GET_CLASS(assertions_node)->get_xmlNode(assertions_node);
 
443
    assertion_xmlNode = assertions_xmlNode->children;
 
444
 
 
445
    while (assertion_xmlNode != NULL) {
 
446
      /* assertion xmlNode  */
 
447
      if (assertion_xmlNode->type == XML_ELEMENT_NODE && \
 
448
          xmlStrEqual(assertion_xmlNode->name, LASSO_SESSION_ASSERTION_NODE)) {
 
449
        /* assertion node */
 
450
        assertion_node = lasso_node_new_from_xmlNode(assertion_xmlNode);
 
451
        providerID = lasso_node_get_attr_value(assertion_node,
 
452
                                                      LASSO_SESSION_REMOTE_PROVIDERID_ATTR,
 
453
                                                      &err);
 
454
        if (providerID != NULL) {
 
455
          assertion = lasso_node_get_child(assertion_node,
 
456
                                           "Assertion",
 
457
                                           NULL, /* lassoLibHRef, FIXME changed for SourceID */
 
458
                                           &err);
 
459
          if (assertion != NULL) {
 
460
            lasso_session_add_assertion(session, providerID, assertion);
 
461
            lasso_node_destroy(assertion);
 
462
          }
 
463
          else {
 
464
            message(G_LOG_LEVEL_CRITICAL, err->message);
 
465
            g_clear_error(&err);
 
466
          }
 
467
        }
 
468
        else {
 
469
          message(G_LOG_LEVEL_CRITICAL, err->message);
 
470
          g_clear_error(&err);
 
471
        }
 
472
        g_free(providerID);
 
473
        lasso_node_destroy(assertion_node);
 
474
      }
 
475
      assertion_xmlNode = assertion_xmlNode->next;
 
476
    }
 
477
  }
 
478
  lasso_node_destroy(assertions_node);
 
479
  lasso_node_destroy(session_node);
 
480
 
 
481
  return session;
 
482
}