Compare commits

..

16 Commits

Author SHA1 Message Date
Stavros
0a7e259d02 fix: review comments 2025-12-22 22:11:17 +02:00
Stavros
7c5fa117fb chore: go mod tidy 2025-12-22 22:08:39 +02:00
Stavros
d8b8be0100 Merge branch 'main' of https://github.com/steveiliop56/tinyauth into feat/unified-config 2025-12-22 22:08:24 +02:00
Stavros
db4ed949e1 chore: resolve go mod and sum conflicts 2025-12-22 22:03:40 +02:00
Stavros
5cfe2babc4 chore: add quotes to all env variables 2025-12-22 22:01:09 +02:00
Stavros
ed28e7a218 refactor: move tinyauth to separate package 2025-12-21 17:37:34 +02:00
Stavros
7db81121e1 fix: review comments 2025-12-21 17:25:04 +02:00
Stavros
195b70b4d7 chore: mod tidy 2025-12-21 11:23:00 +02:00
Stavros
c4529be557 feat: add experimental config file support 2025-12-21 11:21:11 +02:00
Stavros
0374370b0c fix: fix translations not loading 2025-12-17 23:36:01 +02:00
Stavros
7857dba57a chore: remove unused code 2025-12-17 23:31:24 +02:00
Stavros
3e12721844 refactor: update build 2025-12-17 23:21:15 +02:00
Stavros
9c7a4af295 chore: update example env 2025-12-17 19:42:26 +02:00
Stavros
dba5580a7c refactor: remove dependency on traefik 2025-12-17 18:30:43 +02:00
Stavros
e4e99f4805 feat: add initial implementation of a traefik like cli 2025-12-17 16:40:54 +02:00
Stavros
3555569a97 chore: add yaml config ref 2025-12-17 15:17:55 +02:00
5 changed files with 9 additions and 61 deletions

5
.gitignore vendored
View File

@@ -33,7 +33,4 @@
# binary out
/tinyauth.db
/resources
# debug files
__debug_*
/resources

View File

@@ -3,7 +3,6 @@ package controller
import (
"fmt"
"net/http"
"slices"
"strings"
"tinyauth/internal/config"
"tinyauth/internal/service"
@@ -14,8 +13,6 @@ import (
"github.com/rs/zerolog/log"
)
var SupportedProxies = []string{"nginx", "traefik", "caddy", "envoy"}
type Proxy struct {
Proxy string `uri:"proxy" binding:"required"`
}
@@ -43,7 +40,6 @@ func NewProxyController(config ProxyControllerConfig, router *gin.RouterGroup, a
func (controller *ProxyController) SetupRoutes() {
proxyGroup := controller.router.Group("/auth")
proxyGroup.GET("/:proxy", controller.proxyHandler)
proxyGroup.POST("/:proxy", controller.proxyHandler)
}
func (controller *ProxyController) proxyHandler(c *gin.Context) {
@@ -59,7 +55,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
return
}
if !slices.Contains(SupportedProxies, req.Proxy) {
if req.Proxy != "nginx" && req.Proxy != "traefik" && req.Proxy != "caddy" {
log.Warn().Str("proxy", req.Proxy).Msg("Invalid proxy")
c.JSON(400, gin.H{
"status": 400,

View File

@@ -92,18 +92,6 @@ func TestProxyHandler(t *testing.T) {
assert.Equal(t, 307, recorder.Code)
assert.Equal(t, "http://localhost:8080/login?redirect_uri=https%3A%2F%2Fexample.com%2Fsomepath", recorder.Header().Get("Location"))
// Test logged out user (envoy)
recorder = httptest.NewRecorder()
req = httptest.NewRequest("POST", "/api/auth/envoy", nil)
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("X-Forwarded-Host", "example.com")
req.Header.Set("X-Forwarded-Uri", "/somepath")
req.Header.Set("Accept", "text/html")
router.ServeHTTP(recorder, req)
assert.Equal(t, 307, recorder.Code)
assert.Equal(t, "http://localhost:8080/login?redirect_uri=https%3A%2F%2Fexample.com%2Fsomepath", recorder.Header().Get("Location"))
// Test logged out user (nginx)
recorder = httptest.NewRecorder()
req = httptest.NewRequest("GET", "/api/auth/nginx", nil)

View File

@@ -65,7 +65,6 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
goto basic
}
m.auth.RefreshSessionCookie(c)
c.Set("context", &config.UserContext{
Username: cookie.Username,
Name: cookie.Name,
@@ -90,7 +89,6 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
goto basic
}
m.auth.RefreshSessionCookie(c)
c.Set("context", &config.UserContext{
Username: cookie.Username,
Name: cookie.Name,

View File

@@ -1,6 +1,7 @@
package service
import (
"context"
"errors"
"fmt"
"regexp"
@@ -42,6 +43,7 @@ type AuthService struct {
loginMutex sync.RWMutex
ldap *LdapService
database *gorm.DB
ctx context.Context
}
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService {
@@ -55,6 +57,7 @@ func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapS
}
func (auth *AuthService) Init() error {
auth.ctx = context.Background()
return nil
}
@@ -214,7 +217,7 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio
OAuthName: data.OAuthName,
}
err = gorm.G[model.Session](auth.database).Create(c, &session)
err = gorm.G[model.Session](auth.database).Create(auth.ctx, &session)
if err != nil {
return err
@@ -225,40 +228,6 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio
return nil
}
func (auth *AuthService) RefreshSessionCookie(c *gin.Context) error {
cookie, err := c.Cookie(auth.config.SessionCookieName)
if err != nil {
return err
}
session, err := gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).First(c)
if err != nil {
return err
}
currentTime := time.Now().Unix()
if session.Expiry-currentTime > int64(time.Hour.Seconds()) {
return nil
}
newExpiry := currentTime + int64(time.Hour.Seconds())
_, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Updates(c, model.Session{
Expiry: newExpiry,
})
if err != nil {
return err
}
c.SetCookie(auth.config.SessionCookieName, cookie, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", auth.config.CookieDomain), auth.config.SecureCookie, true)
return nil
}
func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
cookie, err := c.Cookie(auth.config.SessionCookieName)
@@ -266,7 +235,7 @@ func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
return err
}
_, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Delete(c)
_, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Delete(auth.ctx)
if err != nil {
return err
@@ -284,7 +253,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie,
return config.SessionCookie{}, err
}
session, err := gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).First(c)
session, err := gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).First(auth.ctx)
if err != nil {
return config.SessionCookie{}, err
@@ -297,7 +266,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie,
currentTime := time.Now().Unix()
if currentTime > session.Expiry {
_, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Delete(c)
_, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Delete(auth.ctx)
if err != nil {
log.Error().Err(err).Msg("Failed to delete expired session")
}