2021-08-12 12:49:10 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-02-11 17:18:18 +01:00
|
|
|
"fmt"
|
2022-02-23 20:02:02 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/management/server/jwtclaims"
|
2022-02-22 11:28:19 +01:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
"github.com/gorilla/mux"
|
2021-08-12 12:49:10 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/wiretrustee/wiretrustee/management/server"
|
2021-10-31 12:06:44 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/util"
|
2021-08-20 22:33:43 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
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 {
|
2022-02-22 11:28:19 +01:00
|
|
|
accountManager server.AccountManager
|
2022-01-24 11:21:30 +01:00
|
|
|
authAudience string
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupKeyResponse is a response sent to the client
|
|
|
|
type SetupKeyResponse struct {
|
2021-08-22 11:29:25 +02:00
|
|
|
Id string
|
|
|
|
Key string
|
|
|
|
Name string
|
|
|
|
Expires time.Time
|
|
|
|
Type server.SetupKeyType
|
|
|
|
Valid bool
|
|
|
|
Revoked bool
|
|
|
|
UsedTimes int
|
|
|
|
LastUsed time.Time
|
2021-08-25 14:16:17 +02:00
|
|
|
State string
|
2021-08-20 22:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupKeyRequest is a request sent by client. This object contains fields that can be modified
|
|
|
|
type SetupKeyRequest struct {
|
|
|
|
Name string
|
|
|
|
Type server.SetupKeyType
|
2021-10-31 12:06:44 +01:00
|
|
|
ExpiresIn *util.Duration
|
2021-08-20 22:33:43 +02:00
|
|
|
Revoked bool
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:28:19 +01:00
|
|
|
func NewSetupKeysHandler(accountManager server.AccountManager, authAudience string) *SetupKeys {
|
2021-08-12 12:49:10 +02:00
|
|
|
return &SetupKeys{
|
|
|
|
accountManager: accountManager,
|
2022-01-24 11:21:30 +01:00
|
|
|
authAudience: authAudience,
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
func (h *SetupKeys) updateKey(accountId string, keyId string, w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := &SetupKeyRequest{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var key *server.SetupKey
|
|
|
|
if req.Revoked {
|
|
|
|
//handle only if being revoked, don't allow to enable key again for now
|
|
|
|
key, err = h.accountManager.RevokeSetupKey(accountId, keyId)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed revoking key", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(req.Name) != 0 {
|
|
|
|
key, err = h.accountManager.RenameSetupKey(accountId, keyId, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed renaming key", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if key != nil {
|
|
|
|
writeSuccess(w, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SetupKeys) getKey(accountId string, keyId string, w http.ResponseWriter, r *http.Request) {
|
2022-02-11 17:18:18 +01:00
|
|
|
account, err := h.accountManager.GetAccountById(accountId)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "account doesn't exist", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, key := range account.SetupKeys {
|
|
|
|
if key.Id == keyId {
|
|
|
|
writeSuccess(w, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http.Error(w, "setup key not found", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SetupKeys) createKey(accountId string, w http.ResponseWriter, r *http.Request) {
|
2021-08-20 22:33:43 +02:00
|
|
|
req := &SetupKeyRequest{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-22 11:29:25 +02:00
|
|
|
if !(req.Type == server.SetupKeyReusable || req.Type == server.SetupKeyOneOff) {
|
|
|
|
http.Error(w, "unknown setup key type "+string(req.Type), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-31 12:06:44 +01:00
|
|
|
setupKey, err := h.accountManager.AddSetupKey(accountId, req.Name, req.Type, req.ExpiresIn)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
errStatus, ok := status.FromError(err)
|
|
|
|
if ok && errStatus.Code() == codes.NotFound {
|
|
|
|
http.Error(w, "account not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, "failed adding setup key", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccess(w, setupKey)
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:18:18 +01:00
|
|
|
func (h *SetupKeys) getSetupKeyAccount(r *http.Request) (*server.Account, error) {
|
2022-02-23 20:02:02 +01:00
|
|
|
extractor := jwtclaims.NewClaimsExtractor(nil)
|
|
|
|
jwtClaims := extractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2022-02-11 17:18:18 +01:00
|
|
|
|
|
|
|
account, err := h.accountManager.GetAccountByUserOrAccountId(jwtClaims.UserId, jwtClaims.AccountId, jwtClaims.Domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed getting account of a user %s: %v", jwtClaims.UserId, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
func (h *SetupKeys) HandleKey(w http.ResponseWriter, r *http.Request) {
|
2022-02-11 17:18:18 +01:00
|
|
|
account, err := h.getSetupKeyAccount(r)
|
2021-12-27 13:17:15 +01:00
|
|
|
if err != nil {
|
2022-02-11 17:18:18 +01:00
|
|
|
log.Error(err)
|
2021-12-27 13:17:15 +01:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
keyId := vars["id"]
|
|
|
|
if len(keyId) == 0 {
|
|
|
|
http.Error(w, "invalid key Id", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
2021-08-23 21:43:05 +02:00
|
|
|
case http.MethodPut:
|
2021-12-27 13:17:15 +01:00
|
|
|
h.updateKey(account.Id, keyId, w, r)
|
2021-08-20 22:33:43 +02:00
|
|
|
return
|
|
|
|
case http.MethodGet:
|
2021-12-27 13:17:15 +01:00
|
|
|
h.getKey(account.Id, keyId, w, r)
|
2021-08-20 22:33:43 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SetupKeys) GetKeys(w http.ResponseWriter, r *http.Request) {
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2022-02-11 17:18:18 +01:00
|
|
|
account, err := h.getSetupKeyAccount(r)
|
2021-12-27 13:17:15 +01:00
|
|
|
if err != nil {
|
2022-02-11 17:18:18 +01:00
|
|
|
log.Error(err)
|
2021-12-27 13:17:15 +01:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2021-08-12 12:49:10 +02:00
|
|
|
switch r.Method {
|
2021-08-23 21:43:05 +02:00
|
|
|
case http.MethodPost:
|
2021-12-27 13:17:15 +01:00
|
|
|
h.createKey(account.Id, w, r)
|
2021-08-23 21:43:05 +02:00
|
|
|
return
|
2021-08-12 12:49:10 +02:00
|
|
|
case http.MethodGet:
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
respBody := []*SetupKeyResponse{}
|
|
|
|
for _, key := range account.SetupKeys {
|
2021-08-20 22:33:43 +02:00
|
|
|
respBody = append(respBody, toResponseBody(key))
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(respBody)
|
|
|
|
if err != nil {
|
2021-12-27 13:17:15 +01:00
|
|
|
log.Errorf("failed encoding account peers %s: %v", account.Id, err)
|
2021-08-12 12:49:10 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
http.Error(w, "", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
|
|
|
|
func writeSuccess(w http.ResponseWriter, key *server.SetupKey) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(toResponseBody(key))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed handling request", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toResponseBody(key *server.SetupKey) *SetupKeyResponse {
|
2021-08-25 14:16:17 +02:00
|
|
|
var state string
|
|
|
|
if key.IsExpired() {
|
|
|
|
state = "expired"
|
|
|
|
} else if key.IsRevoked() {
|
|
|
|
state = "revoked"
|
|
|
|
} else if key.IsOverUsed() {
|
|
|
|
state = "overused"
|
|
|
|
} else {
|
|
|
|
state = "valid"
|
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
return &SetupKeyResponse{
|
2021-08-22 11:29:25 +02:00
|
|
|
Id: key.Id,
|
|
|
|
Key: key.Key,
|
|
|
|
Name: key.Name,
|
|
|
|
Expires: key.ExpiresAt,
|
|
|
|
Type: key.Type,
|
|
|
|
Valid: key.IsValid(),
|
|
|
|
Revoked: key.Revoked,
|
|
|
|
UsedTimes: key.UsedTimes,
|
|
|
|
LastUsed: key.LastUsed,
|
2021-08-25 14:16:17 +02:00
|
|
|
State: state,
|
2021-08-20 22:33:43 +02:00
|
|
|
}
|
|
|
|
}
|