render_auth.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 *m.ReqContext) bool {
  10. key := ctx.GetCookie("renderKey")
  11. if key == "" {
  12. return false
  13. }
  14. renderKeysLock.Lock()
  15. defer renderKeysLock.Unlock()
  16. renderUser, exists := renderKeys[key]
  17. if !exists {
  18. ctx.JsonApiErr(401, "Invalid Render Key", nil)
  19. return true
  20. }
  21. ctx.IsSignedIn = true
  22. ctx.SignedInUser = renderUser
  23. ctx.IsRenderCall = true
  24. return true
  25. }
  26. type renderContextFunc func(key string) (string, error)
  27. func AddRenderAuthKey(orgId int64, userId int64, orgRole m.RoleType) string {
  28. renderKeysLock.Lock()
  29. key := util.GetRandomString(32)
  30. renderKeys[key] = &m.SignedInUser{
  31. OrgId: orgId,
  32. OrgRole: orgRole,
  33. UserId: userId,
  34. }
  35. renderKeysLock.Unlock()
  36. return key
  37. }
  38. func RemoveRenderAuthKey(key string) {
  39. renderKeysLock.Lock()
  40. delete(renderKeys, key)
  41. renderKeysLock.Unlock()
  42. }