Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sets cookie that nodebb should recognize #2186

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"helmet": "^7.0.0",
"html-react-parser": "^5.1.8",
"i18next": "^23.11.5",
"jsonwebtoken": "^9.0.2",
"lexical": "^0.12.4",
"lodash": "^4.17.21",
"openid-client": "^5.6.1",
Expand Down
20 changes: 18 additions & 2 deletions src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import express from "express";
import jwt from "jsonwebtoken";
import { matchPath } from "react-router-dom";
import { getCookie } from "@ndla/util";
import { generateOauthData } from "./helpers/oauthHelper";
Expand All @@ -16,7 +17,7 @@ import { forwardingRoute } from "./routes/forwardingRoute";
import { oembedArticleRoute } from "./routes/oembedArticleRoute";
import { podcastFeedRoute } from "./routes/podcastFeedRoute";
import { sendResponse } from "./serverHelpers";
import config from "../config";
import config, { getEnvironmentVariabel } from "../config";
import { FILM_PAGE_PATH, STORED_LANGUAGE_COOKIE_KEY, UKR_PAGE_PATH } from "../constants";
import { getLocaleInfoFromPath } from "../i18n";
import { routes } from "../routeHelpers";
Expand Down Expand Up @@ -111,6 +112,19 @@ router.get("/login/success", async (req, res) => {
encode: String,
domain,
});

// Set cookie for nodebb to use
const username = "https://n.feide.no/claims/eduPersonPrincipalName";
const decoded = token.id_token ? jwt.decode(token.id_token, {}) : undefined;
const nodebbCookie = {
id: decoded?.sub,
username: decoded?.[username],
fullname: decoded?.name,
email: decoded?.email,
};
const nodebbCookieString = jwt.sign(nodebbCookie, getEnvironmentVariabel("NODEBB_SECRET", "secret"));
res.cookie("nodebb_auth", nodebbCookieString, { domain });

const languageCookie = getCookie(STORED_LANGUAGE_COOKIE_KEY, req.headers.cookie ?? "");
//workaround to ensure language cookie is set before redirecting to state path
if (!languageCookie) {
Expand All @@ -135,7 +149,9 @@ router.get(["/logout", "/:lang/logout"], async (req, res) => {
});

router.get("/logout/session", (req, res) => {
res.clearCookie("feide_auth", { domain: `.${config.feideDomain}` });
const domain = req.hostname === config.feideDomain ? `.${config.feideDomain}` : req.hostname;
res.clearCookie("feide_auth", { domain });
res.clearCookie("nodebb_auth", { domain });
const state = typeof req.query.state === "string" ? req.query.state : "/";
const { basepath, basename } = getLocaleInfoFromPath(state);
const wasPrivateRoute = privateRoutes.some((r) => matchPath(r, basepath));
Expand Down
107 changes: 106 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6524,6 +6524,13 @@ __metadata:
languageName: node
linkType: hard

"buffer-equal-constant-time@npm:1.0.1":
version: 1.0.1
resolution: "buffer-equal-constant-time@npm:1.0.1"
checksum: 10c0/fb2294e64d23c573d0dd1f1e7a466c3e978fe94a4e0f8183937912ca374619773bef8e2aceb854129d2efecbbc515bbd0cc78d2734a3e3031edb0888531bbc8e
languageName: node
linkType: hard

"buffer-from@npm:^1.0.0":
version: 1.1.2
resolution: "buffer-from@npm:1.1.2"
Expand Down Expand Up @@ -7677,6 +7684,15 @@ __metadata:
languageName: node
linkType: hard

"ecdsa-sig-formatter@npm:1.0.11":
version: 1.0.11
resolution: "ecdsa-sig-formatter@npm:1.0.11"
dependencies:
safe-buffer: "npm:^5.0.1"
checksum: 10c0/ebfbf19d4b8be938f4dd4a83b8788385da353d63307ede301a9252f9f7f88672e76f2191618fd8edfc2f24679236064176fab0b78131b161ee73daa37125408c
languageName: node
linkType: hard

"edge-runtime@npm:2.5.9":
version: 2.5.9
resolution: "edge-runtime@npm:2.5.9"
Expand Down Expand Up @@ -10622,6 +10638,24 @@ __metadata:
languageName: node
linkType: hard

"jsonwebtoken@npm:^9.0.2":
version: 9.0.2
resolution: "jsonwebtoken@npm:9.0.2"
dependencies:
jws: "npm:^3.2.2"
lodash.includes: "npm:^4.3.0"
lodash.isboolean: "npm:^3.0.3"
lodash.isinteger: "npm:^4.0.4"
lodash.isnumber: "npm:^3.0.3"
lodash.isplainobject: "npm:^4.0.6"
lodash.isstring: "npm:^4.0.1"
lodash.once: "npm:^4.0.0"
ms: "npm:^2.1.1"
semver: "npm:^7.5.4"
checksum: 10c0/d287a29814895e866db2e5a0209ce730cbc158441a0e5a70d5e940eb0d28ab7498c6bf45029cc8b479639bca94056e9a7f254e2cdb92a2f5750c7f358657a131
languageName: node
linkType: hard

"jsx-ast-utils@npm:^2.4.1 || ^3.0.0, jsx-ast-utils@npm:^3.3.5":
version: 3.3.5
resolution: "jsx-ast-utils@npm:3.3.5"
Expand All @@ -10634,6 +10668,27 @@ __metadata:
languageName: node
linkType: hard

"jwa@npm:^1.4.1":
version: 1.4.1
resolution: "jwa@npm:1.4.1"
dependencies:
buffer-equal-constant-time: "npm:1.0.1"
ecdsa-sig-formatter: "npm:1.0.11"
safe-buffer: "npm:^5.0.1"
checksum: 10c0/5c533540bf38702e73cf14765805a94027c66a0aa8b16bc3e89d8d905e61a4ce2791e87e21be97d1293a5ee9d4f3e5e47737e671768265ca4f25706db551d5e9
languageName: node
linkType: hard

"jws@npm:^3.2.2":
version: 3.2.2
resolution: "jws@npm:3.2.2"
dependencies:
jwa: "npm:^1.4.1"
safe-buffer: "npm:^5.0.1"
checksum: 10c0/e770704533d92df358adad7d1261fdecad4d7b66fa153ba80d047e03ca0f1f73007ce5ed3fbc04d2eba09ba6e7e6e645f351e08e5ab51614df1b0aa4f384dfff
languageName: node
linkType: hard

"kleur@npm:4.1.5":
version: 4.1.5
resolution: "kleur@npm:4.1.5"
Expand Down Expand Up @@ -10844,6 +10899,48 @@ __metadata:
languageName: node
linkType: hard

"lodash.includes@npm:^4.3.0":
version: 4.3.0
resolution: "lodash.includes@npm:4.3.0"
checksum: 10c0/7ca498b9b75bf602d04e48c0adb842dfc7d90f77bcb2a91a2b2be34a723ad24bc1c8b3683ec6b2552a90f216c723cdea530ddb11a3320e08fa38265703978f4b
languageName: node
linkType: hard

"lodash.isboolean@npm:^3.0.3":
version: 3.0.3
resolution: "lodash.isboolean@npm:3.0.3"
checksum: 10c0/0aac604c1ef7e72f9a6b798e5b676606042401dd58e49f051df3cc1e3adb497b3d7695635a5cbec4ae5f66456b951fdabe7d6b387055f13267cde521f10ec7f7
languageName: node
linkType: hard

"lodash.isinteger@npm:^4.0.4":
version: 4.0.4
resolution: "lodash.isinteger@npm:4.0.4"
checksum: 10c0/4c3e023a2373bf65bf366d3b8605b97ec830bca702a926939bcaa53f8e02789b6a176e7f166b082f9365bfec4121bfeb52e86e9040cb8d450e64c858583f61b7
languageName: node
linkType: hard

"lodash.isnumber@npm:^3.0.3":
version: 3.0.3
resolution: "lodash.isnumber@npm:3.0.3"
checksum: 10c0/2d01530513a1ee4f72dd79528444db4e6360588adcb0e2ff663db2b3f642d4bb3d687051ae1115751ca9082db4fdef675160071226ca6bbf5f0c123dbf0aa12d
languageName: node
linkType: hard

"lodash.isplainobject@npm:^4.0.6":
version: 4.0.6
resolution: "lodash.isplainobject@npm:4.0.6"
checksum: 10c0/afd70b5c450d1e09f32a737bed06ff85b873ecd3d3d3400458725283e3f2e0bb6bf48e67dbe7a309eb371a822b16a26cca4a63c8c52db3fc7dc9d5f9dd324cbb
languageName: node
linkType: hard

"lodash.isstring@npm:^4.0.1":
version: 4.0.1
resolution: "lodash.isstring@npm:4.0.1"
checksum: 10c0/09eaf980a283f9eef58ef95b30ec7fee61df4d6bf4aba3b5f096869cc58f24c9da17900febc8ffd67819b4e29de29793190e88dc96983db92d84c95fa85d1c92
languageName: node
linkType: hard

"lodash.lowercase@npm:^4.3.0":
version: 4.3.0
resolution: "lodash.lowercase@npm:4.3.0"
Expand All @@ -10865,6 +10962,13 @@ __metadata:
languageName: node
linkType: hard

"lodash.once@npm:^4.0.0":
version: 4.1.1
resolution: "lodash.once@npm:4.1.1"
checksum: 10c0/46a9a0a66c45dd812fcc016e46605d85ad599fe87d71a02f6736220554b52ffbe82e79a483ad40f52a8a95755b0d1077fba259da8bfb6694a7abbf4a48f1fc04
languageName: node
linkType: hard

"lodash.sortby@npm:^4.7.0":
version: 4.7.0
resolution: "lodash.sortby@npm:4.7.0"
Expand Down Expand Up @@ -11556,6 +11660,7 @@ __metadata:
html-react-parser: "npm:^5.1.8"
i18next: "npm:^23.11.5"
jsdom: "npm:^24.0.0"
jsonwebtoken: "npm:^9.0.2"
lexical: "npm:^0.12.4"
lodash: "npm:^4.17.21"
nock: "npm:^14.0.0-beta.5"
Expand Down Expand Up @@ -13746,7 +13851,7 @@ __metadata:
languageName: node
linkType: hard

"safe-buffer@npm:5.2.1, safe-buffer@npm:^5.1.0, safe-buffer@npm:^5.1.2, safe-buffer@npm:^5.2.1, safe-buffer@npm:~5.2.0":
"safe-buffer@npm:5.2.1, safe-buffer@npm:^5.0.1, safe-buffer@npm:^5.1.0, safe-buffer@npm:^5.1.2, safe-buffer@npm:^5.2.1, safe-buffer@npm:~5.2.0":
version: 5.2.1
resolution: "safe-buffer@npm:5.2.1"
checksum: 10c0/6501914237c0a86e9675d4e51d89ca3c21ffd6a31642efeba25ad65720bce6921c9e7e974e5be91a786b25aa058b5303285d3c15dbabf983a919f5f630d349f3
Expand Down
Loading