2021-08-12 12:49:10 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/wiretrustee/wiretrustee/management/server"
|
|
|
|
"net/http"
|
2021-08-12 13:24:06 +02:00
|
|
|
"time"
|
2021-08-12 12:49:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupKeys is a handler that returns a list of setup keys of the account
|
|
|
|
type SetupKeys struct {
|
|
|
|
accountManager *server.AccountManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupKeyResponse is a response sent to the client
|
|
|
|
type SetupKeyResponse struct {
|
2021-08-12 13:24:06 +02:00
|
|
|
Key string
|
|
|
|
Name string
|
|
|
|
Expires time.Time
|
|
|
|
Type string
|
2021-08-19 21:12:21 +02:00
|
|
|
Valid bool
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSetupKeysHandler(accountManager *server.AccountManager) *SetupKeys {
|
|
|
|
return &SetupKeys{
|
|
|
|
accountManager: accountManager,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SetupKeys) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
accountId := extractAccountIdFromRequestContext(r)
|
|
|
|
//new user -> create a new account
|
|
|
|
account, err := h.accountManager.GetOrCreateAccount(accountId)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed getting user account %s: %v", accountId, err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
respBody := []*SetupKeyResponse{}
|
|
|
|
for _, key := range account.SetupKeys {
|
|
|
|
respBody = append(respBody, &SetupKeyResponse{
|
2021-08-12 13:24:06 +02:00
|
|
|
Key: key.Key,
|
2021-08-19 21:12:21 +02:00
|
|
|
Name: key.Name,
|
|
|
|
Expires: key.ExpiresAt,
|
|
|
|
Type: string(key.Type),
|
|
|
|
Valid: key.IsValid(),
|
2021-08-12 12:49:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(respBody)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed encoding account peers %s: %v", accountId, err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|