Skip to content

Commit f3ec4ba

Browse files
authored
feat: add support for logging in to a basic auth protected app (#203)
1 parent b5799da commit f3ec4ba

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

internal/handlers/handlers.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,12 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
119119
if !authEnabled {
120120
headersParsed := utils.ParseHeaders(labels.Headers)
121121
for key, value := range headersParsed {
122-
log.Debug().Str("key", key).Str("value", value).Msg("Setting header")
123-
c.Header(key, utils.SanitizeHeader(value))
122+
log.Debug().Str("key", key).Msg("Setting header")
123+
c.Header(key, value)
124+
}
125+
if labels.Basic.User != "" && labels.Basic.Password != "" {
126+
log.Debug().Str("username", labels.Basic.User).Msg("Setting basic auth headers")
127+
c.Header("Authorization", fmt.Sprintf("Basic %s", utils.GetBasicAuth(labels.Basic.User, labels.Basic.Password)))
124128
}
125129
c.JSON(200, gin.H{
126130
"status": 200,
@@ -242,8 +246,14 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
242246
// Set the rest of the headers
243247
parsedHeaders := utils.ParseHeaders(labels.Headers)
244248
for key, value := range parsedHeaders {
245-
log.Debug().Str("key", key).Str("value", value).Msg("Setting header")
246-
c.Header(key, utils.SanitizeHeader(value))
249+
log.Debug().Str("key", key).Msg("Setting header")
250+
c.Header(key, value)
251+
}
252+
253+
// Set basic auth headers if configured
254+
if labels.Basic.User != "" && labels.Basic.Password != "" {
255+
log.Debug().Str("username", labels.Basic.User).Msg("Setting basic auth headers")
256+
c.Header("Authorization", fmt.Sprintf("Basic %s", utils.GetBasicAuth(labels.Basic.User, labels.Basic.Password)))
247257
}
248258

249259
// The user is allowed to access the app

internal/types/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,18 @@ type OAuthLabels struct {
9999
Groups string
100100
}
101101

102+
// Basic auth labels for a tinyauth protected container
103+
type BasicLabels struct {
104+
User string
105+
Password string
106+
}
107+
102108
// Labels is a struct that contains the labels for a tinyauth protected container
103109
type Labels struct {
104110
Users string
105111
Allowed string
106112
Headers []string
107113
Domain string
114+
Basic BasicLabels
108115
OAuth OAuthLabels
109116
}

internal/utils/utils.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"encoding/base64"
45
"errors"
56
"net/url"
67
"os"
@@ -201,7 +202,7 @@ func GetLabels(labels map[string]string) (types.Labels, error) {
201202
var labelsParsed types.Labels
202203

203204
// Decode the labels into the labels struct
204-
err := parser.Decode(labels, &labelsParsed, "tinyauth", "tinyauth.users", "tinyauth.allowed", "tinyauth.headers", "tinyauth.domain", "tinyauth.oauth")
205+
err := parser.Decode(labels, &labelsParsed, "tinyauth", "tinyauth.users", "tinyauth.allowed", "tinyauth.headers", "tinyauth.domain", "tinyauth.basic", "tinyauth.oauth")
205206

206207
// Check if there was an error
207208
if err != nil {
@@ -358,3 +359,12 @@ func GenerateIdentifier(str string) string {
358359
// Convert the UUID to a string
359360
return strings.Split(uuidString, "-")[0]
360361
}
362+
363+
// Get a basic auth header from a username and password
364+
func GetBasicAuth(username string, password string) string {
365+
// Create the auth string
366+
auth := username + ":" + password
367+
368+
// Encode the auth string to base64
369+
return base64.StdEncoding.EncodeToString([]byte(auth))
370+
}

0 commit comments

Comments
 (0)