Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func (manager *AccountManager) AddPeer(setupKey string, peerKey string) (*Peer,
}

account.Peers[newPeer.Key] = newPeer
account.SetupKeys[sk.Key] = sk.IncrementUsage()
err = manager.Store.SaveAccount(account)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed adding peer")
Expand Down
37 changes: 23 additions & 14 deletions management/server/http/handler/setupkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ type SetupKeys struct {

// SetupKeyResponse is a response sent to the client
type SetupKeyResponse struct {
Id string
Key string
Name string
Expires time.Time
Type server.SetupKeyType
Valid bool
Revoked bool
Id string
Key string
Name string
Expires time.Time
Type server.SetupKeyType
Valid bool
Revoked bool
UsedTimes int
LastUsed time.Time
}

// SetupKeyRequest is a request sent by client. This object contains fields that can be modified
Expand All @@ -50,6 +52,11 @@ func (h *SetupKeys) CreateKey(w http.ResponseWriter, r *http.Request) {
return
}

if !(req.Type == server.SetupKeyReusable || req.Type == server.SetupKeyOneOff) {
http.Error(w, "unknown setup key type "+string(req.Type), http.StatusBadRequest)
return
}

setupKey, err := h.accountManager.AddSetupKey(accountId, req.Name, req.Type, req.ExpiresIn.Duration)
if err != nil {
errStatus, ok := status.FromError(err)
Expand Down Expand Up @@ -166,12 +173,14 @@ func writeSuccess(w http.ResponseWriter, key *server.SetupKey) {

func toResponseBody(key *server.SetupKey) *SetupKeyResponse {
return &SetupKeyResponse{
Id: key.Id,
Key: key.Key,
Name: key.Name,
Expires: key.ExpiresAt,
Type: key.Type,
Valid: key.IsValid(),
Revoked: key.Revoked,
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,
}
}
10 changes: 10 additions & 0 deletions management/server/setupkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type SetupKey struct {
Revoked bool
// UsedTimes indicates how many times the key was used
UsedTimes int
// LastUsed last time the key was used for peer registration
LastUsed time.Time
}

//Copy copies SetupKey to a new object
Expand All @@ -51,6 +53,14 @@ func (key *SetupKey) Copy() *SetupKey {
}
}

//IncrementUsage makes a copy of a key, increments the UsedTimes by 1 and sets LastUsed to now
func (key *SetupKey) IncrementUsage() *SetupKey {
c := key.Copy()
c.UsedTimes = c.UsedTimes + 1
c.LastUsed = time.Now()
return c
}

// IsValid is true if the key was not revoked, is not expired and used not more than it was supposed to
func (key *SetupKey) IsValid() bool {
expired := time.Now().After(key.ExpiresAt)
Expand Down