2014-10-05 09:50:04 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2014-10-07 16:56:37 -05:00
|
|
|
"strconv"
|
2015-01-14 07:25:12 -06:00
|
|
|
"strings"
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2016-01-13 08:11:23 -06:00
|
|
|
"gopkg.in/macaron.v1"
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-02-26 10:23:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/apikeygen"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-12-13 02:15:52 -06:00
|
|
|
l "github.com/grafana/grafana/pkg/login"
|
2015-03-22 14:14:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-06-30 02:37:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-10-05 09:50:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
*macaron.Context
|
2015-01-16 09:17:35 -06:00
|
|
|
*m.SignedInUser
|
2015-01-16 07:32:18 -06:00
|
|
|
|
2015-04-07 12:21:14 -05:00
|
|
|
Session SessionStore
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-03-11 11:34:11 -05:00
|
|
|
IsSignedIn bool
|
2016-09-23 05:29:53 -05:00
|
|
|
IsRenderCall bool
|
2015-03-11 11:34:11 -05:00
|
|
|
AllowAnonymous bool
|
2016-06-06 16:06:44 -05:00
|
|
|
Logger log.Logger
|
2014-10-06 14:31:54 -05:00
|
|
|
}
|
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
func GetContextHandler() macaron.Handler {
|
2015-04-07 12:21:14 -05:00
|
|
|
return func(c *macaron.Context) {
|
2014-10-05 09:50:04 -05:00
|
|
|
ctx := &Context{
|
2015-03-11 11:34:11 -05:00
|
|
|
Context: c,
|
|
|
|
SignedInUser: &m.SignedInUser{},
|
2015-04-07 12:21:14 -05:00
|
|
|
Session: GetSession(),
|
2015-03-11 11:34:11 -05:00
|
|
|
IsSignedIn: false,
|
|
|
|
AllowAnonymous: false,
|
2016-06-06 16:06:44 -05:00
|
|
|
Logger: log.New("context"),
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
|
|
|
|
2015-04-08 01:59:12 -05:00
|
|
|
// the order in which these are tested are important
|
|
|
|
// look for api key in Authorization header first
|
|
|
|
// then init session and look for userId in session
|
|
|
|
// then look for api key in session (special case for render calls via api)
|
|
|
|
// then test if anonymous access is enabled
|
2016-09-23 05:29:53 -05:00
|
|
|
if initContextWithRenderAuth(ctx) ||
|
|
|
|
initContextWithApiKey(ctx) ||
|
2015-06-30 02:37:52 -05:00
|
|
|
initContextWithBasicAuth(ctx) ||
|
2015-05-02 05:06:58 -05:00
|
|
|
initContextWithAuthProxy(ctx) ||
|
2015-04-07 12:21:14 -05:00
|
|
|
initContextWithUserSessionCookie(ctx) ||
|
|
|
|
initContextWithAnonymousUser(ctx) {
|
2015-01-15 05:16:54 -06:00
|
|
|
}
|
|
|
|
|
2016-06-07 05:20:46 -05:00
|
|
|
ctx.Logger = log.New("context", "userId", ctx.UserId, "orgId", ctx.OrgId, "uname", ctx.Login)
|
2016-06-07 02:29:47 -05:00
|
|
|
ctx.Data["ctx"] = ctx
|
2016-06-06 16:06:44 -05:00
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
c.Map(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 12:21:14 -05:00
|
|
|
func initContextWithAnonymousUser(ctx *Context) bool {
|
|
|
|
if !setting.AnonymousEnabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
|
|
|
|
if err := bus.Dispatch(&orgQuery); err != nil {
|
|
|
|
log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
ctx.IsSignedIn = false
|
|
|
|
ctx.AllowAnonymous = true
|
|
|
|
ctx.SignedInUser = &m.SignedInUser{}
|
|
|
|
ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
|
|
|
|
ctx.OrgId = orgQuery.Result.Id
|
|
|
|
ctx.OrgName = orgQuery.Result.Name
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContextWithUserSessionCookie(ctx *Context) bool {
|
|
|
|
// initialize session
|
|
|
|
if err := ctx.Session.Start(ctx); err != nil {
|
2016-06-11 05:16:33 -05:00
|
|
|
ctx.Logger.Error("Failed to start session", "error", err)
|
2015-04-07 12:21:14 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var userId int64
|
|
|
|
if userId = getRequestUserId(ctx); userId == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
query := m.GetSignedInUserQuery{UserId: userId}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2016-06-11 05:16:33 -05:00
|
|
|
ctx.Logger.Error("Failed to get user with id", "userId", userId)
|
2015-04-07 12:21:14 -05:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
ctx.SignedInUser = query.Result
|
|
|
|
ctx.IsSignedIn = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContextWithApiKey(ctx *Context) bool {
|
|
|
|
var keyString string
|
|
|
|
if keyString = getApiKey(ctx); keyString == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// base64 decode key
|
|
|
|
decoded, err := apikeygen.Decode(keyString)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// fetch key
|
|
|
|
keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
|
|
|
|
if err := bus.Dispatch(&keyQuery); err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
apikey := keyQuery.Result
|
|
|
|
|
|
|
|
// validate api key
|
|
|
|
if !apikeygen.IsValid(decoded, apikey.Key) {
|
|
|
|
ctx.JsonApiErr(401, "Invalid API key", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.IsSignedIn = true
|
|
|
|
ctx.SignedInUser = &m.SignedInUser{}
|
2015-04-08 01:59:12 -05:00
|
|
|
ctx.OrgRole = apikey.Role
|
|
|
|
ctx.ApiKeyId = apikey.Id
|
|
|
|
ctx.OrgId = apikey.OrgId
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 02:37:52 -05:00
|
|
|
func initContextWithBasicAuth(ctx *Context) bool {
|
2016-12-13 02:15:52 -06:00
|
|
|
|
2015-06-30 02:37:52 -05:00
|
|
|
if !setting.BasicAuthEnabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
header := ctx.Req.Header.Get("Authorization")
|
|
|
|
if header == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
username, password, err := util.DecodeBasicAuthHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid Basic Auth Header", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
loginQuery := m.GetUserByLoginQuery{LoginOrEmail: username}
|
|
|
|
if err := bus.Dispatch(&loginQuery); err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Basic auth failed", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
user := loginQuery.Result
|
|
|
|
|
2016-12-13 02:15:52 -06:00
|
|
|
loginUserQuery := l.LoginUserQuery{Username: username, Password: password, User: user}
|
|
|
|
if err := bus.Dispatch(&loginUserQuery); err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Invalid username or password", err)
|
2015-06-30 02:37:52 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
query := m.GetSignedInUserQuery{UserId: user.Id}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
ctx.JsonApiErr(401, "Authentication error", err)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
ctx.SignedInUser = query.Result
|
|
|
|
ctx.IsSignedIn = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
// Handle handles and logs error by given status.
|
|
|
|
func (ctx *Context) Handle(status int, title string, err error) {
|
|
|
|
if err != nil {
|
2016-06-11 05:16:33 -05:00
|
|
|
ctx.Logger.Error(title, "error", err)
|
2015-01-29 08:46:54 -06:00
|
|
|
if setting.Env != setting.PROD {
|
2014-10-05 09:50:04 -05:00
|
|
|
ctx.Data["ErrorMsg"] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 05:23:24 -06:00
|
|
|
ctx.Data["Title"] = title
|
2016-11-16 10:41:44 -06:00
|
|
|
ctx.Data["AppSubUrl"] = setting.AppSubUrl
|
2014-10-07 16:56:37 -05:00
|
|
|
ctx.HTML(status, strconv.Itoa(status))
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
2014-10-05 14:13:01 -05:00
|
|
|
|
2014-12-16 20:09:54 -06:00
|
|
|
func (ctx *Context) JsonOK(message string) {
|
|
|
|
resp := make(map[string]interface{})
|
|
|
|
resp["message"] = message
|
|
|
|
ctx.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
func (ctx *Context) IsApiRequest() bool {
|
|
|
|
return strings.HasPrefix(ctx.Req.URL.Path, "/api")
|
|
|
|
}
|
|
|
|
|
2014-10-07 14:54:38 -05:00
|
|
|
func (ctx *Context) JsonApiErr(status int, message string, err error) {
|
2014-10-06 14:31:54 -05:00
|
|
|
resp := make(map[string]interface{})
|
|
|
|
|
|
|
|
if err != nil {
|
2016-06-11 05:16:33 -05:00
|
|
|
ctx.Logger.Error(message, "error", err)
|
2014-12-16 05:04:08 -06:00
|
|
|
if setting.Env != setting.PROD {
|
|
|
|
resp["error"] = err.Error()
|
2014-10-06 14:31:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case 404:
|
|
|
|
resp["message"] = "Not Found"
|
|
|
|
case 500:
|
|
|
|
resp["message"] = "Internal Server Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
if message != "" {
|
|
|
|
resp["message"] = message
|
|
|
|
}
|
|
|
|
|
2014-11-24 03:17:13 -06:00
|
|
|
ctx.JSON(status, resp)
|
2014-10-06 14:31:54 -05:00
|
|
|
}
|
2015-12-21 16:09:27 -06:00
|
|
|
|
2015-12-22 04:37:44 -06:00
|
|
|
func (ctx *Context) HasUserRole(role m.RoleType) bool {
|
2015-12-21 16:09:27 -06:00
|
|
|
return ctx.OrgRole.Includes(role)
|
|
|
|
}
|
2016-06-03 02:17:36 -05:00
|
|
|
|
2016-12-08 03:25:05 -06:00
|
|
|
func (ctx *Context) HasHelpFlag(flag m.HelpFlags1) bool {
|
|
|
|
return ctx.HelpFlags1.HasFlag(flag)
|
|
|
|
}
|
|
|
|
|
2016-06-03 02:17:36 -05:00
|
|
|
func (ctx *Context) TimeRequest(timer metrics.Timer) {
|
|
|
|
ctx.Data["perfmon.timer"] = timer
|
|
|
|
}
|