From 1b12cbfb345ee5b0022e204844911902d3bb7c3e Mon Sep 17 00:00:00 2001 From: justin Date: Fri, 28 Aug 2015 16:17:56 -0400 Subject: [PATCH] Include support for IRC server operators Added authentication interface for operators with basic implementation --- oper.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ server.go | 2 ++ 2 files changed, 58 insertions(+) create mode 100644 oper.go diff --git a/oper.go b/oper.go new file mode 100644 index 0000000..699d98f --- /dev/null +++ b/oper.go @@ -0,0 +1,56 @@ +package irc + +import "github.com/sorcix/irc" + +// OperAuthMethod is an interface for authenticating server level operators +type OperAuthMethod interface { + Authenticate(username string, password string, conn *Client) +} + +// OperAuthMethodFunc is a wrapper so regular functions can be an OperAuthMethod +type OperAuthMethodFunc func(username string, password string, conn *Client) + +// Authenticate attempts to authenticate a given user +func (o OperAuthMethodFunc) Authenticate(username string, password string, conn *Client) { + o(username, password, conn) +} + +// BasicOperAuthMethod can handle simple username password mappings for operator authentication +type BasicOperAuthMethod struct { + m map[string]string +} + +// NewBasicOperAuthMethod creates and returns a new BasicOperAuthMethod +func NewBasicOperAuthMethod() *BasicOperAuthMethod { + b := BasicOperAuthMethod{} + b.m = map[string]string{} + return &b +} + +// Add adds a new username and password for an acceptable operator +func (b BasicOperAuthMethod) Add(username, password string) { + b.m[username] = password + +} + +// Get returns the password if the username was found as a valid operator, if not found, ok will be false +func (b BasicOperAuthMethod) Get(username string) (password string, ok bool) { + password, ok = b.m[username] + return +} + +// Remove removes an operator from being allowed to authenticate +func (b BasicOperAuthMethod) Remove(username string) { + delete(b.m, username) +} + +// Authenticate locates if an operator of the given username is found, and if so checks if the password matches +func (b BasicOperAuthMethod) Authenticate(username string, password string, client *Client) { + foundPassword, ok := b.m[username] + if !ok || foundPassword != password { + m := irc.Message{Prefix: client.Server.Prefix, Command: irc.ERR_PASSWDMISMATCH, Params: []string{client.Nickname}, Trailing: "Password incorrect"} + client.Encode(&m) + return + } + client.MakeOper() +} diff --git a/server.go b/server.go index 3185f4a..989e51c 100644 --- a/server.go +++ b/server.go @@ -26,6 +26,8 @@ type Server struct { channels map[string]*Channel channelMutex sync.RWMutex + + OperAuthMethod } // ServerConfig contains configuration data for seeding a server