rework the way passwords expire

This commit is contained in:
1e99 2024-10-30 11:25:42 +01:00
parent 28bb9f6753
commit e3805b1a04
3 changed files with 16 additions and 22 deletions

View file

@ -12,8 +12,8 @@ import (
func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encoding) http.HandlerFunc { func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encoding) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) { return func(res http.ResponseWriter, req *http.Request) {
var reqBody struct { var reqBody struct {
Password string `json:"password"` Password string `json:"password"`
ExpiresIn string `json:"expires-in"` ExpiresIn time.Duration `json:"expires-in"`
} }
err := json.NewDecoder(req.Body).Decode(&reqBody) err := json.NewDecoder(req.Body).Decode(&reqBody)
if err != nil { if err != nil {
@ -32,20 +32,14 @@ func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encodin
return return
} }
var expiresIn time.Duration expiresIn := reqBody.ExpiresIn * time.Second
switch reqBody.ExpiresIn { if expiresIn <= 0 {
case "1-hour": http.Error(res, "Too short expires-in", http.StatusBadRequest)
expiresIn = 1 * time.Hour return
case "12-hours": }
expiresIn = 12 * time.Hour
case "1-day": if expiresIn > 2*7*24*time.Hour {
expiresIn = 1 * 24 * time.Hour http.Error(res, "Too long expires-in", http.StatusBadRequest)
case "1-week":
expiresIn = 1 * 7 * 24 * time.Hour
case "2-weeks":
expiresIn = 1 * 7 * 24 * time.Hour
default:
http.Error(res, "Bad expires-in", http.StatusBadRequest)
return return
} }

View file

@ -43,11 +43,11 @@
<label> <label>
Expires in Expires in
<select name="expires-in"> <select name="expires-in">
<option value="1-hour" selected>1 Hour</option> <option value="3600" selected>1 Hour</option>
<option value="12-hours">12 Hours</option> <option value="43200">12 Hours</option>
<option value="1-day">1 Day</option> <option value="86400">1 Day</option>
<option value="1-week">1 Week</option> <option value="604800">1 Week</option>
<option value="2-weeks">2 Weeks</option> <option value="1209600">2 Weeks</option>
</select> </select>
</label> </label>

View file

@ -145,7 +145,7 @@ function init() {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
password: encrypted.password, password: encrypted.password,
"expires-in": data.get("expires-in"), "expires-in": parseInt(data.get("expires-in")),
}), }),
}); });
if (!res.ok) { if (!res.ok) {