diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/ServiceAuth/BasicHttpAuthentication.cs | 79 |
1 files changed, 79 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 | } | ||