[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / corlib / System.Security.Claims / ClaimsPrincipal.cs
1 //
2 // ClaimPrincipal.cs
3 //
4 // Authors:
5 //  Miguel de Icaza (miguel@xamarin.com)
6 //
7 // Copyright 2014 Xamarin Inc
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if NET_4_5
29 using System;
30 using System.Collections.Generic;
31 using System.Security.Principal;
32 using System.Runtime.Serialization;
33 using System.Threading;
34
35 namespace System.Security.Claims {
36
37         [SerializableAttribute]
38         public class ClaimsPrincipal : IPrincipal
39         {
40                 List<ClaimsIdentity> identities;
41
42                 static ClaimsPrincipal ()
43                 {
44                         ClaimsPrincipalSelector = DefaultClaimsPrincipal;
45                 }
46
47                 static ClaimsPrincipal DefaultClaimsPrincipal ()
48                 {
49                         return Thread.CurrentPrincipal as ClaimsPrincipal;
50                 }
51                 
52                 public ClaimsPrincipal ()
53                 {
54                         identities =  new List<ClaimsIdentity>();
55                 }
56
57                 public ClaimsPrincipal (IEnumerable<ClaimsIdentity> identities)
58                 {
59                         if (identities == null)
60                                 throw new ArgumentNullException ("identities");
61                         
62                         this.identities = new List<ClaimsIdentity> (identities);
63                 }
64
65                 public ClaimsPrincipal (IIdentity identity)
66                 {
67                         if (identity == null)
68                                 throw new ArgumentNullException ("identity");
69
70                         identities = new List<ClaimsIdentity> ();
71                         identities.Add (new ClaimsIdentity (identity));
72                 }
73
74                 public ClaimsPrincipal (IPrincipal principal)
75                 {
76                         if (principal == null)
77                                 throw new ArgumentNullException ("principal");
78                         var cp = principal as ClaimsPrincipal;
79                         if (cp != null)
80                                 identities = new List<ClaimsIdentity> (cp.identities);
81                         else {
82                                 identities = new List<ClaimsIdentity> ();
83                                 identities.Add (new ClaimsIdentity (principal.Identity));
84                         }
85                 }
86
87                 [MonoTODO]
88                 protected ClaimsPrincipal (SerializationInfo info, StreamingContext context)
89                 {
90                         throw new NotImplementedException ();
91                 }
92                 
93                 public virtual IEnumerable<Claim> Claims {
94                         get {
95                                 foreach (var ci in identities)
96                                         foreach (var claim in ci.Claims)
97                                                 yield return claim;
98                         }
99                 }
100
101                 public static Func<ClaimsPrincipal> ClaimsPrincipalSelector { get; set; }
102
103                 public static ClaimsPrincipal Current {
104                         get {
105                                 return ClaimsPrincipalSelector ();
106                         }
107                 }
108
109                 public virtual IEnumerable<ClaimsIdentity> Identities {
110                         get {
111                                 return identities;
112                         }
113                 }
114
115                 public static Func<IEnumerable<ClaimsIdentity>, ClaimsIdentity> PrimaryIdentitySelector { get; set; }
116
117                 public virtual IIdentity Identity {
118                         get {
119                                 if (identities == null)
120                                         throw new ArgumentNullException ("Identities");
121
122                                 if (PrimaryIdentitySelector != null)
123                                         return PrimaryIdentitySelector (identities);
124                                                 
125                                 ClaimsIdentity firstCI = null;
126                                 foreach (var ident in identities){
127                                         if (ident is WindowsIdentity)
128                                                 return ident;
129                                         if (firstCI == null && ident is ClaimsIdentity)
130                                                 firstCI = ident as ClaimsIdentity;
131                                 }
132                                 return firstCI;
133                         }
134                 }
135
136                 public virtual void AddIdentities (IEnumerable<ClaimsIdentity> identities)
137                 {
138                         if (identities == null)
139                                 throw new ArgumentNullException ("identities");
140                         foreach (var id in identities)
141                                 this.identities.Add (id);
142                 }
143
144                 public virtual void AddIdentity (ClaimsIdentity identity)
145                 {
146                         if (identity == null)
147                                 throw new ArgumentNullException ("identity");
148                         identities.Add (identity);
149                 }
150
151                 public virtual IEnumerable<Claim> FindAll (Predicate<Claim> match)
152                 {
153                         if (match == null)
154                                 throw new ArgumentNullException ("match");
155                         foreach (var claim in Claims){
156                                 if (match (claim))
157                                         yield return claim;
158                         }
159                 }
160
161                 public virtual Claim FindFirst (Predicate<Claim> match)
162                 {
163                         if (match == null)
164                                 throw new ArgumentNullException ("match");
165                         foreach (var claim in Claims)
166                                 if (match (claim))
167                                         return claim;
168                         return null;
169                 }
170
171                 public virtual bool HasClaim (Predicate<Claim> match)
172                 {
173                         if (match == null)
174                                 throw new ArgumentNullException ("match");
175                         foreach (var claim in Claims)
176                                 if (match (claim))
177                                         return true;
178                         return false;
179                 }
180
181                 public virtual bool IsInRole (string role)
182                 {
183                         foreach (var id in identities){
184                                 if (id.HasClaim (id.RoleClaimType, role))
185                                         return true;
186                         }
187                         return false;
188                 }
189                 
190         }
191 }
192 #endif