Blame


1 5e70998a 2022-04-12 o package mailmux
2 5e70998a 2022-04-12 o
3 5e70998a 2022-04-12 o import (
4 5e70998a 2022-04-12 o "errors"
5 5e70998a 2022-04-12 o )
6 5e70998a 2022-04-12 o
7 7583dd64 2022-04-13 o // User represents a mailmux user.
8 5e70998a 2022-04-12 o type User struct {
9 7583dd64 2022-04-13 o // The name should be a valid email address.
10 5e70998a 2022-04-12 o name string
11 5e70998a 2022-04-12 o password Password
12 5e70998a 2022-04-12 o }
13 5e70998a 2022-04-12 o
14 5e70998a 2022-04-12 o type Password []byte
15 5e70998a 2022-04-12 o
16 5e70998a 2022-04-12 o // A UserStore provides storage and authentication of users.
17 5e70998a 2022-04-12 o type UserStore interface {
18 5e70998a 2022-04-12 o // Change creates or updates the named user with the new password.
19 5e70998a 2022-04-12 o Change(name string, new Password) error
20 5e70998a 2022-04-12 o // Lookup returns the User with name.
21 5e70998a 2022-04-12 o // The error should be ErrUnknownUser if the user is not found.
22 5e70998a 2022-04-12 o Lookup(name string) (User, error)
23 5e70998a 2022-04-12 o // Delete removes the user from the store.
24 5e70998a 2022-04-12 o Delete(name string) error
25 c7ecbae7 2022-04-13 o // Authenticate authenticates the user identified by username with pw.
26 c7ecbae7 2022-04-13 o // error is non-nil if the password is incorrect or on any other error.
27 c7ecbae7 2022-04-13 o Authenticate(username string, pw Password) error
28 5e70998a 2022-04-12 o }
29 5e70998a 2022-04-12 o
30 5e70998a 2022-04-12 o type ticket []byte
31 5e70998a 2022-04-12 o
32 5e70998a 2022-04-12 o var testTicket = []byte(`TEST NOT REAL`)
33 5e70998a 2022-04-12 o
34 5e70998a 2022-04-12 o var (
35 c7ecbae7 2022-04-13 o ErrUserExist = errors.New("user already exists")
36 5e70998a 2022-04-12 o ErrUnknownUser = errors.New("unknown user")
37 5e70998a 2022-04-12 o )
38 5e70998a 2022-04-12 o
39 5e70998a 2022-04-12 o // verified checks whether ownership of username's address has
40 5e70998a 2022-04-12 o // been confirmed.
41 5e70998a 2022-04-12 o // func (store *astore) verified(username string) (bool, error) {
42 5e70998a 2022-04-12 o // _, err := store.Lookup(username)
43 5e70998a 2022-04-12 o // if errors.Is(err, ErrUnknownUser) {
44 5e70998a 2022-04-12 o // return false, err
45 5e70998a 2022-04-12 o // }
46 5e70998a 2022-04-12 o //
47 5e70998a 2022-04-12 o // _, err = os.Stat(path.Join(store.confirmDir, username))
48 5e70998a 2022-04-12 o // if err == nil {
49 5e70998a 2022-04-12 o // return false, nil
50 5e70998a 2022-04-12 o // }
51 5e70998a 2022-04-12 o // if errors.Is(err, fs.ErrNotExist) {
52 5e70998a 2022-04-12 o // return true, nil
53 5e70998a 2022-04-12 o // }
54 5e70998a 2022-04-12 o // return false, fmt.Errorf("check confirm file: %w", err)
55 5e70998a 2022-04-12 o // }
56 5e70998a 2022-04-12 o
57 5e70998a 2022-04-12 o // verify attempts to verify the username using the given token.
58 5e70998a 2022-04-12 o // func (store *astore) verify(username string, tok token) error {
59 5e70998a 2022-04-12 o // f, err := os.Open(path.Join(store.confirmDir, username))
60 5e70998a 2022-04-12 o // if errors.Is(err, fs.ErrNotExist) {
61 5e70998a 2022-04-12 o // return ErrUnknownUser
62 5e70998a 2022-04-12 o // } else if err != nil {
63 5e70998a 2022-04-12 o // return err
64 5e70998a 2022-04-12 o // }
65 5e70998a 2022-04-12 o // defer f.Close()
66 5e70998a 2022-04-12 o // b, err := io.ReadAll(f)
67 5e70998a 2022-04-12 o // if err != nil {
68 5e70998a 2022-04-12 o // return err
69 5e70998a 2022-04-12 o // }
70 5e70998a 2022-04-12 o // if bytes.Equal(b, []byte(tok)) {
71 5e70998a 2022-04-12 o // return os.Remove(path.Join(store.confirmDir, username))
72 5e70998a 2022-04-12 o // }
73 5e70998a 2022-04-12 o // return errors.New("mismatched token")
74 5e70998a 2022-04-12 o // }
75 5e70998a 2022-04-12 o
76 5e70998a 2022-04-12 o func SendConfirmationMail(from, to string, tik ticket) error {
77 5e70998a 2022-04-12 o return nil
78 5e70998a 2022-04-12 o // From: $from
79 5e70998a 2022-04-12 o // To: $to
80 5e70998a 2022-04-12 o // Subject: Confirm address ownership
81 5e70998a 2022-04-12 o //
82 5e70998a 2022-04-12 o // Thanks for signing up to mailmux.net!
83 5e70998a 2022-04-12 o // Confirm ownership of $address by accessing the following link:
84 5e70998a 2022-04-12 o // https://mailmux.net/confirm?username=test@example.com&token=abcd12345
85 5e70998a 2022-04-12 o // Alternatively, go to https://mailmux.net/confirm
86 5e70998a 2022-04-12 o // enter your email address and your token:
87 5e70998a 2022-04-12 o // abcd12345
88 5e70998a 2022-04-12 o //
89 5e70998a 2022-04-12 o // If you have not signed up for mailmux.net, please
90 5e70998a 2022-04-12 o // let us know by forwarding this mail to abuse@mailmux.net.
91 5e70998a 2022-04-12 o // Thank you for helping us fight spam!
92 5e70998a 2022-04-12 o }