1 #if !NO_SYSTEM_WEB_DEPENDENCY
3 // CustomUserNameSecurityTokenAuthenticator.cs
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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.
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.IdentityModel.Claims;
33 using System.IdentityModel.Policy;
34 using System.IdentityModel.Tokens;
35 using System.Security.Principal;
38 namespace System.IdentityModel.Selectors
40 public class CustomUserNameSecurityTokenAuthenticator
41 : UserNameSecurityTokenAuthenticator
43 UserNamePasswordValidator validator;
45 public CustomUserNameSecurityTokenAuthenticator (
46 UserNamePasswordValidator validator)
48 if (validator == null)
49 throw new ArgumentNullException ("validator");
50 this.validator = validator;
53 protected override ReadOnlyCollection<IAuthorizationPolicy>
54 ValidateUserNamePasswordCore (string userName, string password)
56 validator.Validate (userName, password);
57 IAuthorizationPolicy policy =
58 new AuthorizedCustomUserPolicy (userName);
59 return new ReadOnlyCollection<IAuthorizationPolicy> (new IAuthorizationPolicy [] {policy});
62 abstract class SystemIdentityAuthorizationPolicy : IAuthorizationPolicy
66 protected SystemIdentityAuthorizationPolicy (string id)
75 public ClaimSet Issuer {
76 get { return ClaimSet.System; }
80 // This method is expected to be thread safe
81 public bool Evaluate (EvaluationContext ec, ref object state)
84 ec.AddClaimSet (this, CreateClaims ());
86 if (!ec.Properties.ContainsKey ("Identities")) {
87 list = new List<IIdentity> ();
88 ec.Properties ["Identities"] = list;
90 IList<IIdentity> ilist = (IList<IIdentity>) ec.Properties ["Identities"];
91 list = ilist as List<IIdentity>;
93 list = new List<IIdentity> (ilist);
94 ec.Properties ["Identities"] = list;
97 list.Add (CreateIdentity ());
98 ec.RecordExpirationTime (DateTime.MaxValue.AddDays (-1));
100 // FIXME: is it correct that this should always return true?
104 public abstract DateTime ExpirationTime { get; }
106 public abstract ClaimSet CreateClaims ();
108 public abstract IIdentity CreateIdentity ();
111 class AuthorizedCustomUserPolicy : SystemIdentityAuthorizationPolicy
115 public AuthorizedCustomUserPolicy (string user)
116 : base (new UniqueId ().ToString ())
121 public override DateTime ExpirationTime {
122 get { return DateTime.MaxValue; }
125 public override ClaimSet CreateClaims ()
127 return new DefaultClaimSet (Claim.CreateNameClaim (user));
130 public override IIdentity CreateIdentity ()
132 return new GenericIdentity (user);