2015-05-02 05:06:58 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2019-04-08 06:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2019-04-16 07:09:18 -05:00
|
|
|
authproxy "github.com/grafana/grafana/pkg/middleware/auth_proxy"
|
2015-05-02 05:06:58 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
2019-04-08 06:31:46 -05:00
|
|
|
const (
|
|
|
|
|
|
|
|
// cachePrefix is a prefix for the cache key
|
2019-04-16 07:09:18 -05:00
|
|
|
cachePrefix = authproxy.CachePrefix
|
2019-01-23 05:41:15 -06:00
|
|
|
)
|
2018-03-22 16:02:34 -05:00
|
|
|
|
2019-04-08 06:31:46 -05:00
|
|
|
func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *m.ReqContext, orgID int64) bool {
|
2019-04-16 07:09:18 -05:00
|
|
|
auth := authproxy.New(&authproxy.Options{
|
|
|
|
Store: store,
|
|
|
|
Ctx: ctx,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Bail if auth proxy is not enabled
|
2019-05-14 02:18:28 -05:00
|
|
|
if !auth.IsEnabled() {
|
2015-05-02 05:06:58 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// If the there is no header - we can't move forward
|
2019-05-14 02:18:28 -05:00
|
|
|
if !auth.HasHeader() {
|
2015-05-02 05:06:58 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Check if allowed to continue with this IP
|
2019-05-14 02:18:28 -05:00
|
|
|
if result, err := auth.IsAllowedIP(); !result {
|
2019-04-16 07:09:18 -05:00
|
|
|
ctx.Handle(407, err.Error(), err.DetailsError)
|
2016-02-23 07:22:28 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Try to get user id from various sources
|
|
|
|
id, err := auth.GetUserID()
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, err.Error(), err.DetailsError)
|
|
|
|
return true
|
2018-04-16 15:17:01 -05:00
|
|
|
}
|
2018-03-22 16:02:34 -05:00
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Get full user info
|
|
|
|
user, err := auth.GetSignedUser(id)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Handle(500, err.Error(), err.DetailsError)
|
2018-04-16 15:17:01 -05:00
|
|
|
return true
|
2016-02-23 07:22:28 -06:00
|
|
|
}
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Add user info to context
|
|
|
|
ctx.SignedInUser = user
|
|
|
|
ctx.IsSignedIn = true
|
2016-02-23 07:22:28 -06:00
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Remember user data it in cache
|
|
|
|
if err := auth.Remember(); err != nil {
|
|
|
|
ctx.Handle(500, err.Error(), err.DetailsError)
|
|
|
|
return true
|
2016-02-23 07:22:28 -06:00
|
|
|
}
|
|
|
|
|
2015-05-02 05:06:58 -05:00
|
|
|
return true
|
|
|
|
}
|