Files
grafana/pkg/api/apikey.go
T

98 lines
2.5 KiB
Go
Raw Normal View History

2015-01-27 08:26:11 +01:00
package api
import (
2020-11-19 13:34:28 +01:00
"errors"
2019-10-23 10:40:12 +02:00
"time"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
2015-02-05 10:37:13 +01:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/apikeygen"
2019-06-26 09:47:03 +03:00
"github.com/grafana/grafana/pkg/models"
2015-01-27 08:26:11 +01:00
)
func GetAPIKeys(c *models.ReqContext) response.Response {
2019-11-20 13:14:57 +02:00
query := models.GetApiKeysQuery{OrgId: c.OrgId, IncludeExpired: c.QueryBool("includeExpired")}
2015-01-27 08:26:11 +01:00
if err := bus.Dispatch(&query); err != nil {
return response.Error(500, "Failed to list api keys", err)
2015-01-27 08:26:11 +01:00
}
2019-06-26 09:47:03 +03:00
result := make([]*models.ApiKeyDTO, len(query.Result))
2015-01-27 08:26:11 +01:00
for i, t := range query.Result {
2019-06-26 09:47:03 +03:00
var expiration *time.Time = nil
if t.Expires != nil {
v := time.Unix(*t.Expires, 0)
expiration = &v
}
result[i] = &models.ApiKeyDTO{
Id: t.Id,
Name: t.Name,
Role: t.Role,
Expiration: expiration,
2015-01-27 08:26:11 +01:00
}
}
return response.JSON(200, result)
2015-01-27 08:26:11 +01:00
}
func DeleteAPIKey(c *models.ReqContext) response.Response {
2015-01-27 08:26:11 +01:00
id := c.ParamsInt64(":id")
2019-06-26 09:47:03 +03:00
cmd := &models.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId}
2015-01-27 08:26:11 +01:00
err := bus.Dispatch(cmd)
if err != nil {
var status int
if errors.Is(err, models.ErrApiKeyNotFound) {
status = 404
} else {
status = 500
}
return response.Error(status, "Failed to delete API key", err)
2015-01-27 08:26:11 +01:00
}
return response.Success("API key deleted")
2015-01-27 08:26:11 +01:00
}
func (hs *HTTPServer) AddAPIKey(c *models.ReqContext, cmd models.AddApiKeyCommand) response.Response {
2015-01-27 08:26:11 +01:00
if !cmd.Role.IsValid() {
return response.Error(400, "Invalid role specified", nil)
2015-01-27 08:26:11 +01:00
}
2019-06-26 09:47:03 +03:00
if hs.Cfg.ApiKeyMaxSecondsToLive != -1 {
if cmd.SecondsToLive == 0 {
return response.Error(400, "Number of seconds before expiration should be set", nil)
2019-06-26 09:47:03 +03:00
}
if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive {
return response.Error(400, "Number of seconds before expiration is greater than the global limit", nil)
2019-06-26 09:47:03 +03:00
}
}
cmd.OrgId = c.OrgId
2019-10-23 10:40:12 +02:00
newKeyInfo, err := apikeygen.New(cmd.OrgId, cmd.Name)
if err != nil {
return response.Error(500, "Generating API key failed", err)
2019-10-23 10:40:12 +02:00
}
cmd.Key = newKeyInfo.HashedKey
2015-01-27 08:26:11 +01:00
if err := bus.Dispatch(&cmd); err != nil {
2020-11-19 13:34:28 +01:00
if errors.Is(err, models.ErrInvalidApiKeyExpiration) {
return response.Error(400, err.Error(), nil)
2019-06-26 09:47:03 +03:00
}
2020-11-19 13:34:28 +01:00
if errors.Is(err, models.ErrDuplicateApiKey) {
return response.Error(409, err.Error(), nil)
}
return response.Error(500, "Failed to add API Key", err)
2015-01-27 08:26:11 +01:00
}
result := &dtos.NewApiKeyResult{
ID: cmd.Result.Id,
2015-01-27 08:26:11 +01:00
Name: cmd.Result.Name,
Key: newKeyInfo.ClientSecret,
}
2015-01-27 08:26:11 +01:00
return response.JSON(200, result)
2015-01-27 08:26:11 +01:00
}