Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Replace awkward auth user object access in handlers with an explicit func.

Kailash Nadhfcf2449a7059
diff --git a/cmd/campaigns.go b/cmd/campaigns.go
index ffee30b0..1342dcc4 100644
--- a/cmd/campaigns.go
+++ b/cmd/campaigns.go
@@ -54,7 +54,7 @@ var (
func handleGetCampaigns(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
pg = app.paginator.NewFromURL(c.Request().URL.Query())
status = c.QueryParams()["status"]
@@ -710,7 +710,7 @@ func makeOptinCampaignMessage(o campaignReq, app *App) (campaignReq, error) {
func checkCampaignPerm(types auth.PermType, id int, c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
)
perm := auth.PermCampaignsGet
diff --git a/cmd/handlers.go b/cmd/handlers.go
index 548f5617..cb4ecd00 100644
--- a/cmd/handlers.go
+++ b/cmd/handlers.go
@@ -43,7 +43,7 @@ func initHTTPHandlers(e *echo.Echo, app *App) {
// Authenticated /api/* handlers.
api = e.Group("", app.auth.Middleware, func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
- u := c.Get(auth.UserKey)
+ u := c.Get(auth.UserHTTPCtxKey)
// On no-auth, respond with a JSON error.
if err, ok := u.(*echo.HTTPError); ok {
@@ -57,7 +57,8 @@ func initHTTPHandlers(e *echo.Echo, app *App) {
// Authenticated non /api handlers.
a = e.Group("", app.auth.Middleware, func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
- u := c.Get(auth.UserKey)
+ u := c.Get(auth.UserHTTPCtxKey)
+
// On no-auth, redirect to login page
if _, ok := u.(*echo.HTTPError); ok {
u, _ := url.Parse(app.constants.LoginURL)
diff --git a/cmd/lists.go b/cmd/lists.go
index aca7c59e..10b6101e 100644
--- a/cmd/lists.go
+++ b/cmd/lists.go
@@ -14,7 +14,7 @@ import (
func handleGetLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
pg = app.paginator.NewFromURL(c.Request().URL.Query())
query = strings.TrimSpace(c.FormValue("query"))
@@ -70,7 +70,7 @@ func handleGetLists(c echo.Context) error {
func handleGetList(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
id, _ = strconv.Atoi(c.Param("id"))
)
@@ -122,7 +122,7 @@ func handleCreateList(c echo.Context) error {
func handleUpdateList(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
id, _ = strconv.Atoi(c.Param("id"))
)
@@ -160,7 +160,7 @@ func handleUpdateList(c echo.Context) error {
func handleDeleteLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
ids []int
diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index a26dd2dc..928b4a8e 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -56,7 +56,7 @@ func handleGetSubscriber(c echo.Context) error {
var (
app = c.Get("app").(*App)
id, _ = strconv.Atoi(c.Param("id"))
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
)
if id < 1 {
@@ -81,7 +81,7 @@ func handleGetSubscriber(c echo.Context) error {
func handleQuerySubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
pg = app.paginator.NewFromURL(c.Request().URL.Query())
// The "WHERE ?" bit.
@@ -116,7 +116,7 @@ func handleQuerySubscribers(c echo.Context) error {
func handleExportSubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
// The "WHERE ?" bit.
query = sanitizeSQLExp(c.FormValue("query"))
@@ -185,7 +185,7 @@ loop:
func handleCreateSubscriber(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
req subimporter.SubReq
)
@@ -217,7 +217,7 @@ func handleCreateSubscriber(c echo.Context) error {
func handleUpdateSubscriber(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
id, _ = strconv.Atoi(c.Param("id"))
req struct {
@@ -326,7 +326,7 @@ func handleBlocklistSubscribers(c echo.Context) error {
func handleManageSubscriberLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
pID = c.Param("id")
subIDs []int
@@ -468,7 +468,7 @@ func handleBlocklistSubscribersByQuery(c echo.Context) error {
func handleManageSubscriberListsByQuery(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
req subQueryReq
)
diff --git a/cmd/users.go b/cmd/users.go
index aa4a8dc1..22bca298 100644
--- a/cmd/users.go
+++ b/cmd/users.go
@@ -223,7 +223,7 @@ func handleDeleteUsers(c echo.Context) error {
// handleGetUserProfile fetches the uesr profile for the currently logged in user.
func handleGetUserProfile(c echo.Context) error {
var (
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
)
user.Password.String = ""
user.Password.Valid = false
@@ -235,7 +235,7 @@ func handleGetUserProfile(c echo.Context) error {
func handleUpdateUserProfile(c echo.Context) error {
var (
app = c.Get("app").(*App)
- user = c.Get(auth.UserKey).(auth.User)
+ user = auth.GetUser(c)
)
u := auth.User{}
diff --git a/internal/auth/auth.go b/internal/auth/auth.go
index 154ffd4c..c82d0bdc 100644
--- a/internal/auth/auth.go
+++ b/internal/auth/auth.go
@@ -235,31 +235,31 @@ func (o *Auth) Middleware(next echo.HandlerFunc) echo.HandlerFunc {
if len(hdr) > 0 {
key, token, err := parseAuthHeader(hdr)
if err != nil {
- c.Set(UserKey, echo.NewHTTPError(http.StatusForbidden, err.Error()))
+ c.Set(UserHTTPCtxKey, echo.NewHTTPError(http.StatusForbidden, err.Error()))
return next(c)
}
// Validate the token.
user, ok := o.GetAPIToken(key, token)
if !ok {
- c.Set(UserKey, echo.NewHTTPError(http.StatusForbidden, "invalid API credentials"))
+ c.Set(UserHTTPCtxKey, echo.NewHTTPError(http.StatusForbidden, "invalid API credentials"))
return next(c)
}
// Set the user details on the handler context.
- c.Set(UserKey, user)
+ c.Set(UserHTTPCtxKey, user)
return next(c)
}
// Is it a cookie based session?
sess, user, err := o.validateSession(c)
if err != nil {
- c.Set(UserKey, echo.NewHTTPError(http.StatusForbidden, "invalid session"))
+ c.Set(UserHTTPCtxKey, echo.NewHTTPError(http.StatusForbidden, "invalid session"))
return next(c)
}
// Set the user details on the handler context.
- c.Set(UserKey, user)
+ c.Set(UserHTTPCtxKey, user)
c.Set(SessionKey, sess)
return next(c)
}
@@ -267,9 +267,9 @@ func (o *Auth) Middleware(next echo.HandlerFunc) echo.HandlerFunc {
func (o *Auth) Perm(next echo.HandlerFunc, perms ...string) echo.HandlerFunc {
return func(c echo.Context) error {
- u, ok := c.Get(UserKey).(User)
+ u, ok := c.Get(UserHTTPCtxKey).(User)
if !ok {
- c.Set(UserKey, echo.NewHTTPError(http.StatusForbidden, "invalid session"))
+ c.Set(UserHTTPCtxKey, echo.NewHTTPError(http.StatusForbidden, "invalid session"))
return next(c)
}
@@ -343,6 +343,12 @@ func (o *Auth) validateSession(c echo.Context) (*simplesessions.Session, User, e
return sess, user, err
}
+// GetUser retrieves and returns the User object from an authenticated
+// HTTP handler request.
+func GetUser(c echo.Context) User {
+ return c.Get(UserHTTPCtxKey).(User)
+}
+
// parseAuthHeader parses the Authorization header and returns the api_key and access_token.
func parseAuthHeader(h string) (string, string, error) {
const authBasic = "Basic"
diff --git a/internal/auth/models.go b/internal/auth/models.go
index 4276fd2d..a516a84b 100644
--- a/internal/auth/models.go
+++ b/internal/auth/models.go
@@ -20,9 +20,9 @@ const (
)
const (
- // UserKey is the key on which the User profile is set on echo handlers.
- UserKey = "auth_user"
- SessionKey = "auth_session"
+ // UserHTTPCtxKey is the key on which the User profile is set on echo handlers.
+ UserHTTPCtxKey = "auth_user"
+ SessionKey = "auth_session"
)
const (