5 // Miguel de Icaza (miguel@xamarin.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright 2014 Xamarin Inc
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
32 using System.Security.Principal;
33 using System.Runtime.Serialization;
35 namespace System.Security.Claims {
38 public class ClaimsIdentity : IIdentity {
39 [NonSerializedAttribute]
40 public const string DefaultNameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
41 [NonSerializedAttribute]
42 public const string DefaultRoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
43 [NonSerializedAttribute]
44 public const string DefaultIssuer = "LOCAL AUTHORITY";
46 readonly List<Claim> claims;
48 readonly string auth_type;
50 public ClaimsIdentity ()
51 : this (claims: null, authenticationType: null, nameType: null, roleType: null)
54 public ClaimsIdentity(IEnumerable<Claim> claims)
55 : this (claims: claims, authenticationType: null, nameType: null, roleType: null)
58 public ClaimsIdentity (string authenticationType)
59 : this (claims: null, authenticationType: authenticationType, nameType: null, roleType: null)
62 public ClaimsIdentity (IEnumerable<Claim> claims, string authenticationType)
63 : this (claims, authenticationType, null, null)
66 public ClaimsIdentity (string authenticationType, string nameType, string roleType)
67 : this (claims: null, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
70 public ClaimsIdentity (IIdentity identity) : this (identity: identity, claims: null)
74 public ClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
75 : this (identity: null, claims: claims, authenticationType: authenticationType, nameType: nameType, roleType: roleType)
79 public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims)
80 : this (identity, claims, authenticationType: null, nameType: null, roleType: null)
84 public ClaimsIdentity (IIdentity identity, IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType)
86 NameClaimType = string.IsNullOrEmpty (nameType) ? DefaultNameClaimType : nameType;
87 RoleClaimType = string.IsNullOrEmpty (roleType) ? DefaultRoleClaimType : roleType;
88 auth_type = authenticationType;
90 this.claims = new List<Claim> ();
92 if (identity != null) {
93 if (string.IsNullOrEmpty (authenticationType))
94 auth_type = identity.AuthenticationType;
96 var ci = identity as ClaimsIdentity;
99 BootstrapContext = ci.BootstrapContext;
100 foreach (var c in ci.Claims)
104 NameClaimType = string.IsNullOrEmpty (nameType) ? ci.NameClaimType : nameType;
105 RoleClaimType = string.IsNullOrEmpty (roleType) ? ci.RoleClaimType : roleType;
106 } else if (!string.IsNullOrEmpty (identity.Name)) {
107 AddDefaultClaim (identity.Name);
111 if (claims != null) {
117 protected ClaimsIdentity (SerializationInfo info)
119 throw new NotImplementedException ();
123 protected ClaimsIdentity (SerializationInfo info, StreamingContext context)
126 throw new ArgumentNullException ("info");
127 throw new NotImplementedException ();
130 public ClaimsIdentity Actor {
136 throw new InvalidOperationException ("can not set the Actor property to this instance");
142 public virtual string AuthenticationType {
147 public object BootstrapContext { get; set; }
148 public string Label { get; set; }
149 public virtual string Name {
151 var target = NameClaimType;
152 foreach (var c in claims){
153 if (c.Type == target)
159 public string NameClaimType { get; private set; }
160 public string RoleClaimType { get; private set; }
162 public virtual IEnumerable<Claim> Claims {
168 public virtual bool IsAuthenticated {
170 return AuthenticationType != null && AuthenticationType != "";
174 public virtual void AddClaim (Claim claim)
177 throw new ArgumentNullException ("claim");
179 if (claim.Subject != this)
180 claim = claim.Clone (this);
185 public virtual void AddClaims (IEnumerable<Claim> claims)
188 throw new ArgumentNullException ("claims");
190 foreach (var c in claims)
194 internal void AddDefaultClaim (string identityName)
196 this.claims.Add (new Claim (NameClaimType, identityName, "http://www.w3.org/2001/XMLSchema#string", DefaultIssuer, DefaultIssuer, this));
199 public virtual ClaimsIdentity Clone ()
201 return new ClaimsIdentity (null, claims, AuthenticationType, NameClaimType, RoleClaimType){
202 BootstrapContext = this.BootstrapContext,
208 public virtual IEnumerable<Claim> FindAll(Predicate<Claim> match)
211 throw new ArgumentNullException ("match");
212 foreach (var c in claims)
217 public virtual IEnumerable<Claim> FindAll (string type)
220 throw new ArgumentNullException ("type");
221 foreach (var c in claims)
222 if (string.Equals (c.Type, type, StringComparison.OrdinalIgnoreCase))
226 public virtual Claim FindFirst (Predicate<Claim> match)
229 throw new ArgumentNullException ("match");
230 foreach (var c in claims)
236 public virtual Claim FindFirst (string type)
239 throw new ArgumentNullException ("type");
240 foreach (var c in claims)
241 if (string.Equals (c.Type, type, StringComparison.OrdinalIgnoreCase))
246 public virtual bool HasClaim (Predicate<Claim> match)
249 throw new ArgumentNullException ("match");
250 foreach (var c in claims)
256 public virtual bool HasClaim (string type, string value)
259 throw new ArgumentNullException ("type");
261 throw new ArgumentNullException ("value");
262 foreach (var c in claims){
263 if (string.Equals (c.Type, type, StringComparison.OrdinalIgnoreCase) && c.Value == value)
269 public virtual void RemoveClaim (Claim claim)
271 if (!TryRemoveClaim (claim))
272 throw new InvalidOperationException ();
275 [MonoTODO ("This one should return false if the claim is owned by someone else, this does not exist yet")]
276 public virtual bool TryRemoveClaim (Claim claim)
280 claims.Remove (claim);