This commit is contained in:
Stavros
2025-04-03 15:44:47 +03:00
parent e43bd651b6
commit 8bf5a6067e
11 changed files with 266 additions and 211 deletions

View File

@@ -11,23 +11,21 @@ import (
"tinyauth/internal/handlers"
"tinyauth/internal/types"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
func NewAPI(config types.APIConfig, handlers *handlers.Handlers) *API {
return &API{
Config: config,
Handlers: handlers,
Config: config,
}
}
type API struct {
Config types.APIConfig
Router *gin.Engine
Handlers *handlers.Handlers
Config types.APIConfig
}
func (api *API) Init() {
@@ -51,21 +49,6 @@ func (api *API) Init() {
log.Debug().Msg("Setting up file server")
fileServer := http.FileServer(http.FS(dist))
// Setup cookie store
log.Debug().Msg("Setting up cookie store")
store := cookie.NewStore([]byte(api.Config.Secret))
// Use session middleware
store.Options(sessions.Options{
Domain: api.Config.Domain,
Path: "/",
HttpOnly: true,
Secure: api.Config.CookieSecure,
MaxAge: api.Config.SessionExpiry,
})
router.Use(sessions.Sessions("tinyauth", store))
// UI middleware
router.Use(func(c *gin.Context) {
// If not an API request, serve the UI

View File

@@ -19,13 +19,16 @@ import (
"github.com/magiconair/properties/assert"
)
// User
var User = types.User{
Username: "user",
Password: "$2a$10$AvGHLTYv3xiRJ0xV9xs3XeVIlkGTygI9nqIamFYB5Xu.5.0UWF7B6", // pass
}
// Simple API config for tests
var apiConfig = types.APIConfig{
Port: 8080,
Address: "0.0.0.0",
Secret: "super-secret-api-thing-for-tests", // It is 32 chars long
CookieSecure: false,
SessionExpiry: 3600,
Port: 8080,
Address: "0.0.0.0",
}
// Simple handlers config for tests
@@ -38,15 +41,21 @@ var handlersConfig = types.HandlersConfig{
GenericName: "Generic",
}
// Simple auth config for tests
var authConfig = types.AuthConfig{
Domain: "localhost",
Secret: "super-secret-api-thing-for-tests", // It is 32 chars long
CookieSecure: false,
SessionExpiry: 3600,
Users: types.Users{
User,
},
OAuthWhitelist: []string{},
}
// Cookie
var cookie string
// User
var user = types.User{
Username: "user",
Password: "$2a$10$AvGHLTYv3xiRJ0xV9xs3XeVIlkGTygI9nqIamFYB5Xu.5.0UWF7B6", // pass
}
// We need all this to be able to test the API
func getAPI(t *testing.T) *api.API {
// Create docker service
@@ -61,12 +70,7 @@ func getAPI(t *testing.T) *api.API {
}
// Create auth service
auth := auth.NewAuth(docker, types.Users{
{
Username: user.Username,
Password: user.Password,
},
}, nil, apiConfig.SessionExpiry)
auth := auth.NewAuth(authConfig, docker)
// Create providers service
providers := providers.NewProviders(types.OAuthConfig{})