mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 20:55:42 +00:00
chore: remove meaningless comments
This commit is contained in:
@@ -22,23 +22,18 @@ type Server struct {
|
||||
}
|
||||
|
||||
func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server, error) {
|
||||
// Disable gin logs
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
// Create router and use zerolog for logs
|
||||
log.Debug().Msg("Setting up router")
|
||||
router := gin.New()
|
||||
router.Use(zerolog())
|
||||
|
||||
// Read UI assets
|
||||
log.Debug().Msg("Setting up assets")
|
||||
dist, err := fs.Sub(assets.Assets, "dist")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create file server
|
||||
log.Debug().Msg("Setting up file server")
|
||||
fileServer := http.FileServer(http.FS(dist))
|
||||
|
||||
@@ -46,18 +41,11 @@ func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server,
|
||||
router.Use(func(c *gin.Context) {
|
||||
// If not an API request, serve the UI
|
||||
if !strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||
// Check if the file exists
|
||||
_, err := fs.Stat(dist, strings.TrimPrefix(c.Request.URL.Path, "/"))
|
||||
|
||||
// If the file doesn't exist, serve the index.html
|
||||
if os.IsNotExist(err) {
|
||||
c.Request.URL.Path = "/"
|
||||
}
|
||||
|
||||
// Serve the file
|
||||
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||
|
||||
// Stop further processing
|
||||
c.Abort()
|
||||
}
|
||||
})
|
||||
@@ -81,7 +69,6 @@ func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server,
|
||||
// App routes
|
||||
router.GET("/api/healthcheck", handlers.HealthcheckHandler)
|
||||
|
||||
// Return the server
|
||||
return &Server{
|
||||
Config: config,
|
||||
Handlers: handlers,
|
||||
@@ -90,9 +77,7 @@ func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server,
|
||||
}
|
||||
|
||||
func (s *Server) Start() error {
|
||||
// Run server
|
||||
log.Info().Str("address", s.Config.Address).Int("port", s.Config.Port).Msg("Starting server")
|
||||
|
||||
return s.Router.Run(fmt.Sprintf("%s:%d", s.Config.Address, s.Config.Port))
|
||||
}
|
||||
|
||||
|
||||
@@ -21,13 +21,13 @@ import (
|
||||
"github.com/pquerna/otp/totp"
|
||||
)
|
||||
|
||||
// Simple server config for tests
|
||||
// Simple server config
|
||||
var serverConfig = types.ServerConfig{
|
||||
Port: 8080,
|
||||
Address: "0.0.0.0",
|
||||
}
|
||||
|
||||
// Simple handlers config for tests
|
||||
// Simple handlers config
|
||||
var handlersConfig = types.HandlersConfig{
|
||||
AppURL: "http://localhost:8080",
|
||||
Domain: "localhost",
|
||||
@@ -42,7 +42,7 @@ var handlersConfig = types.HandlersConfig{
|
||||
OAuthAutoRedirect: "none",
|
||||
}
|
||||
|
||||
// Simple auth config for tests
|
||||
// Simple auth config
|
||||
var authConfig = types.AuthConfig{
|
||||
Users: types.Users{},
|
||||
OauthWhitelist: "",
|
||||
@@ -56,13 +56,13 @@ var authConfig = types.AuthConfig{
|
||||
Domain: "localhost",
|
||||
}
|
||||
|
||||
// Simple hooks config for tests
|
||||
// Simple hooks config
|
||||
var hooksConfig = types.HooksConfig{
|
||||
Domain: "localhost",
|
||||
}
|
||||
|
||||
// Cookie
|
||||
var cookie = "MTc1MTkyMzM5MnxiME9aTzlGQjZMNEJMdDZMc0lHMk9zcXQyME9SR1ZnUmlaYWZNcWplek5vcVNpdkdHRTZqb09YWkVUYUN6NEt4MkEyOGEyX2hFQWZEUEYtbllDX0h5eDBCb3VyT2phQlRpZWFfRFdTMGw2WUg2VWw4RGdNbEhQclotOUJjblJGaWFQcmhyaWFna0dXRWNud2c1akg5eEpLZ3JzS0pfWktscVZyckZFR1VDX0R5QjFOT0hzMTNKb18ySEMxZlluSWNxa1ByM0VhSzNyMkRtdDNORWJXVGFYSnMzWjFGa0lrZlhSTWduRmttMHhQUXN4UFhNbHFXY0lBWjBnUWpKU0xXMHRubjlKbjV0LXBGdjk0MmpJX0xMX1ZYblVJVW9LWUJoWmpNanVXNkNjamhYWlR2V29rY0RNYWkxY2lMQnpqLUI2cHMyYTZkWWgtWnlFdGN0amh2WURUeUNGT3ZLS1FJVUFIb0NWR1RPMlRtY2c9PXwerwFtb9urOXnwA02qXbLeorMloaK_paQd0in4BAesmg=="
|
||||
var cookie string
|
||||
|
||||
// User
|
||||
var user = types.User{
|
||||
@@ -72,14 +72,7 @@ var user = types.User{
|
||||
|
||||
// Initialize the server for tests
|
||||
func getServer(t *testing.T) *server.Server {
|
||||
// Create docker service
|
||||
docker, err := docker.NewDocker()
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize docker: %v", err)
|
||||
}
|
||||
|
||||
// Create auth service
|
||||
// Create services
|
||||
authConfig.Users = types.Users{
|
||||
{
|
||||
Username: user.Username,
|
||||
@@ -87,69 +80,51 @@ func getServer(t *testing.T) *server.Server {
|
||||
TotpSecret: user.TotpSecret,
|
||||
},
|
||||
}
|
||||
auth := auth.NewAuth(authConfig, docker, nil)
|
||||
|
||||
// Create providers service
|
||||
docker, err := docker.NewDocker()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create docker client: %v", err)
|
||||
}
|
||||
auth := auth.NewAuth(authConfig, nil, nil)
|
||||
providers := providers.NewProviders(types.OAuthConfig{})
|
||||
|
||||
// Create hooks service
|
||||
hooks := hooks.NewHooks(hooksConfig, auth, providers)
|
||||
|
||||
// Create handlers service
|
||||
handlers := handlers.NewHandlers(handlersConfig, auth, hooks, providers, docker)
|
||||
|
||||
// Create server
|
||||
srv, err := server.NewServer(serverConfig, handlers)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create server: %v", err)
|
||||
}
|
||||
|
||||
// Return the server
|
||||
return srv
|
||||
}
|
||||
|
||||
// Test login
|
||||
func TestLogin(t *testing.T) {
|
||||
t.Log("Testing login")
|
||||
|
||||
// Get server
|
||||
srv := getServer(t)
|
||||
|
||||
// Create recorder
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
user := types.LoginRequest{
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
}
|
||||
|
||||
json, err := json.Marshal(user)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error marshalling json: %v", err)
|
||||
}
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("POST", "/api/login", strings.NewReader(string(json)))
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
|
||||
// Get the result cookie
|
||||
cookies := recorder.Result().Cookies()
|
||||
|
||||
// Check if the cookie is set
|
||||
if len(cookies) == 0 {
|
||||
t.Fatalf("Cookie not set")
|
||||
}
|
||||
@@ -158,55 +133,42 @@ func TestLogin(t *testing.T) {
|
||||
cookie = cookies[0].Value
|
||||
}
|
||||
|
||||
// Test app context
|
||||
func TestAppContext(t *testing.T) {
|
||||
// Refresh the cookie
|
||||
TestLogin(t)
|
||||
|
||||
t.Log("Testing app context")
|
||||
|
||||
// Get server
|
||||
srv := getServer(t)
|
||||
|
||||
// Create recorder
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("GET", "/api/app", nil)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Set the cookie
|
||||
// Set the cookie from the previous test
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "tinyauth",
|
||||
Value: cookie,
|
||||
})
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
|
||||
// Read the body of the response
|
||||
body, err := io.ReadAll(recorder.Body)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting body: %v", err)
|
||||
}
|
||||
|
||||
// Unmarshal the body into the user struct
|
||||
var app types.AppContext
|
||||
|
||||
err = json.Unmarshal(body, &app)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error unmarshalling body: %v", err)
|
||||
}
|
||||
|
||||
// Create tests values
|
||||
expected := types.AppContext{
|
||||
Status: 200,
|
||||
Message: "OK",
|
||||
@@ -226,48 +188,34 @@ func TestAppContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test user context
|
||||
func TestUserContext(t *testing.T) {
|
||||
// Refresh the cookie
|
||||
TestLogin(t)
|
||||
|
||||
t.Log("Testing user context")
|
||||
|
||||
// Get server
|
||||
srv := getServer(t)
|
||||
|
||||
// Create recorder
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("GET", "/api/user", nil)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Set the cookie
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "tinyauth-session",
|
||||
Value: cookie,
|
||||
})
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
|
||||
// Read the body of the response
|
||||
body, err := io.ReadAll(recorder.Body)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting body: %v", err)
|
||||
}
|
||||
|
||||
// Unmarshal the body into the user struct
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
}
|
||||
@@ -275,49 +223,37 @@ func TestUserContext(t *testing.T) {
|
||||
var user User
|
||||
|
||||
err = json.Unmarshal(body, &user)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error unmarshalling body: %v", err)
|
||||
}
|
||||
|
||||
// We should get the username back
|
||||
// We should get the user back
|
||||
if user.Username != "user" {
|
||||
t.Fatalf("Expected user, got %s", user.Username)
|
||||
}
|
||||
}
|
||||
|
||||
// Test logout
|
||||
func TestLogout(t *testing.T) {
|
||||
// Refresh the cookie
|
||||
TestLogin(t)
|
||||
|
||||
t.Log("Testing logout")
|
||||
|
||||
// Get server
|
||||
srv := getServer(t)
|
||||
|
||||
// Create recorder
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("POST", "/api/logout", nil)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Set the cookie
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "tinyauth-session",
|
||||
Value: cookie,
|
||||
})
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
|
||||
// Check if the cookie is different (means the cookie is gone)
|
||||
@@ -326,196 +262,133 @@ func TestLogout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test auth endpoint
|
||||
func TestAuth(t *testing.T) {
|
||||
// Refresh the cookie
|
||||
TestLogin(t)
|
||||
|
||||
t.Log("Testing auth endpoint")
|
||||
|
||||
// Get server
|
||||
srv := getServer(t)
|
||||
|
||||
// Create recorder
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("GET", "/api/auth/traefik", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Set the accept header
|
||||
req.Header.Set("Accept", "text/html")
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusTemporaryRedirect)
|
||||
|
||||
// Recreate recorder
|
||||
recorder = httptest.NewRecorder()
|
||||
|
||||
// Recreate the request
|
||||
req, err = http.NewRequest("GET", "/api/auth/traefik", nil)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Test with the cookie
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "tinyauth-session",
|
||||
Value: cookie,
|
||||
})
|
||||
|
||||
// Serve the request again
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
|
||||
// Recreate recorder
|
||||
recorder = httptest.NewRecorder()
|
||||
|
||||
// Recreate the request
|
||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Serve the request again
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusUnauthorized)
|
||||
|
||||
// Recreate recorder
|
||||
recorder = httptest.NewRecorder()
|
||||
|
||||
// Recreate the request
|
||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Test with the cookie
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "tinyauth-session",
|
||||
Value: cookie,
|
||||
})
|
||||
|
||||
// Serve the request again
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestTOTP(t *testing.T) {
|
||||
t.Log("Testing TOTP")
|
||||
|
||||
// Generate totp secret
|
||||
key, err := totp.Generate(totp.GenerateOpts{
|
||||
Issuer: "Tinyauth",
|
||||
AccountName: user.Username,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate TOTP secret: %v", err)
|
||||
}
|
||||
|
||||
// Create secret
|
||||
secret := key.Secret()
|
||||
|
||||
// Set the user's TOTP secret
|
||||
user.TotpSecret = secret
|
||||
|
||||
// Get server
|
||||
srv := getServer(t)
|
||||
|
||||
// Create request
|
||||
user := types.LoginRequest{
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
}
|
||||
|
||||
loginJson, err := json.Marshal(user)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error marshalling json: %v", err)
|
||||
}
|
||||
|
||||
// Create recorder
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequest("POST", "/api/login", strings.NewReader(string(loginJson)))
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
|
||||
// Set the cookie for next test
|
||||
cookie = recorder.Result().Cookies()[0].Value
|
||||
|
||||
// Create TOTP code
|
||||
code, err := totp.GenerateCode(secret, time.Now())
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate TOTP code: %v", err)
|
||||
}
|
||||
|
||||
// Create TOTP request
|
||||
totpRequest := types.TotpRequest{
|
||||
Code: code,
|
||||
}
|
||||
|
||||
// Marshal the TOTP request
|
||||
totpJson, err := json.Marshal(totpRequest)
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error marshalling TOTP request: %v", err)
|
||||
}
|
||||
|
||||
// Create recorder
|
||||
recorder = httptest.NewRecorder()
|
||||
|
||||
// Create request
|
||||
req, err = http.NewRequest("POST", "/api/totp", strings.NewReader(string(totpJson)))
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating request: %v", err)
|
||||
}
|
||||
|
||||
// Set the cookie
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "tinyauth-session",
|
||||
Value: cookie,
|
||||
})
|
||||
|
||||
// Serve the request
|
||||
srv.Router.ServeHTTP(recorder, req)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user