-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1776 from carbon-design-system/fix/readd-authenti…
…cation fix: re-add authentication to web-app Co-authored-by: Joe Harvey <jdharvey-ibm@users.noreply.github.com>
- Loading branch information
Showing
303 changed files
with
410 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright IBM Corp. 2022, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { NextResponse } from 'next/server' | ||
|
||
import sendLocalRequest from '@/utils/sendLocalRequest' | ||
import { isValidIbmEmail } from '@/utils/string' | ||
|
||
class NotAuthorizedException extends Error {} | ||
|
||
function exitWith404(req) { | ||
const url = req.nextUrl.clone() | ||
url.pathname = '/404' | ||
return NextResponse.rewrite(url) | ||
} | ||
|
||
async function applyAuthMiddleware(req) { | ||
if ( | ||
req.nextUrl.pathname === '/404' || | ||
req.nextUrl.pathname.startsWith('/api/') || | ||
req.nextUrl.pathname.startsWith('/_next/') || | ||
req.nextUrl.pathname.startsWith('/vendor/') || | ||
req.nextUrl.pathname === 'favicon.ico' | ||
) { | ||
return NextResponse.next() | ||
} | ||
|
||
const userResponse = await sendLocalRequest(req, '/api/user', true) | ||
|
||
// Guard - non-200 user api response | ||
if (!userResponse.ok) { | ||
throw new NotAuthorizedException() | ||
} | ||
|
||
const user = await userResponse.json() | ||
|
||
// Guard - no valid user | ||
if (!user?.email) { | ||
throw new NotAuthorizedException() | ||
} | ||
|
||
// Guard - not a valid IBMer | ||
if (!isValidIbmEmail(user.email)) { | ||
throw new NotAuthorizedException() | ||
} | ||
|
||
return NextResponse.next() | ||
} | ||
|
||
export default async function middleware(req) { | ||
try { | ||
await applyAuthMiddleware(req) | ||
} catch (err) { | ||
if (err instanceof NotAuthorizedException) { | ||
return exitWith404(req) | ||
} | ||
throw err | ||
} | ||
|
||
return NextResponse.next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.