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
2b05dac0
Commit
2b05dac0
authored
Jan 16, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Api Key role is now correcty added do middleware context
parent
507bff8b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
47 deletions
+54
-47
pkg/api/token.go
+2
-2
pkg/middleware/auth.go
+8
-16
pkg/middleware/middleware.go
+23
-1
pkg/models/token.go
+5
-2
pkg/services/sqlstore/accounts.go
+0
-25
pkg/services/sqlstore/tokens.go
+16
-1
No files found.
pkg/api/token.go
View file @
2b05dac0
...
...
@@ -9,12 +9,12 @@ import (
func
GetTokens
(
c
*
middleware
.
Context
)
{
query
:=
m
.
GetTokensQuery
{
AccountId
:
c
.
AccountId
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to list tokens"
,
err
)
return
}
result
:=
make
([]
*
m
.
TokenDTO
,
len
(
query
.
Result
))
for
i
,
t
:=
range
query
.
Result
{
result
[
i
]
=
&
m
.
TokenDTO
{
...
...
pkg/middleware/auth.go
View file @
2b05dac0
package
middleware
import
(
"errors"
"strconv"
"strings"
"github.com/Unknwon/macaron"
"github.com/torkelo/grafana-pro/pkg/bus"
m
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
)
...
...
@@ -17,11 +15,11 @@ type AuthOptions struct {
ReqSignedIn
bool
}
func
getRequestAccountId
(
c
*
Context
)
(
int64
,
error
)
{
func
getRequestAccountId
(
c
*
Context
)
int64
{
accountId
:=
c
.
Session
.
Get
(
"accountId"
)
if
accountId
!=
nil
{
return
accountId
.
(
int64
)
,
nil
return
accountId
.
(
int64
)
}
// localhost render query
...
...
@@ -32,24 +30,18 @@ func getRequestAccountId(c *Context) (int64, error) {
accountId
=
accId
}
// check api token
return
0
}
func
getApiToken
(
c
*
Context
)
string
{
header
:=
c
.
Req
.
Header
.
Get
(
"Authorization"
)
parts
:=
strings
.
SplitN
(
header
,
" "
,
2
)
if
len
(
parts
)
==
2
||
parts
[
0
]
==
"Bearer"
{
token
:=
parts
[
1
]
userQuery
:=
m
.
GetAccountByTokenQuery
{
Token
:
token
}
if
err
:=
bus
.
Dispatch
(
&
userQuery
);
err
!=
nil
{
return
-
1
,
err
}
return
userQuery
.
Result
.
Id
,
nil
}
// anonymous gues user
if
setting
.
Anonymous
{
return
setting
.
AnonymousAccountId
,
nil
return
token
}
return
-
1
,
errors
.
New
(
"Auth: session account id not found"
)
return
""
}
func
authDenied
(
c
*
Context
)
{
...
...
pkg/middleware/middleware.go
View file @
2b05dac0
...
...
@@ -31,7 +31,7 @@ func GetContextHandler() macaron.Handler {
}
// try get account id from request
if
accountId
,
err
:=
getRequestAccountId
(
ctx
);
err
==
nil
{
if
accountId
:=
getRequestAccountId
(
ctx
);
accountId
!=
0
{
query
:=
m
.
GetSignedInUserQuery
{
AccountId
:
accountId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
log
.
Error
(
3
,
"Failed to get user by id, %v, %v"
,
accountId
,
err
)
...
...
@@ -39,6 +39,28 @@ func GetContextHandler() macaron.Handler {
ctx
.
IsSignedIn
=
true
ctx
.
SignInUser
=
query
.
Result
}
}
else
if
token
:=
getApiToken
(
ctx
);
token
!=
""
{
// Try API Key auth
tokenQuery
:=
m
.
GetTokenByTokenQuery
{
Token
:
token
}
if
err
:=
bus
.
Dispatch
(
&
tokenQuery
);
err
!=
nil
{
ctx
.
JsonApiErr
(
401
,
"Invalid token"
,
err
)
return
}
else
{
tokenInfo
:=
tokenQuery
.
Result
query
:=
m
.
GetSignedInUserQuery
{
AccountId
:
tokenInfo
.
AccountId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
ctx
.
JsonApiErr
(
401
,
"Invalid token"
,
err
)
return
}
ctx
.
IsSignedIn
=
true
ctx
.
SignInUser
=
query
.
Result
// api key role
ctx
.
SignInUser
.
UserRole
=
tokenInfo
.
Role
ctx
.
SignInUser
.
UsingAccountId
=
ctx
.
SignInUser
.
AccountId
ctx
.
SignInUser
.
UsingAccountName
=
ctx
.
SignInUser
.
UserName
}
}
c
.
Map
(
ctx
)
...
...
pkg/models/token.go
View file @
2b05dac0
package
models
import
(
"errors"
"time"
)
var
ErrInvalidToken
=
errors
.
New
(
"Invalid token"
)
type
Token
struct
{
Id
int64
AccountId
int64
`xorm:"not null unique(uix_account_id_name)"`
...
...
@@ -47,9 +50,9 @@ type GetTokensQuery struct {
Result
[]
*
Token
}
type
Get
Account
ByTokenQuery
struct
{
type
Get
Token
ByTokenQuery
struct
{
Token
string
Result
*
Account
Result
*
Token
}
// ------------------------
...
...
pkg/services/sqlstore/accounts.go
View file @
2b05dac0
...
...
@@ -17,7 +17,6 @@ func init() {
bus
.
AddHandler
(
"sql"
,
SetUsingAccount
)
bus
.
AddHandler
(
"sql"
,
GetAccountById
)
bus
.
AddHandler
(
"sql"
,
GetAccountByLogin
)
bus
.
AddHandler
(
"sql"
,
GetAccountByToken
)
bus
.
AddHandler
(
"sql"
,
SearchAccounts
)
bus
.
AddHandler
(
"sql"
,
UpdateAccount
)
bus
.
AddHandler
(
"sql"
,
GetSignedInUser
)
...
...
@@ -111,30 +110,6 @@ func GetAccountById(query *m.GetAccountByIdQuery) error {
return
nil
}
func
GetAccountByToken
(
query
*
m
.
GetAccountByTokenQuery
)
error
{
var
err
error
var
account
m
.
Account
sess
:=
x
.
Join
(
"INNER"
,
"token"
,
"token.account_id = account.id"
)
sess
.
Omit
(
"token.id"
,
"token.account_id"
,
"token.name"
,
"token.token"
,
"token.role"
,
"token.updated"
,
"token.created"
)
has
,
err
:=
sess
.
Where
(
"token.token=?"
,
query
.
Token
)
.
Get
(
&
account
)
if
err
!=
nil
{
return
err
}
else
if
has
==
false
{
return
m
.
ErrAccountNotFound
}
if
account
.
UsingAccountId
==
0
{
account
.
UsingAccountId
=
account
.
Id
}
query
.
Result
=
&
account
return
nil
}
func
GetAccountByLogin
(
query
*
m
.
GetAccountByLoginQuery
)
error
{
if
query
.
LoginOrEmail
==
""
{
return
m
.
ErrAccountNotFound
...
...
pkg/services/sqlstore/tokens.go
View file @
2b05dac0
...
...
@@ -10,9 +10,10 @@ import (
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetTokens
)
bus
.
AddHandler
(
"sql"
,
Add
Token
)
bus
.
AddHandler
(
"sql"
,
GetTokenBy
Token
)
bus
.
AddHandler
(
"sql"
,
UpdateToken
)
bus
.
AddHandler
(
"sql"
,
DeleteToken
)
bus
.
AddHandler
(
"sql"
,
DeleteToken
)
}
func
GetTokens
(
query
*
m
.
GetTokensQuery
)
error
{
...
...
@@ -64,3 +65,17 @@ func UpdateToken(cmd *m.UpdateTokenCommand) error {
return
err
})
}
func
GetTokenByToken
(
query
*
m
.
GetTokenByTokenQuery
)
error
{
var
token
m
.
Token
has
,
err
:=
x
.
Where
(
"token=?"
,
query
.
Token
)
.
Get
(
&
token
)
if
err
!=
nil
{
return
err
}
else
if
has
==
false
{
return
m
.
ErrInvalidToken
}
query
.
Result
=
&
token
return
nil
}
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