Skip to content

Commit

Permalink
Address PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ro-tex committed Mar 15, 2022
1 parent fb443e6 commit c0100d5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
54 changes: 28 additions & 26 deletions api/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,32 @@ func (akp APIKeyPOST) Validate() error {
return nil
}

// FromAPIKey populates the struct's fields from the given API key.
func (rwk *APIKeyResponse) FromAPIKey(ak database.APIKeyRecord) {
rwk.ID = ak.ID
rwk.UserID = ak.UserID
rwk.Public = ak.Public
rwk.Key = ak.Key
rwk.Skylinks = ak.Skylinks
rwk.CreatedAt = ak.CreatedAt
// APIKeyResponseFromAPIKey creates a new APIKeyResponse from the given API key.
func APIKeyResponseFromAPIKey(ak database.APIKeyRecord) *APIKeyResponse {
return &APIKeyResponse{
ID: ak.ID,
UserID: ak.UserID,
Public: ak.Public,
Key: ak.Key,
Skylinks: ak.Skylinks,
CreatedAt: ak.CreatedAt,
}
}

// FromAPIKey populates the struct's fields from the given API key.
func (rwk *APIKeyResponseWithKey) FromAPIKey(ak database.APIKeyRecord) {
rwk.ID = ak.ID
rwk.UserID = ak.UserID
rwk.Public = ak.Public
rwk.Key = ak.Key
rwk.Skylinks = ak.Skylinks
rwk.CreatedAt = ak.CreatedAt
// APIKeyResponseWithKeyFromAPIKey creates a new APIKeyResponseWithKey from the
// given API key.
func APIKeyResponseWithKeyFromAPIKey(ak database.APIKeyRecord) *APIKeyResponseWithKey {
return &APIKeyResponseWithKey{
APIKeyResponse: APIKeyResponse{
ID: ak.ID,
UserID: ak.UserID,
Public: ak.Public,
Key: ak.Key,
Skylinks: ak.Skylinks,
CreatedAt: ak.CreatedAt,
},
Key: ak.Key,
}
}

// userAPIKeyPOST creates a new API key for the user.
Expand All @@ -105,9 +113,7 @@ func (api *API) userAPIKeyPOST(u *database.User, w http.ResponseWriter, req *htt
api.WriteError(w, err, http.StatusInternalServerError)
return
}
var resp APIKeyResponseWithKey
resp.FromAPIKey(*ak)
api.WriteJSON(w, resp)
api.WriteJSON(w, APIKeyResponseWithKeyFromAPIKey(*ak))
}

// userAPIKeyGET returns a single API key.
Expand All @@ -127,9 +133,7 @@ func (api *API) userAPIKeyGET(u *database.User, w http.ResponseWriter, req *http
api.WriteError(w, err, http.StatusInternalServerError)
return
}
var resp APIKeyResponse
resp.FromAPIKey(ak)
api.WriteJSON(w, resp)
api.WriteJSON(w, APIKeyResponseFromAPIKey(ak))
}

// userAPIKeyLIST lists all API keys associated with the user.
Expand All @@ -139,11 +143,9 @@ func (api *API) userAPIKeyLIST(u *database.User, w http.ResponseWriter, req *htt
api.WriteError(w, err, http.StatusInternalServerError)
return
}
resp := make([]APIKeyResponse, 0, len(aks))
resp := make([]*APIKeyResponse, 0, len(aks))
for _, ak := range aks {
var r APIKeyResponse
r.FromAPIKey(ak)
resp = append(resp, r)
resp = append(resp, APIKeyResponseFromAPIKey(ak))
}
api.WriteJSON(w, resp)
}
Expand Down
2 changes: 1 addition & 1 deletion api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (api *API) userLimitsSkylinkGET(u *database.User, w http.ResponseWriter, re
// Validate the skylink.
skylink := ps.ByName("skylink")
if !database.ValidSkylinkHash(skylink) {
api.staticLogger.Tracef("Invalid skylink: %s", skylink)
api.staticLogger.Tracef("Invalid skylink: '%s'", skylink)
api.WriteJSON(w, respAnon)
return
}
Expand Down
6 changes: 3 additions & 3 deletions database/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (db *DB) APIKeyUpdate(ctx context.Context, user User, akID primitive.Object
// Validate all given skylinks.
for _, s := range skylinks {
if !ValidSkylinkHash(s) {
return ErrInvalidSkylink
return errors.AddContext(ErrInvalidSkylink, "offending skylink: "+s)
}
}
filter := bson.M{
Expand Down Expand Up @@ -272,7 +272,7 @@ func (db *DB) APIKeyPatch(ctx context.Context, user User, akID primitive.ObjectI
// Validate all given skylinks.
for _, s := range append(addSkylinks, removeSkylinks...) {
if !ValidSkylinkHash(s) {
return ErrInvalidSkylink
return errors.AddContext(ErrInvalidSkylink, "offending skylink: "+s)
}
}
filter := bson.M{
Expand All @@ -283,7 +283,7 @@ func (db *DB) APIKeyPatch(ctx context.Context, user User, akID primitive.ObjectI
// First, all new skylinks to the record.
if len(addSkylinks) > 0 {
update = bson.M{
"$push": bson.M{"skylinks": bson.M{"$each": addSkylinks}},
"$addToSet": bson.M{"skylinks": bson.M{"$each": addSkylinks}},
}
opts := options.UpdateOptions{
Upsert: &False,
Expand Down
2 changes: 1 addition & 1 deletion test/api/apikeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func testPrivateAPIKeysUsage(t *testing.T, at *test.AccountsTester) {
}
}

// TestPublicAPIKeyFlow validates the creation, listing, and deletion of public
// testPublicAPIKeysFlow validates the creation, listing, and deletion of public
// API keys.
func testPublicAPIKeysFlow(t *testing.T, at *test.AccountsTester) {
name := test.DBNameForTest(t.Name())
Expand Down

0 comments on commit c0100d5

Please sign in to comment.