Began work on hashing api keys

pull/1540/head
Torkel Ödegaard 11 years ago
parent c9f06e1da1
commit 5269422f7c
  1. 8
      pkg/api/api.go
  2. 19
      pkg/api/apikey.go
  3. 30
      pkg/components/apikeygen/apikeygen.go
  4. 1
      pkg/models/apikey.go
  5. 3
      pkg/services/sqlstore/migrations.go
  6. 1
      src/app/features/account/partials/users.html

@ -2,10 +2,10 @@ package api
import ( import (
"github.com/Unknwon/macaron" "github.com/Unknwon/macaron"
"github.com/macaron-contrib/binding"
"github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/macaron-contrib/binding"
) )
// Register adds http routes // Register adds http routes
@ -61,10 +61,8 @@ func Register(r *macaron.Macaron) {
// auth api keys // auth api keys
r.Group("/auth/keys", func() { r.Group("/auth/keys", func() {
r.Combo("/"). r.Get("/", GetApiKeys)
Get(GetApiKeys). r.Post("/", bind(m.AddApiKeyCommand{}), AddApiKey)
Post(bind(m.AddApiKeyCommand{}), AddApiKey).
Put(bind(m.UpdateApiKeyCommand{}), UpdateApiKey)
r.Delete("/:id", DeleteApiKey) r.Delete("/:id", DeleteApiKey)
}, reqAccountAdmin) }, reqAccountAdmin)

@ -21,7 +21,6 @@ func GetApiKeys(c *middleware.Context) {
Id: t.Id, Id: t.Id,
Name: t.Name, Name: t.Name,
Role: t.Role, Role: t.Role,
Key: t.Key,
} }
} }
c.JSON(200, result) c.JSON(200, result)
@ -59,25 +58,7 @@ func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) {
Id: cmd.Result.Id, Id: cmd.Result.Id,
Name: cmd.Result.Name, Name: cmd.Result.Name,
Role: cmd.Result.Role, Role: cmd.Result.Role,
Key: cmd.Result.Key,
} }
c.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")
}

@ -0,0 +1,30 @@
package apikeygen
import (
"strconv"
"github.com/grafana/grafana/pkg/util"
)
type KeyGenResult struct {
HashedKey string
JsonKeyEncoded string
}
type ApiKeyJson struct {
Key string
AccountId int64
Name string
}
func GenerateNewKey(accountId int64, name string) KeyGenResult {
jsonKey := ApiKeyJson{}
jsonKey.AccountId = accountId
jsonKey.Name = name
jsonKey.Key = util.GetRandomString(32)
result := KeyGenResult{}
result.HashedKey = util.EncodePassword([]byte(jsonKey.Key), []byte(strconv.FormatInt(accountId, 10)))
}

@ -60,6 +60,5 @@ type GetApiKeyByKeyQuery struct {
type ApiKeyDTO struct { type ApiKeyDTO struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Key string `json:"key"`
Role RoleType `json:"role"` Role RoleType `json:"role"`
} }

@ -170,9 +170,6 @@ func addApiKeyMigrations(mg *Migrator) {
mg.AddMigration("add index api_key.account_id", new(AddIndexMigration). mg.AddMigration("add index api_key.account_id", new(AddIndexMigration).
Table("api_key").Columns("account_id")) Table("api_key").Columns("account_id"))
mg.AddMigration("add index api_key.key", new(AddIndexMigration).
Table("api_key").Columns("key").Unique())
mg.AddMigration("add index api_key.account_id_name", new(AddIndexMigration). mg.AddMigration("add index api_key.account_id_name", new(AddIndexMigration).
Table("api_key").Columns("account_id", "name").Unique()) Table("api_key").Columns("account_id", "name").Unique())
} }

@ -11,7 +11,6 @@
<h2>Account users</h2> <h2>Account users</h2>
<form name="form"> <form name="form">
<div class="tight-form"> <div class="tight-form">
<ul class="tight-form-list"> <ul class="tight-form-list">

Loading…
Cancel
Save