2015-05-02 05:06:58 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2019-08-20 12:13:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
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"
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-08-20 12:13:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-05-02 05:06:58 -05:00
|
|
|
)
|
|
|
|
|
2019-08-20 12:13:27 -05:00
|
|
|
var header = setting.AuthProxyHeaderName
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqContext, orgID int64) bool {
|
2019-08-20 12:13:27 -05:00
|
|
|
username := ctx.Req.Header.Get(header)
|
2019-04-16 07:09:18 -05:00
|
|
|
auth := authproxy.New(&authproxy.Options{
|
|
|
|
Store: store,
|
|
|
|
Ctx: ctx,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
|
2019-08-20 12:13:27 -05:00
|
|
|
logger := log.New("auth.proxy")
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// 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-08-20 12:13:27 -05:00
|
|
|
logger.Error(
|
|
|
|
"Failed to check whitelisted IP addresses",
|
|
|
|
"message", err.Error(),
|
|
|
|
"error", err.DetailsError,
|
|
|
|
)
|
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-05-17 06:57:26 -05:00
|
|
|
// Try to log in user from various providers
|
|
|
|
id, err := auth.Login()
|
2019-04-16 07:09:18 -05:00
|
|
|
if err != nil {
|
2019-08-20 12:13:27 -05:00
|
|
|
logger.Error(
|
|
|
|
"Failed to login",
|
|
|
|
"username", username,
|
|
|
|
"message", err.Error(),
|
|
|
|
"error", err.DetailsError,
|
|
|
|
)
|
|
|
|
ctx.Handle(407, err.Error(), err.DetailsError)
|
2019-04-16 07:09:18 -05:00
|
|
|
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 {
|
2019-08-20 12:13:27 -05:00
|
|
|
logger.Error(
|
|
|
|
"Failed to get signed user",
|
|
|
|
"username", username,
|
|
|
|
"message", err.Error(),
|
|
|
|
"error", err.DetailsError,
|
|
|
|
)
|
|
|
|
ctx.Handle(407, 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
|
2019-05-17 06:57:26 -05:00
|
|
|
if err := auth.Remember(id); err != nil {
|
2019-08-20 12:13:27 -05:00
|
|
|
logger.Error(
|
|
|
|
"Failed to store user in cache",
|
|
|
|
"username", username,
|
|
|
|
"message", err.Error(),
|
|
|
|
"error", err.DetailsError,
|
|
|
|
)
|
2019-04-16 07:09:18 -05:00
|
|
|
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
|
|
|
|
}
|