| 1 |
package op_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
tu "github.com/zitadel/oidc/v3/internal/testutil" |
| 11 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 12 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 13 |
) |
| 14 |
|
| 15 |
func TestNewJWTProfileVerifier(t *testing.T) { |
| 16 |
want := &op.JWTProfileVerifier{ |
| 17 |
Verifier: oidc.Verifier{ |
| 18 |
Issuer: tu.ValidIssuer, |
| 19 |
MaxAgeIAT: time.Minute, |
| 20 |
Offset: time.Second, |
| 21 |
}, |
| 22 |
Storage: tu.JWTProfileKeyStorage{}, |
| 23 |
} |
| 24 |
got := op.NewJWTProfileVerifier(tu.JWTProfileKeyStorage{}, tu.ValidIssuer, time.Minute, time.Second, op.SubjectCheck(func(request *oidc.JWTTokenRequest) error { |
| 25 |
return oidc.ErrSubjectMissing |
| 26 |
})) |
| 27 |
assert.Equal(t, want.Verifier, got.Verifier) |
| 28 |
assert.Equal(t, want.Storage, got.Storage) |
| 29 |
assert.ErrorIs(t, got.CheckSubject(nil), oidc.ErrSubjectMissing) |
| 30 |
} |
| 31 |
|
| 32 |
func TestVerifyJWTAssertion(t *testing.T) { |
| 33 |
errCtx, cancel := context.WithCancel(context.Background()) |
| 34 |
cancel() |
| 35 |
|
| 36 |
verifier := op.NewJWTProfileVerifier(tu.JWTProfileKeyStorage{}, tu.ValidIssuer, time.Minute, 0) |
| 37 |
tests := []struct { |
| 38 |
name string |
| 39 |
ctx context.Context |
| 40 |
newToken func() (string, *oidc.JWTTokenRequest) |
| 41 |
wantErr error |
| 42 |
}{ |
| 43 |
{ |
| 44 |
name: "parse error", |
| 45 |
ctx: context.Background(), |
| 46 |
newToken: func() (string, *oidc.JWTTokenRequest) { return "!", nil }, |
| 47 |
wantErr: oidc.ErrParse, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "wrong audience", |
| 51 |
ctx: context.Background(), |
| 52 |
newToken: func() (string, *oidc.JWTTokenRequest) { |
| 53 |
return tu.NewJWTProfileAssertion( |
| 54 |
tu.ValidClientID, tu.ValidClientID, []string{"wrong"}, |
| 55 |
time.Now(), tu.ValidExpiration, |
| 56 |
) |
| 57 |
}, |
| 58 |
wantErr: oidc.ErrAudience, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "expired", |
| 62 |
ctx: context.Background(), |
| 63 |
newToken: func() (string, *oidc.JWTTokenRequest) { |
| 64 |
return tu.NewJWTProfileAssertion( |
| 65 |
tu.ValidClientID, tu.ValidClientID, []string{tu.ValidIssuer}, |
| 66 |
time.Now(), time.Now().Add(-time.Hour), |
| 67 |
) |
| 68 |
}, |
| 69 |
wantErr: oidc.ErrExpired, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "invalid iat", |
| 73 |
ctx: context.Background(), |
| 74 |
newToken: func() (string, *oidc.JWTTokenRequest) { |
| 75 |
return tu.NewJWTProfileAssertion( |
| 76 |
tu.ValidClientID, tu.ValidClientID, []string{tu.ValidIssuer}, |
| 77 |
time.Now().Add(time.Hour), tu.ValidExpiration, |
| 78 |
) |
| 79 |
}, |
| 80 |
wantErr: oidc.ErrIatInFuture, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "invalid subject", |
| 84 |
ctx: context.Background(), |
| 85 |
newToken: func() (string, *oidc.JWTTokenRequest) { |
| 86 |
return tu.NewJWTProfileAssertion( |
| 87 |
tu.ValidClientID, "wrong", []string{tu.ValidIssuer}, |
| 88 |
time.Now(), tu.ValidExpiration, |
| 89 |
) |
| 90 |
}, |
| 91 |
wantErr: oidc.ErrSubjectInvalid, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "check signature fail", |
| 95 |
ctx: errCtx, |
| 96 |
newToken: tu.ValidJWTProfileAssertion, |
| 97 |
wantErr: context.Canceled, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "ok", |
| 101 |
ctx: context.Background(), |
| 102 |
newToken: tu.ValidJWTProfileAssertion, |
| 103 |
}, |
| 104 |
} |
| 105 |
for _, tt := range tests { |
| 106 |
t.Run(tt.name, func(t *testing.T) { |
| 107 |
assertion, want := tt.newToken() |
| 108 |
got, err := op.VerifyJWTAssertion(tt.ctx, assertion, verifier) |
| 109 |
if tt.wantErr != nil { |
| 110 |
assert.ErrorIs(t, err, tt.wantErr) |
| 111 |
return |
| 112 |
} |
| 113 |
require.NoError(t, err) |
| 114 |
assert.Equal(t, want, got) |
| 115 |
}) |
| 116 |
} |
| 117 |
} |