Don't create duplicate identities. LP 1180914.
[moonshot-ui.git] / src / moonshot-id.vala
1 public class TrustAnchor : Object
2 {
3   public string ca_cert {get; set; default = "";}
4   public string subject {get; set; default = "";}
5   public string subject_alt  {get; set; default = "";}
6   public string server_cert  {get; set; default = "";}
7   public int Compare(TrustAnchor other)
8   {
9     if (this.ca_cert != other.ca_cert)
10       return 1;
11     if (this.subject != other.subject)
12       return 1;
13     if (this.subject_alt != other.subject_alt)
14       return 1;
15     if (this.server_cert != other.server_cert)
16       return 1;
17     return 0;
18   }
19 }
20
21 public struct Rule
22 {
23   public string pattern;
24   public string always_confirm;
25   public int Compare(Rule other) {
26     if (this.pattern != other.pattern)
27       return 1;
28     if (this.always_confirm != other.always_confirm)
29       return 1;
30     return 0;
31   }
32 }
33
34 public class IdCard : Object
35 {
36   public const string NO_IDENTITY = "No Identity";
37
38   private string _nai;
39   
40   public string display_name { get; set; default = ""; }
41   
42   public string username { get; set; default = ""; }
43 #if GNOME_KEYRING
44   private unowned string _password;
45   public string password {
46     get {
47       return (_password!=null) ? _password : "";
48     }
49     set {
50       if (_password != null) {
51         GnomeKeyring.memory_free((void *)_password);
52         _password = null;
53       }
54       if (value != null)
55         _password = GnomeKeyring.memory_strdup(value); 
56     }
57   }
58 #else
59   public string password { get; set; default = null; }
60 #endif
61
62   public string issuer { get; set; default = ""; }
63   
64   public Rule[] rules {get; set; default = {};}
65   public string[] services { get; set; default = {}; }
66   public bool temporary {get; set; default = false; }
67
68   public TrustAnchor trust_anchor  { get; set; default = new TrustAnchor (); }
69   
70   public unowned string nai { get {  _nai = username + "@" + issuer; return _nai;}}
71
72   public bool store_password { get; set; default = false; }
73
74   public bool IsNoIdentity() 
75   {
76     return (display_name == NO_IDENTITY);
77   }
78
79   public enum DiffFlags {
80     DISPLAY_NAME,
81     USERNAME,
82     PASSWORD,
83     ISSUER,
84     RULES,
85     SERVICES,
86     TRUST_ANCHOR;
87   }
88
89   public int Compare(IdCard other)
90   {
91     int diff = 0;
92     if (this.display_name != other.display_name)
93       diff |= 1 << DiffFlags.DISPLAY_NAME;
94     if (this.username != other.username)
95       diff |= 1 << DiffFlags.USERNAME;
96     if (this.password != other.password)
97       diff |= 1 << DiffFlags.PASSWORD;
98     if (this.issuer != other.issuer)
99       diff |= 1 << DiffFlags.ISSUER;
100     if (CompareRules(this.rules, other.rules)!=0)
101       diff |= 1 << DiffFlags.RULES;
102     if (CompareStringArray(this.services, other.services)!=0)
103       diff |= 1 << DiffFlags.SERVICES;
104     if (this.trust_anchor.Compare(other.trust_anchor)!=0)
105       diff |= 1 << DiffFlags.TRUST_ANCHOR;
106     stdout.printf("Diff Flags: %x\n", diff);
107     return diff;
108   }
109
110   public static IdCard NewNoIdentity() 
111   { 
112     IdCard card = new IdCard();
113     card.display_name = NO_IDENTITY;
114     return card;
115   }
116
117   ~IdCard() {
118     password = null;
119   }
120 }
121
122 public int CompareRules(Rule[] a, Rule[] b)
123 {
124   if (a.length != b.length)
125     return 1;
126   for (int i=0; i<a.length; i++) {
127     if (a[i].Compare(b[i]) != 0)
128       return 1;
129   }
130   return 0;
131 }
132
133 public int CompareStringArray(string[] a, string [] b)
134 {
135   if (a.length != b.length)
136     return 1;
137   for (int i=0; i<a.length; i++) {
138     if (a[i] != b[i])
139       return 1;
140   }
141   return 0;
142 }