~evanj/cms

ref: 10de0b32f424dd6ae362ed23a39b9e40dfc3cfa9 cms/internal/c/user/user.go -rw-r--r-- 3.3 KiB
10de0b32Evan M Jones Fix(rate limiting): Refactor file upload access to RL. 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package user

import (
	"errors"
	"log"
	"net/http"
	"net/url"
	"strconv"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/tier"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/v"
)

var (
	indexHTML   = v.MustParse("html/index.html")
	ErrNoSignup = errors.New("signups are forbidden at this time")
	ErrNoTier   = errors.New("invalid tier")
)

type User struct {
	*c.Controller
	log           *log.Logger
	db            dber
	signupEnabled bool
	stripe        Striper
}

type dber interface {
	UserNew(username, password, verifyPassword string) (user.User, error)
	UserGet(username, password string) (user.User, error)
	UserGetFromToken(token string) (user.User, error)
	SpacesPerUser(user user.User, before int) (space.SpaceList, error)
}

type Striper interface {
	StartCheckout(user user.User, t tier.Tier) (string, string, error)
}

func New(c *c.Controller, log *log.Logger, db dber, signupEnabled bool, s Striper) *User {
	return &User{
		c,
		log,
		db,
		signupEnabled,
		s,
	}
}

func (l *User) logout(w http.ResponseWriter, r *http.Request) {
	l.SetCookieUser(w, r, nil)
	l.Redirect(w, r, "/")
}

func (l *User) login(w http.ResponseWriter, r *http.Request) {
	username := r.FormValue("username")
	password := r.FormValue("password")

	user, err := l.db.UserGet(username, password)
	if err != nil {
		l.Error2(w, r, http.StatusBadRequest, err)
		return
	}

	l.SetCookieUser(w, r, user)
	l.Redirect(w, r, "/")
}

func (l *User) signup(w http.ResponseWriter, r *http.Request) {
	if !l.signupEnabled {
		l.Error2(w, r, http.StatusForbidden, ErrNoSignup)
		return
	}

	username := r.FormValue("username")
	password := r.FormValue("password")
	verify := r.FormValue("verify")
	t, ok := tier.ByName(r.FormValue("tier"))
	if !ok {
		l.Error2(w, r, http.StatusBadRequest, ErrNoTier)
		return
	}

	user, err := l.db.UserNew(username, password, verify)
	if err != nil {
		l.Error2(w, r, http.StatusBadRequest, err)
		return
	}
	l.SetCookieUser(w, r, user)

	if t.Is(tier.Free) {
		l.Redirect(w, r, "/")
		return
	}

	stripeCheckoutSessionID, stripePK, err := l.stripe.StartCheckout(user, t)
	if err != nil {
		l.Error2(w, r, http.StatusInternalServerError, err)
		return
	}

	form := url.Values{
		"StripeCheckoutSessionID": {stripeCheckoutSessionID},
		"StripePK":                {stripePK},
	}

	l.Redirect(w, r, "/page/stripe?"+form.Encode())
}

func (l *User) home(w http.ResponseWriter, r *http.Request) {
	user, err := l.GetCookieUser(w, r)
	if err != nil {
		l.HTML(w, r, indexHTML, map[string]interface{}{
			"Tiers": tier.Tiers,
		})
		return
	}

	// Don't care about the error value here. When error occurs before is zero
	// value.
	before, _ := strconv.Atoi(r.URL.Query().Get("before"))

	spaces, err := l.db.SpacesPerUser(user, before)
	if err != nil {
		l.Error(w, r, http.StatusInternalServerError, "failed to find spaces for user")
		return
	}

	l.HTML(w, r, indexHTML, map[string]interface{}{
		"Tiers":  tier.Tiers,
		"User":   user,
		"Spaces": spaces,
	})
}

func (l *User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	switch r.URL.Path {
	case "/":
		l.home(w, r)
		return
	case "/user/login":
		l.login(w, r)
		return
	case "/user/logout":
		l.logout(w, r)
		return
	case "/user/signup":
		l.signup(w, r)
		return
	}

	http.NotFound(w, r)
}