Prepare XM for Http additions
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Selectors / CustomUserNameSecurityTokenAuthenticator.cs
1 #if !NO_SYSTEM_WEB_DEPENDENCY
2 //
3 // CustomUserNameSecurityTokenAuthenticator.cs
4 //
5 // Author:
6 //      Atsushi Enomoto <atsushi@ximian.com>
7 //
8 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29 using System;
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;
36 using System.Xml;
37
38 namespace System.IdentityModel.Selectors
39 {
40         public class CustomUserNameSecurityTokenAuthenticator
41                 : UserNameSecurityTokenAuthenticator
42         {
43                 UserNamePasswordValidator validator;
44
45                 public CustomUserNameSecurityTokenAuthenticator (
46                         UserNamePasswordValidator validator)
47                 {
48                         if (validator == null)
49                                 throw new ArgumentNullException ("validator");
50                         this.validator = validator;
51                 }
52
53                 protected override ReadOnlyCollection<IAuthorizationPolicy>
54                         ValidateUserNamePasswordCore (string userName, string password)
55                 {
56                         validator.Validate (userName, password);
57                         IAuthorizationPolicy policy =
58                                 new AuthorizedCustomUserPolicy (userName);
59                         return new ReadOnlyCollection<IAuthorizationPolicy> (new IAuthorizationPolicy [] {policy});
60                 }
61
62                 abstract class SystemIdentityAuthorizationPolicy : IAuthorizationPolicy
63                 {
64                         string id;
65
66                         protected SystemIdentityAuthorizationPolicy (string id)
67                         {
68                                 this.id = id;
69                         }
70
71                         public string Id {
72                                 get { return id; }
73                         }
74
75                         public ClaimSet Issuer {
76                                 get { return ClaimSet.System; }
77                         }
78
79
80                         // This method is expected to be thread safe
81                         public bool Evaluate (EvaluationContext ec, ref object state)
82                         {
83                                 lock (ec) {
84                                         ec.AddClaimSet (this, CreateClaims ());
85                                         List<IIdentity> list;
86                                         if (!ec.Properties.ContainsKey ("Identities")) {
87                                                 list = new List<IIdentity> ();
88                                                 ec.Properties ["Identities"] = list;
89                                         } else {
90                                                 IList<IIdentity> ilist = (IList<IIdentity>) ec.Properties ["Identities"];
91                                                 list = ilist as List<IIdentity>;
92                                                 if (list == null) {
93                                                         list = new List<IIdentity> (ilist);
94                                                         ec.Properties ["Identities"] = list;
95                                                 }
96                                         }
97                                         list.Add (CreateIdentity ());
98                                         ec.RecordExpirationTime (DateTime.MaxValue.AddDays (-1));
99                                 }
100                                 // FIXME: is it correct that this should always return true?
101                                 return true;
102                         }
103
104                         public abstract DateTime ExpirationTime { get; }
105
106                         public abstract ClaimSet CreateClaims ();
107
108                         public abstract IIdentity CreateIdentity ();
109                 }
110
111                 class AuthorizedCustomUserPolicy : SystemIdentityAuthorizationPolicy
112                 {
113                         string user;
114
115                         public AuthorizedCustomUserPolicy (string user)
116                                 : base (new UniqueId ().ToString ())
117                         {
118                                 this.user = user;
119                         }
120
121                         public override DateTime ExpirationTime {
122                                 get { return DateTime.MaxValue; }
123                         }
124
125                         public override ClaimSet CreateClaims ()
126                         {
127                                 return new DefaultClaimSet (Claim.CreateNameClaim (user));
128                         }
129
130                         public override IIdentity CreateIdentity ()
131                         {
132                                 return new GenericIdentity (user);
133                         }
134                 }
135         }
136 }
137 #endif