Add support for AUTH commands

This commit is contained in:
idk
2022-04-28 11:23:54 -04:00
parent 6aed9d7cec
commit 40ad754b47
4 changed files with 125 additions and 1 deletions

47
auth.go Normal file
View File

@ -0,0 +1,47 @@
package goSam
import "fmt"
// SetupAuth sends the AUTH ENABLE command and immediately sets up a new Username and
// Password from the arguments
func (c *Client) SetupAuth(user, password string) error {
r, err := c.sendCmd("AUTH ENABLE\n")
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r)
}
r, err = c.sendCmd("AUTH %s %s\n", user, password)
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r)
}
return nil
}
// TeardownAuth sends the AUTH DISABLE command but does not remove the Username and
// Password from the client PasswordManager
func (c *Client) TeardownAuth() error {
r, err := c.sendCmd("AUTH DISABLE\n")
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("TeardownAuth Unknown Reply: %+v\n", r)
}
return nil
}
func (c *Client) RemoveAuthUser(user string) error {
r, err := c.sendCmd("AUTH REMOVE %s\n", user)
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("RemoveAuthUser Unknown Reply: %+v\n", r)
}
return nil
}

44
auth/main.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"fmt"
"github.com/eyedeekay/goSam"
)
/**
THIS is a freestanding test for SAMv3.2 AUTH commands using goSam. It's
intended to be run separate from the other tests so that you don't accidentally end
up setting SAM session passwords and leaving them in the PasswordManager if a test
fails for some reason before you can remove them.
**/
func main() {
client, err := goSam.NewDefaultClient()
if err != nil {
panic(err)
}
err = client.SetupAuth("user", "password")
if err != nil {
panic(err)
}
client2, err := goSam.NewDefaultClient()
if err != nil {
panic(err)
}
conn, err := client2.Dial("", "idk.i2p")
if err != nil {
fmt.Println(err)
}
conn.Close()
err = client.RemoveAuthUser("user")
if err != nil {
panic(err)
}
//fmt.Println(r)
err = client.TeardownAuth()
if err != nil {
panic(err)
}
//r, err = client.NewDestination()
}

View File

@ -24,6 +24,8 @@ type Client struct {
port string
fromport string
toport string
user string
pass string
SamConn net.Conn // Control socket
SamDGConn DatagramConn // Datagram socket
@ -211,7 +213,8 @@ func (c *Client) samaddr() string {
// send the initial handshake command and check that the reply is ok
func (c *Client) hello() error {
r, err := c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d\n", c.sammin, c.sammax)
r, err := c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d %s %s\n", c.sammin, c.sammax, c.getUser(), c.getPass())
if err != nil {
return err
}

View File

@ -62,6 +62,22 @@ func SetHost(s string) func(*Client) error {
}
}
//SetUser sets the username for authentication during the SAM HELLO phase
func SetUser(s string) func(*Client) error {
return func(c *Client) error {
c.user = s
return nil
}
}
//SetUser sets the password for authentication during the SAM HELLO phase
func SetPass(s string) func(*Client) error {
return func(c *Client) error {
c.pass = s
return nil
}
}
func SetSAMMinVersion(i int) func(*Client) error {
return func(c *Client) error {
if i < 0 {
@ -555,3 +571,17 @@ func (c *Client) Print() string {
c.closeidletime() +
c.compresion()
}
func (c *Client) getUser() string {
if c.user == "" {
return ""
}
return fmt.Sprintf("USER=%s", c.user)
}
func (c *Client) getPass() string {
if c.pass == "" {
return ""
}
return fmt.Sprintf("PASSWORD=%s", c.pass)
}