Skip to content

Commit 01e0315

Browse files
authored
Merge pull request #24 from 0xsequence/jwt-nonce-required
Require JWT nonce to be present
2 parents 3c05bc4 + d135510 commit 01e0315

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

rpc/identity.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ func withIssuer(expectedIss string) jwt.ValidatorFunc {
2828

2929
func withSessionHash(expectedSessionHash string) jwt.ValidatorFunc {
3030
return func(ctx context.Context, tok jwt.Token) jwt.ValidationError {
31-
sessAddrClaim, ok := tok.Get("sequence:session_hash")
32-
if ok && sessAddrClaim == expectedSessionHash {
31+
sessHashClaim, ok := tok.Get("sequence:session_hash")
32+
if ok && sessHashClaim == expectedSessionHash {
3333
return nil
3434
}
3535

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

4241
nonceVal, _ := nonceClaim.(string)

rpc/sessions_test.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func TestRPC_RegisterSession(t *testing.T) {
4646
intentBuilderFn func(t *testing.T, data intents.IntentDataOpenSession) *proto.Intent
4747
}{
4848
"Basic": {
49+
tokBuilderFn: func(b *jwt.Builder, url string) {
50+
b.Claim("sequence:session_hash", sessHash)
51+
},
4952
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
5053
require.NoError(t, err)
5154
require.NotNil(t, sess)
@@ -83,6 +86,12 @@ func TestRPC_RegisterSession(t *testing.T) {
8386
require.ErrorContains(t, err, "JWT validation: nonce not satisfied")
8487
},
8588
},
89+
"WithMissingNonce": {
90+
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
91+
require.Nil(t, sess)
92+
require.ErrorContains(t, err, "JWT validation: nonce not satisfied")
93+
},
94+
},
8695
"WithInvalidNonceButValidSessionAddressClaim": {
8796
tokBuilderFn: func(b *jwt.Builder, url string) {
8897
b.Claim("nonce", "0x1234567890abcdef").
@@ -97,7 +106,9 @@ func TestRPC_RegisterSession(t *testing.T) {
97106
},
98107
"WithVerifiedEmail": {
99108
tokBuilderFn: func(b *jwt.Builder, url string) {
100-
b.Claim("email", "[email protected]").Claim("email_verified", "true")
109+
b.Claim("email", "[email protected]").
110+
Claim("email_verified", "true").
111+
Claim("sequence:session_hash", sessHash)
101112
},
102113
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
103114
require.NoError(t, err)
@@ -108,7 +119,9 @@ func TestRPC_RegisterSession(t *testing.T) {
108119
},
109120
"WithUnverifiedEmail": {
110121
tokBuilderFn: func(b *jwt.Builder, url string) {
111-
b.Claim("email", "[email protected]").Claim("email_verified", "false")
122+
b.Claim("email", "[email protected]").
123+
Claim("email_verified", "false").
124+
Claim("sequence:session_hash", sessHash)
112125
},
113126
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
114127
require.NoError(t, err)
@@ -134,7 +147,8 @@ func TestRPC_RegisterSession(t *testing.T) {
134147
},
135148
"IssuerMissingScheme": {
136149
tokBuilderFn: func(b *jwt.Builder, url string) {
137-
b.Issuer(strings.TrimPrefix(url, "http://"))
150+
b.Issuer(strings.TrimPrefix(url, "http://")).
151+
Claim("sequence:session_hash", sessHash)
138152
},
139153
assertFn: func(t *testing.T, sess *proto.Session, err error, p assertionParams) {
140154
require.NoError(t, err)

0 commit comments

Comments
 (0)