mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
moves initWithToken to auth package
This commit is contained in:
parent
734a7d38b2
commit
55b3013eb3
@ -1,8 +1,6 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
@ -51,8 +49,7 @@ func GetContextHandler(ats *auth.UserAuthTokenService) macaron.Handler {
|
|||||||
case initContextWithApiKey(ctx):
|
case initContextWithApiKey(ctx):
|
||||||
case initContextWithBasicAuth(ctx, orgId):
|
case initContextWithBasicAuth(ctx, orgId):
|
||||||
case initContextWithAuthProxy(ctx, orgId):
|
case initContextWithAuthProxy(ctx, orgId):
|
||||||
//case initContextWithUserSessionCookie(ctx, orgId):
|
case ats.InitContextWithToken(ctx, orgId):
|
||||||
case initContextWithToken(ctx, orgId, ats):
|
|
||||||
case initContextWithAnonymousUser(ctx):
|
case initContextWithAnonymousUser(ctx):
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,53 +88,6 @@ func initContextWithAnonymousUser(ctx *m.ReqContext) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func initContextWithToken(ctx *m.ReqContext, orgID int64, ts *auth.UserAuthTokenService) bool {
|
|
||||||
//auth User
|
|
||||||
unhashedToken := ctx.GetCookie("grafana_session")
|
|
||||||
if unhashedToken == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := ts.LookupToken(unhashedToken)
|
|
||||||
if err != nil {
|
|
||||||
ctx.Logger.Info("failed to look up user based on cookie")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
query := m.GetSignedInUserQuery{UserId: user.UserId, OrgId: orgID}
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
|
||||||
ctx.Logger.Error("Failed to get user with id", "userId", user.UserId, "error", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.SignedInUser = query.Result
|
|
||||||
ctx.IsSignedIn = true
|
|
||||||
ctx.UserToken = user
|
|
||||||
|
|
||||||
//rotate session token if needed.
|
|
||||||
rotated, err := ts.RefreshToken(ctx.UserToken, ctx.RemoteAddr(), ctx.Req.UserAgent())
|
|
||||||
if err != nil {
|
|
||||||
ctx.Logger.Error("failed to rotate token", "error", err, "user.id", user.UserId, "user_token.id", user.Id)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if rotated {
|
|
||||||
ctx.Logger.Info("new token", "unhashed token", ctx.UserToken.UnhashedToken)
|
|
||||||
ctx.Resp.Header().Del("Set-Cookie")
|
|
||||||
cookie := http.Cookie{
|
|
||||||
Name: setting.SessionOptions.CookieName,
|
|
||||||
Value: url.QueryEscape(ctx.UserToken.UnhashedToken),
|
|
||||||
HttpOnly: true,
|
|
||||||
Domain: setting.Domain,
|
|
||||||
Path: setting.AppSubUrl + "/",
|
|
||||||
}
|
|
||||||
|
|
||||||
http.SetCookie(ctx.Resp, &cookie)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// func initContextWithUserSessionCookie(ctx *m.ReqContext, orgId int64) bool {
|
// func initContextWithUserSessionCookie(ctx *m.ReqContext, orgId int64) bool {
|
||||||
// // initialize session
|
// // initialize session
|
||||||
// if err := ctx.Session.Start(ctx.Context); err != nil {
|
// if err := ctx.Session.Start(ctx.Context); err != nil {
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/registry"
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
@ -23,6 +24,7 @@ var (
|
|||||||
now = time.Now
|
now = time.Now
|
||||||
RotateTime = 1 * time.Minute
|
RotateTime = 1 * time.Minute
|
||||||
UrgentRotateTime = 30 * time.Second
|
UrgentRotateTime = 30 * time.Second
|
||||||
|
oneYearInSeconds = 31557600 //used as default maxage for session cookies. We validate/rotate them more often.
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserAuthTokenService are used for generating and validating user auth tokens
|
// UserAuthTokenService are used for generating and validating user auth tokens
|
||||||
@ -37,38 +39,70 @@ func (s *UserAuthTokenService) Init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *UserAuthTokenService) InitContextWithToken(ctx *models.ReqContext, orgID int64) bool {
|
||||||
|
//auth User
|
||||||
|
unhashedToken := ctx.GetCookie(setting.SessionOptions.CookieName)
|
||||||
|
if unhashedToken == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := s.LookupToken(unhashedToken)
|
||||||
|
if err != nil {
|
||||||
|
ctx.Logger.Info("failed to look up user based on cookie", "error", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
query := models.GetSignedInUserQuery{UserId: user.UserId, OrgId: orgID}
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
ctx.Logger.Error("Failed to get user with id", "userId", user.UserId, "error", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.SignedInUser = query.Result
|
||||||
|
ctx.IsSignedIn = true
|
||||||
|
ctx.UserToken = user
|
||||||
|
|
||||||
|
//rotate session token if needed.
|
||||||
|
rotated, err := s.RefreshToken(ctx.UserToken, ctx.RemoteAddr(), ctx.Req.UserAgent())
|
||||||
|
if err != nil {
|
||||||
|
ctx.Logger.Error("failed to rotate token", "error", err, "user.id", user.UserId, "user_token.id", user.Id)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if rotated {
|
||||||
|
s.writeSessionCookie(ctx, ctx.UserToken.UnhashedToken, oneYearInSeconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *UserAuthTokenService) writeSessionCookie(ctx *models.ReqContext, value string, maxAge int) {
|
||||||
|
ctx.Logger.Info("new token", "unhashed token", ctx.UserToken.UnhashedToken)
|
||||||
|
ctx.Resp.Header().Del("Set-Cookie")
|
||||||
|
cookie := http.Cookie{
|
||||||
|
Name: setting.SessionOptions.CookieName,
|
||||||
|
Value: url.QueryEscape(value),
|
||||||
|
HttpOnly: true,
|
||||||
|
Domain: setting.Domain,
|
||||||
|
Path: setting.AppSubUrl + "/",
|
||||||
|
Secure: setting.SessionOptions.Secure,
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(ctx.Resp, &cookie)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *UserAuthTokenService) UserAuthenticatedHook(user *models.User, c *models.ReqContext) error {
|
func (s *UserAuthTokenService) UserAuthenticatedHook(user *models.User, c *models.ReqContext) error {
|
||||||
userToken, err := s.CreateToken(user.Id, c.RemoteAddr(), c.Req.UserAgent())
|
userToken, err := s.CreateToken(user.Id, c.RemoteAddr(), c.Req.UserAgent())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Resp.Header().Del("Set-Cookie")
|
s.writeSessionCookie(c, userToken.UnhashedToken, oneYearInSeconds)
|
||||||
cookie := http.Cookie{
|
|
||||||
Name: setting.SessionOptions.CookieName,
|
|
||||||
Value: url.QueryEscape(userToken.UnhashedToken),
|
|
||||||
HttpOnly: true,
|
|
||||||
Domain: setting.Domain,
|
|
||||||
Path: setting.AppSubUrl + "/",
|
|
||||||
Secure: setting.SessionOptions.Secure,
|
|
||||||
}
|
|
||||||
|
|
||||||
http.SetCookie(c.Resp, &cookie)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UserAuthTokenService) UserSignedOutHook(c *models.ReqContext) {
|
func (s *UserAuthTokenService) UserSignedOutHook(c *models.ReqContext) {
|
||||||
c.Resp.Header().Del("Set-Cookie")
|
s.writeSessionCookie(c, "", -1)
|
||||||
cookie := http.Cookie{
|
|
||||||
Name: setting.SessionOptions.CookieName,
|
|
||||||
Value: "",
|
|
||||||
HttpOnly: true,
|
|
||||||
Domain: setting.Domain,
|
|
||||||
Path: setting.AppSubUrl + "/",
|
|
||||||
Secure: setting.SessionOptions.Secure,
|
|
||||||
}
|
|
||||||
http.SetCookie(c.Resp, &cookie)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UserAuthTokenService) CreateToken(userId int64, clientIP, userAgent string) (*models.UserAuthToken, error) {
|
func (s *UserAuthTokenService) CreateToken(userId int64, clientIP, userAgent string) (*models.UserAuthToken, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user