fix redirects on protected routes for anonymous user

This commit is contained in:
Eduard Heimbuch
2020-08-04 09:39:37 +02:00
parent 4c9e96f7e2
commit c8a111f78e
4 changed files with 13 additions and 10 deletions

View File

@@ -68,7 +68,7 @@ class App extends Component<Props> {
content = <Loading />; content = <Loading />;
} else if (error) { } else if (error) {
content = <ErrorPage title={t("app.error.title")} subtitle={t("app.error.subtitle")} error={error} />; content = <ErrorPage title={t("app.error.title")} subtitle={t("app.error.subtitle")} error={error} />;
} else { } else if (me) {
content = <Main authenticated={authenticated} links={links} />; content = <Main authenticated={authenticated} links={links} />;
} }
return ( return (

View File

@@ -26,9 +26,9 @@ import { connect } from "react-redux";
import { WithTranslation, withTranslation } from "react-i18next"; import { WithTranslation, withTranslation } from "react-i18next";
import { Redirect } from "react-router-dom"; import { Redirect } from "react-router-dom";
import { getLogoutFailure, isAuthenticated, isLogoutPending, isRedirecting, logout } from "../modules/auth"; import { getLogoutFailure, isLogoutPending, isRedirecting, logout } from "../modules/auth";
import { ErrorPage, Loading } from "@scm-manager/ui-components"; import { ErrorPage, Loading } from "@scm-manager/ui-components";
import { getLogoutLink } from "../modules/indexResource"; import { getLoginLink, getLogoutLink } from "../modules/indexResource";
type Props = WithTranslation & { type Props = WithTranslation & {
authenticated: boolean; authenticated: boolean;
@@ -61,7 +61,7 @@ class Logout extends React.Component<Props> {
} }
const mapStateToProps = (state: any) => { const mapStateToProps = (state: any) => {
const authenticated = isAuthenticated(state); const authenticated = state.auth.me && !getLoginLink(state);
const loading = isLogoutPending(state); const loading = isLogoutPending(state);
const redirecting = isRedirecting(state); const redirecting = isRedirecting(state);
const error = getLogoutFailure(state); const error = getLogoutFailure(state);

View File

@@ -32,8 +32,8 @@ import java.util.List;
public class AuthenticationRequestDto { public class AuthenticationRequestDto {
@FormParam("grant_type") @FormParam("grantType")
@JsonProperty("grant_type") @JsonProperty("grantType")
private String grantType; private String grantType;
@FormParam("username") @FormParam("username")
@@ -69,7 +69,7 @@ public class AuthenticationRequestDto {
} }
public boolean isValid() { public boolean isValid() {
// password is currently the only valid grant_type // password is currently the only valid grantType
return "password".equals(grantType) && !Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password); return "password".equals(grantType) && !Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password);
} }
} }

View File

@@ -71,7 +71,7 @@ public class IndexDtoGenerator extends HalAppenderMapper {
builder.single(link("loginInfo", loginInfoUrl)); builder.single(link("loginInfo", loginInfoUrl));
} }
if (SecurityUtils.getSubject().isAuthenticated() && !Authentications.isAuthenticatedSubjectAnonymous() || isAnonymousAccess()) { if (shouldAppendSubjectRelatedLinks()) {
builder.single(link("me", resourceLinks.me().self())); builder.single(link("me", resourceLinks.me().self()));
if (Authentications.isAuthenticatedSubjectAnonymous()) { if (Authentications.isAuthenticatedSubjectAnonymous()) {
@@ -122,7 +122,10 @@ public class IndexDtoGenerator extends HalAppenderMapper {
return new IndexDto(builder.build(), embeddedBuilder.build(), scmContextProvider.getVersion()); return new IndexDto(builder.build(), embeddedBuilder.build(), scmContextProvider.getVersion());
} }
private boolean isAnonymousAccess() { private boolean shouldAppendSubjectRelatedLinks() {
return Authentications.isAuthenticatedSubjectAnonymous() && configuration.getAnonymousMode() == AnonymousMode.FULL; return (SecurityUtils.getSubject().isAuthenticated()
&& !Authentications.isAuthenticatedSubjectAnonymous())
|| (Authentications.isAuthenticatedSubjectAnonymous()
&& configuration.getAnonymousMode() == AnonymousMode.FULL);
} }
} }