Skip to content

Commit 54070bc

Browse files
authored
fix: handle falsy env flags for backend auth mode (#2567)
1 parent d6fdbb6 commit 54070bc

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

backend/middleware/middleware.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ import (
55
"log/slog"
66
"net/http"
77
"os"
8+
"strings"
89
"time"
910

1011
"github.com/diggerhq/digger/backend/models"
1112
"github.com/diggerhq/digger/backend/services"
1213
"github.com/gin-gonic/gin"
1314
)
1415

16+
func envFlagEnabled(name string) bool {
17+
v, ok := os.LookupEnv(name)
18+
if !ok {
19+
return false
20+
}
21+
v = strings.TrimSpace(strings.ToLower(v))
22+
return v == "true" || v == "1" || v == "yes" || v == "on"
23+
}
24+
1525
func GetWebMiddleware() gin.HandlerFunc {
16-
if _, ok := os.LookupEnv("JWT_AUTH"); ok {
26+
if envFlagEnabled("JWT_AUTH") {
1727
slog.Info("Using JWT middleware for web routes")
1828
auth := services.Auth{
1929
HttpClient: http.Client{},
@@ -22,10 +32,10 @@ func GetWebMiddleware() gin.HandlerFunc {
2232
ClientId: os.Getenv("FRONTEGG_CLIENT_ID"),
2333
}
2434
return JWTWebAuth(auth)
25-
} else if _, ok := os.LookupEnv("HTTP_BASIC_AUTH"); ok {
35+
} else if envFlagEnabled("HTTP_BASIC_AUTH") {
2636
slog.Info("Using http basic auth middleware for web routes")
2737
return HttpBasicWebAuth()
28-
} else if _, ok := os.LookupEnv("NOOP_AUTH"); ok {
38+
} else if envFlagEnabled("NOOP_AUTH") {
2939
slog.Info("Using noop auth for web routes")
3040
return NoopWebAuth()
3141
} else {
@@ -35,7 +45,7 @@ func GetWebMiddleware() gin.HandlerFunc {
3545
}
3646

3747
func GetApiMiddleware() gin.HandlerFunc {
38-
if _, ok := os.LookupEnv("JWT_AUTH"); ok {
48+
if envFlagEnabled("JWT_AUTH") {
3949
slog.Info("Using JWT middleware for API routes")
4050
auth := services.Auth{
4151
HttpClient: http.Client{},
@@ -44,10 +54,10 @@ func GetApiMiddleware() gin.HandlerFunc {
4454
ClientId: os.Getenv("FRONTEGG_CLIENT_ID"),
4555
}
4656
return JWTBearerTokenAuth(auth)
47-
} else if _, ok := os.LookupEnv("HTTP_BASIC_AUTH"); ok {
57+
} else if envFlagEnabled("HTTP_BASIC_AUTH") {
4858
slog.Info("Using http basic auth middleware for API routes")
4959
return HttpBasicApiAuth()
50-
} else if _, ok := os.LookupEnv("NOOP_AUTH"); ok {
60+
} else if envFlagEnabled("NOOP_AUTH") {
5161
slog.Info("Using noop auth for API routes")
5262
return NoopApiAuth()
5363
} else {

0 commit comments

Comments
 (0)