git-svn-id: svn://losandesgames.com/alfheim-website@49 15359d88-9307-4e75-a9c1-e5686e5897df

This commit is contained in:
vfs committed 2025-10-03 23:10:29 +00:00
1 parent afb402223f
commit 391becf14e
6 files changed
+321 -299

No files matched your search

+13 -13
View File
@@ -42,7 +42,7 @@ func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func authenticated_user(w http.ResponseWriter, r *http.Request) int32 {
func authenticatedUser(w http.ResponseWriter, r *http.Request) int32 {
session, err := store.Get(r, "id")
if err != nil {
log.Println(err)
@@ -79,7 +79,7 @@ func home(w http.ResponseWriter, r *http.Request) {
return
}
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, _ := users.GetAccount(id)
active_subscription := subscriptions.HasActiveSubscription(id)
@@ -143,7 +143,7 @@ func login(w http.ResponseWriter, r *http.Request) {
if len(errors) > 0 {
err := text.Execute(w, TemplateData{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors})
err := text.Execute(w, TemplateData{AuthenticatedUser: authenticatedUser(w, r), FormErrors: errors})
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", 500)
@@ -155,7 +155,7 @@ func login(w http.ResponseWriter, r *http.Request) {
id, err := users.Authenticate(username, password)
if err == models.ErrInvalidCredentials {
errors["generic"] = "Email or Password is incorrect"
err := text.Execute(w, TemplateData{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors})
err := text.Execute(w, TemplateData{AuthenticatedUser: authenticatedUser(w, r), FormErrors: errors})
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", 500)
@@ -180,7 +180,7 @@ func logout(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, err := users.GetAccount(id)
if err != nil {
@@ -215,7 +215,7 @@ func register(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, _ := users.GetAccount(id)
// if err != nil {
@@ -251,7 +251,7 @@ func register(w http.ResponseWriter, r *http.Request) {
if len(errors) > 0 {
err := text.Execute(w, TemplateData{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors})
err := text.Execute(w, TemplateData{AuthenticatedUser: authenticatedUser(w, r), FormErrors: errors})
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", 500)
@@ -281,7 +281,7 @@ func account(w http.ResponseWriter, r *http.Request) {
// log.Fatal(err)
//}
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, err := users.GetAccount(id)
if err != nil {
@@ -314,9 +314,9 @@ func account(w http.ResponseWriter, r *http.Request) {
// }
}
func deleteaccount(w http.ResponseWriter, r *http.Request) {
func deleteAccount(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
switch r.Method {
case http.MethodPost:
@@ -337,7 +337,7 @@ func deleteaccount(w http.ResponseWriter, r *http.Request) {
}
func subscribe(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, err := users.GetAccount(id)
if err != nil {
@@ -378,7 +378,7 @@ func subscribe(w http.ResponseWriter, r *http.Request) {
}
func subscribe_stripe(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, err := users.GetAccount(id)
if err != nil {
@@ -413,7 +413,7 @@ func subscribe_stripe(w http.ResponseWriter, r *http.Request) {
}
func managebilling(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r)
id := authenticatedUser(w, r)
account, err := users.GetAccount(id)
if err != nil {
log.Println(err)
+13 -13
View File
@@ -17,11 +17,11 @@ import (
_ "github.com/lib/pq"
"github.com/stripe/stripe-go/v78"
"alfheimgame.com/alfheim/pkg/models"
"alfheimgame.com/alfheim/pkg/models/postgresql"
)
var users *models.Usermodel
var subscriptions *models.SubscriptionModel
var users *postgresql.AccountModel
var subscriptions *postgresql.SubscriptionModel
var key = []byte("super-secret-key")
var store = sessions.NewCookieStore(key)
@@ -32,7 +32,7 @@ var version string
func main() {
addr := flag.String("addr", "127.0.0.1:8080", "HTTP network addr")
prodaddr := flag.String("prodaddr", "127.0.0.1:4000", "HTTP network addr")
prodAddr := flag.String("prodaddr", "127.0.0.1:4000", "HTTP network addr")
//prodaddr := flag.String("prodaddr", "45.76.84.7:443", "HTTP network addr")
production := flag.Bool("production", false, "Whether to use production port and TLS")
@@ -43,7 +43,7 @@ func main() {
flag.Parse()
if *displayVersion {
fmt.Println("Version: %s", version)
fmt.Printf("Version: %s\n", version)
return
}
@@ -59,8 +59,8 @@ func main() {
}
defer db.Close()
users = &models.Usermodel{db}
subscriptions = &models.SubscriptionModel{db}
users = &postgresql.AccountModel{DB: db}
subscriptions = &postgresql.SubscriptionModel{DB: db}
mux := http.NewServeMux()
@@ -96,10 +96,10 @@ func main() {
mux.HandleFunc("/login", login)
mux.HandleFunc("/logout", logout)
mux.HandleFunc("/register", register)
mux.HandleFunc("/account", require_authenticated_user(account))
mux.HandleFunc("/deleteaccount", require_authenticated_user(deleteaccount))
mux.HandleFunc("/subscribe", require_authenticated_user(subscribe_stripe))
mux.HandleFunc("/managebilling", require_authenticated_user(managebilling))
mux.HandleFunc("/account", requireAuthenticatedUser(account))
mux.HandleFunc("/deleteaccount", requireAuthenticatedUser(deleteAccount))
mux.HandleFunc("/subscribe", requireAuthenticatedUser(subscribe_stripe))
mux.HandleFunc("/managebilling", requireAuthenticatedUser(managebilling))
mux.HandleFunc("/webhook", webhooks)
if *production {
@@ -120,8 +120,8 @@ func main() {
// }
// log.Fatal(server.ListenAndServeTLS("", ""))
log.Fatal(http.ListenAndServe(*prodaddr, log_request(secure_headers(mux))))
log.Fatal(http.ListenAndServe(*prodAddr, logRequest(secureHeaders(mux))))
} else {
log.Fatal(http.ListenAndServe(*addr, log_request(secure_headers(mux))))
log.Fatal(http.ListenAndServe(*addr, logRequest(secureHeaders(mux))))
}
}
+4 -4
View File
@@ -5,7 +5,7 @@ import (
"net/http"
)
func secure_headers(next http.Handler) http.Handler {
func secureHeaders(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("X-Frame-Options", "deny")
@@ -16,7 +16,7 @@ func secure_headers(next http.Handler) http.Handler {
return http.HandlerFunc(fn)
}
func log_request(next http.Handler) http.Handler {
func logRequest(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL)
@@ -26,12 +26,12 @@ func log_request(next http.Handler) http.Handler {
return http.HandlerFunc(fn)
}
func require_authenticated_user(next http.HandlerFunc) http.HandlerFunc {
func requireAuthenticatedUser(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// If the user is not authenticated, redirect them to the login page and
// return from the middleware chain so that no subsequent handlers in
// the chain are executed.
if authenticated_user(w, r) == 0 {
if authenticatedUser(w, r) == 0 {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}