[#85] Allow to customize secret expiry (#93)

This commit is contained in:
Knut Ahlers 2023-06-26 23:01:06 +02:00 committed by GitHub
parent 62ca7b3900
commit 901c85ca11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 233 additions and 61 deletions

33
api.go
View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
"time"
@ -17,10 +18,11 @@ type apiServer struct {
}
type apiResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
Secret string `json:"secret,omitempty"`
SecretId string `json:"secret_id,omitempty"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Secret string `json:"secret,omitempty"`
SecretId string `json:"secret_id,omitempty"`
}
type apiRequest struct {
@ -40,7 +42,16 @@ func (a apiServer) Register(r *mux.Router) {
}
func (a apiServer) handleCreate(res http.ResponseWriter, r *http.Request) {
var secret string
var (
expiry = cfg.SecretExpiry
secret string
)
if !cust.DisableExpiryOverride {
if ev, err := strconv.ParseInt(r.URL.Query().Get("expire"), 10, 64); err == nil && (ev < expiry || cfg.SecretExpiry == 0) {
expiry = ev
}
}
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
tmp := apiRequest{}
@ -58,15 +69,21 @@ func (a apiServer) handleCreate(res http.ResponseWriter, r *http.Request) {
return
}
id, err := a.store.Create(secret, time.Duration(cfg.SecretExpiry)*time.Second)
id, err := a.store.Create(secret, time.Duration(expiry)*time.Second)
if err != nil {
a.errorResponse(res, http.StatusInternalServerError, err, "creating secret")
return
}
var expiresAt *time.Time
if expiry > 0 {
expiresAt = func(v time.Time) *time.Time { return &v }(time.Now().UTC().Add(time.Duration(expiry) * time.Second))
}
a.jsonResponse(res, http.StatusCreated, apiResponse{
Success: true,
SecretId: id,
ExpiresAt: expiresAt,
Success: true,
SecretId: id,
})
}