render_auth.go 1016 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. func AddRenderAuthKey(orgId int64, userId int64, orgRole m.RoleType) string {
  27. renderKeysLock.Lock()
  28. key := util.GetRandomString(32)
  29. renderKeys[key] = &m.SignedInUser{
  30. OrgId: orgId,
  31. OrgRole: orgRole,
  32. UserId: userId,
  33. }
  34. renderKeysLock.Unlock()
  35. return key
  36. }
  37. func RemoveRenderAuthKey(key string) {
  38. renderKeysLock.Lock()
  39. delete(renderKeys, key)
  40. renderKeysLock.Unlock()
  41. }