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.
161 lines
4.6 KiB
Go
161 lines
4.6 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
"github.com/coder/coder/v2/coderd"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestUserOIDCClaims(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
newOIDCTest := func(t *testing.T) (*oidctest.FakeIDP, *codersdk.Client) {
|
|
t.Helper()
|
|
|
|
fake := oidctest.NewFakeIDP(t,
|
|
oidctest.WithServing(),
|
|
)
|
|
cfg := fake.OIDCConfig(t, nil, func(cfg *coderd.OIDCConfig) {
|
|
cfg.AllowSignups = true
|
|
})
|
|
ownerClient := coderdtest.New(t, &coderdtest.Options{
|
|
OIDCConfig: cfg,
|
|
})
|
|
return fake, ownerClient
|
|
}
|
|
|
|
t.Run("OwnClaims", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fake, ownerClient := newOIDCTest(t)
|
|
claims := map[string]any{
|
|
"email": "alice@coder.com",
|
|
"email_verified": true,
|
|
"sub": uuid.NewString(),
|
|
"groups": []string{"admin", "eng"},
|
|
}
|
|
userClient, loginResp := fake.Login(t, ownerClient, claims)
|
|
defer loginResp.Body.Close()
|
|
|
|
inv, root := clitest.New(t, "users", "oidc-claims", "-o", "json")
|
|
clitest.SetupConfig(t, userClient, root)
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
inv.Stdout = buf
|
|
err := inv.WithContext(testutil.Context(t, testutil.WaitMedium)).Run()
|
|
require.NoError(t, err)
|
|
|
|
var resp codersdk.OIDCClaimsResponse
|
|
err = json.Unmarshal(buf.Bytes(), &resp)
|
|
require.NoError(t, err, "unmarshal JSON output")
|
|
require.NotEmpty(t, resp.Claims, "claims should not be empty")
|
|
assert.Equal(t, "alice@coder.com", resp.Claims["email"])
|
|
})
|
|
|
|
t.Run("Table", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fake, ownerClient := newOIDCTest(t)
|
|
claims := map[string]any{
|
|
"email": "bob@coder.com",
|
|
"email_verified": true,
|
|
"sub": uuid.NewString(),
|
|
}
|
|
userClient, loginResp := fake.Login(t, ownerClient, claims)
|
|
defer loginResp.Body.Close()
|
|
|
|
inv, root := clitest.New(t, "users", "oidc-claims")
|
|
clitest.SetupConfig(t, userClient, root)
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
inv.Stdout = buf
|
|
err := inv.WithContext(testutil.Context(t, testutil.WaitMedium)).Run()
|
|
require.NoError(t, err)
|
|
|
|
output := buf.String()
|
|
require.Contains(t, output, "email")
|
|
require.Contains(t, output, "bob@coder.com")
|
|
})
|
|
|
|
t.Run("NotOIDCUser", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
client := coderdtest.New(t, nil)
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
inv, root := clitest.New(t, "users", "oidc-claims")
|
|
clitest.SetupConfig(t, client, root)
|
|
|
|
err := inv.WithContext(testutil.Context(t, testutil.WaitMedium)).Run()
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "not an OIDC user")
|
|
})
|
|
|
|
// Verify that two different OIDC users each only see their own
|
|
// claims. The endpoint has no user parameter, so there is no way
|
|
// to request another user's claims by design.
|
|
t.Run("OnlyOwnClaims", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
aliceFake, aliceOwnerClient := newOIDCTest(t)
|
|
aliceClaims := map[string]any{
|
|
"email": "alice-isolation@coder.com",
|
|
"email_verified": true,
|
|
"sub": uuid.NewString(),
|
|
}
|
|
aliceClient, aliceLoginResp := aliceFake.Login(t, aliceOwnerClient, aliceClaims)
|
|
defer aliceLoginResp.Body.Close()
|
|
|
|
bobFake, bobOwnerClient := newOIDCTest(t)
|
|
bobClaims := map[string]any{
|
|
"email": "bob-isolation@coder.com",
|
|
"email_verified": true,
|
|
"sub": uuid.NewString(),
|
|
}
|
|
bobClient, bobLoginResp := bobFake.Login(t, bobOwnerClient, bobClaims)
|
|
defer bobLoginResp.Body.Close()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
|
|
|
// Alice sees her own claims.
|
|
aliceResp, err := aliceClient.UserOIDCClaims(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "alice-isolation@coder.com", aliceResp.Claims["email"])
|
|
|
|
// Bob sees his own claims.
|
|
bobResp, err := bobClient.UserOIDCClaims(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "bob-isolation@coder.com", bobResp.Claims["email"])
|
|
})
|
|
|
|
t.Run("ClaimsNeverNull", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
fake, ownerClient := newOIDCTest(t)
|
|
// Use minimal claims — just enough for OIDC login.
|
|
claims := map[string]any{
|
|
"email": "minimal@coder.com",
|
|
"email_verified": true,
|
|
"sub": uuid.NewString(),
|
|
}
|
|
userClient, loginResp := fake.Login(t, ownerClient, claims)
|
|
defer loginResp.Body.Close()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
|
resp, err := userClient.UserOIDCClaims(ctx)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp.Claims, "claims should never be nil, expected empty map")
|
|
})
|
|
}
|