auth_proxy.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package middleware
  2. import (
  3. "github.com/grafana/grafana/pkg/infra/remotecache"
  4. authproxy "github.com/grafana/grafana/pkg/middleware/auth_proxy"
  5. m "github.com/grafana/grafana/pkg/models"
  6. )
  7. const (
  8. // cachePrefix is a prefix for the cache key
  9. cachePrefix = authproxy.CachePrefix
  10. )
  11. func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *m.ReqContext, orgID int64) bool {
  12. auth := authproxy.New(&authproxy.Options{
  13. Store: store,
  14. Ctx: ctx,
  15. OrgID: orgID,
  16. })
  17. // Bail if auth proxy is not enabled
  18. if auth.IsEnabled() == false {
  19. return false
  20. }
  21. // If the there is no header - we can't move forward
  22. if auth.HasHeader() == false {
  23. return false
  24. }
  25. // Check if allowed to continue with this IP
  26. if result, err := auth.IsAllowedIP(); result == false {
  27. ctx.Handle(407, err.Error(), err.DetailsError)
  28. return true
  29. }
  30. // Try to get user id from various sources
  31. id, err := auth.GetUserID()
  32. if err != nil {
  33. ctx.Handle(500, err.Error(), err.DetailsError)
  34. return true
  35. }
  36. // Get full user info
  37. user, err := auth.GetSignedUser(id)
  38. if err != nil {
  39. ctx.Handle(500, err.Error(), err.DetailsError)
  40. return true
  41. }
  42. // Add user info to context
  43. ctx.SignedInUser = user
  44. ctx.IsSignedIn = true
  45. // Remember user data it in cache
  46. if err := auth.Remember(); err != nil {
  47. ctx.Handle(500, err.Error(), err.DetailsError)
  48. return true
  49. }
  50. return true
  51. }