2022-11-18 02:56:06 -06:00
|
|
|
package authtest
|
2019-03-08 08:15:17 -06:00
|
|
|
|
2019-04-30 07:42:01 -05:00
|
|
|
import (
|
|
|
|
"context"
|
2020-11-25 00:55:22 -06:00
|
|
|
"net"
|
2022-10-18 11:17:28 -05:00
|
|
|
"time"
|
2019-04-30 07:42:01 -05:00
|
|
|
|
2023-01-27 12:36:54 -06:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth"
|
2022-10-18 11:17:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2023-01-27 12:36:54 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2019-04-30 07:42:01 -05:00
|
|
|
)
|
2019-03-08 08:15:17 -06:00
|
|
|
|
|
|
|
type FakeUserAuthTokenService struct {
|
2022-11-18 02:56:06 -06:00
|
|
|
CreateTokenProvider func(ctx context.Context, user *user.User, clientIP net.IP, userAgent string) (*auth.UserToken, error)
|
2023-03-23 08:39:04 -05:00
|
|
|
RotateTokenProvider func(ctx context.Context, cmd auth.RotateCommand) (*auth.UserToken, error)
|
2023-02-02 07:36:16 -06:00
|
|
|
TryRotateTokenProvider func(ctx context.Context, token *auth.UserToken, clientIP net.IP, userAgent string) (bool, *auth.UserToken, error)
|
2022-11-18 02:56:06 -06:00
|
|
|
LookupTokenProvider func(ctx context.Context, unhashedToken string) (*auth.UserToken, error)
|
|
|
|
RevokeTokenProvider func(ctx context.Context, token *auth.UserToken, soft bool) error
|
2021-03-16 11:44:02 -05:00
|
|
|
RevokeAllUserTokensProvider func(ctx context.Context, userId int64) error
|
|
|
|
ActiveAuthTokenCount func(ctx context.Context) (int64, error)
|
2022-11-18 02:56:06 -06:00
|
|
|
GetUserTokenProvider func(ctx context.Context, userId, userTokenId int64) (*auth.UserToken, error)
|
|
|
|
GetUserTokensProvider func(ctx context.Context, userId int64) ([]*auth.UserToken, error)
|
|
|
|
GetUserRevokedTokensProvider func(ctx context.Context, userId int64) ([]*auth.UserToken, error)
|
2021-03-16 11:44:02 -05:00
|
|
|
BatchRevokedTokenProvider func(ctx context.Context, userIds []int64) error
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeUserAuthTokenService() *FakeUserAuthTokenService {
|
|
|
|
return &FakeUserAuthTokenService{
|
2022-11-18 02:56:06 -06:00
|
|
|
CreateTokenProvider: func(ctx context.Context, user *user.User, clientIP net.IP, userAgent string) (*auth.UserToken, error) {
|
|
|
|
return &auth.UserToken{
|
2019-03-08 08:15:17 -06:00
|
|
|
UserId: 0,
|
|
|
|
UnhashedToken: "",
|
|
|
|
}, nil
|
|
|
|
},
|
2023-02-02 07:36:16 -06:00
|
|
|
TryRotateTokenProvider: func(ctx context.Context, token *auth.UserToken, clientIP net.IP, userAgent string) (bool, *auth.UserToken, error) {
|
|
|
|
return false, nil, nil
|
2019-03-08 08:15:17 -06:00
|
|
|
},
|
2022-11-18 02:56:06 -06:00
|
|
|
LookupTokenProvider: func(ctx context.Context, unhashedToken string) (*auth.UserToken, error) {
|
|
|
|
return &auth.UserToken{
|
2019-03-08 08:15:17 -06:00
|
|
|
UserId: 0,
|
|
|
|
UnhashedToken: "",
|
|
|
|
}, nil
|
|
|
|
},
|
2022-11-18 02:56:06 -06:00
|
|
|
RevokeTokenProvider: func(ctx context.Context, token *auth.UserToken, soft bool) error {
|
2019-03-08 08:15:17 -06:00
|
|
|
return nil
|
|
|
|
},
|
2019-04-30 07:42:01 -05:00
|
|
|
RevokeAllUserTokensProvider: func(ctx context.Context, userId int64) error {
|
2019-03-08 08:15:17 -06:00
|
|
|
return nil
|
|
|
|
},
|
2019-07-02 02:42:35 -05:00
|
|
|
BatchRevokedTokenProvider: func(ctx context.Context, userIds []int64) error {
|
|
|
|
return nil
|
|
|
|
},
|
2019-04-30 07:42:01 -05:00
|
|
|
ActiveAuthTokenCount: func(ctx context.Context) (int64, error) {
|
2019-03-08 08:15:17 -06:00
|
|
|
return 10, nil
|
|
|
|
},
|
2022-11-18 02:56:06 -06:00
|
|
|
GetUserTokenProvider: func(ctx context.Context, userId, userTokenId int64) (*auth.UserToken, error) {
|
2019-03-08 08:15:17 -06:00
|
|
|
return nil, nil
|
|
|
|
},
|
2022-11-18 02:56:06 -06:00
|
|
|
GetUserTokensProvider: func(ctx context.Context, userId int64) ([]*auth.UserToken, error) {
|
2019-03-08 08:15:17 -06:00
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 04:44:44 -06:00
|
|
|
// Init initializes the service.
|
|
|
|
// Required for dependency injection.
|
|
|
|
func (s *FakeUserAuthTokenService) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) CreateToken(ctx context.Context, user *user.User, clientIP net.IP, userAgent string) (*auth.UserToken, error) {
|
2021-01-19 10:55:53 -06:00
|
|
|
return s.CreateTokenProvider(context.Background(), user, clientIP, userAgent)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2023-03-23 08:39:04 -05:00
|
|
|
func (s *FakeUserAuthTokenService) RotateToken(ctx context.Context, cmd auth.RotateCommand) (*auth.UserToken, error) {
|
|
|
|
return s.RotateTokenProvider(ctx, cmd)
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) LookupToken(ctx context.Context, unhashedToken string) (*auth.UserToken, error) {
|
2019-04-30 07:42:01 -05:00
|
|
|
return s.LookupTokenProvider(context.Background(), unhashedToken)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) TryRotateToken(ctx context.Context, token *auth.UserToken, clientIP net.IP,
|
2023-02-02 07:36:16 -06:00
|
|
|
userAgent string) (bool, *auth.UserToken, error) {
|
2019-04-30 07:42:01 -05:00
|
|
|
return s.TryRotateTokenProvider(context.Background(), token, clientIP, userAgent)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) RevokeToken(ctx context.Context, token *auth.UserToken, soft bool) error {
|
2021-03-16 11:44:02 -05:00
|
|
|
return s.RevokeTokenProvider(context.Background(), token, soft)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2019-04-30 07:42:01 -05:00
|
|
|
func (s *FakeUserAuthTokenService) RevokeAllUserTokens(ctx context.Context, userId int64) error {
|
|
|
|
return s.RevokeAllUserTokensProvider(context.Background(), userId)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2019-04-30 07:42:01 -05:00
|
|
|
func (s *FakeUserAuthTokenService) ActiveTokenCount(ctx context.Context) (int64, error) {
|
|
|
|
return s.ActiveAuthTokenCount(context.Background())
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) GetUserToken(ctx context.Context, userId, userTokenId int64) (*auth.UserToken, error) {
|
2019-04-30 07:42:01 -05:00
|
|
|
return s.GetUserTokenProvider(context.Background(), userId, userTokenId)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) GetUserTokens(ctx context.Context, userId int64) ([]*auth.UserToken, error) {
|
2019-04-30 07:42:01 -05:00
|
|
|
return s.GetUserTokensProvider(context.Background(), userId)
|
2019-03-08 08:15:17 -06:00
|
|
|
}
|
2019-07-02 02:42:35 -05:00
|
|
|
|
2022-11-18 02:56:06 -06:00
|
|
|
func (s *FakeUserAuthTokenService) GetUserRevokedTokens(ctx context.Context, userId int64) ([]*auth.UserToken, error) {
|
2021-03-16 11:44:02 -05:00
|
|
|
return s.GetUserRevokedTokensProvider(context.Background(), userId)
|
|
|
|
}
|
|
|
|
|
2019-07-02 02:42:35 -05:00
|
|
|
func (s *FakeUserAuthTokenService) BatchRevokeAllUserTokens(ctx context.Context, userIds []int64) error {
|
|
|
|
return s.BatchRevokedTokenProvider(ctx, userIds)
|
|
|
|
}
|
2022-10-18 11:17:28 -05:00
|
|
|
|
|
|
|
type FakeOAuthTokenService struct {
|
|
|
|
passThruEnabled bool
|
2023-01-27 12:36:54 -06:00
|
|
|
ExpectedAuthUser *login.UserAuth
|
2022-10-18 11:17:28 -05:00
|
|
|
ExpectedErrors map[string]error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *FakeOAuthTokenService) GetCurrentOAuthToken(context.Context, *user.SignedInUser) *oauth2.Token {
|
|
|
|
return &oauth2.Token{
|
|
|
|
AccessToken: ts.ExpectedAuthUser.OAuthAccessToken,
|
|
|
|
RefreshToken: ts.ExpectedAuthUser.OAuthRefreshToken,
|
|
|
|
Expiry: ts.ExpectedAuthUser.OAuthExpiry,
|
|
|
|
TokenType: ts.ExpectedAuthUser.OAuthTokenType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *FakeOAuthTokenService) IsOAuthPassThruEnabled(*datasources.DataSource) bool {
|
|
|
|
return ts.passThruEnabled
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:36:54 -06:00
|
|
|
func (ts *FakeOAuthTokenService) HasOAuthEntry(context.Context, *user.SignedInUser) (*login.UserAuth, bool, error) {
|
2022-10-18 11:17:28 -05:00
|
|
|
if ts.ExpectedAuthUser != nil {
|
|
|
|
return ts.ExpectedAuthUser, true, nil
|
|
|
|
}
|
|
|
|
if error, ok := ts.ExpectedErrors["HasOAuthEntry"]; ok {
|
|
|
|
return nil, false, error
|
|
|
|
}
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:36:54 -06:00
|
|
|
func (ts *FakeOAuthTokenService) InvalidateOAuthTokens(ctx context.Context, usr *login.UserAuth) error {
|
2022-10-18 11:17:28 -05:00
|
|
|
ts.ExpectedAuthUser.OAuthAccessToken = ""
|
|
|
|
ts.ExpectedAuthUser.OAuthRefreshToken = ""
|
|
|
|
ts.ExpectedAuthUser.OAuthExpiry = time.Time{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 12:36:54 -06:00
|
|
|
func (ts *FakeOAuthTokenService) TryTokenRefresh(ctx context.Context, usr *login.UserAuth) error {
|
2022-10-18 11:17:28 -05:00
|
|
|
if err, ok := ts.ExpectedErrors["TryTokenRefresh"]; ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|