2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.Http / Mono.Http.Modules / AuthenticationModule.cs
1 //
2 // Abstract Authentication implementation
3 //
4 // Authors:
5 //      Greg Reinacker (gregr@rassoc.com)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // Copyright 2002-2003 Greg Reinacker, Reinacker & Associates, Inc. All rights reserved.
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 //
11 // Based on "DigestAuthenticationModule.cs". Original source code available at
12 // http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html
13 //
14
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Configuration;
38 using System.Web;
39
40 namespace Mono.Http.Modules
41 {
42         abstract public class AuthenticationModule : IHttpModule
43         {
44                 string authMethod;
45
46                 public AuthenticationModule (string authenticationMethod) 
47                 {
48                         authMethod = authenticationMethod;
49                 }
50
51                 public string AuthenticationMethod { 
52                         get { return authMethod; }
53                 }
54
55                 #region IHttpModule Members
56
57                 public virtual void Init (HttpApplication context) 
58                 {
59                         context.AuthenticateRequest += new EventHandler (this.OnAuthenticateRequest);
60                         context.EndRequest += new EventHandler (this.OnEndRequest);
61                 }
62
63                 public virtual void Dispose () {}
64
65                 #endregion
66
67                 #region Event Handlers
68
69                 public virtual void OnAuthenticateRequest (object source, EventArgs eventArgs) 
70                 {
71                         if (!AuthenticationRequired)
72                                 return;
73
74                         HttpApplication app = (HttpApplication) source;
75                         string authdata = Authorization (app, AuthenticationMethod);
76                         if ((authdata == null) || (!AcceptCredentials (app, authdata))) {
77                                 DenyAccess (app);
78                                 return;
79                         }
80                 }
81
82                 abstract public void OnEndRequest (object source, EventArgs eventArgs);
83
84                 #endregion
85
86                 abstract protected bool AcceptCredentials (HttpApplication app, string authentication);
87
88                 protected bool AuthenticationRequired {
89                         get { return (AuthenticationMethod == ConfigurationSettings.AppSettings ["Authentication"]); }
90                 }
91
92                 protected void DenyAccess (HttpApplication app) 
93                 {
94                         app.Response.StatusCode = 401;
95                         app.Response.StatusDescription = "Access Denied";
96                         // Write to response stream as well, to give user visual 
97                         // indication of error during development
98                         app.Response.Write ("401 Access Denied");
99                         app.CompleteRequest ();
100                 }
101
102                 protected string Authorization (HttpApplication app, string authenticationMethod) 
103                 {
104                         string autz = app.Request.Headers ["Authorization"];
105                         if ((autz == null) || (autz.Length == 0)) {
106                                 // No credentials; anonymous request
107                                 return null;
108                         }
109                         
110                         if (autz.ToUpper ().StartsWith (authenticationMethod.ToUpper ())) {
111                                 return autz.Substring (authenticationMethod.Length + 1);
112                         }
113
114                         return null;
115                 }
116         }
117 }
118