Fix for running against RabbitMQ 2.2
[mono.git] / mcs / class / System.Web / System.Web.Security / RolePrincipal.cs
1 //
2 // System.Web.Security.RolePrincipal
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ben Maurer
9 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections.Specialized;
32 using System.Security.Permissions;
33 using System.Security.Principal;
34 using System.Web.Configuration;
35 using System.Web.Util;
36 using System.IO;
37 using System.Text;
38
39 namespace System.Web.Security {
40
41         [Serializable]
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 #if NET_4_0
44         public
45 #else
46         public sealed
47 #endif
48         class RolePrincipal : IPrincipal {
49
50                 IIdentity _identity;
51                 bool _listChanged;
52                 string[] _cachedArray;
53                 HybridDictionary _cachedRoles;
54                 readonly string _providerName;
55
56                 int _version = 1;
57                 string _cookiePath;
58                 DateTime _issueDate;
59                 DateTime _expireDate;
60
61
62                 public RolePrincipal (IIdentity identity)
63                 {
64                         if (identity == null)
65                                 throw new ArgumentNullException ("identity");
66                         
67                         this._identity = identity;
68                         this._cookiePath = RoleManagerConfig.CookiePath;
69                         this._issueDate = DateTime.Now;
70                         this._expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
71                 }
72
73                 public RolePrincipal (IIdentity identity, string encryptedTicket)
74                         : this (identity)
75                 {
76                         DecryptTicket (encryptedTicket);
77                 }
78
79                 public RolePrincipal (string providerName, IIdentity identity)
80                         : this (identity)
81                 {
82                         if (providerName == null)
83                                 throw new ArgumentNullException ("providerName");
84
85                         this._providerName = providerName;
86                 }
87
88                 public RolePrincipal (string providerName, IIdentity identity, string encryptedTicket)
89                         : this (providerName, identity)
90                 {
91                         DecryptTicket (encryptedTicket);
92                 }
93
94                 public string [] GetRoles ()
95                 {
96                         if (!_identity.IsAuthenticated)
97                                 return new string[0];
98
99                         if (!IsRoleListCached || Expired) {
100                                 _cachedArray = Provider.GetRolesForUser (_identity.Name);
101                                 _cachedRoles = new HybridDictionary (true);
102
103                                 foreach (string r in _cachedArray)
104                                         _cachedRoles.Add(r, r);
105
106                                 _listChanged = true;
107                         }
108
109                         return _cachedArray;
110                 }
111                 
112                 public bool IsInRole (string role)
113                 {
114                         if (!_identity.IsAuthenticated)
115                                 return false;
116
117                         GetRoles ();
118
119                         return _cachedRoles [role] != null;
120                 }
121                 
122                 public string ToEncryptedTicket ()
123                 {
124                         string roles = string.Join (",", GetRoles ());
125                         string cookiePath = RoleManagerConfig.CookiePath;
126                         int approxTicketLen = roles.Length + cookiePath.Length + 64;
127
128                         if (_cachedArray.Length > Roles.MaxCachedResults)
129                                return null;
130
131                         MemoryStream ticket = new MemoryStream (approxTicketLen);
132                         BinaryWriter writer = new BinaryWriter (ticket);
133
134                         // version
135                         writer.Write (Version);
136                 
137                         // issue datetime
138                         DateTime issueDate = DateTime.Now;
139                         writer.Write (issueDate.Ticks);
140
141                         // expiration datetime
142                         writer.Write (_expireDate.Ticks);
143
144                         writer.Write (cookiePath);
145                         writer.Write (roles);
146
147                         CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
148
149                         byte[] ticket_data = ticket.GetBuffer ();
150                         if (cookieProtection == CookieProtection.All) {
151                                 ticket_data = MachineKeySectionUtils.EncryptSign (MachineConfig, ticket_data);
152                         } else if (cookieProtection == CookieProtection.Encryption) {
153                                 ticket_data = MachineKeySectionUtils.Encrypt (MachineConfig, ticket_data);
154                         } else if (cookieProtection == CookieProtection.Validation) {
155                                 ticket_data = MachineKeySectionUtils.Sign (MachineConfig, ticket_data);
156                         }
157
158                         return GetBase64FromBytes (ticket_data, 0, ticket_data.Length);
159                 }
160
161                 void DecryptTicket (string encryptedTicket)
162                 {
163                         if (encryptedTicket == null || encryptedTicket == String.Empty)
164                                 throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
165
166                         byte [] ticketBytes = GetBytesFromBase64 (encryptedTicket);
167                         byte [] decryptedTicketBytes = null;
168
169                         CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
170
171                         if (cookieProtection == CookieProtection.All) {
172                                 decryptedTicketBytes = MachineKeySectionUtils.VerifyDecrypt (MachineConfig, ticketBytes);
173                         } else if (cookieProtection == CookieProtection.Encryption) {
174                                 decryptedTicketBytes = MachineKeySectionUtils.Decrypt (MachineConfig, ticketBytes);
175                         } else if (cookieProtection == CookieProtection.Validation) {
176                                 decryptedTicketBytes = MachineKeySectionUtils.Verify (MachineConfig, ticketBytes);
177                         }
178
179                         if (decryptedTicketBytes == null)
180                                 throw new HttpException ("ticket validation failed");
181
182                         MemoryStream ticket = new MemoryStream (decryptedTicketBytes);
183                         BinaryReader reader = new BinaryReader (ticket);
184
185                         // version
186                         _version = reader.ReadInt32 ();
187
188                         // issued date
189                         _issueDate = new DateTime (reader.ReadInt64 ());
190
191                         // expire date
192                         _expireDate = new DateTime (reader.ReadInt64 ());
193
194                         // cookie path
195                         _cookiePath = reader.ReadString ();
196                         
197                         // roles
198                         string roles = reader.ReadString ();
199
200                         if (!Expired) {
201                                 InitializeRoles (roles);
202                                 //update ticket if less than half of CookieTimeout remaining.
203                                 if (Roles.CookieSlidingExpiration){
204                                         if (_expireDate-DateTime.Now < TimeSpan.FromTicks (RoleManagerConfig.CookieTimeout.Ticks/2))    {
205                                                 _issueDate = DateTime.Now;
206                                                 _expireDate = DateTime.Now.Add (RoleManagerConfig.CookieTimeout);
207                                                 SetDirty ();
208                                         }
209                                 }
210                         } else {
211                                 // issue a new ticket
212                                 _issueDate = DateTime.Now;
213                                 _expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
214                         }
215                 }
216
217                 void InitializeRoles (string decryptedRoles)
218                 {
219                         _cachedArray = decryptedRoles.Split (',');
220                         _cachedRoles = new HybridDictionary (true);
221
222                         foreach (string r in _cachedArray)
223                                 _cachedRoles.Add (r, r);
224                 }
225
226                 public bool CachedListChanged {
227                         get { return _listChanged; }
228                 }
229                 
230                 public string CookiePath {
231                         get { return _cookiePath; }
232                 }
233                 
234                 public bool Expired {
235                         get { return ExpireDate < DateTime.Now; }
236                 }
237                 
238                 public DateTime ExpireDate {
239                         get { return _expireDate; }
240                 }
241                 
242                 public IIdentity Identity {
243                         get { return _identity; }
244                 }
245                 
246                 public bool IsRoleListCached {
247                         get { return (_cachedRoles != null) && RoleManagerConfig.CacheRolesInCookie; }
248                 }
249                 
250                 public DateTime IssueDate {
251                         get { return _issueDate; }
252                 }
253                 
254                 public string ProviderName {
255                         get { return String.IsNullOrEmpty(_providerName) ? Provider.Name : _providerName; }
256                 }
257                 
258                 public int Version {
259                         get { return _version; }
260                 }
261
262                 RoleProvider Provider {
263                         get {
264                                 if (String.IsNullOrEmpty (_providerName))
265                                         return Roles.Provider;
266
267                                 return Roles.Providers [_providerName];
268                         }
269                 }
270
271                 public void SetDirty ()
272                 {
273                         _listChanged = true;
274                         _cachedRoles = null;
275                         _cachedArray = null;
276                 }
277
278                 static string GetBase64FromBytes (byte [] bytes, int offset, int len)
279                 {
280                         return Convert.ToBase64String (bytes, offset, len);
281                 }
282
283                 static byte [] GetBytesFromBase64 (string base64String)
284                 {
285                         return Convert.FromBase64String (base64String);
286                 }
287
288                 RoleManagerSection RoleManagerConfig
289                 {
290                         get { return (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager"); }
291                 }
292
293                 MachineKeySection MachineConfig
294                 {
295                         get { return (MachineKeySection) WebConfigurationManager.GetSection ("system.web/machineKey"); }
296                 }
297         }
298 }
299
300