Bläddra i källkod

Began work on hashing api keys

Torkel Ödegaard 11 år sedan
förälder
incheckning
5269422f7c

+ 3 - 5
pkg/api/api.go

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

+ 0 - 19
pkg/api/apikey.go

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

+ 30 - 0
pkg/components/apikeygen/apikeygen.go

@@ -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)))
+
+}

+ 0 - 1
pkg/models/apikey.go

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

+ 0 - 3
pkg/services/sqlstore/migrations.go

@@ -170,9 +170,6 @@ func addApiKeyMigrations(mg *Migrator) {
 	mg.AddMigration("add index api_key.account_id", new(AddIndexMigration).
 		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).
 		Table("api_key").Columns("account_id", "name").Unique())
 }

+ 0 - 1
src/app/features/account/partials/users.html

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