Skip to content

Commit

Permalink
Fix the OIDC login button when using a RoutePrefix.
Browse files Browse the repository at this point in the history
Make the OIDC login button relative so that it uses the route Prefix
from the base.
  • Loading branch information
bigkevmcd committed Sep 19, 2023
1 parent 047c56a commit 231cc8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pkg/server/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func RegisterAuthServer(mux *http.ServeMux, prefix string, srv *AuthServer, logi
return err
}

mux.Handle(prefix, srv.OAuth2Flow())
mux.HandleFunc(prefix, srv.OAuth2Flow)

mux.HandleFunc(prefix+"/callback", srv.Callback)
mux.Handle(prefix+"/sign_in", middleware.Handle(srv.SignIn()))
mux.HandleFunc(prefix+"/userinfo", srv.UserInfo)
Expand Down
14 changes: 6 additions & 8 deletions pkg/server/auth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,13 @@ func (s *AuthServer) oauth2Config(scopes []string) *oauth2.Config {
}
}

func (s *AuthServer) OAuth2Flow() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
if !s.oidcEnabled() {
JSONError(s.Log, rw, "oidc provider not configured", http.StatusBadRequest)
return
}

s.startAuthFlow(rw, r)
func (s *AuthServer) OAuth2Flow(rw http.ResponseWriter, r *http.Request) {
if !s.oidcEnabled() {
JSONError(s.Log, rw, "oidc provider not configured", http.StatusBadRequest)
return
}

s.startAuthFlow(rw, r)
}

func (s *AuthServer) Callback(rw http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function SignIn({ darkModeEnabled = true }: Props) {

const handleOIDCSubmit = () => {
const CURRENT_URL = window.origin;
return (window.location.href = `/oauth2?return_url=${encodeURIComponent(
return (window.location.href = `./oauth2?return_url=${encodeURIComponent(
CURRENT_URL
)}`);
};
Expand Down
24 changes: 23 additions & 1 deletion ui/pages/__tests__/Signin.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from "@testing-library/react";
import { render, screen, fireEvent } from "@testing-library/react";
import * as React from "react";
import { withContext, withTheme } from "../../lib/test-utils";
import SignIn from "../SignIn";
Expand All @@ -17,6 +17,19 @@ const renderSignIn = (featureFlags: Record<string, string>) => {
};

describe("SignIn", () => {
const location = window.location;

beforeEach(() => {
Object.defineProperty(window, "location", {
writable: true,
value: { assign: jest.fn() },
});
});

afterEach(() => {
window.location = location;
});

it("should show no buttons or user/password fields with no flags set", async () => {
renderSignIn({});
expect(screen.queryByText(defaultButtonLabel)).toBeNull();
Expand All @@ -35,6 +48,15 @@ describe("SignIn", () => {
expect(screen.queryByText(defaultButtonLabel)).toBeTruthy();
});

it("should redirect to the oauth2 endpoint with the correct return_url", async () => {
renderSignIn({ OIDC_AUTH: "true" });
fireEvent.click(screen.queryByText(defaultButtonLabel));

expect(window.location.href).toEqual(
"./oauth2?return_url=http%3A%2F%2Flocalhost"
);
});

it("should show both buttons if both flags are set", async () => {
renderSignIn({
OIDC_AUTH: "true",
Expand Down

0 comments on commit 231cc8f

Please sign in to comment.