add Token authentication support
Added CRUD methods for Tokens. Extend Auth Handler to check for the presence of a Bearer Authorization header to authenticate against. If there is no header, or the token is not valid, the Auth Handler falls back to looking for a Session.
This commit is contained in:
@@ -17,6 +17,7 @@ func init() {
|
||||
bus.AddHandler("sql", SetUsingAccount)
|
||||
bus.AddHandler("sql", GetAccountById)
|
||||
bus.AddHandler("sql", GetAccountByLogin)
|
||||
bus.AddHandler("sql", GetAccountByToken)
|
||||
}
|
||||
|
||||
func CreateAccount(cmd *m.CreateAccountCommand) error {
|
||||
@@ -109,6 +110,27 @@ func GetAccountById(query *m.GetAccountByIdQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
|
||||
var err error
|
||||
|
||||
var account m.Account
|
||||
has, err := x.Where("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 {
|
||||
var err error
|
||||
|
||||
|
||||
@@ -38,7 +38,8 @@ func init() {
|
||||
tables = make([]interface{}, 0)
|
||||
|
||||
tables = append(tables, new(m.Account), new(m.Dashboard),
|
||||
new(m.Collaborator), new(m.DataSource), new(DashboardTag))
|
||||
new(m.Collaborator), new(m.DataSource), new(DashboardTag),
|
||||
new(m.Token))
|
||||
}
|
||||
|
||||
func Init() {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetTokens)
|
||||
bus.AddHandler("sql", AddToken)
|
||||
bus.AddHandler("sql", UpdateToken)
|
||||
bus.AddHandler("sql", DeleteToken)
|
||||
}
|
||||
|
||||
func GetTokens(query *m.GetTokensQuery) error {
|
||||
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId).Asc("name")
|
||||
|
||||
query.Result = make([]*m.Token, 0)
|
||||
return sess.Find(&query.Result)
|
||||
}
|
||||
|
||||
func DeleteToken(cmd *m.DeleteTokenCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM token WHERE id=? and account_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.Id, cmd.AccountId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func AddToken(cmd *m.AddTokenCommand) error {
|
||||
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
t := m.Token{
|
||||
AccountId: cmd.AccountId,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Token: cmd.Token,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(&t); err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.Result = &t
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateToken(cmd *m.UpdateTokenCommand) error {
|
||||
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
t := m.Token{
|
||||
Id: cmd.Id,
|
||||
AccountId: cmd.AccountId,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Where("id=? and account_id=?", t.Id, t.AccountId).Update(&t)
|
||||
return err
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user