5 // Miguel de Icaza (miguel@xamarin.com)
7 // Copyright 2014 Xamarin Inc
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
31 using System.Security.Principal;
32 using System.Runtime.Serialization;
33 namespace System.Security.Claims {
36 public class ClaimsIdentity : IIdentity {
37 [NonSerializedAttribute]
38 public const string DefaultNameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
39 [NonSerializedAttribute]
40 public const string DefaultRoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
41 [NonSerializedAttribute]
42 public const string DefaultIssuer = "LOCAL AUTHORITY";
48 public ClaimsIdentity ()
49 : this (claims: null, authenticationType: null, nameType: null, roleType: null)
52 public ClaimsIdentity (string authenticationType)
53 : this (claims: null, authenticationType: authenticationType, nameType: null, roleType: null)
56 public ClaimsIdentity (IEnumerable<Claim> claims, string authenticationType)
57 : this (claims, authenticationType, null, null)
60 public ClaimsIdentity (string authenticationType, string nameType, string roleType)
61 : this (claims: null, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
64 public ClaimsIdentity (IIdentity identity) : this (identity: identity, claims: null)
68 public ClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
69 : this (identity: null, claims: claims, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
71 claims = claims == null ? new List<Claim> (): new List<Claim> (claims);
73 // Special case: if empty, set to null.
74 if (authenticationType == "")
77 auth_type = authenticationType;
79 NameClaimType = nameType == null ? DefaultNameClaimType : nameType;
80 RoleClaimType = roleType == null ? DefaultRoleClaimType : roleType;
83 public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims)
84 : this (identity, claims, authenticationType: null, nameType: null, roleType: null)
87 public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
89 var ci = identity as ClaimsIdentity;
90 NameClaimType = nameType == null ? DefaultNameClaimType : nameType;
91 RoleClaimType = roleType == null ? DefaultRoleClaimType : roleType;
93 this.claims = new List<Claim> ();
96 BootstrapContext = ci.BootstrapContext;
97 foreach (var c in ci.Claims)
100 if (claims != null) {
101 foreach (var c in claims)
105 NameClaimType = ci.NameClaimType;
106 RoleClaimType = ci.RoleClaimType;
107 auth_type = ci.AuthenticationType;
112 protected ClaimsIdentity (SerializationInfo info)
114 throw new NotImplementedException ();
118 protected ClaimsIdentity (SerializationInfo info, StreamingContext context)
121 throw new ArgumentNullException ("info");
122 throw new NotImplementedException ();
125 public ClaimsIdentity Actor {
131 throw new InvalidOperationException ("can not set the Actor property to this instance");
136 public virtual string AuthenticationType {
141 public object BootstrapContext { get; set; }
142 public string Label { get; set; }
143 public virtual string Name {
145 var target = NameClaimType;
146 foreach (var c in claims){
147 if (c.Type == target)
153 public string NameClaimType { get; private set; }
154 public string RoleClaimType { get; private set; }
156 public virtual IEnumerable<Claim> Claims {
162 public virtual bool IsAuthenticated {
164 return AuthenticationType != null && AuthenticationType != "";
168 public virtual void AddClaim (Claim claim)
171 throw new ArgumentNullException ("claim");
175 public virtual void AddClaims (IEnumerable<Claim> claims)
178 throw new ArgumentNullException ("claims");
179 foreach (var c in claims)
183 public virtual ClaimsIdentity Clone ()
185 return new ClaimsIdentity (null, claims, AuthenticationType, NameClaimType, RoleClaimType){
186 BootstrapContext = this.BootstrapContext,
192 public virtual IEnumerable<Claim> FindAll(Predicate<Claim> match)
195 throw new ArgumentNullException ("match");
196 foreach (var c in claims)
201 public virtual IEnumerable<Claim> FindAll(string type)
204 throw new ArgumentNullException ("type");
205 foreach (var c in claims)
210 public virtual Claim FindFirst (Predicate<Claim> match)
213 throw new ArgumentNullException ("match");
214 foreach (var c in claims)
220 public virtual Claim FindFirst (string type)
223 throw new ArgumentNullException ("type");
224 foreach (var c in claims)
230 public virtual bool HasClaim (Predicate<Claim> match)
233 throw new ArgumentNullException ("match");
234 foreach (var c in claims)
240 public virtual bool HasClaim (string type, string value)
243 throw new ArgumentNullException ("type");
245 throw new ArgumentNullException ("value");
246 foreach (var c in claims){
247 if (c.Type == type && c.Value == value)
253 public virtual void RemoveClaim (Claim claim)
255 if (!TryRemoveClaim (claim))
256 throw new InvalidOperationException ();
259 [MonoTODO ("This one should return false if the claim is owned by someone else, this does not exist yet")]
260 public virtual bool TryRemoveClaim (Claim claim)
264 claims.Remove (claim);