Skip to content

Commit 091fdd6

Browse files
authored
fix: redirect unauthorized git users to login screen (#10995)
* fix: redirect to login screen if unauthorized git user * consolidated language * fix redirect
1 parent 5d2e87f commit 091fdd6

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

coderd/httpmw/apikey.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,18 @@ func RedirectToLogin(rw http.ResponseWriter, r *http.Request, dashboardURL *url.
538538
// (like temporary redirect does).
539539
http.Redirect(rw, r, u.String(), http.StatusSeeOther)
540540
}
541+
542+
// CustomRedirectToLogin redirects the user to the login page with the `message` and
543+
// `redirect` query parameters set, with a provided code
544+
func CustomRedirectToLogin(rw http.ResponseWriter, r *http.Request, redirect string, message string, code int) {
545+
q := url.Values{}
546+
q.Add("message", message)
547+
q.Add("redirect", redirect)
548+
549+
u := &url.URL{
550+
Path: "/login",
551+
RawQuery: q.Encode(),
552+
}
553+
554+
http.Redirect(rw, r, u.String(), code)
555+
}

coderd/userauth.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
510510

511511
var selectedMemberships []*github.Membership
512512
var organizationNames []string
513+
redirect := state.Redirect
513514
if !api.GithubOAuth2Config.AllowEveryone {
514515
memberships, err := api.GithubOAuth2Config.ListOrganizationMemberships(ctx, oauthClient)
515516
if err != nil {
@@ -535,9 +536,7 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
535536
}
536537
}
537538
if len(selectedMemberships) == 0 {
538-
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
539-
Message: "You aren't a member of the authorized Github organizations!",
540-
})
539+
httpmw.CustomRedirectToLogin(rw, r, redirect, "You aren't a member of the authorized Github organizations!", http.StatusUnauthorized)
541540
return
542541
}
543542
}
@@ -574,9 +573,7 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
574573
}
575574
}
576575
if allowedTeam == nil {
577-
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
578-
Message: fmt.Sprintf("You aren't a member of an authorized team in the %v Github organization(s)!", organizationNames),
579-
})
576+
httpmw.CustomRedirectToLogin(rw, r, redirect, fmt.Sprintf("You aren't a member of an authorized team in the %v Github organization(s)!", organizationNames), http.StatusUnauthorized)
580577
return
581578
}
582579
}
@@ -658,7 +655,6 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) {
658655
http.SetCookie(rw, cookie)
659656
}
660657

661-
redirect := state.Redirect
662658
if redirect == "" {
663659
redirect = "/"
664660
}

site/src/pages/LoginPage/LoginPageView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const LoginPageView: FC<LoginPageViewProps> = ({
2424
const redirectTo = retrieveRedirect(location.search);
2525
// This allows messages to be displayed at the top of the sign in form.
2626
// Helpful for any redirects that want to inform the user of something.
27-
const info = new URLSearchParams(location.search).get("info") || undefined;
27+
const message = new URLSearchParams(location.search).get("message");
2828
const applicationName = getApplicationName();
2929
const logoURL = getLogoURL();
3030
const applicationLogo = logoURL ? (
@@ -52,7 +52,7 @@ export const LoginPageView: FC<LoginPageViewProps> = ({
5252
redirectTo={redirectTo}
5353
isSigningIn={isSigningIn}
5454
error={error}
55-
info={info}
55+
message={message}
5656
onSubmit={onSignIn}
5757
/>
5858
<footer css={styles.footer}>

site/src/pages/LoginPage/SignInForm.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Interpolation, type Theme } from "@emotion/react";
2-
import { type FC } from "react";
2+
import { ReactNode, type FC } from "react";
33
import type { AuthMethods } from "api/typesGenerated";
44
import { PasswordSignInForm } from "./PasswordSignInForm";
55
import { OAuthSignInForm } from "./OAuthSignInForm";
@@ -63,7 +63,7 @@ export interface SignInFormProps {
6363
isSigningIn: boolean;
6464
redirectTo: string;
6565
error?: unknown;
66-
info?: string;
66+
message?: ReactNode;
6767
authMethods?: AuthMethods;
6868
onSubmit: (credentials: { email: string; password: string }) => void;
6969
}
@@ -73,7 +73,7 @@ export const SignInForm: FC<React.PropsWithChildren<SignInFormProps>> = ({
7373
redirectTo,
7474
isSigningIn,
7575
error,
76-
info,
76+
message,
7777
onSubmit,
7878
}) => {
7979
const oAuthEnabled = Boolean(
@@ -91,9 +91,9 @@ export const SignInForm: FC<React.PropsWithChildren<SignInFormProps>> = ({
9191
</div>
9292
)}
9393

94-
{Boolean(info) && Boolean(error) && (
94+
{message && (
9595
<div css={styles.alert}>
96-
<Alert severity="info">{info}</Alert>
96+
<Alert severity="info">{message}</Alert>
9797
</div>
9898
)}
9999

site/src/pages/UserSettingsPage/SecurityPage/SingleSignOnSection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const useSingleSignOnSection = () => {
6262
// The redirect on success should be back to the login page with a nice message.
6363
// The user should be logged out if this worked.
6464
encodeURIComponent(
65-
`/login?info=Login type has been changed to ${loginTypeMsg}. Log in again using the new method.`,
65+
`/login?message=Login type has been changed to ${loginTypeMsg}. Log in again using the new method.`,
6666
),
6767
);
6868
},

0 commit comments

Comments
 (0)