Skip to content

Commit

Permalink
feat: support enable without token
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Jul 1, 2024
1 parent 8b67633 commit 35f049f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
33 changes: 22 additions & 11 deletions porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const (
)

type Porter struct {
server *grpc.Server
wrapper wrapper
logger log.Logger
app *kratos.App
consulConfig *capi.Config
serverConfig *ServerConfig
server *grpc.Server
requireAsUser bool
wrapper wrapper
logger log.Logger
app *kratos.App
consulConfig *capi.Config
serverConfig *ServerConfig
}

type PorterConfig struct {
Expand Down Expand Up @@ -66,6 +67,12 @@ func WithPorterConsulConfig(config *capi.Config) PorterOption {
}
}

func WithAsUser() PorterOption {
return func(p *Porter) {
p.requireAsUser = true
}
}

func (p *Porter) Run() error {
return p.app.Run()
}
Expand Down Expand Up @@ -104,11 +111,12 @@ func NewPorter(ctx context.Context, config PorterConfig, handler Handler, option
return nil, err
}
c := wrapper{
Handler: handler,
Config: config,
Logger: p.logger,
Token: nil,
Client: client,
Handler: handler,
Config: config,
Logger: p.logger,
requireToken: p.requireAsUser,
Token: nil,
Client: client,
}
p.wrapper = c
p.server = NewServer(
Expand Down Expand Up @@ -179,6 +187,9 @@ func WellKnownToString(e protoreflect.Enum) string {
}

func (p *Porter) AsUser(ctx context.Context, userID int64) (*LibrarianClient, error) {
if !p.requireAsUser {
return nil, errors.New("init porter with `WithAsUser` option to use this method")
}
if p.wrapper.Token == nil {
return nil, errors.New("porter not enabled")
}
Expand Down
34 changes: 20 additions & 14 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
)

type wrapper struct {
Handler Handler
Config PorterConfig
Logger log.Logger
Token *tokenInfo
Client sephirah.LibrarianSephirahServiceClient
Handler Handler
Config PorterConfig
Logger log.Logger
requireToken bool
Token *tokenInfo
Client sephirah.LibrarianSephirahServiceClient
}

type tokenInfo struct {
Expand All @@ -49,15 +50,20 @@ func (s *wrapper) EnablePorter(ctx context.Context, req *pb.EnablePorterRequest)
return nil, fmt.Errorf("porter already enabled by %d", s.Token.enabler)
}
}
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+req.GetRefreshToken())
resp, err := s.Client.RefreshToken(ctx, new(sephirah.RefreshTokenRequest))
if err != nil {
return nil, err
}
s.Token = &tokenInfo{
enabler: req.GetSephirahId(),
AccessToken: resp.GetAccessToken(),
refreshToken: resp.GetRefreshToken(),
if s.requireToken {
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+req.GetRefreshToken())
resp, err := s.Client.RefreshToken(ctx, new(sephirah.RefreshTokenRequest))
if err != nil {
return nil, err
}
s.Token = &tokenInfo{
enabler: req.GetSephirahId(),
AccessToken: resp.GetAccessToken(),
refreshToken: resp.GetRefreshToken(),
}
} else {
s.Token = new(tokenInfo)
s.Token.enabler = req.GetSephirahId()
}
return &pb.EnablePorterResponse{}, nil
}
Expand Down

0 comments on commit 35f049f

Please sign in to comment.