英文:
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=...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论