Live: check logged-in-user for grafana live (#27436)

This commit is contained in:
Ryan McKinley
2020-09-15 00:01:14 -07:00
committed by GitHub
parent a587bf4f56
commit aa40320b45
6 changed files with 33 additions and 182 deletions
+26 -12
View File
@@ -124,26 +124,40 @@ func InitalizeBroker() (*GrafanaLive, error) {
})
b.Handler = func(ctx *models.ReqContext) {
// Put authentication Credentials into request Context. Since we don't
// have any session backend here we simply set user ID as empty string.
// Users with empty ID called anonymous users, in real app you should
// decide whether anonymous users allowed to connect to your server
// or not. There is also another way to set Credentials - returning them
// from ConnectingHandler which is called after client sent first command
// to server called Connect. See _examples folder in repo to find real-life
// auth samples (OAuth2, Gin sessions, JWT etc).
user := ctx.SignedInUser
if user == nil {
ctx.Resp.WriteHeader(401)
return
}
dto := models.UserProfileDTO{
Id: user.UserId,
Name: user.Name,
Email: user.Email,
Login: user.Login,
IsGrafanaAdmin: user.IsGrafanaAdmin,
OrgId: user.OrgId,
}
jsonData, err := json.Marshal(dto)
if err != nil {
logger.Debug("error reading user", "dto", dto)
ctx.Resp.WriteHeader(404)
return
}
logger.Info("Logged in user", "user", user)
cred := &centrifuge.Credentials{
UserID: "",
UserID: fmt.Sprintf("%d", user.UserId),
Info: jsonData,
}
newCtx := centrifuge.SetCredentials(ctx.Req.Context(), cred)
path := ctx.Req.URL.Path
logger.Debug("Handle", "path", path)
r := ctx.Req.Request
r = r.WithContext(newCtx) // Set a user ID
// Check if this is a direct websocket connection
path := ctx.Req.URL.Path
if strings.Contains(path, "live/ws") {
wsHandler.ServeHTTP(ctx.Resp, r)
return