auth_proxy.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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() {
  19. return false
  20. }
  21. // If the there is no header - we can't move forward
  22. if !auth.HasHeader() {
  23. return false
  24. }
  25. // Check if allowed to continue with this IP
  26. if result, err := auth.IsAllowedIP(); !result {
  27. ctx.Logger.Error("auth proxy: failed to check whitelisted ip addresses", "message", err.Error(), "error", err.DetailsError)
  28. ctx.Handle(407, err.Error(), err.DetailsError)
  29. return true
  30. }
  31. // Try to log in user from various providers
  32. id, err := auth.Login()
  33. if err != nil {
  34. ctx.Logger.Error("auth proxy: failed to login", "message", err.Error(), "error", err.DetailsError)
  35. ctx.Handle(500, err.Error(), err.DetailsError)
  36. return true
  37. }
  38. // Get full user info
  39. user, err := auth.GetSignedUser(id)
  40. if err != nil {
  41. ctx.Logger.Error("auth proxy: failed to get signed in user", "message", err.Error(), "error", err.DetailsError)
  42. ctx.Handle(500, err.Error(), err.DetailsError)
  43. return true
  44. }
  45. // Add user info to context
  46. ctx.SignedInUser = user
  47. ctx.IsSignedIn = true
  48. // Remember user data it in cache
  49. if err := auth.Remember(id); err != nil {
  50. ctx.Logger.Error("auth proxy: failed to store user in cache", "message", err.Error(), "error", err.DetailsError)
  51. ctx.Handle(500, err.Error(), err.DetailsError)
  52. return true
  53. }
  54. return true
  55. }