Skip to content

Commit

Permalink
rpc: require JWT nonce to be present
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Feb 26, 2024
1 parent f51fa38 commit d135510
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 3 additions & 4 deletions rpc/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ func withIssuer(expectedIss string) jwt.ValidatorFunc {

func withSessionHash(expectedSessionHash string) jwt.ValidatorFunc {
return func(ctx context.Context, tok jwt.Token) jwt.ValidationError {
sessAddrClaim, ok := tok.Get("sequence:session_hash")
if ok && sessAddrClaim == expectedSessionHash {
sessHashClaim, ok := tok.Get("sequence:session_hash")
if ok && sessHashClaim == expectedSessionHash {
return nil
}

nonceClaim, ok := tok.Get("nonce")
if !ok {
// TODO: we might always want to require nonce to be present
return nil
return jwt.NewValidationError(fmt.Errorf("nonce not satisfied"))
}

nonceVal, _ := nonceClaim.(string)
Expand Down
20 changes: 17 additions & 3 deletions rpc/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestRPC_RegisterSession(t *testing.T) {
intentBuilderFn func(t *testing.T, data intents.IntentDataOpenSession) *proto.Intent
}{
"Basic": {
tokBuilderFn: func(b *jwt.Builder, url string) {
b.Claim("sequence:session_hash", sessHash)
},
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
require.NoError(t, err)
require.NotNil(t, sess)
Expand Down Expand Up @@ -83,6 +86,12 @@ func TestRPC_RegisterSession(t *testing.T) {
require.ErrorContains(t, err, "JWT validation: nonce not satisfied")
},
},
"WithMissingNonce": {
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
require.Nil(t, sess)
require.ErrorContains(t, err, "JWT validation: nonce not satisfied")
},
},
"WithInvalidNonceButValidSessionAddressClaim": {
tokBuilderFn: func(b *jwt.Builder, url string) {
b.Claim("nonce", "0x1234567890abcdef").
Expand All @@ -97,7 +106,9 @@ func TestRPC_RegisterSession(t *testing.T) {
},
"WithVerifiedEmail": {
tokBuilderFn: func(b *jwt.Builder, url string) {
b.Claim("email", "[email protected]").Claim("email_verified", "true")
b.Claim("email", "[email protected]").
Claim("email_verified", "true").
Claim("sequence:session_hash", sessHash)
},
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
require.NoError(t, err)
Expand All @@ -108,7 +119,9 @@ func TestRPC_RegisterSession(t *testing.T) {
},
"WithUnverifiedEmail": {
tokBuilderFn: func(b *jwt.Builder, url string) {
b.Claim("email", "[email protected]").Claim("email_verified", "false")
b.Claim("email", "[email protected]").
Claim("email_verified", "false").
Claim("sequence:session_hash", sessHash)
},
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
require.NoError(t, err)
Expand All @@ -134,7 +147,8 @@ func TestRPC_RegisterSession(t *testing.T) {
},
"IssuerMissingScheme": {
tokBuilderFn: func(b *jwt.Builder, url string) {
b.Issuer(strings.TrimPrefix(url, "http://"))
b.Issuer(strings.TrimPrefix(url, "http://")).
Claim("sequence:session_hash", sessHash)
},
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
require.NoError(t, err)
Expand Down

0 comments on commit d135510

Please sign in to comment.