79 lines
-53 Removals
83 lines
+57 Additions
package api
package api
import (
import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/torkelo/grafana-pro/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/grafana/grafana/pkg/components/apikeygen"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/grafana/grafana/pkg/models"
"github.com/torkelo/grafana-pro/pkg/util"
"time"
)
)
func GetAPIKeys(c *models.ReqContext) Response {
func GetApiKeys(c *middleware.Context) {
query := models.GetApiKeysQuery{OrgId: c.OrgId}
query := m.GetApiKeysQuery{AccountId: c.AccountId}
if err := bus.Dispatch(&query); err != nil {
if err := bus.Dispatch(&query); err != nil {
return Error(500, "Failed to list api keys", err)
c.JsonApiErr(500, "Failed to list api keys", err)
return
}
}
result := make([]*models.ApiKeyDTO, len(query.Result))
result := make([]*m.ApiKeyDTO, len(query.Result))
for i, t := range query.Result {
for i, t := range query.Result {
var expiration *time.Time = nil
result[i] = &m.ApiKeyDTO{
if t.Expires != nil {
Id: t.Id,
v := time.Unix(*t.Expires, 0)
Name: t.Name,
expiration = &v
Role: t.Role,
}
Key: t.Key,
result[i] = &models.ApiKeyDTO{
Id: t.Id,
Name: t.Name,
Role: t.Role,
Expiration: expiration,
}
}
}
}
c.JSON(200, result)
return JSON(200, result)
}
}
func DeleteAPIKey(c *models.ReqContext) Response {
func DeleteApiKey(c *middleware.Context) {
id := c.ParamsInt64(":id")
id := c.ParamsInt64(":id")
cmd := &models.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId}
cmd := &m.DeleteApiKeyCommand{Id: id, AccountId: c.AccountId}
err := bus.Dispatch(cmd)
err := bus.Dispatch(cmd)
if err != nil {
if err != nil {
return Error(500, "Failed to delete API key", err)
c.JsonApiErr(500, "Failed to delete API key", err)
return
}
}
return Success("API key deleted")
c.JsonOK("API key deleted")
}
}
func (hs *HTTPServer) AddAPIKey(c *models.ReqContext, cmd models.AddApiKeyCommand) Response {
func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) {
if !cmd.Role.IsValid() {
if !cmd.Role.IsValid() {
return Error(400, "Invalid role specified", nil)
c.JsonApiErr(400, "Invalid role specified", nil)
}
return
if hs.Cfg.ApiKeyMaxSecondsToLive != -1 {
if cmd.SecondsToLive == 0 {
return Error(400, "Number of seconds before expiration should be set", nil)
}
if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive {
return Error(400, "Number of seconds before expiration is greater than the global limit", nil)
}
}
}
cmd.OrgId = c.OrgId
newKeyInfo := apikeygen.New(cmd.OrgId, cmd.Name)
cmd.AccountId = c.AccountId
cmd.Key = newKeyInfo.HashedKey
cmd.Key = util.GetRandomString(64)
if err := bus.Dispatch(&cmd); err != nil {
if err := bus.Dispatch(&cmd); err != nil {
if err == models.ErrInvalidApiKeyExpiration {
c.JsonApiErr(500, "Failed to add API key", err)
return Error(400, err.Error(), nil)
return
}
return Error(500, "Failed to add API key", err)
}
}
result := &dtos.NewApiKeyResult{
result := &m.ApiKeyDTO{
Id: cmd.Result.Id,
Name: cmd.Result.Name,
Name: cmd.Result.Name,
Key: newKeyInfo.ClientSecret}
Role: cmd.Result.Role,
Key: cmd.Result.Key,
}
return JSON(200, result)
c.JSON(200, result)
}
func UpdateApiKey(c *middleware.Context, cmd m.UpdateApiKeyCommand) {
if !cmd.Role.IsValid() {
c.JsonApiErr(400, "Invalid role specified", nil)
return
}
cmd.AccountId = c.AccountId
err := bus.Dispatch(&cmd)
if err != nil {
c.JsonApiErr(500, "Failed to update api key", err)
return
}
c.JsonOK("API key updated")
}
}
Editor
Clear
Export as PDF