-
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.
fix(security): add basic auth to articles
- Loading branch information
1 parent
fb3aae9
commit b4e635d
Showing
6 changed files
with
84 additions
and
26 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
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 |
---|---|---|
@@ -1,28 +1,51 @@ | ||
import jwt from "jsonwebtoken"; | ||
|
||
const auth = (req, res, next) => { | ||
try { | ||
const token = req.header("Authorization"); | ||
if (!token) | ||
return res.status(400).json({ msg: "Invalid Authentication - no token" }); | ||
try { | ||
const token = req.header("Authorization"); | ||
if (!token) | ||
return res.status(400).json({ msg: "Invalid Authentication - no token" }); | ||
|
||
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) => { | ||
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, user) => { | ||
if (err instanceof jwt.TokenExpiredError) { | ||
return res | ||
.status(400) | ||
.json({ msg: "Token Expired Error", err}); | ||
return res.status(400).json({ msg: "Token Expired Error", err }); | ||
} | ||
if (err) | ||
return res | ||
.status(400) | ||
.json({ msg: "Invalid Authentication - invalid token"}); | ||
|
||
req.user = user; | ||
next(); | ||
}); | ||
} catch (err) { | ||
return res.status(500).json({ msg: err.message }); | ||
} | ||
return res | ||
.status(400) | ||
.json({ msg: "Invalid Authentication - invalid token" }); | ||
|
||
req.user = user; | ||
next(); | ||
}); | ||
} catch (err) { | ||
return res.status(500).json({ msg: err.message }); | ||
} | ||
}; | ||
|
||
export function basicAuth(req, res, next) { | ||
const authheader = req.headers.authorization; | ||
console.log({ authheader }); | ||
if (!authheader) { | ||
res.setHeader("WWW-Authenticate", "Basic"); | ||
return res.status(401).json({ msg: "Unauthentication - Auth401" }); | ||
} | ||
|
||
const auth = new Buffer.from(authheader.split(" ")[1], "base64") | ||
.toString() | ||
.split(":"); | ||
|
||
const user = auth[0]; | ||
const pass = auth[1]; | ||
|
||
if (user == "admin" && pass == "password") { | ||
// If Authorized user | ||
|
||
next(); | ||
} else { | ||
res.setHeader("WWW-Authenticate", "Basic"); | ||
return res.status(401).json({ msg: "Unauthentication - Auth401" }); | ||
} | ||
} | ||
|
||
export default auth; |