Chore: Remove bus from oauthtoken (#46811)

This commit is contained in:
Serge Zaitsev 2022-03-21 21:13:57 +01:00 committed by GitHub
parent 45745debe5
commit 9315ddd57c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,10 +4,10 @@ import (
"context" "context"
"errors" "errors"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/login/social" "github.com/grafana/grafana/pkg/login/social"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/login"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@ -17,6 +17,7 @@ var (
type Service struct { type Service struct {
SocialService social.Service SocialService social.Service
AuthInfoService login.AuthInfoService
} }
type OAuthTokenService interface { type OAuthTokenService interface {
@ -24,9 +25,10 @@ type OAuthTokenService interface {
IsOAuthPassThruEnabled(*models.DataSource) bool IsOAuthPassThruEnabled(*models.DataSource) bool
} }
func ProvideService(socialService social.Service) *Service { func ProvideService(socialService social.Service, authInfoService login.AuthInfoService) *Service {
return &Service{ return &Service{
SocialService: socialService, SocialService: socialService,
AuthInfoService: authInfoService,
} }
} }
@ -38,7 +40,7 @@ func (o *Service) GetCurrentOAuthToken(ctx context.Context, user *models.SignedI
} }
authInfoQuery := &models.GetAuthInfoQuery{UserId: user.UserId} authInfoQuery := &models.GetAuthInfoQuery{UserId: user.UserId}
if err := bus.Dispatch(ctx, authInfoQuery); err != nil { if err := o.AuthInfoService.GetAuthInfo(ctx, authInfoQuery); err != nil {
if errors.Is(err, models.ErrUserNotFound) { if errors.Is(err, models.ErrUserNotFound) {
// Not necessarily an error. User may be logged in another way. // Not necessarily an error. User may be logged in another way.
logger.Debug("no OAuth token for user found", "userId", user.UserId, "username", user.Login) logger.Debug("no OAuth token for user found", "userId", user.UserId, "username", user.Login)
@ -88,7 +90,7 @@ func (o *Service) GetCurrentOAuthToken(ctx context.Context, user *models.SignedI
AuthId: authInfoQuery.Result.AuthId, AuthId: authInfoQuery.Result.AuthId,
OAuthToken: token, OAuthToken: token,
} }
if err := bus.Dispatch(ctx, updateAuthCommand); err != nil { if err := o.AuthInfoService.UpdateAuthInfo(ctx, updateAuthCommand); err != nil {
logger.Error("failed to update auth info during token refresh", "userId", user.UserId, "username", user.Login, "error", err) logger.Error("failed to update auth info during token refresh", "userId", user.UserId, "username", user.Login, "error", err)
return nil return nil
} }