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

View file

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

View file

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