Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improved the login API, and allowed it to be set in the connection st…

…ring, still don't know the best way to auto-authenticate session yet.
  • Loading branch information...
commit e93edc75a4f524c746756529765952a35da62644 1 parent 2b47d58
@nberardi nberardi authored
View
BIN  lib/NuGet.exe
Binary file not shown
View
8 src/CassandraContext.cs
@@ -26,8 +26,8 @@ public static ConnectionBuilder CurrentConnectionBuilder
/// <param name="keyspace"></param>
/// <param name="server"></param>
/// <param name="timeout"></param>
- public CassandraContext(string keyspace, Server server)
- : this(keyspace, server.Host, server.Port, server.Timeout) { }
+ public CassandraContext(string keyspace, Server server, string username = null, string password = null)
+ : this(keyspace, server.Host, server.Port, server.Timeout, username, password) { }
/// <summary>
///
@@ -37,8 +37,8 @@ public CassandraContext(string keyspace, Server server)
/// <param name="port"></param>
/// <param name="timeout"></param>
/// <param name="provider"></param>
- public CassandraContext(string keyspace, string host, int port = 9160, int timeout = 0)
- : this(new ConnectionBuilder(keyspace, host, port, timeout)) { }
+ public CassandraContext(string keyspace, string host, int port = 9160, int timeout = 0, string username = null, string password = null)
+ : this(new ConnectionBuilder(keyspace, host, port, timeout, username: username, password: password)) { }
/// <summary>
///
View
42 src/CassandraSession.cs
@@ -132,33 +132,20 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
WriteConsistency = write;
Keyspace = new CassandraKeyspace(connectionProvider.Builder.Keyspace);
+ IsAuthenticated = false;
Current = this;
}
- /// <summary>
- /// Gets ConnectionProvider.
- /// </summary>
public IConnectionProvider ConnectionProvider { get; private set; }
- /// <summary>
- ///
- /// </summary>
public ConsistencyLevel ReadConsistency { get; private set; }
- /// <summary>
- ///
- /// </summary>
public ConsistencyLevel WriteConsistency { get; private set; }
- /// <summary>
- ///
- /// </summary>
public CassandraKeyspace Keyspace { get; private set; }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
+ public bool IsAuthenticated { get; private set; }
+
internal Cassandra.Client GetClient(bool setKeyspace = true)
{
if (_connection == null)
@@ -168,18 +155,27 @@ internal Cassandra.Client GetClient(bool setKeyspace = true)
_connection.Open();
if (setKeyspace)
- _connection.SetKeyspace(Keyspace.KeyspaceName);
+ _connection.SetKeyspace(Keyspace.KeyspaceName)
return _connection.Client;
}
- /// <summary>
- ///
- /// </summary>
- /// <param name="authRequest"></param>
- public void Login(AuthenticationRequest authRequest)
+ public void Login(string username, string password)
{
- GetClient().login(authRequest);
+ var auth = new AuthenticationRequest {
+ Credentials = new Dictionary<string, string> { { "username", username }, { "password", password } }
+ };
+
+ try
+ {
+ GetClient().login(auth);
+ IsAuthenticated = true;
+ }
+ catch (Exception exc)
+ {
+ IsAuthenticated = false;
+ throw new CassandraException("Login failed.", exc);
+ }
}
#region IDisposable Members
View
34 src/Connections/ConnectionBuilder.cs
@@ -15,7 +15,7 @@ public class ConnectionBuilder
/// <param name="port"></param>
/// <param name="timeout"></param>
/// <param name="provider"></param>
- public ConnectionBuilder(string keyspace, string host, int port = 9160, int timeout = 0, bool pooled = false, int poolSize = 25, int lifetime = 0)
+ public ConnectionBuilder(string keyspace, string host, int port = 9160, int timeout = 0, bool pooled = false, int poolSize = 25, int lifetime = 0, string username = null, string password = null)
{
Keyspace = keyspace;
Servers = new List<Server>() { new Server(host, port) };
@@ -26,6 +26,8 @@ public ConnectionBuilder(string keyspace, string host, int port = 9160, int time
ConnectionString = GetConnectionString();
ReadConsistency = ConsistencyLevel.QUORUM;
WriteConsistency = ConsistencyLevel.QUORUM;
+ Username = username;
+ Password = password;
}
/// <summary>
@@ -210,6 +212,24 @@ private void InitializeConnectionString(string connectionString)
}
#endregion
+
+ #region Username
+
+ if (pairs.ContainsKey("Username"))
+ {
+ Username = pairs["Username"];
+ }
+
+ #endregion
+
+ #region Password
+
+ if (pairs.ContainsKey("Password"))
+ {
+ Password = pairs["Password"];
+ }
+
+ #endregion
}
private string GetConnectionString()
@@ -225,6 +245,8 @@ private string GetConnectionString()
b.AppendFormat(format, "Lifetime", Lifetime);
b.AppendFormat(format, "Read", ReadConsistency);
b.AppendFormat(format, "Write", WriteConsistency);
+ b.AppendFormat(format, "Username", Username);
+ b.AppendFormat(format, "Password", Password == null ? "" : new String('X', Password.Length));
return b.ToString();
}
@@ -272,6 +294,16 @@ private string GetConnectionString()
/// <summary>
///
/// </summary>
+ public string Username { get; private set; }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public string Password { get; private set; }
+
+ /// <summary>
+ ///
+ /// </summary>
public string ConnectionString { get; private set; }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.