From 96a13c5ac56cc46bce25477248662f07c2751f6c Mon Sep 17 00:00:00 2001 From: Simao Rodrigues Date: Wed, 27 Mar 2024 09:42:53 +0000 Subject: [PATCH] Add support for basic Auth --- frontend/package.json | 1 + frontend/src/middleware.ts | 17 +++ frontend/src/pages/api/auth/[...nextauth].ts | 36 ++++++ frontend/yarn.lock | 123 +++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 frontend/src/middleware.ts create mode 100644 frontend/src/pages/api/auth/[...nextauth].ts diff --git a/frontend/package.json b/frontend/package.json index 05353847..f98e1b83 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -59,6 +59,7 @@ "lucide-react": "^0.274.0", "mapbox-gl": "3.1.2", "next": "13.5.6", + "next-auth": "^4.24.7", "next-usequerystate": "1.9.2", "orval": "6.18.1", "postcss": "8.4.21", diff --git a/frontend/src/middleware.ts b/frontend/src/middleware.ts new file mode 100644 index 00000000..f778ab85 --- /dev/null +++ b/frontend/src/middleware.ts @@ -0,0 +1,17 @@ +import { NextRequest, NextResponse } from 'next/server'; + +import nextAuthMiddleware, { NextRequestWithAuth } from 'next-auth/middleware'; + +export function middleware(params: NextRequest) { + const authRequired = + !!process.env.HTTP_AUTH_USERNAME && + !!process.env.HTTP_AUTH_PASSWORD && + !!process.env.NEXTAUTH_SECRET && + !!process.env.NEXTAUTH_URL; + + if (authRequired) { + return nextAuthMiddleware(params as NextRequestWithAuth); + } + + return NextResponse.next(); +} diff --git a/frontend/src/pages/api/auth/[...nextauth].ts b/frontend/src/pages/api/auth/[...nextauth].ts new file mode 100644 index 00000000..844baabb --- /dev/null +++ b/frontend/src/pages/api/auth/[...nextauth].ts @@ -0,0 +1,36 @@ +import NextAuth from 'next-auth'; +import CredentialsProvider from 'next-auth/providers/credentials'; + +export const authOptions = { + providers: [ + CredentialsProvider({ + name: 'credentials', + credentials: { + username: { label: 'Username', type: 'text' }, + password: { label: 'Password', type: 'password' }, + }, + async authorize(credentials) { + /** Return `true` if the user and password are correct */ + const matchCredentials = (username: string, password: string) => { + let valid = true; + + valid = username === process.env.HTTP_AUTH_USERNAME && valid; + valid = password === process.env.HTTP_AUTH_PASSWORD && valid; + + return valid; + }; + + const validCredentials = + !!credentials && matchCredentials(credentials.username, credentials.password); + + if (validCredentials) { + return { id: 'shared-user' }; + } + + return null; + }, + }), + ], +}; + +export default NextAuth(authOptions); diff --git a/frontend/yarn.lock b/frontend/yarn.lock index ea8e76d6..d18d1983 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -72,6 +72,15 @@ __metadata: languageName: node linkType: hard +"@babel/runtime@npm:^7.20.13": + version: 7.24.1 + resolution: "@babel/runtime@npm:7.24.1" + dependencies: + regenerator-runtime: ^0.14.0 + checksum: 5c8f3b912ba949865f03b3cf8395c60e1f4ebd1033fbd835bdfe81b6cac8a87d85bc3c7aded5fcdf07be044c9ab8c818f467abe0deca50020c72496782639572 + languageName: node + linkType: hard + "@deck.gl/aggregation-layers@npm:8.9.31": version: 8.9.31 resolution: "@deck.gl/aggregation-layers@npm:8.9.31" @@ -1259,6 +1268,13 @@ __metadata: languageName: node linkType: hard +"@panva/hkdf@npm:^1.0.2": + version: 1.1.1 + resolution: "@panva/hkdf@npm:1.1.1" + checksum: f0dd12903751d8792420353f809ed3c7de860cf506399759fff5f59f7acfef8a77e2b64012898cee7e5b047708fa0bd91dff5ef55a502bf8ea11aad9842160da + languageName: node + linkType: hard + "@pkgjs/parseargs@npm:^0.11.0": version: 0.11.0 resolution: "@pkgjs/parseargs@npm:0.11.0" @@ -5778,6 +5794,13 @@ __metadata: languageName: node linkType: hard +"cookie@npm:^0.5.0": + version: 0.5.0 + resolution: "cookie@npm:0.5.0" + checksum: 1f4bd2ca5765f8c9689a7e8954183f5332139eb72b6ff783d8947032ec1fdf43109852c178e21a953a30c0dd42257828185be01b49d1eb1a67fd054ca588a180 + languageName: node + linkType: hard + "copy-descriptor@npm:^0.1.0": version: 0.1.1 resolution: "copy-descriptor@npm:0.1.1" @@ -8906,6 +8929,13 @@ __metadata: languageName: node linkType: hard +"jose@npm:^4.15.5": + version: 4.15.5 + resolution: "jose@npm:4.15.5" + checksum: 7dde76447c7707bd4b448f914b216f3858e701aa83f00447434252461af5b9e159dcbffb88badea3f9616739526763581267c9560622f0a058df8d68c86d7f79 + languageName: node + linkType: hard + "jotai@npm:2.4.3": version: 2.4.3 resolution: "jotai@npm:2.4.3" @@ -9913,6 +9943,31 @@ __metadata: languageName: node linkType: hard +"next-auth@npm:^4.24.7": + version: 4.24.7 + resolution: "next-auth@npm:4.24.7" + dependencies: + "@babel/runtime": ^7.20.13 + "@panva/hkdf": ^1.0.2 + cookie: ^0.5.0 + jose: ^4.15.5 + oauth: ^0.9.15 + openid-client: ^5.4.0 + preact: ^10.6.3 + preact-render-to-string: ^5.1.19 + uuid: ^8.3.2 + peerDependencies: + next: ^12.2.5 || ^13 || ^14 + nodemailer: ^6.6.5 + react: ^17.0.2 || ^18 + react-dom: ^17.0.2 || ^18 + peerDependenciesMeta: + nodemailer: + optional: true + checksum: e7849ecf86394d86f08730b96f869c9353b6cc003794fcd953db2949f1ab06a2ce4f7a67312fdb8c7b3bbe7e99a8a215f11ef4eba5ec93f6c3e8ac1954e8b3e6 + languageName: node + linkType: hard + "next-usequerystate@npm:1.9.2": version: 1.9.2 resolution: "next-usequerystate@npm:1.9.2" @@ -10181,6 +10236,13 @@ __metadata: languageName: node linkType: hard +"oauth@npm:^0.9.15": + version: 0.9.15 + resolution: "oauth@npm:0.9.15" + checksum: 957c0d8d85300398dcb0e293953650c0fc3facc795bee8228238414f19f59cef5fd4ee8d17a972c142924c10c5f6ec50ef80f77f4a6cc6e3c98f9d22c027801c + languageName: node + linkType: hard + "object-assign@npm:*, object-assign@npm:^4.1.0, object-assign@npm:^4.1.1": version: 4.1.1 resolution: "object-assign@npm:4.1.1" @@ -10199,6 +10261,13 @@ __metadata: languageName: node linkType: hard +"object-hash@npm:^2.2.0": + version: 2.2.0 + resolution: "object-hash@npm:2.2.0" + checksum: 55ba841e3adce9c4f1b9b46b41983eda40f854e0d01af2802d3ae18a7085a17168d6b81731d43fdf1d6bcbb3c9f9c56d22c8fea992203ad90a38d7d919bc28f1 + languageName: node + linkType: hard + "object-hash@npm:^3.0.0": version: 3.0.0 resolution: "object-hash@npm:3.0.0" @@ -10315,6 +10384,13 @@ __metadata: languageName: node linkType: hard +"oidc-token-hash@npm:^5.0.3": + version: 5.0.3 + resolution: "oidc-token-hash@npm:5.0.3" + checksum: 35fa19aea9ff2c509029ec569d74b778c8a215b92bd5e6e9bc4ebbd7ab035f44304ff02430a6397c3fb7c1d15ebfa467807ca0bcd31d06ba610b47798287d303 + languageName: node + linkType: hard + "once@npm:^1.3.0, once@npm:^1.3.1, once@npm:^1.4.0": version: 1.4.0 resolution: "once@npm:1.4.0" @@ -10351,6 +10427,18 @@ __metadata: languageName: node linkType: hard +"openid-client@npm:^5.4.0": + version: 5.6.5 + resolution: "openid-client@npm:5.6.5" + dependencies: + jose: ^4.15.5 + lru-cache: ^6.0.0 + object-hash: ^2.2.0 + oidc-token-hash: ^5.0.3 + checksum: 2240079f761173b10635ce5fefbac04b6820f54e00d588ab2afdddb6c0f0ab6568e663cf1ab6a4a2297fbdbb73e42d78b8190f91dba7e1b80d287b2127fcbc7c + languageName: node + linkType: hard + "optionator@npm:^0.9.1": version: 0.9.3 resolution: "optionator@npm:0.9.3" @@ -10795,6 +10883,24 @@ __metadata: languageName: node linkType: hard +"preact-render-to-string@npm:^5.1.19": + version: 5.2.6 + resolution: "preact-render-to-string@npm:5.2.6" + dependencies: + pretty-format: ^3.8.0 + peerDependencies: + preact: ">=10" + checksum: be8d5d8fb502d422c503e68af7bcccb6facd942f3ae9a4d093ebe3f1d4f0b15c540624bdac434d53a2a8e8fb7afa4606383414e937c40933ca43445470a026ff + languageName: node + linkType: hard + +"preact@npm:^10.6.3": + version: 10.20.1 + resolution: "preact@npm:10.20.1" + checksum: af5ed9bdf44bfa5487479c09fa971a32902987f277c74d6244f9d9466ccd5c1efd3a0949e7dc2227177623878b1adafcc5c50cee6617a84f72d1cebe55ff76ba + languageName: node + linkType: hard + "prelude-ls@npm:^1.2.1": version: 1.2.1 resolution: "prelude-ls@npm:1.2.1" @@ -10829,6 +10935,13 @@ __metadata: languageName: node linkType: hard +"pretty-format@npm:^3.8.0": + version: 3.8.0 + resolution: "pretty-format@npm:3.8.0" + checksum: 21a114d43ef06978f8f7f6212be4649b0b094f05d9b30e14e37550bf35c8ca24d8adbca9e5adc4cc15d9eaf7a1e7a30478a4dc37b30982bfdf0292a5b385484c + languageName: node + linkType: hard + "printable-characters@npm:^1.0.42": version: 1.0.42 resolution: "printable-characters@npm:1.0.42" @@ -11859,6 +11972,7 @@ __metadata: lucide-react: ^0.274.0 mapbox-gl: 3.1.2 next: 13.5.6 + next-auth: ^4.24.7 next-usequerystate: 1.9.2 orval: 6.18.1 postcss: 8.4.21 @@ -13024,6 +13138,15 @@ __metadata: languageName: node linkType: hard +"uuid@npm:^8.3.2": + version: 8.3.2 + resolution: "uuid@npm:8.3.2" + bin: + uuid: dist/bin/uuid + checksum: 5575a8a75c13120e2f10e6ddc801b2c7ed7d8f3c8ac22c7ed0c7b2ba6383ec0abda88c905085d630e251719e0777045ae3236f04c812184b7c765f63a70e58df + languageName: node + linkType: hard + "validate-npm-package-name@npm:3.0.0": version: 3.0.0 resolution: "validate-npm-package-name@npm:3.0.0"