irc/plugins/auth/auth.go

68 lines
1.7 KiB
Go

package auth
import (
"context"
"encoding/base64"
"tuxpa.in/a/irc/plugins/caps/ircmw"
"tuxpa.in/a/irc/pkg/ircv3"
)
type SaslPlain struct {
Username string
Password string
}
func (saslplain *SaslPlain) Middleware(next ircv3.Handler) ircv3.Handler {
return ircv3.HandlerFunc(func(ctx context.Context, w ircv3.MessageWriter, m *ircv3.Message) {
if m.Command == "" {
ircmw.AddPending(ctx, 1)
w.WriteMessage(ircv3.NewMessage("CAP", "REQ", "sasl"))
}
if m.Command == "CAP" && m.Param(0) == "*" && m.Param(1) == "ACK" && m.Param(2) == "sasl" {
w.WriteMessage(ircv3.NewMessage("AUTHENTICATE", "PLAIN"))
}
if m.Command == "AUTHENTICATE" && m.Param(0) == "+" {
w.WriteMessage(ircv3.NewMessage("AUTHENTICATE", base64.StdEncoding.EncodeToString([]byte(
saslplain.Username+string([]byte{0})+
saslplain.Username+string([]byte{0})+
saslplain.Password,
))))
}
switch m.Command {
case "903", "904", "905", "906", "907":
ircmw.AddPending(ctx, -1)
}
next.Handle(ctx, w, m)
})
}
type User struct {
Username string
Realname string
Hostname string
Server string
}
func (u *User) Middleware(next ircv3.Handler) ircv3.Handler {
return ircv3.HandlerFunc(func(ctx context.Context, w ircv3.MessageWriter, m *ircv3.Message) {
if m.Command == "" {
w.WriteMessage(ircv3.NewMessage("USER", u.Username, u.Hostname, u.Server, u.Realname))
}
next.Handle(ctx, w, m)
})
}
type Nick struct {
Nick string
}
func (u *Nick) Middleware(next ircv3.Handler) ircv3.Handler {
return ircv3.HandlerFunc(func(ctx context.Context, w ircv3.MessageWriter, m *ircv3.Message) {
if m.Command == "" {
w.WriteMessage(ircv3.NewMessage("NICK", u.Nick))
}
next.Handle(ctx, w, m)
})
}