Skip to content

Commit

Permalink
Add authentication support
Browse files Browse the repository at this point in the history
  • Loading branch information
rykov committed Jan 9, 2012
1 parent 7116c0f commit 70abdfa
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

1 comment on commit 70abdfa

@kreynolds
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible for you to add a test against a live database for this?

Please sign in to comment.