Files
coder/enterprise/coderd/enidpsync/groups_test.go
T
Dean Sheather 688a0f9b3e refactor: use go-jose for all JWT handling (#31931)
Closes ENG-1510.

First-party code used two JWT libraries: `go-jose/v4` (workspace apps,
jwtutils, tailnet resume, agent hooks, chats) and `golang-jwt/jwt/v4`
(licenses, Azure OIDC PKI, test signers, and `jwt.MapClaims` in
idpsync). This moves everything to go-jose and removes
`golang-jwt/jwt/v4` from `go.mod`.

- `enterprise/coderd/license`: verification uses `jwt.ParseSigned` with
an EdDSA allowlist and `kid` lookup. Time claims are validated with zero
leeway, matching golang-jwt's previous behavior instead of go-jose's 1
minute default. `ParseClaimsIgnoreNbf` still returns `ErrMultipleIssues`
for any verification or time-claim failure other than `nbf`. `Claims`
now embeds `jwtutils.RegisteredClaims`, so `ExpiresAt` becomes `Expiry`
and `NumericDate` uses `.Time()`.
- `coderd/oauthpki`: the Azure client assertion is signed with go-jose
and keeps the `typ: JWT` and `x5t` headers that the existing test
asserts.
- Test helpers (`coderdtest`, `oidctest`, `coderdenttest`) sign with
go-jose.
- `jwt.MapClaims` is replaced with `map[string]any`. No callers used its
methods.
- A depguard rule (`single_jwt_library`) rejects new
`github.com/golang-jwt/jwt` imports.

`golang-jwt/jwt/v5` stays as an indirect dependency through
`SherClockHolmes/webpush-go`.

<details>
<summary>Decision log</summary>

- **Why go-jose over golang-jwt:** `coderd/jwtutils/jwe.go` needs JWE,
which golang-jwt doesn't support, so only go-jose can be the single
library. go-jose was already used for the more security-sensitive token
paths, and `coreos/go-oidc` depends on it anyway. Its `ParseSigned`
requires an explicit algorithm allowlist. Moving the golang-jwt usages,
which were mostly `MapClaims`, was the smaller change.
- **Expiry edge case:** golang-jwt treated a license as expired at `now
>= exp`, and go-jose treats it as expired at `now > exp`. With
whole-second timestamps the difference is at most one second.
- **Not evaluated:** a comparison of CVE history or maintenance pace
between the two libraries.

</details>

Validation: `make pre-commit`. Tests: `enterprise/coderd/license`,
`enterprise/coderd` (license, trial, OIDC, and sync tests),
`enterprise/trialer`, `enterprise/coderd/usage`,
`enterprise/coderd/enidpsync`, `coderd/oauthpki`, `coderd/idpsync`,
`coderd/coderdtest/oidctest`, `coderd` (OIDC, login, Google instance
identity, and OAuth2 tests), `coderd/externalauth`,
`coderd/provisionerdserver`, `cli`. I confirmed the depguard rule flags
a `golang-jwt/jwt/v5` import.

> Generated with Coder Agents on behalf of @deansheather.
2026-11-30 11:23:42 +01:00

96 lines
2.3 KiB
Go

package enidpsync_test
import (
"testing"
"github.com/stretchr/testify/require"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/entitlements"
"github.com/coder/coder/v2/coderd/idpsync"
"github.com/coder/coder/v2/coderd/runtimeconfig"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/enidpsync"
"github.com/coder/coder/v2/testutil"
)
func TestEnterpriseParseGroupClaims(t *testing.T) {
t.Parallel()
entitled := entitlements.New()
entitled.Modify(func(entitlements *codersdk.Entitlements) {
entitlements.Features[codersdk.FeatureTemplateRBAC] = codersdk.Feature{
Entitlement: codersdk.EntitlementEntitled,
Enabled: true,
}
})
t.Run("NoEntitlements", func(t *testing.T) {
t.Parallel()
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
runtimeconfig.NewManager(),
entitlements.New(),
idpsync.DeploymentSyncSettings{})
ctx := testutil.Context(t, testutil.WaitMedium)
params, err := s.ParseGroupClaims(ctx, map[string]any{})
require.Nil(t, err)
require.False(t, params.SyncEntitled)
})
t.Run("NotInAllowList", func(t *testing.T) {
t.Parallel()
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
runtimeconfig.NewManager(),
entitled,
idpsync.DeploymentSyncSettings{
GroupField: "groups",
GroupAllowList: map[string]struct{}{
"foo": {},
},
})
ctx := testutil.Context(t, testutil.WaitMedium)
// Try with incorrect group
_, err := s.ParseGroupClaims(ctx, map[string]any{
"groups": []string{"bar"},
})
require.NotNil(t, err)
require.Equal(t, 403, err.Code)
// Try with no groups
_, err = s.ParseGroupClaims(ctx, map[string]any{})
require.NotNil(t, err)
require.Equal(t, 403, err.Code)
})
t.Run("InAllowList", func(t *testing.T) {
t.Parallel()
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
runtimeconfig.NewManager(),
entitled,
idpsync.DeploymentSyncSettings{
GroupField: "groups",
GroupAllowList: map[string]struct{}{
"foo": {},
},
})
ctx := testutil.Context(t, testutil.WaitMedium)
claims := map[string]any{
"groups": []string{"foo", "bar"},
}
params, err := s.ParseGroupClaims(ctx, claims)
require.Nil(t, err)
require.True(t, params.SyncEntitled)
require.Equal(t, claims, params.MergedClaims)
})
}