在嵌套函数中设置一个值

huangapple go评论87阅读模式
英文:

Setting a value within a nested function

问题

给出了以下Go语言代码,欢迎批评,因为我的Go语言已经生疏了。这是一个基本的刷新令牌流程,但我们将这些函数包装在一个事务函数中,以便在刷新流程中出现问题时可以回滚。

我们需要返回newToken的值,但它只在refresh函数中保留我们需要的值,当它返回到refreshToken时,根据我传递给它的对象或指针,它返回新的空令牌或nil。

package service

import (
	"context"
	"database/sql"
	"errors"
	"strings"

	"github.com/api-oauth/pkg/models"
	"github.com/api-oauth/pkg/repository"
	"golang.org/x/oauth2"
)

const minParts = 2

type OAuthAPI interface {
	Exchange(context.Context, string) (*oauth2.Token, error)
	Refresh(context.Context, string) (*oauth2.Token, error)
}

type Service struct {
	repo     repository.RepoInterface
	oauthAPI OAuthAPI
}

func New(api OAuthAPI, repo repository.RepoInterface) *Service {
	return &Service{repo: repo, oauthAPI: api}
}

var (
	ErrNoRefreshTokenFound = errors.New("no refresh token found")
	errNoScopesReturn      = errors.New("no scopes returned")
	errNoProfileFound      = errors.New("no profile found")
)

func (s *Service) RefreshToken(ctx context.Context, clientID string, userID int64) (*oauth2.Token, error) {
	var newToken *oauth2.Token

	err := s.repo.InTx(s.refresh(ctx, clientID, userID, newToken))
	if err != nil {
		return nil, err
	}

	return newToken, nil
}

func (s *Service) refresh(ctx context.Context, clientID string, userID int64, newToken *oauth2.Token) func(repo repository.Auth) error {
	return func(repo repository.Auth) error {
		oldToken, err := repo.GetToken(ctx, userID, clientID)
		if err != nil && err != sql.ErrNoRows {
			return err
		}

		if oldToken == nil {
			return ErrNoRefreshTokenFound
		}

		newToken, err = s.oauthAPI.Refresh(ctx, oldToken.Refresh)
		if err != nil {
			return err
		}

		rec := models.TokenRecord{
			UserID:    userID,
			ClientID:  clientID,
			ProfileID: oldToken.ProfileID,
			Access:    newToken.AccessToken,
			Refresh:   newToken.RefreshToken,
			Expiry:    newToken.Expiry,
		}

		err = repo.UpdateToken(ctx, &rec)
		if err != nil {
			return err
		}

		return nil
	}
}

尝试使RefreshToken返回更新的令牌信息。

英文:

Been given this golang code, feel free critique is hard as my golang is rusty. Basic refresh token flow, however we are wrapping the functions in a TX function to allow rollback should something go wrong in the refresh flow.

We need to return the newToken value however it only hold the value we need during the refresh function when it return to refreshToken depending on if I pass it an object or pointer is returns new empty token or nil respectively.

package service
import (
"context"
"database/sql"
"errors"
"strings"
"github.com/api-oauth/pkg/models"
"github.com/api-oauth/pkg/repository"
"golang.org/x/oauth2"
)
const minParts = 2
type OAuthAPI interface {
Exchange(context.Context, string) (*oauth2.Token, error)
Refresh(context.Context, string) (*oauth2.Token, error)
}
type Service struct {
repo     repository.RepoInterface
oauthAPI OAuthAPI
}
func New(api OAuthAPI, repo repository.RepoInterface) *Service {
return &Service{repo: repo, oauthAPI: api}
}
var (
ErrNoRefreshTokenFound = errors.New("no refresh token found")
errNoScopesReturn      = errors.New("no scopes returned")
errNoProfileFound      = errors.New("no profile found")
)
func (s *Service) RefreshToken(ctx context.Context, clientID string, userID int64) (*oauth2.Token, error) {
var newToken *oauth2.Token
err := s.repo.InTx(s.refresh(ctx, clientID, userID, newToken))
if err != nil {
return nil, err
}
return newToken, nil
}
func (s *Service) refresh(ctx context.Context, clientID string, userID int64, newToken *oauth2.Token) func(repo repository.Auth) error {
return func(repo repository.Auth) error {
oldToken, err := repo.GetToken(ctx, userID, clientID)
if err != nil && err != sql.ErrNoRows {
return err
}
if oldToken == nil {
return ErrNoRefreshTokenFound
}
newToken, err = s.oauthAPI.Refresh(ctx, oldToken.Refresh)
if err != nil {
return err
}
rec := models.TokenRecord{
UserID:    userID,
ClientID:  clientID,
ProfileID: oldToken.ProfileID,
Access:    newToken.AccessToken,
Refresh:   newToken.RefreshToken,
Expiry:    newToken.Expiry,
}
err = repo.UpdateToken(ctx, &rec)
if err != nil {
return err
}
return nil
}
}

Trying to have RefreshToken return the updated token information

答案1

得分: 2

传递newToken的地址:

func (s *Service) RefreshToken(ctx context.Context, clientID string, userID int64) (*oauth2.Token, error) {
    var newToken *oauth2.Token

    err := s.repo.InTx(s.refresh(ctx, clientID, userID, &newToken))
    ...

并且将refresh函数改为:

func (s *Service) refresh(ctx context.Context, clientID string, userID int64, newToken **oauth2.Token) func(repo repository.Auth) error {
  ...
  *newToken, err = ...
英文:

Pass address of newToken:

func (s *Service) RefreshToken(ctx context.Context, clientID string, userID int64) (*oauth2.Token, error) {
var newToken *oauth2.Token
err := s.repo.InTx(s.refresh(ctx, clientID, userID, &newToken))
...

And, change the refresh function to:

func (s *Service) refresh(ctx context.Context, clientID string, userID int64, newToken **oauth2.Token) func(repo repository.Auth) error {
...
*newToken, err=...

huangapple
  • 本文由 发表于 2022年3月12日 00:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/71441458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定