summaryrefslogtreecommitdiff
path: root/src/common/gsignond-identity-info.c
blob: 9e41a41829747d108aeb12b8aff31cacc82cd97a (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of gsignond
 *
 * Copyright (C) 2012-2013 Intel Corporation.
 *
 * Contact: Imran Zaman <imran.zaman@linux.intel.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <gsignond/gsignond-identity-info.h>
#include "gsignond-identity-info-internal.h"


static gboolean
_gsignond_identity_info_seq_cmp (
        GSequence *one,
        GSequence *two)
{
    GSequenceIter *iter1 = NULL, *iter2 = NULL;
    gboolean equal = TRUE;

    if (one == NULL && two == NULL)
        return TRUE;

    if ((one != NULL && two == NULL) ||
        (one == NULL && two != NULL) ||
        (g_sequence_get_length (one) != g_sequence_get_length (two)))
        return FALSE;

    if (one == two)
        return TRUE;

    iter1 = g_sequence_get_begin_iter (one);
    while (!g_sequence_iter_is_end (iter1)) {
        iter2 = g_sequence_get_iter_at_pos (two,
                    g_sequence_iter_get_position (iter1));
        if (g_strcmp0 (g_sequence_get (iter1), g_sequence_get (iter2)) != 0) {
            equal = FALSE;
            break;
        }
        iter1 = g_sequence_iter_next (iter1);
    }

    return equal;
}

static gint
_compare_strings (
		const gchar* a,
		const gchar* b,
		gpointer data)
{
	(void)data;
	return g_strcmp0 (a,b);
}

static GVariant *
_gsignond_identity_info_sequence_to_variant (GSequence *seq)

{
    GSequenceIter * iter = NULL;
    GVariant *var = NULL;
    GVariantBuilder builder;

    if (!seq) return NULL;

    g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
    iter = g_sequence_get_begin_iter (seq);
    while (!g_sequence_iter_is_end (iter)) {
        const gchar * d = g_sequence_get (iter);
        g_variant_builder_add (&builder, "s", d);
        iter = g_sequence_iter_next (iter);
    }
    var = g_variant_builder_end (&builder);
    return var;
}

static GSequence *
_gsignond_identity_info_variant_to_sequence (GVariant *var)

{
    GVariantIter iter;
    GSequence *seq = NULL;
    gchar *item = NULL;

    if (!var) return NULL;

    seq = g_sequence_new ((GDestroyNotify)g_free);
    g_variant_iter_init (&iter, var);
    while (g_variant_iter_next (&iter, "s", &item)) {
        g_sequence_insert_sorted (seq,
                                  item,
                                  (GCompareDataFunc) _compare_strings,
                                  NULL);
    }
    return seq;
}

static gchar **
_gsignond_identity_info_sequence_to_array (GSequence *seq)
{
    gchar **items, **temp;
    GSequenceIter *iter;

    if (!seq) return NULL;

    items = g_malloc0 ((g_sequence_get_length (seq) + 1) * sizeof (gchar *));
    temp = items;
    for (iter = g_sequence_get_begin_iter (seq);
         iter != g_sequence_get_end_iter (seq);
         iter = g_sequence_iter_next (iter)) {
        *temp = g_sequence_get (iter);
        temp++;
    }
    return items;
}

static GSequence *
_gsignond_identity_info_array_to_sequence (gchar **items)

{
    GSequence *seq = NULL;

    if (!items) return NULL;

    seq = g_sequence_new ((GDestroyNotify) g_free);
    while (*items) {
        g_sequence_insert_sorted (seq,
                                  *items,
                                  (GCompareDataFunc) _compare_strings,
                                  NULL);
        items++;
    }
    return seq;
}

static gboolean
_gsignond_identity_info_sec_context_list_cmp (
        GSignondSecurityContextList *one,
        GSignondSecurityContextList *two)
{
    GSignondSecurityContextList *list_elem1 = NULL, *list_elem2 = NULL;
    gboolean equal = TRUE;

    if (one == NULL && two == NULL)
        return TRUE;

    if ((one != NULL && two == NULL) ||
        (one == NULL && two != NULL) ||
        (g_list_length (one) != g_list_length (two)))
        return FALSE;

    if (one == two)
        return TRUE;

    list_elem1 = one;
    for ( ; list_elem1 != NULL; list_elem1 = g_list_next (list_elem1)) {
        list_elem2 = g_list_nth (two, g_list_position (one, list_elem1));
        if (!gsignond_security_context_match (
                (GSignondSecurityContext *)list_elem1->data,
                (GSignondSecurityContext *)list_elem2->data)) {
            equal = FALSE;
            break;
        }
    }

    return equal;
}

static gboolean
_gsignond_identity_info_methods_cmp (
        GHashTable *one,
        GHashTable *two)
{
    GHashTableIter iter1;
    GSequence *mechs1 = NULL, *mechs2 = NULL;
    gchar *key = NULL;
    gboolean equal = TRUE;

    if (one == NULL && two == NULL)
        return TRUE;

    if ((one != NULL && two == NULL) ||
        (one == NULL && two != NULL) ||
        (g_hash_table_size (one) != g_hash_table_size (two)))
        return FALSE;

    if (one == two)
        return TRUE;

    g_hash_table_iter_init(&iter1, one);
    while (g_hash_table_iter_next (&iter1, (gpointer *)&key,
            (gpointer *)&mechs1)) {
        mechs2 = (GSequence *)g_hash_table_lookup (two, key);
        equal = _gsignond_identity_info_seq_cmp (mechs1, mechs2);
        if (!equal) {
            break;
        }
    }

    return equal;
}

/**
 * gsignond_identity_info_new:
 *
 * Creates new instance of GSignondIdentityInfo.
 *
 * Returns: (transfer full) #GSignondIdentityInfo object if successful,
 * NULL otherwise.
 */
GSignondIdentityInfo *
gsignond_identity_info_new (void)
{
    GSignondIdentityInfo *info;

    info = gsignond_dictionary_new ();
    gsignond_identity_info_set_id (info, GSIGNOND_IDENTITY_INFO_NEW_IDENTITY);

    return info;
}

/**
 * gsignond_identity_info_copy:
 * @info: instance of #GSignondIdentityInfo
 *
 * Creates a copy of info structure.
 *
 * Returns: copy of the info.
 */
GSignondIdentityInfo *
gsignond_identity_info_copy (GSignondIdentityInfo *info)
{
    if (!info)
        return NULL;

    return gsignond_dictionary_copy (info);
}

/**
 * gsignond_identity_info_ref:
 * @info: instance of #GSignondIdentityInfo
 *
 * Increment reference count of the info structure.
 */
void
gsignond_identity_info_ref (GSignondIdentityInfo *info)
{
    g_return_if_fail (info != NULL);

    gsignond_dictionary_ref (info);
}

/**
 * gsignond_identity_info_unref:
 * @info: instance of #GSignondIdentityInfo
 *
 * Decrement reference count of the info structure.
 */
void
gsignond_identity_info_unref (GSignondIdentityInfo *info)
{
    if (!info)
        return;

    gsignond_dictionary_unref (info);
}

/**
 * gsignond_identity_info_get_id:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the id from the info.
 *
 * Returns: the id; negative id is returned in case of failure.
 */
guint32
gsignond_identity_info_get_id (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_ID);

    g_return_val_if_fail (var != NULL, -1);

    return g_variant_get_uint32 (var);
}

/**
 * gsignond_identity_info_set_id:
 * @info: instance of #GSignondIdentityInfo
 *
 * @id: id to be set
 *
 * Sets the id of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_id (
        GSignondIdentityInfo *info,
        guint32 id)
{
    g_assert (info != NULL);

    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_ID,
            g_variant_new_uint32 (id));
}

/**
 * gsignond_identity_info_get_is_identity_new:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the info whether the identity is new or not.
 *
 * Returns: TRUE if new, FALSE otherwise.
 */
gboolean
gsignond_identity_info_get_is_identity_new (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    return GSIGNOND_IDENTITY_INFO_NEW_IDENTITY ==
            gsignond_identity_info_get_id (info);
}

/**
 * gsignond_identity_info_set_identity_new:
 * @info: instance of #GSignondIdentityInfo
 *
 * Sets the id of the identity info to be new.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_identity_new (
        GSignondIdentityInfo *info)
{
    g_assert (info != NULL);
    
    return gsignond_identity_info_set_id (
            info,
            GSIGNOND_IDENTITY_INFO_NEW_IDENTITY);
}

/**
 * gsignond_identity_info_get_username:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the username from the info.
 *
 * Returns: the username if successful, NULL otherwise.
 */
const gchar *
gsignond_identity_info_get_username (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);
    
    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_USERNAME);
    if (var != NULL) {
        return g_variant_get_string (var, NULL);
    }
    return NULL;
}

/**
 * gsignond_identity_info_set_username:
 * @info: instance of #GSignondIdentityInfo
 *
 * @username: username to be set
 *
 * Sets the username of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_username (
        GSignondIdentityInfo *info,
        const gchar *username)
{
    g_assert (info != NULL);

    if (!username) {
        return gsignond_dictionary_remove (info,
                            GSIGNOND_IDENTITY_INFO_USERNAME);
    }
    return gsignond_dictionary_set (
                    info,
                    GSIGNOND_IDENTITY_INFO_USERNAME,
                    g_variant_new_string (username));
}

/**
 * gsignond_identity_info_remove_username:
 * @info: instance of #GSignondIdentityInfo
 *
 * Removes username from the info.
 */
void
gsignond_identity_info_remove_username (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);
    
    gsignond_dictionary_remove (info, GSIGNOND_IDENTITY_INFO_USERNAME);
}

/**
 * gsignond_identity_info_get_is_username_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the is_username_secret flag from the info.
 *
 * Returns: the is_username_secret flag.
 */
gboolean
gsignond_identity_info_get_is_username_secret (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info,
            GSIGNOND_IDENTITY_INFO_USERNAME_IS_SECRET);
    if (var != NULL) {
        return g_variant_get_boolean (var);
    }
    return FALSE;
}

/**
 * gsignond_identity_info_set_username_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * @store_secret: store_secret to be set
 *
 * Sets the store_secret of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_username_secret (
        GSignondIdentityInfo *info,
        gboolean username_secret)
{
    g_assert (info != NULL);

    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_USERNAME_IS_SECRET,
            g_variant_new_boolean(username_secret));
}

/**
 * gsignond_identity_info_get_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the secret from the info.
 *
 * Returns: the secret if successful, NULL otherwise.
 */
const gchar *
gsignond_identity_info_get_secret (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_SECRET);
    if (var != NULL) {
        return g_variant_get_string (var, NULL);
    }
    return NULL;
}

/**
 * gsignond_identity_info_set_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * @secret: secret to be set
 *
 * Sets the secret of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_secret (
        GSignondIdentityInfo *info,
        const gchar *secret)
{
    g_assert (info != NULL);

    if (!secret) {
        return gsignond_dictionary_remove (info,
                GSIGNOND_IDENTITY_INFO_SECRET);
    }
    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_SECRET,
            g_variant_new_string (secret));
}

/**
 * gsignond_identity_info_remove_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * Removes secret from the info.
 */
void
gsignond_identity_info_remove_secret (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    gsignond_dictionary_remove (info, GSIGNOND_IDENTITY_INFO_SECRET);
}

/**
 * gsignond_identity_info_get_store_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the store_secret flag from the info.
 *
 * Returns: the store_secret flag.
 */
gboolean
gsignond_identity_info_get_store_secret (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_STORESECRET);
    if (var != NULL) {
        return g_variant_get_boolean (var);
    }
    return FALSE;
}

/**
 * gsignond_identity_info_set_store_secret:
 * @info: instance of #GSignondIdentityInfo
 *
 * @store_secret: store_secret to be set
 *
 * Sets the store_secret of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_store_secret (
        GSignondIdentityInfo *info,
        gboolean store_secret)
{
    g_assert (info != NULL);

    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_STORESECRET,
            g_variant_new_boolean(store_secret));
}

/**
 * gsignond_identity_info_get_caption:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the caption from the info.
 *
 * Returns: the caption if successful, NULL otherwise.
 */
const gchar *
gsignond_identity_info_get_caption (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_CAPTION);
    if (var != NULL) {
        return g_variant_get_string (var, NULL);
    }
    return NULL;
}

/**
 * gsignond_identity_info_set_caption:
 * @info: instance of #GSignondIdentityInfo
 *
 * @caption: caption to be set
 *
 * Sets the caption of the info.
 *
 * Returns: TRUE in case of success, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_caption (
        GSignondIdentityInfo *info,
        const gchar *caption)
{
    g_assert (info != NULL);

    if (!caption) {
        return gsignond_dictionary_remove (info,
                GSIGNOND_IDENTITY_INFO_CAPTION);
    }
    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_CAPTION,
            g_variant_new_string (caption));
}

/**
 * gsignond_identity_info_get_realms:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the realms from the info.
 *
 * Returns: (transfer full) the realms if successful, NULL Otherwise.
 * when done realms should be freed using g_sequence_free.
 */
GSequence *
gsignond_identity_info_get_realms (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_REALMS);
    if (var != NULL) {
        return _gsignond_identity_info_variant_to_sequence (var);
    }
    return NULL;
}

/**
 * gsignond_identity_info_set_realms:
 * @info: instance of #GSignondIdentityInfo
 *
 * @realms: (transfer none) realms to be set
 *
 * Sets the realms of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_realms (
        GSignondIdentityInfo *info,
        GSequence *realms)
{
    g_assert (info != NULL);

    g_return_val_if_fail (realms != NULL, FALSE);
    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_REALMS,
            _gsignond_identity_info_sequence_to_variant (realms));
}

/**
 * gsignond_identity_info_get_methods:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the methods from the info whereas #GHashTable consists of
 * <gchar*,GSequence*> and #GSequence is a sequence of gchar *.
 *
 * Returns: (transfer full) the methods if successful, NULL otherwise.
 * when done, methods should be freed using g_hash_table_unref.
 */
GHashTable *
gsignond_identity_info_get_methods (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    GHashTable *methods = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_AUTHMETHODS);
    if (var != NULL) {
        GVariantIter iter;
        gchar *vmethod;
        gchar **vmechanisms = NULL;
        GSequence *seq = NULL;

        methods = g_hash_table_new_full ((GHashFunc) g_str_hash,
                                         (GEqualFunc) g_str_equal,
                                         (GDestroyNotify) g_free,
                                         (GDestroyNotify) g_sequence_free);

        g_variant_iter_init (&iter, var);
        while (g_variant_iter_next (&iter, "{s^as}", &vmethod, &vmechanisms))
        {
            /* ownership of all content is transferred */
            seq = _gsignond_identity_info_array_to_sequence (vmechanisms);
            g_hash_table_insert (methods, vmethod, seq);
            g_free (vmechanisms);
        }
    }
    return methods;
}

/**
 * gsignond_identity_info_set_methods:
 * @info: instance of #GSignondIdentityInfo
 *
 * @methods: (transfer none) methods to be set whereas #GHashTable consists of
 * <gchar*,#GSequence*> and #GSequence is a sequence of gchar *.
 *
 * Sets the methods of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_methods (
        GSignondIdentityInfo *info,
        GHashTable *methods)
{
    g_assert (info != NULL);

    gchar **items = NULL;
    GVariantBuilder builder;

    GHashTableIter iter;
    const gchar *method;
    GSequence *mechanisms = NULL;

    g_return_val_if_fail (info != NULL, FALSE);
    g_return_val_if_fail (methods != NULL, FALSE);

    g_variant_builder_init (&builder, (const GVariantType *)"a{sas}");
    g_hash_table_iter_init (&iter, methods);
    while (g_hash_table_iter_next (&iter,
                                   (gpointer)&method,
                                   (gpointer)&mechanisms))
    {
        items = _gsignond_identity_info_sequence_to_array (mechanisms);
        g_variant_builder_add (&builder, "{s^as}", method, items);
        g_free (items);
    }
    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_AUTHMETHODS,
            g_variant_builder_end (&builder));
}

/**
 * gsignond_identity_info_get_mechanisms:
 * @info: instance of #GSignondIdentityInfo
 *
 * @method: the method for which mechanisms are sought
 *
 * Retrieves the mechanisms from the info.
 *
 * Returns: (transfer full) the mechanisms if successful, NULL otherwise.
 * when done, mechanisms should be freed using g_sequence_free; #GSequence is a
 * sequence of gchar *.
 */
GSequence *
gsignond_identity_info_get_mechanisms (
        GSignondIdentityInfo *info,
        const gchar *method)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    GSequence *mechanisms = NULL;

    g_return_val_if_fail (method != NULL, NULL);

    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_AUTHMETHODS);
    if (var != NULL) {
        GVariantIter iter;
        gchar *vmethod;
        gchar **vmechanisms;

        g_variant_iter_init (&iter, var);
        while (g_variant_iter_next (&iter, "{s^as}", &vmethod, &vmechanisms))
        {
            /* ownership of content is transferred */
            if (vmethod != NULL && g_strcmp0 (vmethod, method) == 0) {
                mechanisms = _gsignond_identity_info_array_to_sequence (
                                 vmechanisms);
                g_free (vmethod);
                g_free (vmechanisms);
                break;
            }
            g_free (vmethod); vmethod = NULL;
            g_strfreev (vmechanisms); vmechanisms = NULL;
        }
    }
    return mechanisms;
}

/**
 * gsignond_identity_info_remove_method:
 * @info: instance of #GSignondIdentityInfo
 *
 * Removes the method from the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_remove_method (
        GSignondIdentityInfo *info,
        const gchar *method)
{
    g_assert (info != NULL);

    GHashTable *methods = NULL;
    gboolean ret = FALSE;

    g_return_val_if_fail (method != NULL, FALSE);

    methods = gsignond_identity_info_get_methods (info);
    if (methods && g_hash_table_remove (methods, method)) {
        ret = gsignond_identity_info_set_methods (info, methods);
    }
    if (methods)
        g_hash_table_unref (methods);
    return ret;
}

/**
 * gsignond_identity_info_get_access_control_list:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the access control list from the info.
 *
 * Returns: (transfer full) the list if successful, NULL otherwise.
 * when done, list should be freed using gsignond_security_context_list_free.
 */
GSignondSecurityContextList *
gsignond_identity_info_get_access_control_list (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_ACL);
    if (var != NULL) {
        return gsignond_security_context_list_from_variant (var);
    }
    return NULL;
}

/**
 * gsignond_identity_info_set_access_control_list:
 * @info: instance of #GSignondIdentityInfo
 *
 * @acl: (transfer none) access control list to be set
 *
 * Sets the access control list of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_access_control_list (
        GSignondIdentityInfo *info,
        const GSignondSecurityContextList *acl)
{
    g_assert (info != NULL);

    g_return_val_if_fail (acl != NULL, FALSE);
    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_ACL,
            gsignond_security_context_list_to_variant (acl));
}

/**
 * gsignond_identity_info_get_owner:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the id from the info.
 *
 * Returns: (transfer full) the owner if successful, NULL otherwise.
 * when done, owner list should be freed using
 * gsignond_security_context_free.
 */
GSignondSecurityContext *
gsignond_identity_info_get_owner (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_OWNER);
    if (var != NULL) {
        return gsignond_security_context_from_variant (var);
    }
    return NULL;
}

/**
 * gsignond_identity_info_set_owner:
 * @info: instance of #GSignondIdentityInfo
 *
 * @owners: (transfer none) owner to be set
 *
 * Sets the owner of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_owner (
        GSignondIdentityInfo *info,
        const GSignondSecurityContext *owners)
{
    g_assert (info != NULL);

    g_return_val_if_fail (owners != NULL, FALSE);
    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_OWNER,
            gsignond_security_context_to_variant (owners));
}

/**
 * gsignond_identity_info_get_validated:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the validated flag from the info.
 *
 * Returns: the validated flag.
 */
gboolean
gsignond_identity_info_get_validated (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_VALIDATED);
    if (var != NULL) {
        return g_variant_get_boolean (var);
    }
    return FALSE;
}

/**
 * gsignond_identity_info_set_validated:
 * @info: instance of #GSignondIdentityInfo
 *
 * @validated: validated flag to be set
 *
 * Sets the validated flag of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_validated (
        GSignondIdentityInfo *info,
        gboolean validated)
{
    g_assert (info != NULL);

    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_VALIDATED,
            g_variant_new_boolean (validated));
}

/**
 * gsignond_identity_info_get_identity_type:
 * @info: instance of #GSignondIdentityInfo
 *
 * Retrieves the type from the info.
 *
 * Returns: the type; negative type is returned in case of failure.
 */
guint32
gsignond_identity_info_get_identity_type (GSignondIdentityInfo *info)
{
    g_assert (info != NULL);

    GVariant *var = NULL;
    var = gsignond_dictionary_get (info, GSIGNOND_IDENTITY_INFO_TYPE);
    if (var != NULL) {
        return g_variant_get_int32 (var);
    }
    return -1;
}

/**
 * gsignond_identity_info_set_identity_type:
 * @info: instance of #GSignondIdentityInfo
 *
 * @type: type to be set
 *
 * Sets the type of the info.
 *
 * Returns: TRUE if successful, FALSE otherwise.
 */
gboolean
gsignond_identity_info_set_identity_type (
        GSignondIdentityInfo *info,
        guint32 type)
{
    g_assert (info != NULL);

    return gsignond_dictionary_set (
            info,
            GSIGNOND_IDENTITY_INFO_TYPE,
            g_variant_new_int32 (type));
}

/**
 * gsignond_identity_info_compare:
 * @info: instance1 of #GSignondIdentityInfo
 *
 * @other: instance2 of #GSignondIdentityInfo
 *
 * Compares two instances of #GSignondIdentityInfo for equality.
 *
 * Returns: TRUE if the two instances are equal, FALSE otherwise.
 */
gboolean
gsignond_identity_info_compare (
        GSignondIdentityInfo *info,
        GSignondIdentityInfo *other)
{
    g_assert (info != NULL && other != NULL);

    GSequence *info_realms = NULL, *other_realms = NULL;
    GHashTable *info_methods = NULL, *other_methods = NULL;
    GSignondSecurityContextList *info_acl = NULL, *other_acl = NULL;
    GSignondSecurityContext *info_owner = NULL, *other_owner = NULL;
    gboolean equal = FALSE;

    if (info == other)
        return TRUE;

    if (info == NULL || other == NULL)
        return FALSE;

    if (gsignond_identity_info_get_id (info) !=
        gsignond_identity_info_get_id (other)) {
        return FALSE;
    }

    if (g_strcmp0 (gsignond_identity_info_get_username (info),
        gsignond_identity_info_get_username (other)) != 0) {
        return FALSE;
    }

    if (g_strcmp0 (gsignond_identity_info_get_secret (info),
        gsignond_identity_info_get_secret (other)) != 0) {
        return FALSE;
    }

    if (gsignond_identity_info_get_store_secret (info) !=
        gsignond_identity_info_get_store_secret (other)) {
        return FALSE;
    }

    if (g_strcmp0 (gsignond_identity_info_get_caption (info),
        gsignond_identity_info_get_caption (other)) != 0) {
        return FALSE;
    }

    info_realms = gsignond_identity_info_get_realms (info);
    other_realms = gsignond_identity_info_get_realms (other);
    equal = _gsignond_identity_info_seq_cmp (info_realms, other_realms);
    if (info_realms) g_sequence_free (info_realms);
    if (other_realms) g_sequence_free (other_realms);
    if (!equal) {
        return FALSE;
    }

    info_methods = gsignond_identity_info_get_methods (info);
    other_methods = gsignond_identity_info_get_methods (other);
    equal = _gsignond_identity_info_methods_cmp (info_methods, other_methods);
    if (info_methods) g_hash_table_unref (info_methods);
    if (other_methods) g_hash_table_unref (other_methods);
    if (!equal) {
        return FALSE;
    }

    info_acl = gsignond_identity_info_get_access_control_list (info);
    if (info_acl)
        info_acl = g_list_sort (
                        info_acl,
                        (GCompareFunc)gsignond_security_context_compare);
    other_acl = gsignond_identity_info_get_access_control_list (other);
    if (other_acl)
        other_acl = g_list_sort (
                        other_acl,
                        (GCompareFunc)gsignond_security_context_compare);
    equal = _gsignond_identity_info_sec_context_list_cmp (info_acl, other_acl);
    if (info_acl) gsignond_security_context_list_free (info_acl);
    if (other_acl) gsignond_security_context_list_free (other_acl);
    if (!equal) {
        return FALSE;
    }

    info_owner = gsignond_identity_info_get_owner (info);
    other_owner = gsignond_identity_info_get_owner (other);
    equal = gsignond_security_context_match (info_owner, other_owner);
    if (info_owner) gsignond_security_context_free (info_owner);
    if (other_owner) gsignond_security_context_free (other_owner);
    if (!equal) {
        return FALSE;
    }

    if (gsignond_identity_info_get_validated (info) !=
        gsignond_identity_info_get_validated (other)) {
        return FALSE;
    }

    if (gsignond_identity_info_get_identity_type (info) !=
        gsignond_identity_info_get_identity_type (other)) {
        return FALSE;
    }

    return TRUE;
}

void
gsignond_identity_info_list_free (GSignondIdentityInfoList *list)
{
    g_return_if_fail (list != NULL);
    g_list_free_full (list, (GDestroyNotify)gsignond_identity_info_unref);
}