render_auth.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package middleware
  2. import (
  3. "sync"
  4. m "github.com/grafana/grafana/pkg/models"
  5. "github.com/grafana/grafana/pkg/util"
  6. )
  7. var renderKeysLock sync.Mutex
  8. var renderKeys map[string]*m.SignedInUser = make(map[string]*m.SignedInUser)
  9. func initContextWithRenderAuth(ctx *Context) bool {
  10. key := ctx.GetCookie("renderKey")
  11. if key == "" {
  12. return false
  13. }
  14. renderKeysLock.Lock()
  15. defer renderKeysLock.Unlock()
  16. if renderUser, exists := renderKeys[key]; !exists {
  17. ctx.JsonApiErr(401, "Invalid Render Key", nil)
  18. return true
  19. } else {
  20. ctx.IsSignedIn = true
  21. ctx.SignedInUser = renderUser
  22. ctx.IsRenderCall = true
  23. return true
  24. }
  25. }
  26. type renderContextFunc func(key string) (string, error)
  27. func AddRenderAuthKey(orgId int64) string {
  28. renderKeysLock.Lock()
  29. key := util.GetRandomString(32)
  30. renderKeys[key] = &m.SignedInUser{
  31. OrgId: orgId,
  32. OrgRole: m.ROLE_VIEWER,
  33. }
  34. renderKeysLock.Unlock()
  35. return key
  36. }
  37. func RemoveRenderAuthKey(key string) {
  38. renderKeysLock.Lock()
  39. delete(renderKeys, key)
  40. renderKeysLock.Unlock()
  41. }