Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
871c84d1
Unverified
Commit
871c84d1
authored
Feb 05, 2019
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changes needed for api/middleware due to configuration settings
parent
0915f931
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
16 deletions
+32
-16
pkg/api/login.go
+3
-2
pkg/api/login_oauth.go
+2
-1
pkg/middleware/middleware.go
+15
-9
pkg/middleware/middleware_test.go
+12
-4
No files found.
pkg/api/login.go
View file @
871c84d1
...
...
@@ -137,7 +137,7 @@ func (hs *HTTPServer) loginUserWithUser(user *m.User, c *m.ReqContext) {
hs
.
log
.
Error
(
"failed to create auth token"
,
"error"
,
err
)
}
middleware
.
WriteSessionCookie
(
c
,
userToken
.
GetToken
(),
middleware
.
OneYearInSecond
s
)
middleware
.
WriteSessionCookie
(
c
,
userToken
.
GetToken
(),
hs
.
Cfg
.
LoginMaxLifetimeDay
s
)
}
func
(
hs
*
HTTPServer
)
Logout
(
c
*
m
.
ReqContext
)
{
...
...
@@ -185,7 +185,8 @@ func (hs *HTTPServer) trySetEncryptedCookie(ctx *m.ReqContext, cookieName string
Value
:
hex
.
EncodeToString
(
encryptedError
),
HttpOnly
:
true
,
Path
:
setting
.
AppSubUrl
+
"/"
,
Secure
:
hs
.
Cfg
.
SecurityHTTPSCookies
,
Secure
:
hs
.
Cfg
.
CookieSecure
,
SameSite
:
hs
.
Cfg
.
CookieSameSite
,
})
return
nil
...
...
pkg/api/login_oauth.go
View file @
871c84d1
...
...
@@ -214,7 +214,8 @@ func (hs *HTTPServer) writeCookie(w http.ResponseWriter, name string, value stri
Value
:
value
,
HttpOnly
:
true
,
Path
:
setting
.
AppSubUrl
+
"/"
,
Secure
:
hs
.
Cfg
.
SecurityHTTPSCookies
,
Secure
:
hs
.
Cfg
.
CookieSecure
,
SameSite
:
hs
.
Cfg
.
CookieSameSite
,
})
}
...
...
pkg/middleware/middleware.go
View file @
871c84d1
...
...
@@ -4,6 +4,7 @@ import (
"net/http"
"net/url"
"strconv"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/apikeygen"
...
...
@@ -168,11 +169,8 @@ func initContextWithBasicAuth(ctx *m.ReqContext, orgId int64) bool {
return
true
}
const
cookieName
=
"grafana_session"
const
OneYearInSeconds
=
31557600
//used as default maxage for session cookies. We validate/rotate them more often.
func
initContextWithToken
(
authTokenService
authtoken
.
UserAuthTokenService
,
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
{
rawToken
:=
ctx
.
GetCookie
(
c
ookieName
)
rawToken
:=
ctx
.
GetCookie
(
setting
.
LoginC
ookieName
)
if
rawToken
==
""
{
return
false
}
...
...
@@ -200,26 +198,34 @@ func initContextWithToken(authTokenService authtoken.UserAuthTokenService, ctx *
}
if
rotated
{
WriteSessionCookie
(
ctx
,
token
.
GetToken
(),
OneYearInSecond
s
)
WriteSessionCookie
(
ctx
,
token
.
GetToken
(),
setting
.
LoginMaxLifetimeDay
s
)
}
return
true
}
func
WriteSessionCookie
(
ctx
*
m
.
ReqContext
,
value
string
,
max
Age
int
)
{
func
WriteSessionCookie
(
ctx
*
m
.
ReqContext
,
value
string
,
max
LifetimeDays
int
)
{
if
setting
.
Env
==
setting
.
DEV
{
ctx
.
Logger
.
Info
(
"new token"
,
"unhashed token"
,
value
)
}
var
maxAge
int
if
maxLifetimeDays
<=
0
{
maxAge
=
-
1
}
else
{
maxAgeHours
:=
(
time
.
Duration
(
setting
.
LoginMaxLifetimeDays
)
*
24
*
time
.
Hour
)
+
time
.
Hour
maxAge
=
int
(
maxAgeHours
.
Seconds
())
}
ctx
.
Resp
.
Header
()
.
Del
(
"Set-Cookie"
)
cookie
:=
http
.
Cookie
{
Name
:
c
ookieName
,
Name
:
setting
.
LoginC
ookieName
,
Value
:
url
.
QueryEscape
(
value
),
HttpOnly
:
true
,
Path
:
setting
.
AppSubUrl
+
"/"
,
Secure
:
false
,
// TODO: use setting SecurityHTTPSCookies
Secure
:
setting
.
CookieSecure
,
MaxAge
:
maxAge
,
SameSite
:
http
.
SameSiteLaxMode
,
// TODO: use setting LoginCookieSameSite
SameSite
:
setting
.
CookieSameSite
,
}
http
.
SetCookie
(
ctx
.
Resp
,
&
cookie
)
...
...
pkg/middleware/middleware_test.go
View file @
871c84d1
...
...
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"path/filepath"
"testing"
"time"
msession
"github.com/go-macaron/session"
"github.com/grafana/grafana/pkg/bus"
...
...
@@ -197,13 +198,17 @@ func TestMiddlewareContext(t *testing.T) {
return
true
,
nil
}
maxAgeHours
:=
(
time
.
Duration
(
setting
.
LoginMaxLifetimeDays
)
*
24
*
time
.
Hour
)
maxAge
:=
(
maxAgeHours
+
time
.
Hour
)
.
Seconds
()
expectedCookie
:=
&
http
.
Cookie
{
Name
:
c
ookieName
,
Name
:
setting
.
LoginC
ookieName
,
Value
:
"rotated"
,
Path
:
setting
.
AppSubUrl
+
"/"
,
HttpOnly
:
true
,
MaxAge
:
OneYearInSeconds
,
SameSite
:
http
.
SameSiteLaxMode
,
MaxAge
:
int
(
maxAge
),
Secure
:
setting
.
CookieSecure
,
SameSite
:
setting
.
CookieSameSite
,
}
sc
.
fakeReq
(
"GET"
,
"/"
)
.
exec
()
...
...
@@ -545,6 +550,9 @@ func middlewareScenario(desc string, fn scenarioFunc) {
Convey
(
desc
,
func
()
{
defer
bus
.
ClearBusHandlers
()
setting
.
LoginCookieName
=
"grafana_session"
setting
.
LoginMaxLifetimeDays
=
30
sc
:=
&
scenarioContext
{}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
...
...
@@ -655,7 +663,7 @@ func (sc *scenarioContext) exec() {
if
sc
.
tokenSessionCookie
!=
""
{
sc
.
req
.
AddCookie
(
&
http
.
Cookie
{
Name
:
c
ookieName
,
Name
:
setting
.
LoginC
ookieName
,
Value
:
sc
.
tokenSessionCookie
,
})
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment