Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
if !authEnabled {
headersParsed := utils.ParseHeaders(labels.Headers)
for key, value := range headersParsed {
log.Debug().Str("key", key).Str("value", value).Msg("Setting header")
c.Header(key, utils.SanitizeHeader(value))
log.Debug().Str("key", key).Msg("Setting header")
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)))
}
Comment thread
steveiliop56 marked this conversation as resolved.
c.JSON(200, gin.H{
"status": 200,
Expand Down Expand Up @@ -242,8 +246,14 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
// Set the rest of the headers
parsedHeaders := utils.ParseHeaders(labels.Headers)
for key, value := range parsedHeaders {
log.Debug().Str("key", key).Str("value", value).Msg("Setting header")
c.Header(key, utils.SanitizeHeader(value))
log.Debug().Str("key", key).Msg("Setting header")
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)))
}
Comment thread
steveiliop56 marked this conversation as resolved.

// The user is allowed to access the app
Expand Down
7 changes: 7 additions & 0 deletions internal/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,18 @@ type OAuthLabels struct {
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
type Labels struct {
Users string
Allowed string
Headers []string
Domain string
Basic BasicLabels
OAuth OAuthLabels
}
12 changes: 11 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/base64"
"errors"
"net/url"
"os"
Expand Down Expand Up @@ -201,7 +202,7 @@ func GetLabels(labels map[string]string) (types.Labels, error) {
var labelsParsed types.Labels

// 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
if err != nil {
Expand Down Expand Up @@ -358,3 +359,12 @@ func GenerateIdentifier(str string) string {
// Convert the UUID to a string
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))
}