Allow querying key-tokens by token (including querying by id) [skip-tests]
This commit is contained in:
parent
86b8c47ed5
commit
301240b896
@ -109,7 +109,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
|
|||||||
// @ID api-tokenkeys-get-preview
|
// @ID api-tokenkeys-get-preview
|
||||||
// @Tags API-v2
|
// @Tags API-v2
|
||||||
//
|
//
|
||||||
// @Param kid path string true "TokenKeyID"
|
// @Param kid path string true "TokenKeyID (actual token || token-id)"
|
||||||
//
|
//
|
||||||
// @Success 200 {object} models.KeyTokenPreview
|
// @Success 200 {object} models.KeyTokenPreview
|
||||||
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
// @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]
|
// @Router /api/v2/preview/keys/{kid} [GET]
|
||||||
func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPResponse {
|
func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||||
type uri struct {
|
type uri struct {
|
||||||
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
|
KeyID string `uri:"kid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var u uri
|
var u uri
|
||||||
@ -136,15 +136,35 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
|
|||||||
return *permResp
|
return *permResp
|
||||||
}
|
}
|
||||||
|
|
||||||
keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID)
|
if kid := models.KeyTokenID(u.KeyID); kid.Valid() == nil {
|
||||||
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()))
|
// 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()))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user