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 "os"
6 5e70998a 2022-04-12 o "path"
7 5e70998a 2022-04-12 o )
8 5e70998a 2022-04-12 o
9 5e70998a 2022-04-12 o type User struct {
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 5e70998a 2022-04-12 o //Authenticate(username, password string) (ticket string, err error)
26 5e70998a 2022-04-12 o }
27 5e70998a 2022-04-12 o
28 5e70998a 2022-04-12 o type ticket []byte
29 5e70998a 2022-04-12 o
30 5e70998a 2022-04-12 o var testTicket = []byte(`TEST NOT REAL`)
31 5e70998a 2022-04-12 o
32 5e70998a 2022-04-12 o var (
33 5e70998a 2022-04-12 o ErrUserExist = errors.New("user already exists")
34 5e70998a 2022-04-12 o ErrUnknownUser = errors.New("unknown user")
35 5e70998a 2022-04-12 o )
36 5e70998a 2022-04-12 o
37 5e70998a 2022-04-12 o // verified checks whether ownership of username's address has
38 5e70998a 2022-04-12 o // been confirmed.
39 5e70998a 2022-04-12 o // func (store *astore) verified(username string) (bool, error) {
40 5e70998a 2022-04-12 o // _, err := store.Lookup(username)
41 5e70998a 2022-04-12 o // if errors.Is(err, ErrUnknownUser) {
42 5e70998a 2022-04-12 o // return false, err
43 5e70998a 2022-04-12 o // }
44 5e70998a 2022-04-12 o //
45 5e70998a 2022-04-12 o // _, err = os.Stat(path.Join(store.confirmDir, username))
46 5e70998a 2022-04-12 o // if err == nil {
47 5e70998a 2022-04-12 o // return false, nil
48 5e70998a 2022-04-12 o // }
49 5e70998a 2022-04-12 o // if errors.Is(err, fs.ErrNotExist) {
50 5e70998a 2022-04-12 o // return true, nil
51 5e70998a 2022-04-12 o // }
52 5e70998a 2022-04-12 o // return false, fmt.Errorf("check confirm file: %w", err)
53 5e70998a 2022-04-12 o // }
54 5e70998a 2022-04-12 o
55 5e70998a 2022-04-12 o // verify attempts to verify the username using the given token.
56 5e70998a 2022-04-12 o // func (store *astore) verify(username string, tok token) error {
57 5e70998a 2022-04-12 o // f, err := os.Open(path.Join(store.confirmDir, username))
58 5e70998a 2022-04-12 o // if errors.Is(err, fs.ErrNotExist) {
59 5e70998a 2022-04-12 o // return ErrUnknownUser
60 5e70998a 2022-04-12 o // } else if err != nil {
61 5e70998a 2022-04-12 o // return err
62 5e70998a 2022-04-12 o // }
63 5e70998a 2022-04-12 o // defer f.Close()
64 5e70998a 2022-04-12 o // b, err := io.ReadAll(f)
65 5e70998a 2022-04-12 o // if err != nil {
66 5e70998a 2022-04-12 o // return err
67 5e70998a 2022-04-12 o // }
68 5e70998a 2022-04-12 o // if bytes.Equal(b, []byte(tok)) {
69 5e70998a 2022-04-12 o // return os.Remove(path.Join(store.confirmDir, username))
70 5e70998a 2022-04-12 o // }
71 5e70998a 2022-04-12 o // return errors.New("mismatched token")
72 5e70998a 2022-04-12 o // }
73 5e70998a 2022-04-12 o
74 5e70998a 2022-04-12 o func SendConfirmationMail(from, to string, tik ticket) error {
75 5e70998a 2022-04-12 o return nil
76 5e70998a 2022-04-12 o // From: $from
77 5e70998a 2022-04-12 o // To: $to
78 5e70998a 2022-04-12 o // Subject: Confirm address ownership
79 5e70998a 2022-04-12 o //
80 5e70998a 2022-04-12 o // Thanks for signing up to mailmux.net!
81 5e70998a 2022-04-12 o // Confirm ownership of $address by accessing the following link:
82 5e70998a 2022-04-12 o // https://mailmux.net/confirm?username=test@example.com&token=abcd12345
83 5e70998a 2022-04-12 o // Alternatively, go to https://mailmux.net/confirm
84 5e70998a 2022-04-12 o // enter your email address and your token:
85 5e70998a 2022-04-12 o // abcd12345
86 5e70998a 2022-04-12 o //
87 5e70998a 2022-04-12 o // If you have not signed up for mailmux.net, please
88 5e70998a 2022-04-12 o // let us know by forwarding this mail to abuse@mailmux.net.
89 5e70998a 2022-04-12 o // Thank you for helping us fight spam!
90 5e70998a 2022-04-12 o }