-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #15 from DIG-Network/release/v0.0.1-alpha.16
Release/v0.0.1 alpha.16
- Loading branch information
Showing
6 changed files
with
100 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,56 @@ | ||
import { getCredentials } from "../utils/authUtils"; | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
export const verifyAuthorization = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const credentials = await getCredentials(); | ||
|
||
if ( | ||
!credentials || | ||
credentials.password === "CHANGEME" || | ||
credentials.username === "CHANGEME" | ||
) { | ||
return res | ||
.status(500) | ||
.send( | ||
"The propagation server does not have a valid username and password set. Please set the DIG_USERNAME and DIG_PASSWORD environment variables." | ||
); | ||
} | ||
|
||
const authHeader = req.headers.authorization; | ||
|
||
if (!authHeader || !authHeader.startsWith("Basic ")) { | ||
// No Authorization header or it's not Basic Auth | ||
res.setHeader("WWW-Authenticate", 'Basic realm="Access to the site", charset="UTF-8"'); | ||
return res.status(401).send("Unauthorized: Missing Basic Auth header"); | ||
} | ||
|
||
// Decode base64 credentials | ||
const base64Credentials = authHeader.split(" ")[1]; | ||
const decodedCredentials = Buffer.from(base64Credentials, "base64").toString("utf-8"); | ||
const [username, password] = decodedCredentials.split(":"); | ||
|
||
if (!username || !password) { | ||
res.setHeader("WWW-Authenticate", 'Basic realm="Access to the site", charset="UTF-8"'); | ||
return res.status(401).send("Unauthorized: Invalid Basic Auth format"); | ||
} | ||
|
||
// Compare credentials | ||
if (username !== credentials.username || password !== credentials.password) { | ||
res.setHeader("WWW-Authenticate", 'Basic realm="Access to the site", charset="UTF-8"'); | ||
return res.status(401).send("Unauthorized: Invalid username or password"); | ||
} | ||
|
||
next(); // Credentials are valid; proceed to the next middleware | ||
} catch (error) { | ||
console.error("Error in verifyCredentials middleware:", error); | ||
return res | ||
.status(500) | ||
.send("An error occurred while verifying the credentials."); | ||
} | ||
}; | ||
|
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