2022-08-10 05:10:47 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
2022-08-10 05:10:47 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/log"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/models"
|
2022-08-14 21:40:57 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/ratelimit"
|
2022-08-10 05:10:47 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/session"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Login controller.
|
|
|
|
func Login() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("account/login.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// Posting?
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
var (
|
|
|
|
// Collect form fields.
|
|
|
|
username = strings.ToLower(r.PostFormValue("username"))
|
|
|
|
password = r.PostFormValue("password")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Look up their account.
|
|
|
|
user, err := models.FindUser(username)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Incorrect username or password.")
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// Rate limit failed login attempts.
|
|
|
|
limiter := &ratelimit.Limiter{
|
|
|
|
Namespace: "login",
|
|
|
|
ID: user.ID,
|
|
|
|
Limit: config.LoginRateLimit,
|
|
|
|
Window: config.LoginRateLimitWindow,
|
|
|
|
CooldownAt: config.LoginRateLimitCooldownAt,
|
|
|
|
Cooldown: config.LoginRateLimitCooldown,
|
|
|
|
}
|
2022-08-10 05:10:47 +00:00
|
|
|
|
|
|
|
// Verify password.
|
|
|
|
if err := user.CheckPassword(password); err != nil {
|
2022-08-14 21:40:57 +00:00
|
|
|
if err := limiter.Ping(); err != nil {
|
|
|
|
session.FlashError(w, r, err.Error())
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
session.FlashError(w, r, "Incorrect username or password.")
|
|
|
|
templates.Redirect(w, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK. Log in the user's session.
|
|
|
|
session.LoginUser(w, r, user)
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// Clear their rate limiter.
|
|
|
|
if err := limiter.Clear(); err != nil {
|
|
|
|
log.Error("Failed to clear login rate limiter: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// Redirect to their dashboard.
|
|
|
|
session.Flash(w, r, "Login successful.")
|
|
|
|
templates.Redirect(w, "/me")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, nil); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout controller.
|
|
|
|
func Logout() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session.Flash(w, r, "You have been successfully logged out.")
|
|
|
|
session.LogoutUser(w, r)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
})
|
|
|
|
}
|