Skip to content

Commit

Permalink
Empty Disclosure Fix (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchulzeStTSI authored Oct 17, 2024
1 parent e8d5d34 commit b33e29b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
12 changes: 7 additions & 5 deletions sd-jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/MichaelFraser99/go-sd-jwt/disclosure"
e "github.com/MichaelFraser99/go-sd-jwt/internal/error"
"github.com/MichaelFraser99/go-sd-jwt/internal/utils"
"github.com/MichaelFraser99/go-sd-jwt/kbjwt"
"hash"
"slices"
"strings"
"time"

"github.com/MichaelFraser99/go-sd-jwt/disclosure"
e "github.com/MichaelFraser99/go-sd-jwt/internal/error"
"github.com/MichaelFraser99/go-sd-jwt/internal/utils"
"github.com/MichaelFraser99/go-sd-jwt/kbjwt"
)

// SdJwt this object represents a valid SD-JWT. Created using the FromToken function which performs the required validation.
Expand Down Expand Up @@ -247,9 +248,10 @@ func (s *SdJwt) GetDisclosedClaims() (map[string]any, error) {
h.Reset()
}

if len(indexesFound) == 0 {
if len(indexesFound) == 0 && len(disclosuresToCheck) > 0 {
return nil, fmt.Errorf("no matching digest found for: %v", utils.StringifyDisclosures(disclosuresToCheck))
}

slices.Sort(indexesFound)
slices.Reverse(indexesFound)
for _, i := range indexesFound {
Expand Down
22 changes: 20 additions & 2 deletions sd-jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"testing"

"github.com/MichaelFraser99/go-jose/jws"
"github.com/MichaelFraser99/go-jose/model"
go_sd_jwt "github.com/MichaelFraser99/go-sd-jwt"
"github.com/MichaelFraser99/go-sd-jwt/disclosure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -653,3 +654,20 @@ func TestNew_AllDuplicateDigestScenarios(t *testing.T) {
}
}
}

func TestSDJwtWithoutSD(t *testing.T) {
testJwt := "eyJ0eXAiOiJzZCtqd3QiLCJhbGciOiJFUzI1NiJ9.eyJmaXJzdG5hbWUiOiJKb2huIiwibGFzdG5hbWUiOiJEb2UiLCJzc24iOiIxMjMtNDUtNjc4OSIsImlkIjoiMTIzNCIsIl9zZF9hbGciOiJTSEEtMjU2In0.sUA_aYeA4YNQ1Paxna30VLAce1KdxvYMPEIduCwSD6X_Z56ZrBY5fbUBM5JVQ3vceS86CCghr8wkemdhQYRdfA~"
sdJwt, err := go_sd_jwt.New(testJwt)

if err != nil {
t.Log("Token not parseable")
}

_, err = sdJwt.GetDisclosedClaims()

if err != nil {
t.Log("Token cant survive without Selective Discloures")
t.Error("The token has empty selective disclosure but fails in parsing.")
}

}

0 comments on commit b33e29b

Please sign in to comment.