mirror of https://github.com/grafana/grafana
Auth: Enable retries and transaction for some db calls for auth tokens (#16785)
the WithSession wrapper handles retries and connection management so the caller dont have to worry about it.pull/16813/head
parent
eb82a75668
commit
9660356638
@ -1,81 +1,85 @@ |
||||
package auth |
||||
|
||||
import "github.com/grafana/grafana/pkg/models" |
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
type FakeUserAuthTokenService struct { |
||||
CreateTokenProvider func(userId int64, clientIP, userAgent string) (*models.UserToken, error) |
||||
TryRotateTokenProvider func(token *models.UserToken, clientIP, userAgent string) (bool, error) |
||||
LookupTokenProvider func(unhashedToken string) (*models.UserToken, error) |
||||
RevokeTokenProvider func(token *models.UserToken) error |
||||
RevokeAllUserTokensProvider func(userId int64) error |
||||
ActiveAuthTokenCount func() (int64, error) |
||||
GetUserTokenProvider func(userId, userTokenId int64) (*models.UserToken, error) |
||||
GetUserTokensProvider func(userId int64) ([]*models.UserToken, error) |
||||
CreateTokenProvider func(ctx context.Context, userId int64, clientIP, userAgent string) (*models.UserToken, error) |
||||
TryRotateTokenProvider func(ctx context.Context, token *models.UserToken, clientIP, userAgent string) (bool, error) |
||||
LookupTokenProvider func(ctx context.Context, unhashedToken string) (*models.UserToken, error) |
||||
RevokeTokenProvider func(ctx context.Context, token *models.UserToken) error |
||||
RevokeAllUserTokensProvider func(ctx context.Context, userId int64) error |
||||
ActiveAuthTokenCount func(ctx context.Context) (int64, error) |
||||
GetUserTokenProvider func(ctx context.Context, userId, userTokenId int64) (*models.UserToken, error) |
||||
GetUserTokensProvider func(ctx context.Context, userId int64) ([]*models.UserToken, error) |
||||
} |
||||
|
||||
func NewFakeUserAuthTokenService() *FakeUserAuthTokenService { |
||||
return &FakeUserAuthTokenService{ |
||||
CreateTokenProvider: func(userId int64, clientIP, userAgent string) (*models.UserToken, error) { |
||||
CreateTokenProvider: func(ctx context.Context, userId int64, clientIP, userAgent string) (*models.UserToken, error) { |
||||
return &models.UserToken{ |
||||
UserId: 0, |
||||
UnhashedToken: "", |
||||
}, nil |
||||
}, |
||||
TryRotateTokenProvider: func(token *models.UserToken, clientIP, userAgent string) (bool, error) { |
||||
TryRotateTokenProvider: func(ctx context.Context, token *models.UserToken, clientIP, userAgent string) (bool, error) { |
||||
return false, nil |
||||
}, |
||||
LookupTokenProvider: func(unhashedToken string) (*models.UserToken, error) { |
||||
LookupTokenProvider: func(ctx context.Context, unhashedToken string) (*models.UserToken, error) { |
||||
return &models.UserToken{ |
||||
UserId: 0, |
||||
UnhashedToken: "", |
||||
}, nil |
||||
}, |
||||
RevokeTokenProvider: func(token *models.UserToken) error { |
||||
RevokeTokenProvider: func(ctx context.Context, token *models.UserToken) error { |
||||
return nil |
||||
}, |
||||
RevokeAllUserTokensProvider: func(userId int64) error { |
||||
RevokeAllUserTokensProvider: func(ctx context.Context, userId int64) error { |
||||
return nil |
||||
}, |
||||
ActiveAuthTokenCount: func() (int64, error) { |
||||
ActiveAuthTokenCount: func(ctx context.Context) (int64, error) { |
||||
return 10, nil |
||||
}, |
||||
GetUserTokenProvider: func(userId, userTokenId int64) (*models.UserToken, error) { |
||||
GetUserTokenProvider: func(ctx context.Context, userId, userTokenId int64) (*models.UserToken, error) { |
||||
return nil, nil |
||||
}, |
||||
GetUserTokensProvider: func(userId int64) ([]*models.UserToken, error) { |
||||
GetUserTokensProvider: func(ctx context.Context, userId int64) ([]*models.UserToken, error) { |
||||
return nil, nil |
||||
}, |
||||
} |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) CreateToken(userId int64, clientIP, userAgent string) (*models.UserToken, error) { |
||||
return s.CreateTokenProvider(userId, clientIP, userAgent) |
||||
func (s *FakeUserAuthTokenService) CreateToken(ctx context.Context, userId int64, clientIP, userAgent string) (*models.UserToken, error) { |
||||
return s.CreateTokenProvider(context.Background(), userId, clientIP, userAgent) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) LookupToken(unhashedToken string) (*models.UserToken, error) { |
||||
return s.LookupTokenProvider(unhashedToken) |
||||
func (s *FakeUserAuthTokenService) LookupToken(ctx context.Context, unhashedToken string) (*models.UserToken, error) { |
||||
return s.LookupTokenProvider(context.Background(), unhashedToken) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) TryRotateToken(token *models.UserToken, clientIP, userAgent string) (bool, error) { |
||||
return s.TryRotateTokenProvider(token, clientIP, userAgent) |
||||
func (s *FakeUserAuthTokenService) TryRotateToken(ctx context.Context, token *models.UserToken, clientIP, userAgent string) (bool, error) { |
||||
return s.TryRotateTokenProvider(context.Background(), token, clientIP, userAgent) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) RevokeToken(token *models.UserToken) error { |
||||
return s.RevokeTokenProvider(token) |
||||
func (s *FakeUserAuthTokenService) RevokeToken(ctx context.Context, token *models.UserToken) error { |
||||
return s.RevokeTokenProvider(context.Background(), token) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) RevokeAllUserTokens(userId int64) error { |
||||
return s.RevokeAllUserTokensProvider(userId) |
||||
func (s *FakeUserAuthTokenService) RevokeAllUserTokens(ctx context.Context, userId int64) error { |
||||
return s.RevokeAllUserTokensProvider(context.Background(), userId) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) ActiveTokenCount() (int64, error) { |
||||
return s.ActiveAuthTokenCount() |
||||
func (s *FakeUserAuthTokenService) ActiveTokenCount(ctx context.Context) (int64, error) { |
||||
return s.ActiveAuthTokenCount(context.Background()) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) GetUserToken(userId, userTokenId int64) (*models.UserToken, error) { |
||||
return s.GetUserTokenProvider(userId, userTokenId) |
||||
func (s *FakeUserAuthTokenService) GetUserToken(ctx context.Context, userId, userTokenId int64) (*models.UserToken, error) { |
||||
return s.GetUserTokenProvider(context.Background(), userId, userTokenId) |
||||
} |
||||
|
||||
func (s *FakeUserAuthTokenService) GetUserTokens(userId int64) ([]*models.UserToken, error) { |
||||
return s.GetUserTokensProvider(userId) |
||||
func (s *FakeUserAuthTokenService) GetUserTokens(ctx context.Context, userId int64) ([]*models.UserToken, error) { |
||||
return s.GetUserTokensProvider(context.Background(), userId) |
||||
} |
||||
|
Loading…
Reference in new issue