Skip to content

Commit ec67ea3

Browse files
committed
refactor: detect if using browser or headless client for better responses
1 parent 3649d0d commit ec67ea3

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

docker-compose.dev.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ services:
88
volumes:
99
- /var/run/docker.sock:/var/run/docker.sock
1010

11-
nginx:
12-
container_name: nginx
13-
image: nginx:latest
11+
whoami:
12+
container_name: whoami
13+
image: traefik/whoami:latest
1414
labels:
1515
traefik.enable: true
16-
traefik.http.routers.nginx.rule: Host(`nginx.dev.local`)
16+
traefik.http.routers.nginx.rule: Host(`whoami.dev.local`)
1717
traefik.http.services.nginx.loadbalancer.server.port: 80
1818
traefik.http.routers.nginx.middlewares: tinyauth
1919

internal/api/api.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,24 @@ func (api *API) SetupRoutes() {
131131
return
132132
}
133133

134-
log.Debug().Interface("proxy", proxy.Proxy).Msg("Got proxy")
134+
// Check if the request is coming from a browser (tools like curl/bruno use */* and they don't include the text/html)
135+
isBrowser := strings.Contains(c.Request.Header.Get("Accept"), "text/html")
136+
137+
if isBrowser {
138+
log.Debug().Msg("Request is most likely coming from a browser")
139+
} else {
140+
log.Debug().Msg("Request is most likely not coming from a browser")
141+
}
135142

136-
// Check if using basic auth
137-
_, _, basicAuth := c.Request.BasicAuth()
143+
log.Debug().Interface("proxy", proxy.Proxy).Msg("Got proxy")
138144

139145
// Check if auth is enabled
140146
authEnabled, authEnabledErr := api.Auth.AuthEnabled(c)
141147

142148
// Handle error
143149
if authEnabledErr != nil {
144-
// Return 500 if nginx is the proxy or if the request is using basic auth
145-
if proxy.Proxy == "nginx" || basicAuth {
150+
// Return 500 if nginx is the proxy or if the request is not coming from a browser
151+
if proxy.Proxy == "nginx" || !isBrowser {
146152
log.Error().Err(authEnabledErr).Msg("Failed to check if auth is enabled")
147153
c.JSON(500, gin.H{
148154
"status": 500,
@@ -186,8 +192,8 @@ func (api *API) SetupRoutes() {
186192

187193
// Check if there was an error
188194
if appAllowedErr != nil {
189-
// Return 500 if nginx is the proxy or if the request is using basic auth
190-
if proxy.Proxy == "nginx" || basicAuth {
195+
// Return 500 if nginx is the proxy or if the request is not coming from a browser
196+
if proxy.Proxy == "nginx" || !isBrowser {
191197
log.Error().Err(appAllowedErr).Msg("Failed to check if app is allowed")
192198
c.JSON(500, gin.H{
193199
"status": 500,
@@ -208,9 +214,11 @@ func (api *API) SetupRoutes() {
208214
if !appAllowed {
209215
log.Warn().Str("username", userContext.Username).Str("host", host).Msg("User not allowed")
210216

211-
// Return 401 if nginx is the proxy or if the request is using an Authorization header
212-
if proxy.Proxy == "nginx" || basicAuth {
213-
c.Header("WWW-Authenticate", "Basic realm=\"tinyauth\"")
217+
// Set WWW-Authenticate header
218+
c.Header("WWW-Authenticate", "Basic realm=\"tinyauth\"")
219+
220+
// Return 401 if nginx is the proxy or if the request is not coming from a browser
221+
if proxy.Proxy == "nginx" || !isBrowser {
214222
c.JSON(401, gin.H{
215223
"status": 401,
216224
"message": "Unauthorized",
@@ -252,9 +260,11 @@ func (api *API) SetupRoutes() {
252260
// The user is not logged in
253261
log.Debug().Msg("Unauthorized")
254262

255-
// Return 401 if nginx is the proxy or if the request is using an Authorization header
256-
if proxy.Proxy == "nginx" || basicAuth {
257-
c.Header("WWW-Authenticate", "Basic realm=\"tinyauth\"")
263+
// Set www-authenticate header
264+
c.Header("WWW-Authenticate", "Basic realm=\"tinyauth\"")
265+
266+
// Return 401 if nginx is the proxy or if the request is not coming from a browser
267+
if proxy.Proxy == "nginx" || !isBrowser {
258268
c.JSON(401, gin.H{
259269
"status": 401,
260270
"message": "Unauthorized",

0 commit comments

Comments
 (0)