Skip to content

Commit

Permalink
poista AWSALB ja cookiesession keksit frontendissä, jos väyläkirjautu…
Browse files Browse the repository at this point in the history
…mista ei löydy
  • Loading branch information
tomi korkalainen committed Feb 17, 2023
1 parent 0ff7d59 commit 7806000
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
10 changes: 10 additions & 0 deletions backend/src/error/NoVaylaAuthenticationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ClientError } from "./ClientError";

export class NoVaylaAuthenticationError extends ClientError {
constructor(m?: string) {
super("NoVaylaAuthenticationError", m);

// Set the prototype explicitly.
Object.setPrototypeOf(this, NoVaylaAuthenticationError.prototype);
}
}
3 changes: 2 additions & 1 deletion backend/src/user/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createSignedCookies } from "./signedCookie";
import { apiConfig } from "../../../common/abstractApi";
import { isAorL } from "../util/userUtil";
import { NoHassuAccessError } from "../error/NoHassuAccessError";
import { NoVaylaAuthenticationError } from "../error/NoVaylaAuthenticationError";

function parseRoles(roles: string): string[] | undefined {
return roles
Expand Down Expand Up @@ -99,7 +100,7 @@ export function getVaylaUser(): NykyinenKayttaja | undefined {

export function requireVaylaUser(): NykyinenKayttaja {
if (!(globalThis as any).currentUser) {
throw new IllegalAccessError("Väylä-kirjautuminen puuttuu");
throw new NoVaylaAuthenticationError("Väylä-kirjautuminen puuttuu");
}
return (globalThis as any).currentUser;
}
Expand Down
6 changes: 3 additions & 3 deletions backend/test/apiHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as sinon from "sinon";
import { projektiDatabase } from "../src/database/projektiDatabase";
import { ProjektiFixture } from "./fixture/projektiFixture";
import { UserFixture } from "./fixture/userFixture";
import { IllegalAccessError } from "../src/error/IllegalAccessError";
import { velho } from "../src/velho/velhoClient";
import { api } from "../integrationtest/api/apiClient";
import { personSearch } from "../src/personSearch/personSearchClient";
Expand Down Expand Up @@ -36,6 +35,7 @@ import { mockBankHolidays } from "./mocks";
import assert from "assert";
import fs from "fs";
import { SchedulerMock } from "../integrationtest/api/testUtil/util";
import { NoVaylaAuthenticationError } from "../src/error/NoVaylaAuthenticationError";

const chai = require("chai");
const { expect } = chai;
Expand Down Expand Up @@ -365,7 +365,7 @@ describe("apiHandler", () => {

// Verify that projekti is not visible for anonymous users
userFixture.logout();
await expect(api.lataaProjekti(fixture.PROJEKTI1_OID)).to.eventually.be.rejectedWith(IllegalAccessError);
await expect(api.lataaProjekti(fixture.PROJEKTI1_OID)).to.eventually.be.rejectedWith(NoVaylaAuthenticationError);
userFixture.loginAs(UserFixture.pekkaProjari);

// Send aloituskuulutus to be approved
Expand Down Expand Up @@ -439,7 +439,7 @@ describe("apiHandler", () => {

createProjektiStub.resolves();

await chai.assert.isRejected(api.tallennaProjekti(fixture.tallennaProjektiInput), IllegalAccessError);
await chai.assert.isRejected(api.tallennaProjekti(fixture.tallennaProjektiInput), NoVaylaAuthenticationError);
});
});

Expand Down
4 changes: 4 additions & 0 deletions migration-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 35 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@mui/material": "5.10.6",
"@mui/system": "5.10.6",
"@mui/x-date-pickers": "5.0.2",
"@types/js-cookie": "^3.0.2",
"@types/mime-types": "2.1.1",
"@types/nodemailer": "6.4.4",
"@types/pdfkit": "0.12.3",
Expand All @@ -42,6 +43,7 @@
"graphql": "14.7.0",
"graphql-tag": "2.12.5",
"html-react-parser": "3.0.8",
"js-cookie": "^3.0.1",
"jsonschema": "1.4.1",
"jsonwebtoken": "9.0.0",
"jwk-to-pem": "2.0.5",
Expand Down
20 changes: 19 additions & 1 deletion src/components/ApiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import useTranslation from "next-translate/useTranslation";
import { Translate } from "next-translate";
import { GraphQLError } from "graphql";
import { NoHassuAccessError } from "backend/src/error/NoHassuAccessError";
import { NoVaylaAuthenticationError } from "backend/src/error/NoVaylaAuthenticationError";
import Cookies from "js-cookie";

type ApiContextType = { api: API; unauthorized: boolean };

Expand Down Expand Up @@ -65,15 +67,31 @@ function ApiProvider({ children }: Props) {
const errorArray: readonly GraphQLError[] = Array.isArray(errors) ? errors : [errors];
const unauthorized = errorArray.some((error) => (error as any)?.errorInfo?.errorSubType === new NoHassuAccessError().className);
setIsUnauthorized(unauthorized);
// Do not show snackbar errors on unauthorized 'page'
if (!unauthorized) {
commonErrorHandler(errorResponse);
}
const noVaylaAuthentication = errorArray.some(
(error) => (error as any)?.errorInfo?.errorSubType === new NoVaylaAuthenticationError().className
);
if (noVaylaAuthentication) {
removeAWSALBAndCookieSessionCookies();
router.push("/yllapito/kirjaudu");
}
};
const api = createApiWithAdditionalErrorHandling(commonErrorHandler, authenticatedErrorHandler);
return { api, unauthorized: isUnauthorized };
}, [isUnauthorized, isYllapito, showErrorMessage, t]);
}, [isUnauthorized, isYllapito, router, showErrorMessage, t]);

return <ApiContext.Provider value={value}>{children}</ApiContext.Provider>;
}

function removeAWSALBAndCookieSessionCookies() {
Object.keys(Cookies.get() || {})
.filter((cookie) => cookie.startsWith("AWSALB") || cookie.startsWith("cookiesession"))
.forEach((cookie) => {
Cookies.remove(cookie);
});
}

export { ApiProvider };

0 comments on commit 7806000

Please sign in to comment.