Skip to content

Commit

Permalink
Remove Context method from dynotest.Issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyp committed Mar 14, 2024
1 parent 17448ae commit f9a1f57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions dynoid/dynoid_test.go
@@ -1,13 +1,14 @@
package dynoid

import (
"context"
"testing"

"github.com/heroku/x/dynoid/dynoidtest"
)

func TestVerification(t *testing.T) {
iss, err := dynoidtest.New()
ctx, iss, err := dynoidtest.NewWithContext(context.Background())
if err != nil {
t.Fatal(err)
}
Expand All @@ -19,7 +20,6 @@ func TestVerification(t *testing.T) {

verifier := NewWithCallback("heroku", AllowHerokuHost(dynoidtest.IssuerHost))

ctx := iss.Context()
if _, err = verifier.Verify(ctx, token); err != nil {
t.Error(err)
}
Expand Down
16 changes: 6 additions & 10 deletions dynoid/dynoidtest/dynoidtest.go
Expand Up @@ -23,23 +23,23 @@ const (

type Issuer struct {
key *rsa.PrivateKey
ctx context.Context
}

func New() (*Issuer, error) {
return NewWithContext(context.Background())
_, iss, err := NewWithContext(context.Background())
return iss, err
}

func NewWithContext(ctx context.Context) (*Issuer, error) {
func NewWithContext(ctx context.Context) (context.Context, *Issuer, error) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
return ctx, nil, err
}

iss := &Issuer{key: key}
iss.ctx = oidc.ClientContext(ctx, iss.HTTPClient())
ctx = oidc.ClientContext(ctx, iss.HTTPClient())

return iss, nil
return ctx, iss, nil
}

func (iss *Issuer) GenerateIDToken(clientID string) (string, error) {
Expand All @@ -59,10 +59,6 @@ func (iss *Issuer) GenerateIDToken(clientID string) (string, error) {
return token.SignedString(iss.key)
}

func (iss *Issuer) Context() context.Context {
return iss.ctx
}

func (iss *Issuer) HTTPClient() *http.Client {
return &http.Client{Transport: &roundTripper{issuer: iss}}
}
Expand Down
8 changes: 4 additions & 4 deletions dynoid/middleware/dynoid_test.go
Expand Up @@ -24,7 +24,7 @@ func TestAuthorize(t *testing.T) {
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
})

ctx, generate := newIssuer(t)
ctx, generate := newTokenGenerator(t)

tests := map[string]struct {
AuthorizationHeader string
Expand Down Expand Up @@ -59,13 +59,13 @@ func TestAuthorize(t *testing.T) {
}
}

func newIssuer(t *testing.T) (context.Context, func(clientID string) (header string)) {
issuer, err := dynoidtest.New()
func newTokenGenerator(t *testing.T) (context.Context, func(clientID string) (header string)) {
ctx, issuer, err := dynoidtest.NewWithContext(context.Background())
if err != nil {
t.Fatalf("failed to get new issuer (%v)", err)
}

return issuer.Context(), func(clientID string) string {
return ctx, func(clientID string) string {
token, err := issuer.GenerateIDToken(clientID)
if err != nil {
t.Fatalf("failed to generate token (%v)", err)
Expand Down

0 comments on commit f9a1f57

Please sign in to comment.