diff options
Diffstat (limited to 'OpenSim/Framework/ServiceAuth')
-rw-r--r-- | OpenSim/Framework/ServiceAuth/BasicHttpAuthentication.cs | 79 | ||||
-rw-r--r-- | OpenSim/Framework/ServiceAuth/IServiceAuth.cs | 15 | ||||
-rw-r--r-- | OpenSim/Framework/ServiceAuth/ServiceAuth.cs | 23 |
3 files changed, 117 insertions, 0 deletions
diff --git a/OpenSim/Framework/ServiceAuth/BasicHttpAuthentication.cs b/OpenSim/Framework/ServiceAuth/BasicHttpAuthentication.cs new file mode 100644 index 0000000..f33a045 --- /dev/null +++ b/OpenSim/Framework/ServiceAuth/BasicHttpAuthentication.cs | |||
@@ -0,0 +1,79 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Collections.Specialized; | ||
4 | using System.Reflection; | ||
5 | |||
6 | using Nini.Config; | ||
7 | using log4net; | ||
8 | |||
9 | namespace OpenSim.Framework.ServiceAuth | ||
10 | { | ||
11 | public class BasicHttpAuthentication : IServiceAuth | ||
12 | { | ||
13 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
14 | |||
15 | private string m_Username, m_Password; | ||
16 | private string m_CredentialsB64; | ||
17 | |||
18 | private string remove_me; | ||
19 | |||
20 | public string Credentials | ||
21 | { | ||
22 | get { return m_CredentialsB64; } | ||
23 | } | ||
24 | |||
25 | public BasicHttpAuthentication(IConfigSource config, string section) | ||
26 | { | ||
27 | remove_me = section; | ||
28 | m_Username = Util.GetConfigVarFromSections<string>(config, "HttpAuthUsername", new string[] { "Network", section }, string.Empty); | ||
29 | m_Password = Util.GetConfigVarFromSections<string>(config, "HttpAuthPassword", new string[] { "Network", section }, string.Empty); | ||
30 | string str = m_Username + ":" + m_Password; | ||
31 | byte[] encData_byte = Util.UTF8.GetBytes(str); | ||
32 | |||
33 | m_CredentialsB64 = Convert.ToBase64String(encData_byte); | ||
34 | m_log.DebugFormat("[HTTP BASIC AUTH]: {0} {1} [{2}]", m_Username, m_Password, section); | ||
35 | } | ||
36 | |||
37 | public void AddAuthorization(NameValueCollection headers) | ||
38 | { | ||
39 | //m_log.DebugFormat("[HTTP BASIC AUTH]: Adding authorization for {0}", remove_me); | ||
40 | headers["Authorization"] = "Basic " + m_CredentialsB64; | ||
41 | } | ||
42 | |||
43 | public bool Authenticate(string data) | ||
44 | { | ||
45 | string recovered = Util.Base64ToString(data); | ||
46 | if (!String.IsNullOrEmpty(recovered)) | ||
47 | { | ||
48 | string[] parts = recovered.Split(new char[] { ':' }); | ||
49 | if (parts.Length >= 2) | ||
50 | { | ||
51 | return m_Username.Equals(parts[0]) && m_Password.Equals(parts[1]); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | return false; | ||
56 | } | ||
57 | |||
58 | public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d) | ||
59 | { | ||
60 | //m_log.DebugFormat("[HTTP BASIC AUTH]: Authenticate in {0}", remove_me); | ||
61 | if (requestHeaders != null) | ||
62 | { | ||
63 | string value = requestHeaders.Get("Authorization"); | ||
64 | if (value != null) | ||
65 | { | ||
66 | value = value.Trim(); | ||
67 | if (value.StartsWith("Basic ")) | ||
68 | { | ||
69 | value = value.Replace("Basic ", string.Empty); | ||
70 | if (Authenticate(value)) | ||
71 | return true; | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | d("WWW-Authenticate", "Basic realm = \"Asset Server\""); | ||
76 | return false; | ||
77 | } | ||
78 | } | ||
79 | } | ||
diff --git a/OpenSim/Framework/ServiceAuth/IServiceAuth.cs b/OpenSim/Framework/ServiceAuth/IServiceAuth.cs new file mode 100644 index 0000000..415dc12 --- /dev/null +++ b/OpenSim/Framework/ServiceAuth/IServiceAuth.cs | |||
@@ -0,0 +1,15 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Collections.Specialized; | ||
4 | |||
5 | namespace OpenSim.Framework.ServiceAuth | ||
6 | { | ||
7 | public delegate void AddHeaderDelegate(string key, string value); | ||
8 | |||
9 | public interface IServiceAuth | ||
10 | { | ||
11 | bool Authenticate(string data); | ||
12 | bool Authenticate(NameValueCollection headers, AddHeaderDelegate d); | ||
13 | void AddAuthorization(NameValueCollection headers); | ||
14 | } | ||
15 | } | ||
diff --git a/OpenSim/Framework/ServiceAuth/ServiceAuth.cs b/OpenSim/Framework/ServiceAuth/ServiceAuth.cs new file mode 100644 index 0000000..bc32d90 --- /dev/null +++ b/OpenSim/Framework/ServiceAuth/ServiceAuth.cs | |||
@@ -0,0 +1,23 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | |||
4 | using Nini.Config; | ||
5 | |||
6 | namespace OpenSim.Framework.ServiceAuth | ||
7 | { | ||
8 | public class ServiceAuth | ||
9 | { | ||
10 | public static IServiceAuth Create(IConfigSource config, string section) | ||
11 | { | ||
12 | string authType = Util.GetConfigVarFromSections<string>(config, "AuthType", new string[] { "Network", section }, "None"); | ||
13 | |||
14 | switch (authType) | ||
15 | { | ||
16 | case "BasicHttpAuthentication": | ||
17 | return new BasicHttpAuthentication(config, section); | ||
18 | } | ||
19 | |||
20 | return null; | ||
21 | } | ||
22 | } | ||
23 | } | ||