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.
160 lines
5.0 KiB
Go
160 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"cdr.dev/slog/v3/sloggers/sloghuman"
|
|
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// Flags
|
|
var (
|
|
expiry = flag.Duration("expiry", time.Minute*5, "Token expiry")
|
|
clientID = flag.String("client-id", "static-client-id", "Client ID, set empty to be random")
|
|
clientSecret = flag.String("client-sec", "static-client-secret", "Client Secret, set empty to be random")
|
|
deviceFlow = flag.Bool("device-flow", false, "Enable device flow")
|
|
// By default, no regex means it will never match anything. So at least default to matching something.
|
|
extRegex = flag.String("ext-regex", `^(https?://)?example\.com(/.*)?$`, "External auth regex")
|
|
tooManyRequests = flag.String("429", "", "Simulate too many requests for a given endpoint.")
|
|
)
|
|
|
|
func main() {
|
|
testing.Init()
|
|
_ = flag.Set("test.timeout", "0")
|
|
|
|
flag.Parse()
|
|
|
|
// This is just a way to run tests outside go test
|
|
testing.Main(func(_, _ string) (bool, error) {
|
|
return true, nil
|
|
}, []testing.InternalTest{
|
|
{
|
|
Name: "Run Fake IDP",
|
|
F: RunIDP(),
|
|
},
|
|
}, nil, nil)
|
|
}
|
|
|
|
type withClientSecret struct {
|
|
// We never unmarshal this in prod, but we need this field for testing.
|
|
ClientSecret string `json:"client_secret"`
|
|
codersdk.ExternalAuthConfig
|
|
}
|
|
|
|
// RunIDP needs the testing.T because our oidctest package requires the
|
|
// testing.T.
|
|
func RunIDP() func(t *testing.T) {
|
|
tooManyRequestParams := oidctest.With429Arguments{}
|
|
if *tooManyRequests != "" {
|
|
for _, v := range strings.Split(*tooManyRequests, ",") {
|
|
v = strings.ToLower(strings.TrimSpace(v))
|
|
switch v {
|
|
case "all":
|
|
tooManyRequestParams.AllPaths = true
|
|
case "auth":
|
|
tooManyRequestParams.AuthorizePath = true
|
|
case "token":
|
|
tooManyRequestParams.TokenPath = true
|
|
case "keys":
|
|
tooManyRequestParams.KeysPath = true
|
|
case "userinfo":
|
|
tooManyRequestParams.UserInfoPath = true
|
|
case "device":
|
|
tooManyRequestParams.DeviceAuth = true
|
|
case "device-verify":
|
|
tooManyRequestParams.DeviceVerify = true
|
|
default:
|
|
log.Printf("Unknown too-many-requests value: %s\nView the `testidp/main.go` for valid values.", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
return func(t *testing.T) {
|
|
idp := oidctest.NewFakeIDP(t,
|
|
oidctest.WithServing(),
|
|
oidctest.WithStaticUserInfo(map[string]any{
|
|
// This is a static set of auth fields. Might be beneficial to make flags
|
|
// to allow different values here. This is only required for using the
|
|
// testIDP as primary auth. External auth does not ever fetch these fields.
|
|
"sub": uuid.MustParse("26c6a19c-b9b8-493b-a991-88a4c3310314"),
|
|
"email": "oidc_member@coder.com",
|
|
"preferred_username": "oidc_member",
|
|
"email_verified": true,
|
|
"groups": []string{"testidp", "qa", "engineering"},
|
|
"roles": []string{"testidp", "admin", "higher_power"},
|
|
}),
|
|
oidctest.WithDefaultIDClaims(map[string]any{
|
|
"sub": uuid.MustParse("26c6a19c-b9b8-493b-a991-88a4c3310314"),
|
|
}),
|
|
oidctest.WithDefaultExpire(*expiry),
|
|
oidctest.WithStaticCredentials(*clientID, *clientSecret),
|
|
oidctest.WithIssuer("http://localhost:4500"),
|
|
oidctest.WithLogger(slog.Make(sloghuman.Sink(os.Stderr))),
|
|
oidctest.With429(tooManyRequestParams),
|
|
)
|
|
id, sec := idp.AppCredentials()
|
|
prov := idp.WellknownConfig()
|
|
const appID = "fake"
|
|
coderCfg := idp.ExternalAuthConfig(t, appID, &oidctest.ExternalAuthConfigOptions{
|
|
UseDeviceAuth: *deviceFlow,
|
|
})
|
|
|
|
log.Println("IDP Issuer URL", idp.IssuerURL())
|
|
log.Println("Coderd Flags")
|
|
|
|
deviceCodeURL := ""
|
|
if coderCfg.DeviceAuth != nil {
|
|
deviceCodeURL = coderCfg.DeviceAuth.CodeURL
|
|
}
|
|
|
|
cfg := withClientSecret{
|
|
ClientSecret: sec,
|
|
ExternalAuthConfig: codersdk.ExternalAuthConfig{
|
|
Type: appID,
|
|
ClientID: id,
|
|
ClientSecret: sec,
|
|
ID: appID,
|
|
AuthURL: prov.AuthURL,
|
|
TokenURL: prov.TokenURL,
|
|
ValidateURL: prov.ExternalAuthURL,
|
|
AppInstallURL: coderCfg.AppInstallURL,
|
|
AppInstallationsURL: coderCfg.AppInstallationsURL,
|
|
NoRefresh: false,
|
|
Scopes: []string{"openid", "email", "profile"},
|
|
ExtraTokenKeys: coderCfg.ExtraTokenKeys,
|
|
DeviceFlow: *deviceFlow,
|
|
DeviceCodeURL: deviceCodeURL,
|
|
Regex: *extRegex,
|
|
DisplayName: coderCfg.DisplayName,
|
|
DisplayIcon: coderCfg.DisplayIcon,
|
|
},
|
|
}
|
|
|
|
data, err := json.Marshal([]withClientSecret{cfg})
|
|
require.NoError(t, err)
|
|
log.Printf(`--external-auth-providers='%s'`, string(data))
|
|
log.Println("As primary OIDC auth")
|
|
log.Printf(`--oidc-issuer-url=%s --oidc-client-id=%s --oidc-client-secret=%s`, idp.IssuerURL().String(), *clientID, *clientSecret)
|
|
|
|
log.Println("Press Ctrl+C to exit")
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
// Block until ctl+c
|
|
<-c
|
|
log.Println("Closing")
|
|
}
|
|
}
|