OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
AuthenticationServiceBase.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using OpenMetaverse;
30 using log4net;
31 using Nini.Config;
32 using System.Reflection;
33 using OpenSim.Server.Base;
34 using OpenSim.Services.Interfaces;
35 using OpenSim.Data;
36 using OpenSim.Framework;
37 using OpenSim.Services.Base;
38 
39 namespace OpenSim.Services.AuthenticationService
40 {
41  // Generic Authentication service used for identifying
42  // and authenticating principals.
43  // Principals may be clients acting on users' behalf,
44  // or any other components that need
45  // verifiable identification.
46  //
48  {
49  private static readonly ILog m_log =
50  LogManager.GetLogger(
51  MethodBase.GetCurrentMethod().DeclaringType);
52 
54  protected IUserAccountService m_UserAccountService = null;
55 
56  public AuthenticationServiceBase(IConfigSource config, IUserAccountService acct) : this(config)
57  {
58  m_UserAccountService = acct;
59  }
60 
61  public AuthenticationServiceBase(IConfigSource config) : base(config)
62  {
63  string dllName = String.Empty;
64  string connString = String.Empty;
65  string realm = "auth";
66 
67  //
68  // Try reading the [AuthenticationService] section first, if it exists
69  //
70  IConfig authConfig = config.Configs["AuthenticationService"];
71  if (authConfig != null)
72  {
73  dllName = authConfig.GetString("StorageProvider", dllName);
74  connString = authConfig.GetString("ConnectionString", connString);
75  realm = authConfig.GetString("Realm", realm);
76  }
77 
78  //
79  // Try reading the [DatabaseService] section, if it exists
80  //
81  IConfig dbConfig = config.Configs["DatabaseService"];
82  if (dbConfig != null)
83  {
84  if (dllName == String.Empty)
85  dllName = dbConfig.GetString("StorageProvider", String.Empty);
86  if (connString == String.Empty)
87  connString = dbConfig.GetString("ConnectionString", String.Empty);
88  }
89 
90  //
91  // We tried, but this doesn't exist. We can't proceed.
92  //
93  if (dllName == String.Empty || realm == String.Empty)
94  throw new Exception("No StorageProvider configured");
95 
96  m_Database = LoadPlugin<IAuthenticationData>(dllName,
97  new Object[] {connString, realm});
98  if (m_Database == null)
99  throw new Exception(string.Format("Could not find a storage interface in module {0}", dllName));
100  }
101 
102  public bool Verify(UUID principalID, string token, int lifetime)
103  {
104  return m_Database.CheckToken(principalID, token, lifetime);
105  }
106 
107  public virtual bool Release(UUID principalID, string token)
108  {
109  return m_Database.CheckToken(principalID, token, 0);
110  }
111 
112  public virtual bool SetPassword(UUID principalID, string password)
113  {
114  string passwordSalt = Util.Md5Hash(UUID.Random().ToString());
115  string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + passwordSalt);
116 
117  AuthenticationData auth = m_Database.Get(principalID);
118  if (auth == null)
119  {
120  auth = new AuthenticationData();
121  auth.PrincipalID = principalID;
122  auth.Data = new System.Collections.Generic.Dictionary<string, object>();
123  auth.Data["accountType"] = "UserAccount";
124  auth.Data["webLoginKey"] = UUID.Zero.ToString();
125  }
126  auth.Data["passwordHash"] = md5PasswdHash;
127  auth.Data["passwordSalt"] = passwordSalt;
128  if (!m_Database.Store(auth))
129  {
130  m_log.DebugFormat("[AUTHENTICATION DB]: Failed to store authentication data");
131  return false;
132  }
133 
134  m_log.InfoFormat("[AUTHENTICATION DB]: Set password for principalID {0}", principalID);
135  return true;
136  }
137 
138  public virtual AuthInfo GetAuthInfo(UUID principalID)
139  {
140  AuthenticationData data = m_Database.Get(principalID);
141 
142  if (data == null)
143  {
144  return null;
145  }
146  else
147  {
148  AuthInfo info
149  = new AuthInfo()
150  {
151  PrincipalID = data.PrincipalID,
152  AccountType = data.Data["accountType"] as string,
153  PasswordHash = data.Data["passwordHash"] as string,
154  PasswordSalt = data.Data["passwordSalt"] as string,
155  WebLoginKey = data.Data["webLoginKey"] as string
156  };
157 
158  return info;
159  }
160  }
161 
162  public virtual bool SetAuthInfo(AuthInfo info)
163  {
165  auth.PrincipalID = info.PrincipalID;
166  auth.Data = new System.Collections.Generic.Dictionary<string, object>();
167  auth.Data["accountType"] = info.AccountType;
168  auth.Data["webLoginKey"] = info.WebLoginKey;
169  auth.Data["passwordHash"] = info.PasswordHash;
170  auth.Data["passwordSalt"] = info.PasswordSalt;
171 
172  if (!m_Database.Store(auth))
173  {
174  m_log.ErrorFormat("[AUTHENTICATION DB]: Failed to store authentication info.");
175  return false;
176  }
177 
178  m_log.DebugFormat("[AUTHENTICATION DB]: Set authentication info for principalID {0}", info.PrincipalID);
179  return true;
180  }
181 
182  protected string GetToken(UUID principalID, int lifetime)
183  {
184  UUID token = UUID.Random();
185 
186  if (m_Database.SetToken(principalID, token.ToString(), lifetime))
187  return token.ToString();
188 
189  return String.Empty;
190  }
191 
192  }
193 }
AuthenticationServiceBase(IConfigSource config, IUserAccountService acct)
Interactive OpenSim region server
Definition: OpenSim.cs:55
bool Verify(UUID principalID, string token, int lifetime)
An interface for connecting to the authentication datastore