feat: add support for logging in to a basic auth protected app (#203)

This commit is contained in:
Stavros
2025-06-20 11:33:06 +03:00
committed by GitHub
parent b5799da703
commit f3ec4baf3c
3 changed files with 32 additions and 5 deletions

View File

@@ -119,8 +119,12 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
if !authEnabled { if !authEnabled {
headersParsed := utils.ParseHeaders(labels.Headers) headersParsed := utils.ParseHeaders(labels.Headers)
for key, value := range headersParsed { for key, value := range headersParsed {
log.Debug().Str("key", key).Str("value", value).Msg("Setting header") log.Debug().Str("key", key).Msg("Setting header")
c.Header(key, utils.SanitizeHeader(value)) c.Header(key, value)
}
if labels.Basic.User != "" && labels.Basic.Password != "" {
log.Debug().Str("username", labels.Basic.User).Msg("Setting basic auth headers")
c.Header("Authorization", fmt.Sprintf("Basic %s", utils.GetBasicAuth(labels.Basic.User, labels.Basic.Password)))
} }
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
@@ -242,8 +246,14 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
// Set the rest of the headers // Set the rest of the headers
parsedHeaders := utils.ParseHeaders(labels.Headers) parsedHeaders := utils.ParseHeaders(labels.Headers)
for key, value := range parsedHeaders { for key, value := range parsedHeaders {
log.Debug().Str("key", key).Str("value", value).Msg("Setting header") log.Debug().Str("key", key).Msg("Setting header")
c.Header(key, utils.SanitizeHeader(value)) c.Header(key, value)
}
// Set basic auth headers if configured
if labels.Basic.User != "" && labels.Basic.Password != "" {
log.Debug().Str("username", labels.Basic.User).Msg("Setting basic auth headers")
c.Header("Authorization", fmt.Sprintf("Basic %s", utils.GetBasicAuth(labels.Basic.User, labels.Basic.Password)))
} }
// The user is allowed to access the app // The user is allowed to access the app

View File

@@ -99,11 +99,18 @@ type OAuthLabels struct {
Groups string Groups string
} }
// Basic auth labels for a tinyauth protected container
type BasicLabels struct {
User string
Password string
}
// Labels is a struct that contains the labels for a tinyauth protected container // Labels is a struct that contains the labels for a tinyauth protected container
type Labels struct { type Labels struct {
Users string Users string
Allowed string Allowed string
Headers []string Headers []string
Domain string Domain string
Basic BasicLabels
OAuth OAuthLabels OAuth OAuthLabels
} }

View File

@@ -1,6 +1,7 @@
package utils package utils
import ( import (
"encoding/base64"
"errors" "errors"
"net/url" "net/url"
"os" "os"
@@ -201,7 +202,7 @@ func GetLabels(labels map[string]string) (types.Labels, error) {
var labelsParsed types.Labels var labelsParsed types.Labels
// Decode the labels into the labels struct // Decode the labels into the labels struct
err := parser.Decode(labels, &labelsParsed, "tinyauth", "tinyauth.users", "tinyauth.allowed", "tinyauth.headers", "tinyauth.domain", "tinyauth.oauth") err := parser.Decode(labels, &labelsParsed, "tinyauth", "tinyauth.users", "tinyauth.allowed", "tinyauth.headers", "tinyauth.domain", "tinyauth.basic", "tinyauth.oauth")
// Check if there was an error // Check if there was an error
if err != nil { if err != nil {
@@ -358,3 +359,12 @@ func GenerateIdentifier(str string) string {
// Convert the UUID to a string // Convert the UUID to a string
return strings.Split(uuidString, "-")[0] return strings.Split(uuidString, "-")[0]
} }
// Get a basic auth header from a username and password
func GetBasicAuth(username string, password string) string {
// Create the auth string
auth := username + ":" + password
// Encode the auth string to base64
return base64.StdEncoding.EncodeToString([]byte(auth))
}