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.
This commit is contained in:
1 parent
6993824dd1
commit
688a0f9b3e
33 files changed
+383
-381
No files matched your search
@@ -34,7 +34,8 @@ import (
|
||||
|
||||
"cloud.google.com/go/compute/metadata"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/go-jose/go-jose/v4"
|
||||
"github.com/go-jose/go-jose/v4/jwt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -1623,7 +1624,7 @@ func RequestExternalAuthCallback(t testing.TB, providerID string, client *coders
|
||||
func NewGoogleInstanceIdentity(t testing.TB, instanceID string, expired bool, audience string) (*idtoken.Validator, *metadata.Client) {
|
||||
keyID, err := cryptorand.String(12)
|
||||
require.NoError(t, err)
|
||||
claims := jwt.MapClaims{
|
||||
claims := map[string]any{
|
||||
"aud": audience,
|
||||
"google": map[string]interface{}{
|
||||
"compute_engine": map[string]string{
|
||||
@@ -1634,11 +1635,14 @@ func NewGoogleInstanceIdentity(t testing.TB, instanceID string, expired bool, au
|
||||
if !expired {
|
||||
claims["exp"] = time.Now().AddDate(1, 0, 0).Unix()
|
||||
}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
||||
token.Header["kid"] = keyID
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
require.NoError(t, err)
|
||||
signedKey, err := token.SignedString(privateKey)
|
||||
signer, err := jose.NewSigner(jose.SigningKey{
|
||||
Algorithm: jose.RS256,
|
||||
Key: privateKey,
|
||||
}, (&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", keyID))
|
||||
require.NoError(t, err)
|
||||
signedKey, err := jwt.Signed(signer).Claims(claims).Serialize()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Taken from: https://github.com/googleapis/google-api-go-client/blob/4bb729045d611fa77bdbeb971f6a1204ba23161d/idtoken/validate.go#L57-L75
|
||||
|
||||
Reference in new issue
Block a user