Skip to content

Commit 06dcac4

Browse files
committed
fix: remove signing key creation (when not found)
1 parent 4390119 commit 06dcac4

File tree

6 files changed

+14
-77
lines changed

6 files changed

+14
-77
lines changed

example/internal/mock/storage.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,12 @@ func (s *AuthStorage) CreateToken(_ context.Context, authReq op.TokenRequest) (s
157157
func (s *AuthStorage) TerminateSession(_ context.Context, userID, clientID string) error {
158158
return nil
159159
}
160-
func (s *AuthStorage) GetSigningKey(_ context.Context, keyCh chan<- jose.SigningKey, _ chan<- error, _ <-chan time.Time) {
160+
func (s *AuthStorage) GetSigningKey(_ context.Context, keyCh chan<- jose.SigningKey) {
161161
keyCh <- jose.SigningKey{Algorithm: jose.RS256, Key: s.key}
162162
}
163163
func (s *AuthStorage) GetKey(_ context.Context) (*rsa.PrivateKey, error) {
164164
return s.key, nil
165165
}
166-
func (s *AuthStorage) SaveNewKeyPair(ctx context.Context) error {
167-
return nil
168-
}
169166
func (s *AuthStorage) GetKeySet(_ context.Context) (*jose.JSONWebKeySet, error) {
170167
pubkey := s.key.Public()
171168
return &jose.JSONWebKeySet{

pkg/op/mock/storage.mock.go

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/op/op.go

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"time"
88

9-
"github.com/caos/logging"
109
"github.com/gorilla/handlers"
1110
"github.com/gorilla/mux"
1211
"github.com/gorilla/schema"
@@ -132,7 +131,7 @@ func NewOpenIDProvider(ctx context.Context, config *Config, storage Storage, opO
132131

133132
keyCh := make(chan jose.SigningKey)
134133
o.signer = NewSigner(ctx, storage, keyCh)
135-
go EnsureKey(ctx, storage, keyCh, o.timer, o.retry)
134+
go storage.GetSigningKey(ctx, keyCh)
136135

137136
o.httpHandler = CreateRouter(o, o.interceptors...)
138137

@@ -282,36 +281,6 @@ func (o *openIDKeySet) VerifySignature(ctx context.Context, jws *jose.JSONWebSig
282281
return payload, err
283282
}
284283

285-
func EnsureKey(ctx context.Context, storage Storage, keyCh chan<- jose.SigningKey, timer <-chan time.Time, retry func(int) (bool, int)) {
286-
count := 0
287-
timer = time.After(0)
288-
errCh := make(chan error)
289-
go storage.GetSigningKey(ctx, keyCh, errCh, timer)
290-
for {
291-
select {
292-
case <-ctx.Done():
293-
return
294-
case err := <-errCh:
295-
if err == nil {
296-
continue
297-
}
298-
_, ok := err.(StorageNotFoundError)
299-
if ok {
300-
err := storage.SaveNewKeyPair(ctx)
301-
if err == nil {
302-
continue
303-
}
304-
}
305-
ok, count = retry(count)
306-
if ok {
307-
timer = time.After(0)
308-
continue
309-
}
310-
logging.Log("OP-n6ynVE").WithError(err).Panic("error in key signer")
311-
}
312-
}
313-
}
314-
315284
type Option func(o *openidProvider) error
316285

317286
func WithCustomAuthEndpoint(endpoint Endpoint) Option {
@@ -382,27 +351,6 @@ func WithHttpInterceptors(interceptors ...HttpInterceptor) Option {
382351
}
383352
}
384353

385-
func WithRetry(max int, sleep time.Duration) Option {
386-
return func(o *openidProvider) error {
387-
o.retry = func(count int) (bool, int) {
388-
count++
389-
if count == max {
390-
return false, count
391-
}
392-
time.Sleep(sleep)
393-
return true, count
394-
}
395-
return nil
396-
}
397-
}
398-
399-
func WithTimer(timer <-chan time.Time) Option {
400-
return func(o *openidProvider) error {
401-
o.timer = timer
402-
return nil
403-
}
404-
}
405-
406354
func buildInterceptor(interceptors ...HttpInterceptor) func(http.HandlerFunc) http.Handler {
407355
return func(handlerFunc http.HandlerFunc) http.Handler {
408356
handler := handlerFuncToHandler(handlerFunc)

pkg/op/signer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ func (s *tokenSigner) Health(_ context.Context) error {
3434
if s.signer == nil {
3535
return errors.New("no signer")
3636
}
37+
if string(s.alg) == "" {
38+
return errors.New("no signing algorithm")
39+
}
3740
return nil
3841
}
3942

pkg/op/storage.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ type AuthStorage interface {
2020

2121
TerminateSession(context.Context, string, string) error
2222

23-
GetSigningKey(context.Context, chan<- jose.SigningKey, chan<- error, <-chan time.Time)
23+
GetSigningKey(context.Context, chan<- jose.SigningKey)
2424
GetKeySet(context.Context) (*jose.JSONWebKeySet, error)
25-
SaveNewKeyPair(context.Context) error
2625
}
2726

2827
type OPStorage interface {

pkg/utils/sign.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"encoding/json"
5+
"errors"
56

67
"gopkg.in/square/go-jose.v2"
78
)
@@ -15,6 +16,9 @@ func Sign(object interface{}, signer jose.Signer) (string, error) {
1516
}
1617

1718
func SignPayload(payload []byte, signer jose.Signer) (string, error) {
19+
if signer == nil {
20+
return "", errors.New("missing signer")
21+
}
1822
result, err := signer.Sign(payload)
1923
if err != nil {
2024
return "", err

0 commit comments

Comments
 (0)