Skip to content

Commit

Permalink
Merge pull request #128 from shogo82148/fix-typo-of-verifier
Browse files Browse the repository at this point in the history
fix typo of verifier
  • Loading branch information
shogo82148 authored Oct 8, 2023
2 parents e11514d + 66c82ff commit 18c6b25
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"agcmkw",
"AQAB",
"ciphertext",
"COSE",
"cryptorand",
"Diffie-Hellman",
"ECDH",
Expand All @@ -23,6 +24,7 @@
"pbse2",
"pubinfo",
"pubkey",
"rawurl",
"RSAES",
"rsaoaep",
"rsapkcs",
Expand Down
8 changes: 4 additions & 4 deletions jws/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func ExampleParse() {
log.Fatal(err)
}
v := &jws.Verifier{
AlgorithmVerfier: jws.AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &jws.JWKKeyFinder{JWK: key},
AlgorithmVerifier: jws.AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &jws.JWKKeyFinder{JWK: key},
}

raw := "eyJhbGciOiJFZERTQSJ9" +
Expand Down Expand Up @@ -51,8 +51,8 @@ func ExampleVerifier_Verify() {
log.Fatal(err)
}
v := &jws.Verifier{
AlgorithmVerfier: jws.AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &jws.JWKKeyFinder{JWK: key},
AlgorithmVerifier: jws.AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &jws.JWKKeyFinder{JWK: key},
}

raw := "eyJhbGciOiJFZERTQSJ9" +
Expand Down
6 changes: 3 additions & 3 deletions jws/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func FuzzJWS(f *testing.F) {
}
var header1 *Header
v := &Verifier{
AlgorithmVerfier: UnsecureAnyAlgorithm,
AlgorithmVerifier: UnsecureAnyAlgorithm,
KeyFinder: FindKeyFunc(func(_ context.Context, protected, unprotected *Header) (sig.SigningKey, error) {
alg := protected.Algorithm()
if !alg.Available() {
Expand Down Expand Up @@ -322,7 +322,7 @@ func FuzzJWSCompact(f *testing.F) {
}
var sigKey sig.SigningKey
v1 := &Verifier{
AlgorithmVerfier: UnsecureAnyAlgorithm,
AlgorithmVerifier: UnsecureAnyAlgorithm,
KeyFinder: FindKeyFunc(func(_ context.Context, header, _ *Header) (sig.SigningKey, error) {
alg := header.Algorithm()
if !alg.Available() {
Expand Down Expand Up @@ -355,7 +355,7 @@ func FuzzJWSCompact(f *testing.F) {
}

v2 := &Verifier{
AlgorithmVerfier: UnsecureAnyAlgorithm,
AlgorithmVerifier: UnsecureAnyAlgorithm,
KeyFinder: FindKeyFunc(func(_ context.Context, header, _ *Header) (sig.SigningKey, error) {
return sigKey, nil
}),
Expand Down
34 changes: 17 additions & 17 deletions jws/jws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.HS256},
KeyFinder: &JWKKeyFinder{JWK: key},
AlgorithmVerifier: AllowedAlgorithms{jwa.HS256},
KeyFinder: &JWKKeyFinder{JWK: key},
}

msg, err := Parse(raw)
Expand Down Expand Up @@ -113,8 +113,8 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.RS256},
KeyFinder: &JWKKeyFinder{JWK: key},
AlgorithmVerifier: AllowedAlgorithms{jwa.RS256},
KeyFinder: &JWKKeyFinder{JWK: key},
}

msg, err := Parse(raw)
Expand Down Expand Up @@ -159,8 +159,8 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.ES256},
KeyFinder: &JWKKeyFinder{JWK: key},
AlgorithmVerifier: AllowedAlgorithms{jwa.ES256},
KeyFinder: &JWKKeyFinder{JWK: key},
}

msg, err := Parse(raw)
Expand Down Expand Up @@ -211,8 +211,8 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.ES512},
KeyFinder: &JWKKeyFinder{JWK: key},
AlgorithmVerifier: AllowedAlgorithms{jwa.ES512},
KeyFinder: &JWKKeyFinder{JWK: key},
}

msg, err := Parse(raw)
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.None},
AlgorithmVerifier: AllowedAlgorithms{jwa.None},
KeyFinder: FindKeyFunc(func(ctx context.Context, header, _ *Header) (sig.SigningKey, error) {
return header.Algorithm().New().NewSigningKey(nil), nil
}),
Expand Down Expand Up @@ -278,8 +278,8 @@ func TestVerify(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &JWKKeyFinder{JWK: key},
AlgorithmVerifier: AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &JWKKeyFinder{JWK: key},
}

raw := "eyJhbGciOiJFZERTQSJ9" +
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestUnmarshalJSON(t *testing.T) {
}

v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.RS256},
AlgorithmVerifier: AllowedAlgorithms{jwa.RS256},
KeyFinder: FindKeyFunc(func(_ context.Context, protected, header *Header) (sig.SigningKey, error) {
if header.KeyID() != "2010-12-29" {
return nil, errors.New("unknown key id")
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestUnmarshalJSON(t *testing.T) {
}

v = &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.ES256},
AlgorithmVerifier: AllowedAlgorithms{jwa.ES256},
KeyFinder: FindKeyFunc(func(_ context.Context, protected, header *Header) (sig.SigningKey, error) {
if header.KeyID() != "e9bc097a-ce51-4036-9562-d2ade882db0d" {
return nil, errors.New("unknown key id")
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestUnmarshalJSON(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.ES256},
AlgorithmVerifier: AllowedAlgorithms{jwa.ES256},
KeyFinder: FindKeyFunc(func(_ context.Context, protected, header *Header) (sig.SigningKey, error) {
if header.KeyID() != "e9bc097a-ce51-4036-9562-d2ade882db0d" {
return nil, errors.New("unknown key id")
Expand Down Expand Up @@ -481,7 +481,7 @@ func TestUnmarshalJSON(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.HS256},
AlgorithmVerifier: AllowedAlgorithms{jwa.HS256},
KeyFinder: FindKeyFunc(func(_ context.Context, protected, header *Header) (sig.SigningKey, error) {
rawKey := `{` +
`"kty":"oct",` +
Expand Down Expand Up @@ -850,8 +850,8 @@ func TestKeyTypeMissmatch(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{jwa.ES256},
KeyFinder: &JWKKeyFinder{JWK: key},
AlgorithmVerifier: AllowedAlgorithms{jwa.ES256},
KeyFinder: &JWKKeyFinder{JWK: key},
}
msg, err := Parse(raw)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions jws/rfc7520_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestRFC7520(t *testing.T) {
t.Fatal(err)
}
v := &Verifier{
AlgorithmVerfier: AllowedAlgorithms{tv.Input.Algorithm},
AlgorithmVerifier: AllowedAlgorithms{tv.Input.Algorithm},
KeyFinder: FindKeyFunc(func(_ context.Context, protected, unprotected *Header) (key sig.SigningKey, err error) {
if protected.KeyID() != tv.Input.Key.KeyID() {
return nil, errors.New("key not found")
Expand All @@ -75,7 +75,7 @@ func TestRFC7520(t *testing.T) {

// verify the signature of the JSON serialization.
v = &Verifier{
AlgorithmVerfier: AllowedAlgorithms{tv.Input.Algorithm},
AlgorithmVerifier: AllowedAlgorithms{tv.Input.Algorithm},
KeyFinder: FindKeyFunc(func(_ context.Context, protected, unprotected *Header) (key sig.SigningKey, err error) {
if protected.KeyID() != tv.Input.Key.KeyID() {
return nil, errors.New("key not found")
Expand Down
14 changes: 7 additions & 7 deletions jws/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

var errVerifyFailed = errors.New("jws: failed to verify the message")

// AlgorithmVerfier verifies the algorithm used for signing.
type AlgorithmVerfier interface {
// AlgorithmVerifier verifies the algorithm used for signing.
type AlgorithmVerifier interface {
VerifyAlgorithm(ctx context.Context, alg jwa.SignatureAlgorithm) error
}

Expand All @@ -25,7 +25,7 @@ func (a AllowedAlgorithms) VerifyAlgorithm(ctx context.Context, alg jwa.Signatur
return errors.New("jws: signing algorithm is not allowed")
}

// UnsecureAnyAlgorithm is an AlgorithmVerfier that accepts any algorithm.
// UnsecureAnyAlgorithm is an AlgorithmVerifier that accepts any algorithm.
var UnsecureAnyAlgorithm = unsecureAnyAlgorithmVerifier{}

type unsecureAnyAlgorithmVerifier struct{}
Expand All @@ -38,14 +38,14 @@ func (unsecureAnyAlgorithmVerifier) VerifyAlgorithm(ctx context.Context, alg jwa
type Verifier struct {
_NamedFieldsRequired struct{}

AlgorithmVerfier AlgorithmVerfier
KeyFinder KeyFinder
AlgorithmVerifier AlgorithmVerifier
KeyFinder KeyFinder
}

// Verify verifies the JWS message.
func (v *Verifier) Verify(ctx context.Context, msg *Message) (protected *Header, payload []byte, err error) {
_ = v._NamedFieldsRequired
if v.AlgorithmVerfier == nil || v.KeyFinder == nil {
if v.AlgorithmVerifier == nil || v.KeyFinder == nil {
return nil, nil, errors.New("jws: verifier is not configured")
}

Expand All @@ -60,7 +60,7 @@ func (v *Verifier) Verify(ctx context.Context, msg *Message) (protected *Header,
buf := make([]byte, size)

for _, sig := range msg.Signatures {
if err := v.AlgorithmVerfier.VerifyAlgorithm(ctx, sig.protected.alg); err != nil {
if err := v.AlgorithmVerifier.VerifyAlgorithm(ctx, sig.protected.alg); err != nil {
continue
}
key, err := v.KeyFinder.FindKey(ctx, sig.protected, sig.header)
Expand Down
2 changes: 1 addition & 1 deletion jwt/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExampleParse() {
"eyJhdWQiOiJodHRwczovL2dpdGh1Yi5jb20vc2hvZ284MjE0OCIsImlzcyI6Imh0dHBzOi8vZ2l0aHViLmNvbS9zaG9nbzgyMTQ4L2dvYXQifQ." +
"2p0nndDnxqsA9u1unq2bLPJiJpSj0hOfCNXe1b_Dsu7LskZPj1lFxv56rptqalzYVmR8kcrMyEIrRb94gr_KBw"
p := &jwt.Parser{
AlgorithmVerfier: jwt.AllowedAlgorithms{jwa.EdDSA},
AlgorithmVerifier: jwt.AllowedAlgorithms{jwa.EdDSA},
KeyFinder: &jwt.JWKKeyFiner{Key: key},
IssuerSubjectVerifier: jwt.Issuer("https://github.com/shogo82148/goat"),
AudienceVerifier: jwt.Audience("https://github.com/shogo82148"),
Expand Down
4 changes: 2 additions & 2 deletions jwt/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func FuzzJWT(f *testing.F) {
sigKey = alg.New().NewSigningKey(k)
return sigKey, nil
}),
AlgorithmVerfier: UnsecureAnyAlgorithm,
AlgorithmVerifier: UnsecureAnyAlgorithm,
IssuerSubjectVerifier: UnsecureAnyIssuerSubject,
AudienceVerifier: UnsecureAnyAudience,
}
Expand All @@ -125,7 +125,7 @@ func FuzzJWT(f *testing.F) {
KeyFinder: FindKeyFunc(func(ctx context.Context, header *jws.Header) (sig.SigningKey, error) {
return sigKey, nil
}),
AlgorithmVerfier: UnsecureAnyAlgorithm,
AlgorithmVerifier: UnsecureAnyAlgorithm,
IssuerSubjectVerifier: UnsecureAnyIssuerSubject,
AudienceVerifier: UnsecureAnyAudience,
}
Expand Down
8 changes: 4 additions & 4 deletions jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestParse(t *testing.T) {
}
p := &Parser{
KeyFinder: &JWKKeyFiner{Key: key},
AlgorithmVerfier: AllowedAlgorithms{jwa.HS256},
AlgorithmVerifier: AllowedAlgorithms{jwa.HS256},
IssuerSubjectVerifier: Issuer("joe"),
AudienceVerifier: UnsecureAnyAudience,
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestParse(t *testing.T) {
alg := header.Algorithm().New()
return alg.NewSigningKey(nil), nil
}),
AlgorithmVerfier: AllowedAlgorithms{jwa.None},
AlgorithmVerifier: AllowedAlgorithms{jwa.None},
IssuerSubjectVerifier: Issuer("joe"),
AudienceVerifier: UnsecureAnyAudience,
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestParse_Claims(t *testing.T) {
alg := jwa.None.New()
return alg.NewSigningKey(nil), nil
}),
AlgorithmVerfier: AllowedAlgorithms{jwa.None},
AlgorithmVerifier: AllowedAlgorithms{jwa.None},
IssuerSubjectVerifier: UnsecureAnyIssuerSubject,
AudienceVerifier: UnsecureAnyAudience,
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func BenchmarkParse(b *testing.B) {
}
p := &Parser{
KeyFinder: &JWKKeyFiner{Key: key},
AlgorithmVerfier: AllowedAlgorithms{jwa.HS256},
AlgorithmVerifier: AllowedAlgorithms{jwa.HS256},
IssuerSubjectVerifier: Issuer("joe"),
AudienceVerifier: UnsecureAnyAudience,
}
Expand Down
14 changes: 7 additions & 7 deletions jwt/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func (f FindKeyFunc) FindKey(ctx context.Context, header *jws.Header) (sig.Signi
return f(ctx, header)
}

// AlgorithmVerfier verifies the algorithm used for signing.
type AlgorithmVerfier interface {
// AlgorithmVerifier verifies the algorithm used for signing.
type AlgorithmVerifier interface {
VerifyAlgorithm(ctx context.Context, alg jwa.SignatureAlgorithm) error
}

// UnsecureAnyAlgorithm is an AlgorithmVerfier that accepts any algorithm.
// UnsecureAnyAlgorithm is an AlgorithmVerifier that accepts any algorithm.
var UnsecureAnyAlgorithm = unsecureAnyAlgorithmVerifier{}

type unsecureAnyAlgorithmVerifier struct{}
Expand All @@ -42,7 +42,7 @@ func (unsecureAnyAlgorithmVerifier) VerifyAlgorithm(ctx context.Context, alg jwa
return nil
}

// AllowedAlgorithms is an AlgorithmVerfier that accepts only the specified algorithms.
// AllowedAlgorithms is an AlgorithmVerifier that accepts only the specified algorithms.
type AllowedAlgorithms []jwa.SignatureAlgorithm

func (a AllowedAlgorithms) VerifyAlgorithm(ctx context.Context, alg jwa.SignatureAlgorithm) error {
Expand Down Expand Up @@ -108,15 +108,15 @@ type Parser struct {
_NamedFieldsRequired struct{}

KeyFinder KeyFinder
AlgorithmVerfier AlgorithmVerfier
AlgorithmVerifier AlgorithmVerifier
IssuerSubjectVerifier IssuerSubjectVerifier
AudienceVerifier AudienceVerifier
}

func (p *Parser) Parse(ctx context.Context, data []byte) (*Token, error) {
// verify the parser options
_ = p._NamedFieldsRequired
if p.KeyFinder == nil || p.AlgorithmVerfier == nil || p.IssuerSubjectVerifier == nil || p.AudienceVerifier == nil {
if p.KeyFinder == nil || p.AlgorithmVerifier == nil || p.IssuerSubjectVerifier == nil || p.AudienceVerifier == nil {
return nil, errors.New("jwt: parser is not configured")
}

Expand Down Expand Up @@ -154,7 +154,7 @@ func (p *Parser) Parse(ctx context.Context, data []byte) (*Token, error) {
if header.UnmarshalJSON(buf[:n]) != nil {
return nil, fmt.Errorf("jwt: failed to parse header: %w", err)
}
if err := p.AlgorithmVerfier.VerifyAlgorithm(ctx, header.Algorithm()); err != nil {
if err := p.AlgorithmVerifier.VerifyAlgorithm(ctx, header.Algorithm()); err != nil {
return nil, fmt.Errorf("jwt: failed to verify algorithm: %w", err)
}

Expand Down

0 comments on commit 18c6b25

Please sign in to comment.