Merge branch 'main' into use-ed25519-in-examples

This commit is contained in:
G1gg1L3s 2023-07-01 00:00:06 +03:00
commit 7473f0f30a
No known key found for this signature in database
GPG Key ID: B00B1350F7E96369
8 changed files with 10 additions and 20 deletions

View File

@ -1,11 +1,10 @@
package jwt package jwt
import ( import (
"errors"
"crypto" "crypto"
"crypto/ed25519" "crypto/ed25519"
"crypto/rand" "crypto/rand"
"errors"
) )
var ( var (

View File

@ -22,7 +22,7 @@ func (je joinedError) Is(err error) bool {
// wrappedErrors is a workaround for wrapping multiple errors in environments // wrappedErrors is a workaround for wrapping multiple errors in environments
// where Go 1.20 is not available. It basically uses the already implemented // where Go 1.20 is not available. It basically uses the already implemented
// functionatlity of joinedError to handle multiple errors with supplies a // functionality of joinedError to handle multiple errors with supplies a
// custom error message that is identical to the one we produce in Go 1.20 using // custom error message that is identical to the one we produce in Go 1.20 using
// multiple %w directives. // multiple %w directives.
type wrappedErrors struct { type wrappedErrors struct {

View File

@ -110,11 +110,10 @@ func Example_getTokenViaHTTP() {
claims := token.Claims.(*CustomClaimsExample) claims := token.Claims.(*CustomClaimsExample)
fmt.Println(claims.CustomerInfo.Name) fmt.Println(claims.CustomerInfo.Name)
//Output: test // Output: test
} }
func Example_useTokenViaHTTP() { func Example_useTokenViaHTTP() {
// Make a sample token // Make a sample token
// In a real world situation, this token will have been acquired from // In a real world situation, this token will have been acquired from
// some other API call (see Example_getTokenViaHTTP) // some other API call (see Example_getTokenViaHTTP)

View File

@ -46,9 +46,9 @@ func TestVerifyAud(t *testing.T) {
// []interface{} // []interface{}
{Name: "Empty []interface{} Aud without match required", MapClaims: MapClaims{"aud": nilListInterface}, Expected: true, Required: false, Comparison: "example.com"}, {Name: "Empty []interface{} Aud without match required", MapClaims: MapClaims{"aud": nilListInterface}, Expected: true, Required: false, Comparison: "example.com"},
{Name: "[]interface{} Aud wit match required", MapClaims: MapClaims{"aud": []interface{}{"a", "foo", "example.com"}}, Expected: true, Required: true, Comparison: "example.com"}, {Name: "[]interface{} Aud with match required", MapClaims: MapClaims{"aud": []interface{}{"a", "foo", "example.com"}}, Expected: true, Required: true, Comparison: "example.com"},
{Name: "[]interface{} Aud wit match but invalid types", MapClaims: MapClaims{"aud": []interface{}{"a", 5, "example.com"}}, Expected: false, Required: true, Comparison: "example.com"}, {Name: "[]interface{} Aud with match but invalid types", MapClaims: MapClaims{"aud": []interface{}{"a", 5, "example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
{Name: "[]interface{} Aud int wit match required", MapClaims: MapClaims{"aud": intListInterface}, Expected: false, Required: true, Comparison: "example.com"}, {Name: "[]interface{} Aud int with match required", MapClaims: MapClaims{"aud": intListInterface}, Expected: false, Required: true, Comparison: "example.com"},
// interface{} // interface{}
{Name: "Empty interface{} Aud without match not required", MapClaims: MapClaims{"aud": nilInterface}, Expected: true, Required: false, Comparison: "example.com"}, {Name: "Empty interface{} Aud without match not required", MapClaims: MapClaims{"aud": nilInterface}, Expected: true, Required: false, Comparison: "example.com"},

View File

@ -42,7 +42,6 @@ func init() {
// Load private keys // Load private keys
jwtTestRSAPrivateKey = test.LoadRSAPrivateKeyFromDisk("test/sample_key") jwtTestRSAPrivateKey = test.LoadRSAPrivateKeyFromDisk("test/sample_key")
jwtTestEC256PrivateKey = test.LoadECPrivateKeyFromDisk("test/ec256-private.pem") jwtTestEC256PrivateKey = test.LoadECPrivateKeyFromDisk("test/ec256-private.pem")
} }
var jwtTestData = []struct { var jwtTestData = []struct {
@ -352,11 +351,9 @@ func signToken(claims jwt.Claims, signingMethod jwt.SigningMethod) string {
} }
func TestParser_Parse(t *testing.T) { func TestParser_Parse(t *testing.T) {
// Iterate over test data set and run tests // Iterate over test data set and run tests
for _, data := range jwtTestData { for _, data := range jwtTestData {
t.Run(data.name, func(t *testing.T) { t.Run(data.name, func(t *testing.T) {
// If the token string is blank, use helper function to generate string // If the token string is blank, use helper function to generate string
if data.tokenString == "" { if data.tokenString == "" {
data.tokenString = signToken(data.claims, data.signingMethod) data.tokenString = signToken(data.claims, data.signingMethod)
@ -428,7 +425,6 @@ func TestParser_Parse(t *testing.T) {
} }
func TestParser_ParseUnverified(t *testing.T) { func TestParser_ParseUnverified(t *testing.T) {
// Iterate over test data set and run tests // Iterate over test data set and run tests
for _, data := range jwtTestData { for _, data := range jwtTestData {
// Skip test data, that intentionally contains malformed tokens, as they would lead to an error // Skip test data, that intentionally contains malformed tokens, as they would lead to an error
@ -670,13 +666,11 @@ func TestSetPadding(t *testing.T) {
err, err,
) )
} }
}) })
} }
} }
func BenchmarkParseUnverified(b *testing.B) { func BenchmarkParseUnverified(b *testing.B) {
// Iterate over test data set and run tests // Iterate over test data set and run tests
for _, data := range jwtTestData { for _, data := range jwtTestData {
// If the token string is blank, use helper function to generate string // If the token string is blank, use helper function to generate string

View File

@ -17,7 +17,7 @@ func ExampleHeaderExtractor() {
} else { } else {
fmt.Println(err) fmt.Println(err)
} }
//Output: A // Output: A
} }
func ExampleArgumentExtractor() { func ExampleArgumentExtractor() {
@ -28,5 +28,5 @@ func ExampleArgumentExtractor() {
} else { } else {
fmt.Println(err) fmt.Println(err)
} }
//Output: A // Output: A
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math" "math"
"reflect"
"strconv" "strconv"
"time" "time"
) )
@ -121,14 +120,14 @@ func (s *ClaimStrings) UnmarshalJSON(data []byte) (err error) {
for _, vv := range v { for _, vv := range v {
vs, ok := vv.(string) vs, ok := vv.(string)
if !ok { if !ok {
return &json.UnsupportedTypeError{Type: reflect.TypeOf(vv)} return ErrInvalidType
} }
aud = append(aud, vs) aud = append(aud, vs)
} }
case nil: case nil:
return nil return nil
default: default:
return &json.UnsupportedTypeError{Type: reflect.TypeOf(v)} return ErrInvalidType
} }
*s = aud *s = aud

View File

@ -41,7 +41,6 @@ func TestSingleArrayMarshal(t *testing.T) {
expected := `"test"` expected := `"test"`
b, err := json.Marshal(s) b, err := json.Marshal(s)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %s", err) t.Errorf("Unexpected error: %s", err)
} }