fix: do not append domains to users that have an email as the username

This commit is contained in:
Stavros
2026-02-02 16:08:05 +02:00
parent fd16f91011
commit 51d95fa455
5 changed files with 27 additions and 9 deletions

View File

@@ -160,7 +160,7 @@ code {
}
pre {
@apply bg-accent border border-border rounded-md p-2;
@apply bg-accent border border-border rounded-md p-2 whitespace-break-spaces;
}
.lead {

View File

@@ -2,7 +2,6 @@ package controller
import (
"fmt"
"strings"
"time"
"github.com/steveiliop56/tinyauth/internal/repository"
@@ -114,8 +113,8 @@ func (controller *UserController) loginHandler(c *gin.Context) {
err := controller.auth.CreateSessionCookie(c, &repository.Session{
Username: user.Username,
Name: utils.Capitalize(req.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(req.Username), controller.config.CookieDomain),
Name: utils.Capitalize(user.Username),
Email: utils.CompileUserEmail(user.Username, controller.config.CookieDomain),
Provider: "local",
TotpPending: true,
})
@@ -141,7 +140,7 @@ func (controller *UserController) loginHandler(c *gin.Context) {
sessionCookie := repository.Session{
Username: req.Username,
Name: utils.Capitalize(req.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(req.Username), controller.config.CookieDomain),
Email: utils.CompileUserEmail(req.Username, controller.config.CookieDomain),
Provider: "local",
}
@@ -255,7 +254,7 @@ func (controller *UserController) totpHandler(c *gin.Context) {
sessionCookie := repository.Session{
Username: user.Username,
Name: utils.Capitalize(user.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(user.Username), controller.config.CookieDomain),
Email: utils.CompileUserEmail(user.Username, controller.config.CookieDomain),
Provider: "local",
}

View File

@@ -1,7 +1,6 @@
package middleware
import (
"fmt"
"slices"
"strings"
"time"
@@ -186,7 +185,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
c.Set("context", &config.UserContext{
Username: user.Username,
Name: utils.Capitalize(user.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(user.Username), m.config.CookieDomain),
Email: utils.CompileUserEmail(user.Username, m.config.CookieDomain),
Provider: "local",
IsLoggedIn: true,
TotpEnabled: user.TotpSecret != "",
@@ -208,7 +207,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
c.Set("context", &config.UserContext{
Username: basic.Username,
Name: utils.Capitalize(basic.Username),
Email: fmt.Sprintf("%s@%s", strings.ToLower(basic.Username), m.config.CookieDomain),
Email: utils.CompileUserEmail(basic.Username, m.config.CookieDomain),
Provider: "ldap",
IsLoggedIn: true,
LdapGroups: strings.Join(ldapUser.Groups, ","),

View File

@@ -49,3 +49,11 @@ func TestCoalesceToString(t *testing.T) {
// Test with nil input
assert.Equal(t, "", utils.CoalesceToString(nil))
}
func TestCompileUserEmail(t *testing.T) {
// Test with valid email
assert.Equal(t, "user@example.com", utils.CompileUserEmail("user@example.com", "example.com"))
// Test with invalid email
assert.Equal(t, "user@example.com", utils.CompileUserEmail("user", "example.com"))
}

View File

@@ -2,6 +2,8 @@ package utils
import (
"errors"
"fmt"
"net/mail"
"strings"
"github.com/steveiliop56/tinyauth/internal/config"
@@ -90,3 +92,13 @@ func ParseUser(userStr string) (config.User, error) {
return user, nil
}
func CompileUserEmail(username string, domain string) string {
_, err := mail.ParseAddress(username)
if err != nil {
return fmt.Sprintf("%s@%s", strings.ToLower(username), domain)
}
return username
}