Skip to content

Commit

Permalink
Merge pull request kreynolds#25 from rykov/master
Browse files Browse the repository at this point in the history
Add authentication support
  • Loading branch information
Kelley Reynolds committed Feb 21, 2012
2 parents 7116c0f + 70abdfa commit 4256db1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/cassandra-cql/database.rb
Expand Up @@ -42,6 +42,7 @@ def connect!
obj = self
@connection.add_callback(:post_connect) do
execute("USE #{@keyspace}")
@connection.login(@auth_request) if @auth_request
end
end

Expand Down Expand Up @@ -110,5 +111,15 @@ def schema
# TODO: This should be replaced with a CQL call that doesn't exist yet
Schema.new(@connection.describe_keyspace(@keyspace))
end

def login!(username, password)
request = CassandraCQL::Thrift::AuthenticationRequest.new
request.credentials = {'username' => username, 'password' => password}
ret = @connection.login(request)
# To avoid a double login on the initial connect, we set
# @auth_request after the first successful login.
@auth_request = request
ret
end
end
end
9 changes: 9 additions & 0 deletions spec/database_spec.rb
Expand Up @@ -13,4 +13,13 @@
end
end

describe "login!" do
it "should call login! on connection" do
creds = { 'username' => 'myuser', 'password' => 'mypass' }
@connection.connection.should_receive(:login) do |auth|
auth.credentials.should eq(creds)
end
@connection.login!(creds['username'], creds['password'])
end
end
end

0 comments on commit 4256db1

Please sign in to comment.