Auth: Fix correct error for updateapikey in context handler (#61544)

* fix: correct error for updateapikey

* refactor: send the correct err forward

* update: based on review
This commit is contained in:
Eric Leijonmarck 2023-01-16 15:16:06 +01:00 committed by GitHub
parent 997105c20d
commit 07bbc0716c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -350,11 +350,17 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo
return true
}
// update api_key last used date
if err := h.apiKeyService.UpdateAPIKeyLastUsedDate(reqContext.Req.Context(), apikey.Id); err != nil {
reqContext.JsonApiErr(http.StatusInternalServerError, InvalidAPIKey, errKey)
return true
}
// non-blocking update api_key last used date
go func(id int64) {
defer func() {
if err := recover(); err != nil {
reqContext.Logger.Error("api key authentication panic", "err", err)
}
}()
if err := h.apiKeyService.UpdateAPIKeyLastUsedDate(context.Background(), id); err != nil {
reqContext.Logger.Warn("failed to update last use date for api key", "id", id)
}
}(apikey.Id)
if apikey.ServiceAccountId == nil || *apikey.ServiceAccountId < 1 { //There is no service account attached to the apikey
// Use the old APIkey method. This provides backwards compatibility.