Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to Signer interface #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions secevent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"crypto/rand"
"fmt"
"time"
"context"

"github.com/sgnl-ai/caep.dev/secevent/pkg/schemes/caep"
"github.com/sgnl-ai/caep.dev/secevent/pkg/builder"
Expand Down Expand Up @@ -98,7 +99,7 @@ func main() {
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -328,14 +329,15 @@ import (
"strings"
"github.com/golang-jwt/jwt/v5"
"github.com/sgnl-ai/caep.dev/secevent/pkg/signing"
"context"
)

// CustomSigner implements the Signer interface
type CustomSigner struct {
// Fields for HSM client or external service configuration
}

func (s *CustomSigner) Sign(claims jwt.Claims) (string, error) {
func (s *CustomSigner) Sign(ctx context.Context, claims jwt.Claims) (string, error) {
// Create token with claims
token := jwt.NewWithClaims(s.signingMethod, claims)

Expand All @@ -350,7 +352,7 @@ func (s *CustomSigner) Sign(claims jwt.Claims) (string, error) {
}

// Use your HSM or external service to sign the string
signature, err := externalSign(signingString)
signature, err := externalSign(ctx, signingString)
if err != nil {
return "", err
}
Expand All @@ -360,7 +362,7 @@ func (s *CustomSigner) Sign(claims jwt.Claims) (string, error) {
}

// externalSign is a placeholder function representing the external signing process
func externalSign(signingString string) (string, error) {
func externalSign(ctx context.Context, signingString string) (string, error) {
// Implement the signing logic using your HSM or external service
return "signature", nil
}
Expand All @@ -373,7 +375,7 @@ func main() {
WithIssuer("https://issuer.example.com")
// ... other SecEvent configuration

signedToken, err := customSigner.Sign(secEvent)
signedToken, err := customSigner.Sign(context.Background(), secEvent)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -50,7 +51,7 @@ func main() {
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -50,7 +51,7 @@ func main() {
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -47,7 +48,7 @@ func main() {
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
panic(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -154,7 +155,7 @@ func generateSignedAssuranceLevelChangeSecEvent(privateKey *ecdsa.PrivateKey) (s
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
return "", fmt.Errorf("failed to sign event: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -155,7 +156,7 @@ func generateSignedAssuranceLevelChangeSecEvent(privateKey *ecdsa.PrivateKey) (s
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
return "", fmt.Errorf("failed to sign event: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -147,7 +148,7 @@ func generateSignedSessionRevokedSecEvent(privateKey *ecdsa.PrivateKey) (string,
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
return "", fmt.Errorf("failed to sign event: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -148,7 +149,7 @@ func generateSignedSessionRevokedSecEvent(privateKey *ecdsa.PrivateKey) (string,
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
return "", fmt.Errorf("failed to sign event: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -137,7 +138,7 @@ func generateSignedStreamUpdateSecEvent(privateKey *ecdsa.PrivateKey) (string, e
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
return "", fmt.Errorf("failed to sign event: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -138,7 +139,7 @@ func generateSignedStreamUpdateSecEvent(privateKey *ecdsa.PrivateKey) (string, e
}

// Sign the SecEvent
signedToken, err := signer.Sign(secEvent)
signedToken, err := signer.Sign(context.Background(), secEvent)
if err != nil {
return "", fmt.Errorf("failed to sign event: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions secevent/pkg/signing/signer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package signing

import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
Expand All @@ -11,7 +12,7 @@ import (

// Signer defines the interface for signing tokens
type Signer interface {
Sign(claims jwt.Claims) (string, error)
Sign(ctx context.Context, claims jwt.Claims) (string, error)
}

// DefaultSigner uses the user supplied private key to sign
Expand Down Expand Up @@ -57,7 +58,8 @@ func NewSigner(signingKey crypto.PrivateKey, opts ...SignerOption) (*DefaultSign
return signer, nil
}

func (s *DefaultSigner) Sign(claims jwt.Claims) (string, error) {
func (s *DefaultSigner) Sign(ctx context.Context, claims jwt.Claims) (string, error) {
// ctx is unused in this implementation since signing is purely local and operation is fast
token := jwt.NewWithClaims(s.signingMethod, claims)

if s.keyID != nil {
Expand Down
Loading