Allow querying key-tokens by token (including querying by id) [skip-tests]
All checks were successful
Build Docker and Deploy / Run Unit-Tests (push) Has been skipped
Build Docker and Deploy / Build Docker Container (push) Successful in 47s
Build Docker and Deploy / Deploy to Server (push) Successful in 6s

This commit is contained in:
Mike Schwörer 2025-04-12 22:19:41 +02:00
parent 86b8c47ed5
commit 301240b896
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -109,7 +109,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
// @ID api-tokenkeys-get-preview
// @Tags API-v2
//
// @Param kid path string true "TokenKeyID"
// @Param kid path string true "TokenKeyID (actual token || token-id)"
//
// @Success 200 {object} models.KeyTokenPreview
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
@ -120,7 +120,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
// @Router /api/v2/preview/keys/{kid} [GET]
func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPResponse {
type uri struct {
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
KeyID string `uri:"kid"`
}
var u uri
@ -136,15 +136,35 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
return *permResp
}
keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
if kid := models.KeyTokenID(u.KeyID); kid.Valid() == nil {
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.Preview()))
// Query by token.id
keytoken, err := h.database.GetKeyTokenByID(ctx, kid)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.Preview()))
} else {
// Query by token.token
keytoken, err := h.database.GetKeyTokenByToken(ctx, u.KeyID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.Preview()))
}
})
}