Sha256: 04a04c4d0d7c9d151c002148e705dbc635737f27b592dd6750a53a59bc98e8d7

Contents?: true

Size: 1.09 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
async-redis-0.11.0 lib/async/redis/protocol/authenticated.rb