Skip to content

Commit a98a91a

Browse files
committed
fix: only use groups in OAuth
1 parent 5278fbe commit a98a91a

3 files changed

Lines changed: 73 additions & 51 deletions

File tree

frontend/src/pages/continue-page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const ContinuePage = () => {
7070
</Button>
7171
<Button
7272
fullWidth
73-
mt="sm"
73+
mt="xs"
7474
color="gray"
7575
onClick={() => (window.location.href = "/")}
7676
>
@@ -110,7 +110,7 @@ export const ContinuePage = () => {
110110
</Button>
111111
<Button
112112
fullWidth
113-
mt="sm"
113+
mt="xs"
114114
color="gray"
115115
onClick={() => (window.location.href = "/")}
116116
>

frontend/src/pages/unauthorized-page.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Button, Code, Paper, Text } from "@mantine/core";
22
import { Layout } from "../components/layouts/layout";
33
import { Navigate } from "react-router";
44
import { Trans, useTranslation } from "react-i18next";
5-
import React from "react";
5+
import React, { useEffect } from "react";
66
import { isValidQuery } from "../utils/utils";
7+
import { useIsMounted } from "../lib/hooks/use-is-mounted";
78

89
export const UnauthorizedPage = () => {
910
const queryString = window.location.search;
@@ -12,13 +13,31 @@ export const UnauthorizedPage = () => {
1213
const groupErr = params.get("groupErr") ?? "";
1314
const resource = params.get("resource") ?? "";
1415

16+
const [isGroupErr, setIsGroupErr] = React.useState(false);
17+
18+
const useMounted = useIsMounted();
19+
20+
useEffect(() => {
21+
if (useMounted()) {
22+
if (isValidQuery(groupErr)) {
23+
if (groupErr === "true") {
24+
setIsGroupErr(true);
25+
return;
26+
}
27+
setIsGroupErr(false);
28+
return;
29+
}
30+
setIsGroupErr(false);
31+
}
32+
}, []);
33+
1534
const { t } = useTranslation();
1635

1736
if (!isValidQuery(username)) {
1837
return <Navigate to="/" />;
1938
}
2039

21-
if (isValidQuery(resource) && !isValidQuery(groupErr)) {
40+
if (isValidQuery(resource) && !isGroupErr) {
2241
return (
2342
<UnauthorizedLayout>
2443
<Trans
@@ -31,7 +50,7 @@ export const UnauthorizedPage = () => {
3150
);
3251
}
3352

34-
if (isValidQuery(groupErr) && isValidQuery(resource)) {
53+
if (isGroupErr && isValidQuery(resource)) {
3554
return (
3655
<UnauthorizedLayout>
3756
<Trans

internal/handlers/handlers.go

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
7575
// Get the container labels
7676
labels, err := h.Docker.GetLabels(appId)
7777

78+
log.Debug().Interface("labels", labels).Msg("Got labels")
79+
7880
// Check if there was an error
7981
if err != nil {
8082
log.Error().Err(err).Msg("Failed to get container labels")
@@ -183,54 +185,55 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
183185
return
184186
}
185187

186-
log.Debug().Interface("labels", labels).Msg("Got labels")
187-
188-
// Check if user is in required groups
189-
groupOk := h.Auth.OAuthGroup(c, userContext, labels)
190-
191-
log.Debug().Bool("groupOk", groupOk).Msg("Checking if user is in required groups")
192-
193-
// The user is not allowed to access the app
194-
if !groupOk {
195-
log.Warn().Str("username", userContext.Username).Str("host", host).Msg("User is not in required groups")
196-
197-
// Set WWW-Authenticate header
198-
c.Header("WWW-Authenticate", "Basic realm=\"tinyauth\"")
199-
200-
if proxy.Proxy == "nginx" || !isBrowser {
201-
c.JSON(401, gin.H{
202-
"status": 401,
203-
"message": "Unauthorized",
204-
})
205-
return
206-
}
207-
208-
// Values
209-
values := types.UnauthorizedQuery{
210-
Resource: strings.Split(host, ".")[0],
211-
GroupErr: true,
212-
}
213-
214-
// Use either username or email
215-
if userContext.OAuth {
216-
values.Username = userContext.Email
217-
} else {
218-
values.Username = userContext.Username
219-
}
220-
221-
// Build query
222-
queries, err := query.Values(values)
223-
224-
// Handle error (no need to check for nginx/headers since we are sure we are using caddy/traefik)
225-
if err != nil {
226-
log.Error().Err(err).Msg("Failed to build queries")
227-
c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("%s/error", h.Config.AppURL))
188+
// Check groups if using OAuth
189+
if userContext.OAuth {
190+
// Check if user is in required groups
191+
groupOk := h.Auth.OAuthGroup(c, userContext, labels)
192+
193+
log.Debug().Bool("groupOk", groupOk).Msg("Checking if user is in required groups")
194+
195+
// The user is not allowed to access the app
196+
if !groupOk {
197+
log.Warn().Str("username", userContext.Username).Str("host", host).Msg("User is not in required groups")
198+
199+
// Set WWW-Authenticate header
200+
c.Header("WWW-Authenticate", "Basic realm=\"tinyauth\"")
201+
202+
if proxy.Proxy == "nginx" || !isBrowser {
203+
c.JSON(401, gin.H{
204+
"status": 401,
205+
"message": "Unauthorized",
206+
})
207+
return
208+
}
209+
210+
// Values
211+
values := types.UnauthorizedQuery{
212+
Resource: strings.Split(host, ".")[0],
213+
GroupErr: true,
214+
}
215+
216+
// Use either username or email
217+
if userContext.OAuth {
218+
values.Username = userContext.Email
219+
} else {
220+
values.Username = userContext.Username
221+
}
222+
223+
// Build query
224+
queries, err := query.Values(values)
225+
226+
// Handle error (no need to check for nginx/headers since we are sure we are using caddy/traefik)
227+
if err != nil {
228+
log.Error().Err(err).Msg("Failed to build queries")
229+
c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("%s/error", h.Config.AppURL))
230+
return
231+
}
232+
233+
// We are using caddy/traefik so redirect
234+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/unauthorized?%s", h.Config.AppURL, queries.Encode()))
228235
return
229236
}
230-
231-
// We are using caddy/traefik so redirect
232-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/unauthorized?%s", h.Config.AppURL, queries.Encode()))
233-
return
234237
}
235238

236239
c.Header("Remote-User", utils.SanitizeHeader(userContext.Username))

0 commit comments

Comments
 (0)