Api Key role is now correcty added do middleware context

This commit is contained in:
Torkel Ödegaard
2015-01-16 16:15:35 +01:00
parent 507bff8b59
commit 2b05dac071
6 changed files with 54 additions and 47 deletions

View File

@ -1,13 +1,11 @@
package middleware
import (
"errors"
"strconv"
"strings"
"github.com/Unknwon/macaron"
"github.com/torkelo/grafana-pro/pkg/bus"
m "github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
)
@ -17,11 +15,11 @@ type AuthOptions struct {
ReqSignedIn bool
}
func getRequestAccountId(c *Context) (int64, error) {
func getRequestAccountId(c *Context) int64 {
accountId := c.Session.Get("accountId")
if accountId != nil {
return accountId.(int64), nil
return accountId.(int64)
}
// localhost render query
@ -32,24 +30,18 @@ func getRequestAccountId(c *Context) (int64, error) {
accountId = accId
}
// check api token
return 0
}
func getApiToken(c *Context) string {
header := c.Req.Header.Get("Authorization")
parts := strings.SplitN(header, " ", 2)
if len(parts) == 2 || parts[0] == "Bearer" {
token := parts[1]
userQuery := m.GetAccountByTokenQuery{Token: token}
if err := bus.Dispatch(&userQuery); err != nil {
return -1, err
}
return userQuery.Result.Id, nil
return token
}
// anonymous gues user
if setting.Anonymous {
return setting.AnonymousAccountId, nil
}
return -1, errors.New("Auth: session account id not found")
return ""
}
func authDenied(c *Context) {