2016-09-23 05:29:53 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var renderKeysLock sync.Mutex
|
|
|
|
var renderKeys map[string]*m.SignedInUser = make(map[string]*m.SignedInUser)
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func initContextWithRenderAuth(ctx *m.ReqContext) bool {
|
2016-09-23 05:29:53 -05:00
|
|
|
key := ctx.GetCookie("renderKey")
|
|
|
|
if key == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
renderKeysLock.Lock()
|
|
|
|
defer renderKeysLock.Unlock()
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
renderUser, exists := renderKeys[key]
|
|
|
|
if !exists {
|
2016-09-23 05:29:53 -05:00
|
|
|
ctx.JsonApiErr(401, "Invalid Render Key", nil)
|
|
|
|
return true
|
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
|
|
|
|
ctx.IsSignedIn = true
|
|
|
|
ctx.SignedInUser = renderUser
|
|
|
|
ctx.IsRenderCall = true
|
|
|
|
return true
|
2016-09-23 05:29:53 -05:00
|
|
|
}
|
|
|
|
|
2017-06-21 18:23:36 -05:00
|
|
|
func AddRenderAuthKey(orgId int64, userId int64, orgRole m.RoleType) string {
|
2016-09-23 05:29:53 -05:00
|
|
|
renderKeysLock.Lock()
|
|
|
|
|
|
|
|
key := util.GetRandomString(32)
|
|
|
|
|
|
|
|
renderKeys[key] = &m.SignedInUser{
|
|
|
|
OrgId: orgId,
|
2017-06-21 18:23:36 -05:00
|
|
|
OrgRole: orgRole,
|
|
|
|
UserId: userId,
|
2016-09-23 05:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
renderKeysLock.Unlock()
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveRenderAuthKey(key string) {
|
|
|
|
renderKeysLock.Lock()
|
|
|
|
delete(renderKeys, key)
|
|
|
|
renderKeysLock.Unlock()
|
|
|
|
}
|