Add oauth pass-thru option for datasources
This commit is contained in:
@@ -33,6 +33,7 @@ func AddMigrations(mg *Migrator) {
|
||||
addUserAuthMigrations(mg)
|
||||
addServerlockMigrations(mg)
|
||||
addUserAuthTokenMigrations(mg)
|
||||
addUserAuthOAuthMigrations(mg)
|
||||
}
|
||||
|
||||
func addMigrationLogMigrations(mg *Migrator) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package migrations
|
||||
|
||||
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
|
||||
func addUserAuthOAuthMigrations(mg *Migrator) {
|
||||
userAuthV2 := Table{Name: "user_auth"}
|
||||
|
||||
mg.AddMigration("Add OAuth access token to user_auth", NewAddColumnMigration(userAuthV2, &Column{
|
||||
Name: "o_auth_access_token", Type: DB_Text, Nullable: true, Length: 255,
|
||||
}))
|
||||
mg.AddMigration("Add OAuth refresh token to user_auth", NewAddColumnMigration(userAuthV2, &Column{
|
||||
Name: "o_auth_refresh_token", Type: DB_Text, Nullable: true, Length: 255,
|
||||
}))
|
||||
mg.AddMigration("Add OAuth token type to user_auth", NewAddColumnMigration(userAuthV2, &Column{
|
||||
Name: "o_auth_token_type", Type: DB_Text, Nullable: true, Length: 255,
|
||||
}))
|
||||
mg.AddMigration("Add OAuth expiry to user_auth", NewAddColumnMigration(userAuthV2, &Column{
|
||||
Name: "o_auth_expiry", Type: DB_DateTime, Nullable: true,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Add index to user_id column in user_auth", NewAddIndexMigration(userAuthV2, &Index{
|
||||
Cols: []string{"user_id"},
|
||||
}))
|
||||
|
||||
}
|
||||
@@ -5,12 +5,15 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetUserByAuthInfo)
|
||||
bus.AddHandler("sql", GetAuthInfo)
|
||||
bus.AddHandler("sql", SetAuthInfo)
|
||||
bus.AddHandler("sql", UpdateAuthInfo)
|
||||
bus.AddHandler("sql", DeleteAuthInfo)
|
||||
}
|
||||
|
||||
@@ -94,7 +97,7 @@ func GetUserByAuthInfo(query *m.GetUserByAuthInfoQuery) error {
|
||||
}
|
||||
|
||||
// create authInfo record to link accounts
|
||||
if authQuery.Result == nil && query.AuthModule != "" && query.AuthId != "" {
|
||||
if authQuery.Result == nil && query.AuthModule != "" {
|
||||
cmd2 := &m.SetAuthInfoCommand{
|
||||
UserId: user.Id,
|
||||
AuthModule: query.AuthModule,
|
||||
@@ -111,6 +114,7 @@ func GetUserByAuthInfo(query *m.GetUserByAuthInfoQuery) error {
|
||||
|
||||
func GetAuthInfo(query *m.GetAuthInfoQuery) error {
|
||||
userAuth := &m.UserAuth{
|
||||
UserId: query.UserId, // TODO this doesn't have an index in the db
|
||||
AuthModule: query.AuthModule,
|
||||
AuthId: query.AuthId,
|
||||
}
|
||||
@@ -122,6 +126,28 @@ func GetAuthInfo(query *m.GetAuthInfoQuery) error {
|
||||
return m.ErrUserNotFound
|
||||
}
|
||||
|
||||
if userAuth.OAuthAccessToken != "" {
|
||||
accessToken, err := util.Decrypt([]byte(userAuth.OAuthAccessToken), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userAuth.OAuthAccessToken = string(accessToken)
|
||||
}
|
||||
if userAuth.OAuthRefreshToken != "" {
|
||||
refreshToken, err := util.Decrypt([]byte(userAuth.OAuthRefreshToken), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userAuth.OAuthRefreshToken = string(refreshToken)
|
||||
}
|
||||
if userAuth.OAuthTokenType != "" {
|
||||
tokenType, err := util.Decrypt([]byte(userAuth.OAuthTokenType), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userAuth.OAuthTokenType = string(tokenType)
|
||||
}
|
||||
|
||||
query.Result = userAuth
|
||||
return nil
|
||||
}
|
||||
@@ -135,11 +161,69 @@ func SetAuthInfo(cmd *m.SetAuthInfoCommand) error {
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
if cmd.OAuthToken != nil {
|
||||
secretAccessToken, err := util.Encrypt([]byte(cmd.OAuthToken.AccessToken), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretRefreshToken, err := util.Encrypt([]byte(cmd.OAuthToken.RefreshToken), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretTokenType, err := util.Encrypt([]byte(cmd.OAuthToken.TokenType), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
authUser.OAuthAccessToken = string(secretAccessToken)
|
||||
authUser.OAuthRefreshToken = string(secretRefreshToken)
|
||||
authUser.OAuthTokenType = string(secretTokenType)
|
||||
authUser.OAuthExpiry = cmd.OAuthToken.Expiry
|
||||
}
|
||||
|
||||
_, err := sess.Insert(authUser)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateAuthInfo(cmd *m.UpdateAuthInfoCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
authUser := &m.UserAuth{
|
||||
UserId: cmd.UserId,
|
||||
AuthModule: cmd.AuthModule,
|
||||
AuthId: cmd.AuthId,
|
||||
Created: time.Now(),
|
||||
}
|
||||
|
||||
if cmd.OAuthToken != nil {
|
||||
secretAccessToken, err := util.Encrypt([]byte(cmd.OAuthToken.AccessToken), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretRefreshToken, err := util.Encrypt([]byte(cmd.OAuthToken.RefreshToken), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretTokenType, err := util.Encrypt([]byte(cmd.OAuthToken.TokenType), setting.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
authUser.OAuthAccessToken = string(secretAccessToken)
|
||||
authUser.OAuthRefreshToken = string(secretRefreshToken)
|
||||
authUser.OAuthTokenType = string(secretTokenType)
|
||||
authUser.OAuthExpiry = cmd.OAuthToken.Expiry
|
||||
}
|
||||
|
||||
cond := &m.UserAuth{
|
||||
UserId: cmd.UserId,
|
||||
AuthModule: cmd.AuthModule,
|
||||
}
|
||||
|
||||
_, err := sess.Update(authUser, cond)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteAuthInfo(cmd *m.DeleteAuthInfoCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
_, err := sess.Delete(cmd.UserAuth)
|
||||
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
@@ -126,5 +128,46 @@ func TestUserAuth(t *testing.T) {
|
||||
So(err, ShouldEqual, m.ErrUserNotFound)
|
||||
So(query.Result, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Can set & retrieve oauth token information", func() {
|
||||
token := &oauth2.Token{
|
||||
AccessToken: "testaccess",
|
||||
RefreshToken: "testrefresh",
|
||||
Expiry: time.Now(),
|
||||
TokenType: "Bearer",
|
||||
}
|
||||
|
||||
// Find a user to set tokens on
|
||||
login := "loginuser0"
|
||||
|
||||
// Calling GetUserByAuthInfoQuery on an existing user will populate an entry in the user_auth table
|
||||
query := &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test", AuthId: "test"}
|
||||
err = GetUserByAuthInfo(query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result.Login, ShouldEqual, login)
|
||||
|
||||
cmd := &m.UpdateAuthInfoCommand{
|
||||
UserId: query.Result.Id,
|
||||
AuthId: query.AuthId,
|
||||
AuthModule: query.AuthModule,
|
||||
OAuthToken: token,
|
||||
}
|
||||
err = UpdateAuthInfo(cmd)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
getAuthQuery := &m.GetAuthInfoQuery{
|
||||
UserId: query.Result.Id,
|
||||
}
|
||||
|
||||
err = GetAuthInfo(getAuthQuery)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(getAuthQuery.Result.OAuthAccessToken, ShouldEqual, token.AccessToken)
|
||||
So(getAuthQuery.Result.OAuthRefreshToken, ShouldEqual, token.RefreshToken)
|
||||
So(getAuthQuery.Result.OAuthTokenType, ShouldEqual, token.TokenType)
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user