commit c7ecbae7a01d99416d38b142023e4701508d903b from: Oliver Lowe date: Wed Apr 13 08:31:45 2022 UTC wip commit - a7e7d1d5189113e16d1cd8eb7a6d044d3eca0aca commit + c7ecbae7a01d99416d38b142023e4701508d903b blob - 98a9f070192863dc80ac6d29db43f0aa03ec5064 blob + 81ef5523f4eb5c7954232f88156d459c9429bc3a --- client.go +++ client.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "io" "net/http" "mailmux.net/aliases" @@ -94,10 +93,7 @@ func (c *Client) Aliases() (aliases.Aliases, error) { } defer resp.Body.Close() - buf.Reset() - io.Copy(buf, resp.Body) - fmt.Println(string(buf.Bytes())) - rmsg, err := ParseMcall(buf) + rmsg, err := ParseMcall(resp.Body) if err != nil { return nil, fmt.Errorf("list aliases: parse response: %w", err) } blob - 51e0bf7797f62622a549f18aa0f43c9b2b4b2d2e blob + c059ade036f84a30c881da06b1f534ae3f56668f --- cmd/mailmux/mailmux.go +++ cmd/mailmux/mailmux.go @@ -27,15 +27,16 @@ func (srv *server) aliasHandler(w http.ResponseWriter, rerror(w, err.Error(), http.StatusBadRequest) return } - _, err = srv.users.Lookup(tmsg.Username) + + fmt.Println("authenticating...") + err = srv.users.Authenticate(tmsg.Username, mailmux.Password(tmsg.Password)) if err != nil { rerror(w, "unauthorised", http.StatusUnauthorized) log.Println(err) return } + fmt.Println("authenticated?") - // TODO authenticate user - switch tmsg.Type { case mailmux.Tnew: srv.newAlias(w, tmsg) @@ -139,12 +140,11 @@ func main() { } srv := &server{} - db, err := mailmux.OpenUserDB(path.Join(cdir, "/mailmux/db"), ticketDir) + srv.users, err = mailmux.OpenUserDB(path.Join(cdir, "/mailmux/db"), ticketDir) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - srv.users = db srv.aliaspath = "/tmp/aliases" srv.aliases, err = aliases.Load(srv.aliaspath) blob - 26966c2e5dd6cb30b90898b3dedd30a52773a6a6 blob + 2050aa72e2ed4a6392495d6fed4365660326a41c --- cmd/mailmux/mailmux_test.go +++ cmd/mailmux/mailmux_test.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "net" "net/http" "os" @@ -67,15 +66,13 @@ func TestBadRegistration(t *testing.T) { func TestAliases(t *testing.T) { client := newTestServerClient(t) for i := 0; i <= 100; i++ { - a, err := client.NewAlias() + _, err := client.NewAlias() if err != nil { t.Fatal(err) } - fmt.Println(a) } - a, err := client.Aliases() + _, err := client.Aliases() if err != nil { t.Fatal(err) } - fmt.Println(a) } blob - f147c4457cb40a47a4144343f9916eac478b7d86 blob + 4e046afa5926a6d65019874e82b357817e575f3d --- mcall.go +++ mcall.go @@ -20,10 +20,11 @@ const ( Tremove ) -// An Mcall is a message passed between mailmux client and servers. +// Mcall represents a message passed between mailmux clients and servers. // Operations requested by clients are T-messages (such as Tregister). -// Servers respond with the corresponding R-message (such as Rregister) or Rerror to inform the client, -// with a diagnostic message, that the request was not completed successfully. +// Servers respond with the corresponding R-message (such as Rregister). +// Servers may instead respond with Rerror to inform the client, with a diagnostic message, +// that the request was not completed successfully. // This design is loosely based on the Plan 9 network file protocol 9P. type Mcall struct { Type uint @@ -34,7 +35,7 @@ type Mcall struct { Expiry time.Time // Tnew, Rnew } -// ParseMcall parses and validates one Mcall from r. +// ParseMcall parses and validates a JSON-encoded Mcall from r. func ParseMcall(r io.Reader) (*Mcall, error) { var mc Mcall if err := json.NewDecoder(r).Decode(&mc); err != nil { blob - 8b4fa6214d6f6971d2f7bf401433e257f6da7426 blob + 306664ff760c64461dba0958bc9f8ae5d4ff2b58 --- user.go +++ user.go @@ -22,7 +22,9 @@ type UserStore interface { Lookup(name string) (User, error) // Delete removes the user from the store. Delete(name string) error - //Authenticate(username, password string) (ticket string, err error) + // Authenticate authenticates the user identified by username with pw. + // error is non-nil if the password is incorrect or on any other error. + Authenticate(username string, pw Password) error } type ticket []byte @@ -30,7 +32,7 @@ type ticket []byte var testTicket = []byte(`TEST NOT REAL`) var ( - ErrUserExist = errors.New("user already exists") + ErrUserExist = errors.New("user already exists") ErrUnknownUser = errors.New("unknown user") )