Sha256: bc9dd62bef4e86a12509436a6cb9ed5b2b8265358ee4968e849a5c207f62d854

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'protocol/redis'

module Async
	module Redis
		module Protocol
			# Executes AUTH after the user has established a connection.
			class Authenticated
				# Authentication has failed for some reason.
				class AuthenticationError < StandardError
				end
				
				# Create a new authenticated protocol.
				#
				# @parameter credentials [Array] The credentials to use for authentication.
				# @parameter protocol [Object] The delegated protocol for connecting.
				def initialize(credentials, protocol = Async::Redis::Protocol::RESP2)
					@credentials = credentials
					@protocol = protocol
				end
				
				attr :credentials
				
				# Create a new client and authenticate it.
				def client(stream)
					client = @protocol.client(stream)
					
					client.write_request(["AUTH", *@credentials])
					response = client.read_response
					
					if response != "OK"
						raise AuthenticationError, "Could not authenticate: #{response}"
					end
					
					return client
				end
			end
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
async-redis-0.10.1 lib/async/redis/protocol/authenticated.rb
async-redis-0.10.0 lib/async/redis/protocol/authenticated.rb
async-redis-0.9.0 lib/async/redis/protocol/authenticated.rb