-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add naive auth to server requests
- Loading branch information
Showing
6 changed files
with
172 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta name="robots" content="noindex" /> | ||
<title>Ontime Login</title> | ||
<style> | ||
body, | ||
html { | ||
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, | ||
sans-serif; | ||
background: #101010; | ||
color: #fafafa; | ||
text-align: center; | ||
box-sizing: border-box; | ||
} | ||
|
||
h1 { | ||
padding-top: 30vh; | ||
font-size: 3rem; | ||
color: #ff7597; | ||
font-weight: 400; | ||
} | ||
|
||
input { | ||
all: unset; | ||
text-align: start; | ||
padding-left: 0.5rem; | ||
background-color: #fffffa; | ||
color: black; | ||
|
||
height: 2rem; | ||
border-radius: 2px; | ||
margin-right: 0.5rem; | ||
} | ||
input:focus { | ||
outline: revert; | ||
color: #262626; | ||
} | ||
|
||
button { | ||
all: unset; | ||
color: #779be7; | ||
background: #2d2d2d; | ||
height: 2rem; | ||
padding-inline: 2rem; | ||
border-radius: 2px; | ||
cursor: pointer; | ||
} | ||
|
||
button:hover { | ||
background: #404040; | ||
} | ||
|
||
button:focus { | ||
outline: revert; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<form method="post" class="form"> | ||
<h1>Welcome to Ontime</h1> | ||
<input type="hidden" name="redirect" value="" id="redirect" /> | ||
<input type="password" name="password" placeholder="Password" required /> | ||
<button type="submit">Login</button> | ||
</form> | ||
</body> | ||
<script> | ||
// Get the query parameters from the URL | ||
const params = new URLSearchParams(window.location.search); | ||
const redirect = params.get('redirect'); | ||
|
||
// Set the value of the hidden input field | ||
if (redirect) { | ||
document.getElementById('redirect').value = redirect; | ||
} | ||
</script> | ||
</html> |
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,32 @@ | ||
import cookieParser from 'cookie-parser'; | ||
|
||
app.use(cookieParser()); | ||
|
||
// this password will need to come here from somewhere (ie: env) | ||
const password = 'password'; | ||
export const authenticatedMiddleware = (req, res, next) => { | ||
if (!password) { | ||
next(); | ||
} | ||
|
||
// the token will either be in the params or in the cookie | ||
const token = req.query.token || req.cookies?.token; | ||
if (token === password) { | ||
return next(); | ||
} | ||
|
||
const redirectUrl = `${prefix}/login?redirect=${encodeURIComponent(req.originalUrl)}`; | ||
res.redirect(`${prefix}/login?redirect=${redirectUrl}`); | ||
}; | ||
|
||
// on the form post we check the password and set the cookie | ||
// TODO: check that the user cookie is removed if the password changes | ||
app.post(`${prefix}/login`, (req, res) => { | ||
if (!password || req.body.password === password) { | ||
res.cookie('token', password, { httpOnly: true, secure: true, sameSite: 'strict' }); | ||
res.redirect(`${prefix}${req.body.redirect}`); | ||
return; | ||
} | ||
|
||
res.status(401).send('Unauthorized'); | ||
}); |
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.