Skip to content

Commit

Permalink
fix: change channel for GetSigningKey to time
Browse files Browse the repository at this point in the history
  • Loading branch information
livio-a committed Feb 14, 2020
1 parent 913eec6 commit a2e2f06
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/internal/mock/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s *AuthStorage) AuthRequestByID(_ context.Context, id string) (op.AuthRequ
}
return a, nil
}
func (s *AuthStorage) GetSigningKey(_ context.Context, keyCh chan<- jose.SigningKey, _ chan<- error, _ <-chan bool) {
func (s *AuthStorage) GetSigningKey(_ context.Context, keyCh chan<- jose.SigningKey, _ chan<- error, _ <-chan time.Time) {
keyCh <- jose.SigningKey{Algorithm: jose.RS256, Key: s.key}
}
func (s *AuthStorage) GetKey(_ context.Context) (*rsa.PrivateKey, error) {
Expand Down
26 changes: 17 additions & 9 deletions pkg/op/default_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type DefaultOP struct {
encoder *schema.Encoder
interceptor HttpInterceptor
retry func(int) (bool, int)
timer <-chan time.Time
}

type Config struct {
Expand Down Expand Up @@ -123,6 +124,13 @@ func WithRetry(max int, sleep time.Duration) DefaultOPOpts {
}
}

func WithTimer(timer <-chan time.Time) DefaultOPOpts {
return func(o *DefaultOP) error {
o.timer = timer
return nil
}
}

func NewDefaultOP(ctx context.Context, config *Config, storage Storage, opOpts ...DefaultOPOpts) (OpenIDProvider, error) {
err := ValidateIssuer(config.Issuer)
if err != nil {
Expand All @@ -133,18 +141,19 @@ func NewDefaultOP(ctx context.Context, config *Config, storage Storage, opOpts .
config: config,
storage: storage,
endpoints: DefaultEndpoints,
timer: make(<-chan time.Time),
}

keyCh := make(chan jose.SigningKey)
p.signer = NewDefaultSigner(ctx, storage, keyCh)
go p.ensureKey(ctx, storage, keyCh)

for _, optFunc := range opOpts {
if err := optFunc(p); err != nil {
return nil, err
}
}

keyCh := make(chan jose.SigningKey)
p.signer = NewDefaultSigner(ctx, storage, keyCh)
go p.ensureKey(ctx, storage, keyCh, p.timer)

router := CreateRouter(p, p.interceptor)
p.http = &http.Server{
Addr: ":" + config.Port,
Expand Down Expand Up @@ -252,12 +261,11 @@ func (p *DefaultOP) HandleUserinfo(w http.ResponseWriter, r *http.Request) {
Userinfo(w, r, p)
}

func (p *DefaultOP) ensureKey(ctx context.Context, storage Storage, keyCh chan<- jose.SigningKey) {
func (p *DefaultOP) ensureKey(ctx context.Context, storage Storage, keyCh chan<- jose.SigningKey, timer <-chan time.Time) {
count := 0
explicit := make(chan bool)
timer = time.After(0)
errCh := make(chan error)
go storage.GetSigningKey(ctx, keyCh, errCh, explicit)
explicit <- true
go storage.GetSigningKey(ctx, keyCh, errCh, timer)
for {
select {
case <-ctx.Done():
Expand All @@ -275,7 +283,7 @@ func (p *DefaultOP) ensureKey(ctx context.Context, storage Storage, keyCh chan<-
}
ok, count = p.retry(count)
if ok {
explicit <- true
timer = time.After(0)
continue
}
logging.Log("OP-n6ynVE").WithError(err).Panic("error in key signer")
Expand Down
3 changes: 2 additions & 1 deletion pkg/op/mock/storage.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/op/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type AuthStorage interface {
AuthRequestByID(context.Context, string) (AuthRequest, error)
DeleteAuthRequest(context.Context, string) error

GetSigningKey(context.Context, chan<- jose.SigningKey, chan<- error, <-chan bool)
GetSigningKey(context.Context, chan<- jose.SigningKey, chan<- error, <-chan time.Time)
GetKeySet(context.Context) (*jose.JSONWebKeySet, error)
SaveNewKeyPair(context.Context) error
}
Expand Down

0 comments on commit a2e2f06

Please sign in to comment.