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
8ae066ab
Unverified
Commit
8ae066ab
authored
Feb 06, 2019
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move authtoken package into auth package
parent
86786207
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
34 additions
and
37 deletions
+34
-37
pkg/cmd/grafana-server/server.go
+1
-0
pkg/services/auth/auth.go
+0
-1
pkg/services/auth/auth_token.go
+11
-15
pkg/services/auth/auth_token_test.go
+10
-9
pkg/services/auth/model.go
+8
-8
pkg/services/auth/token_cleanup.go
+3
-3
pkg/services/auth/token_cleanup_test.go
+1
-1
No files found.
pkg/cmd/grafana-server/server.go
View file @
8ae066ab
...
...
@@ -32,6 +32,7 @@ import (
_
"github.com/grafana/grafana/pkg/metrics"
_
"github.com/grafana/grafana/pkg/plugins"
_
"github.com/grafana/grafana/pkg/services/alerting"
_
"github.com/grafana/grafana/pkg/services/auth"
_
"github.com/grafana/grafana/pkg/services/cleanup"
_
"github.com/grafana/grafana/pkg/services/notifications"
_
"github.com/grafana/grafana/pkg/services/provisioning"
...
...
pkg/services/auth/auth.go
deleted
100644 → 0
View file @
86786207
package
auth
pkg/services/auth/auth
token/auth
_token.go
→
pkg/services/auth/auth_token.go
View file @
8ae066ab
package
auth
token
package
auth
import
(
"crypto/sha256"
...
...
@@ -16,30 +16,26 @@ import (
)
func
init
()
{
registry
.
Register
(
&
registry
.
Descriptor
{
Name
:
"AuthTokenService"
,
Instance
:
&
UserAuthTokenServiceImpl
{},
InitPriority
:
registry
.
Low
,
})
registry
.
RegisterService
(
&
UserAuthTokenService
{})
}
var
getTime
=
time
.
Now
const
urgentRotateTime
=
1
*
time
.
Minute
type
UserAuthTokenService
Impl
struct
{
type
UserAuthTokenService
struct
{
SQLStore
*
sqlstore
.
SqlStore
`inject:""`
ServerLockService
*
serverlock
.
ServerLockService
`inject:""`
Cfg
*
setting
.
Cfg
`inject:""`
log
log
.
Logger
}
func
(
s
*
UserAuthTokenService
Impl
)
Init
()
error
{
func
(
s
*
UserAuthTokenService
)
Init
()
error
{
s
.
log
=
log
.
New
(
"auth"
)
return
nil
}
func
(
s
*
UserAuthTokenService
Impl
)
CreateToken
(
userId
int64
,
clientIP
,
userAgent
string
)
(
*
models
.
UserToken
,
error
)
{
func
(
s
*
UserAuthTokenService
)
CreateToken
(
userId
int64
,
clientIP
,
userAgent
string
)
(
*
models
.
UserToken
,
error
)
{
clientIP
=
util
.
ParseIPAddress
(
clientIP
)
token
,
err
:=
util
.
RandomHex
(
16
)
if
err
!=
nil
{
...
...
@@ -77,7 +73,7 @@ func (s *UserAuthTokenServiceImpl) CreateToken(userId int64, clientIP, userAgent
return
&
userToken
,
err
}
func
(
s
*
UserAuthTokenService
Impl
)
LookupToken
(
unhashedToken
string
)
(
*
models
.
UserToken
,
error
)
{
func
(
s
*
UserAuthTokenService
)
LookupToken
(
unhashedToken
string
)
(
*
models
.
UserToken
,
error
)
{
hashedToken
:=
hashToken
(
unhashedToken
)
if
setting
.
Env
==
setting
.
DEV
{
s
.
log
.
Debug
(
"looking up token"
,
"unhashed"
,
unhashedToken
,
"hashed"
,
hashedToken
)
...
...
@@ -95,7 +91,7 @@ func (s *UserAuthTokenServiceImpl) LookupToken(unhashedToken string) (*models.Us
}
if
!
exists
{
return
nil
,
ErrAuth
TokenNotFound
return
nil
,
models
.
ErrUser
TokenNotFound
}
if
model
.
AuthToken
!=
hashedToken
&&
model
.
PrevAuthToken
==
hashedToken
&&
model
.
AuthTokenSeen
{
...
...
@@ -142,7 +138,7 @@ func (s *UserAuthTokenServiceImpl) LookupToken(unhashedToken string) (*models.Us
return
&
userToken
,
err
}
func
(
s
*
UserAuthTokenService
Impl
)
TryRotateToken
(
token
*
models
.
UserToken
,
clientIP
,
userAgent
string
)
(
bool
,
error
)
{
func
(
s
*
UserAuthTokenService
)
TryRotateToken
(
token
*
models
.
UserToken
,
clientIP
,
userAgent
string
)
(
bool
,
error
)
{
if
token
==
nil
{
return
false
,
nil
}
...
...
@@ -201,9 +197,9 @@ func (s *UserAuthTokenServiceImpl) TryRotateToken(token *models.UserToken, clien
return
false
,
nil
}
func
(
s
*
UserAuthTokenService
Impl
)
RevokeToken
(
token
*
models
.
UserToken
)
error
{
func
(
s
*
UserAuthTokenService
)
RevokeToken
(
token
*
models
.
UserToken
)
error
{
if
token
==
nil
{
return
ErrAuth
TokenNotFound
return
models
.
ErrUser
TokenNotFound
}
model
:=
userAuthTokenFromUserToken
(
token
)
...
...
@@ -215,7 +211,7 @@ func (s *UserAuthTokenServiceImpl) RevokeToken(token *models.UserToken) error {
if
rowsAffected
==
0
{
s
.
log
.
Debug
(
"user auth token not found/revoked"
,
"tokenId"
,
model
.
Id
,
"userId"
,
model
.
UserId
,
"clientIP"
,
model
.
ClientIp
,
"userAgent"
,
model
.
UserAgent
)
return
ErrAuth
TokenNotFound
return
models
.
ErrUser
TokenNotFound
}
s
.
log
.
Debug
(
"user auth token revoked"
,
"tokenId"
,
model
.
Id
,
"userId"
,
model
.
UserId
,
"clientIP"
,
model
.
ClientIp
,
"userAgent"
,
model
.
UserAgent
)
...
...
pkg/services/auth/auth
token/auth
_token_test.go
→
pkg/services/auth/auth_token_test.go
View file @
8ae066ab
package
auth
token
package
auth
import
(
"encoding/json"
...
...
@@ -46,7 +46,7 @@ func TestUserAuthToken(t *testing.T) {
Convey
(
"When lookup hashed token should return user auth token not found error"
,
func
()
{
userToken
,
err
:=
userAuthTokenService
.
LookupToken
(
userToken
.
AuthToken
)
So
(
err
,
ShouldEqual
,
ErrAuth
TokenNotFound
)
So
(
err
,
ShouldEqual
,
models
.
ErrUser
TokenNotFound
)
So
(
userToken
,
ShouldBeNil
)
})
...
...
@@ -61,13 +61,13 @@ func TestUserAuthToken(t *testing.T) {
Convey
(
"revoking nil token should return error"
,
func
()
{
err
=
userAuthTokenService
.
RevokeToken
(
nil
)
So
(
err
,
ShouldEqual
,
ErrAuth
TokenNotFound
)
So
(
err
,
ShouldEqual
,
models
.
ErrUser
TokenNotFound
)
})
Convey
(
"revoking non-existing token should return error"
,
func
()
{
userToken
.
Id
=
1000
err
=
userAuthTokenService
.
RevokeToken
(
userToken
)
So
(
err
,
ShouldEqual
,
ErrAuth
TokenNotFound
)
So
(
err
,
ShouldEqual
,
models
.
ErrUser
TokenNotFound
)
})
})
...
...
@@ -112,7 +112,7 @@ func TestUserAuthToken(t *testing.T) {
}
notGood
,
err
:=
userAuthTokenService
.
LookupToken
(
userToken
.
UnhashedToken
)
So
(
err
,
ShouldEqual
,
ErrAuth
TokenNotFound
)
So
(
err
,
ShouldEqual
,
models
.
ErrUser
TokenNotFound
)
So
(
notGood
,
ShouldBeNil
)
})
...
...
@@ -140,7 +140,7 @@ func TestUserAuthToken(t *testing.T) {
}
notGood
,
err
:=
userAuthTokenService
.
LookupToken
(
userToken
.
UnhashedToken
)
So
(
err
,
ShouldEqual
,
ErrAuth
TokenNotFound
)
So
(
err
,
ShouldEqual
,
models
.
ErrUser
TokenNotFound
)
So
(
notGood
,
ShouldBeNil
)
})
})
...
...
@@ -164,7 +164,8 @@ func TestUserAuthToken(t *testing.T) {
So
(
err
,
ShouldBeNil
)
var
tok
models
.
UserToken
model
.
toUserToken
(
&
tok
)
err
=
model
.
toUserToken
(
&
tok
)
So
(
err
,
ShouldBeNil
)
getTime
=
func
()
time
.
Time
{
return
t
.
Add
(
time
.
Hour
)
...
...
@@ -419,7 +420,7 @@ func createTestContext(t *testing.T) *testContext {
t
.
Helper
()
sqlstore
:=
sqlstore
.
InitTestDB
(
t
)
tokenService
:=
&
UserAuthTokenService
Impl
{
tokenService
:=
&
UserAuthTokenService
{
SQLStore
:
sqlstore
,
Cfg
:
&
setting
.
Cfg
{
LoginMaxInactiveLifetimeDays
:
7
,
...
...
@@ -438,7 +439,7 @@ func createTestContext(t *testing.T) *testContext {
type
testContext
struct
{
sqlstore
*
sqlstore
.
SqlStore
tokenService
*
UserAuthTokenService
Impl
tokenService
*
UserAuthTokenService
}
func
(
c
*
testContext
)
getAuthTokenByID
(
id
int64
)
(
*
userAuthToken
,
error
)
{
...
...
pkg/services/auth/
authtoken/
model.go
→
pkg/services/auth/model.go
View file @
8ae066ab
package
auth
token
package
auth
import
(
"errors"
"fmt"
"github.com/grafana/grafana/pkg/models"
)
// Typed errors
var
(
ErrAuthTokenNotFound
=
errors
.
New
(
"user auth token not found"
)
)
type
userAuthToken
struct
{
Id
int64
UserId
int64
...
...
@@ -33,7 +27,11 @@ func userAuthTokenFromUserToken(ut *models.UserToken) *userAuthToken {
return
&
uat
}
func
(
uat
*
userAuthToken
)
fromUserToken
(
ut
*
models
.
UserToken
)
{
func
(
uat
*
userAuthToken
)
fromUserToken
(
ut
*
models
.
UserToken
)
error
{
if
uat
==
nil
{
return
fmt
.
Errorf
(
"needs pointer to userAuthToken struct"
)
}
uat
.
Id
=
ut
.
Id
uat
.
UserId
=
ut
.
UserId
uat
.
AuthToken
=
ut
.
AuthToken
...
...
@@ -46,6 +44,8 @@ func (uat *userAuthToken) fromUserToken(ut *models.UserToken) {
uat
.
CreatedAt
=
ut
.
CreatedAt
uat
.
UpdatedAt
=
ut
.
UpdatedAt
uat
.
UnhashedToken
=
ut
.
UnhashedToken
return
nil
}
func
(
uat
*
userAuthToken
)
toUserToken
(
ut
*
models
.
UserToken
)
error
{
...
...
pkg/services/auth/
authtoken/sessio
n_cleanup.go
→
pkg/services/auth/
toke
n_cleanup.go
View file @
8ae066ab
package
auth
token
package
auth
import
(
"context"
"time"
)
func
(
srv
*
UserAuthTokenService
Impl
)
Run
(
ctx
context
.
Context
)
error
{
func
(
srv
*
UserAuthTokenService
)
Run
(
ctx
context
.
Context
)
error
{
if
srv
.
Cfg
.
ExpiredTokensCleanupIntervalDays
<=
0
{
srv
.
log
.
Debug
(
"cleanup of expired auth tokens are disabled"
)
return
nil
...
...
@@ -31,7 +31,7 @@ func (srv *UserAuthTokenServiceImpl) Run(ctx context.Context) error {
}
}
func
(
srv
*
UserAuthTokenService
Impl
)
deleteExpiredTokens
(
maxInactiveLifetime
,
maxLifetime
time
.
Duration
)
(
int64
,
error
)
{
func
(
srv
*
UserAuthTokenService
)
deleteExpiredTokens
(
maxInactiveLifetime
,
maxLifetime
time
.
Duration
)
(
int64
,
error
)
{
createdBefore
:=
getTime
()
.
Add
(
-
maxLifetime
)
rotatedBefore
:=
getTime
()
.
Add
(
-
maxInactiveLifetime
)
...
...
pkg/services/auth/
authtoken/sessio
n_cleanup_test.go
→
pkg/services/auth/
toke
n_cleanup_test.go
View file @
8ae066ab
package
auth
token
package
auth
import
(
"fmt"
...
...
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