diff --git a/README.md b/README.md index 6690b63..c576927 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,9 @@ curl -s -X POST localhost:8444/token -d grant_type=oidc -d id_token= [org] [rw]` lines (# comments ok)") groupsFile := fs.String("groups-file", "", "IdP group mapping: `group [org] [rw]` lines; no org = admin; first match wins") oidcIssuer := fs.String("oidc-issuer", "", "OIDC issuer URL of the corporate IdP; enables grant_type=oidc") - oidcAudience := fs.String("oidc-audience", "", "required aud claim on IdP tokens (empty = not checked)") + oidcAudience := fs.String("oidc-audience", "", "required aud claim on IdP tokens: the client ID of priompt's app at the IdP (required with -oidc-issuer)") ttl := fs.Duration("ttl", 15*time.Minute, "lifetime of issued tokens") fs.Parse(args) @@ -148,6 +148,12 @@ func serveCmd(args []string) { if *groupsFile == "" { log.Fatal("-oidc-issuer requires -groups-file (or every login would be denied)") } + // One IdP issuer signs ID tokens for every app in the tenant. Without an + // audience check, a token issued to any of them would be exchangeable here. + if *oidcAudience == "" { + log.Fatal("-oidc-issuer requires -oidc-audience (the client ID of priompt's app at the IdP), " + + "or ID tokens issued to other apps in the tenant would be accepted") + } s.groups = loadGroups(*groupsFile) idp, err := newIDP(*oidcIssuer, *oidcAudience) if err != nil { diff --git a/cmd/priompt-auth/main_test.go b/cmd/priompt-auth/main_test.go index 419f90b..27d6cbe 100644 --- a/cmd/priompt-auth/main_test.go +++ b/cmd/priompt-auth/main_test.go @@ -139,9 +139,14 @@ func newFakeIdP(t *testing.T) *fakeIdP { } func (f *fakeIdP) idToken(t *testing.T, iss, sub string, groups []string, exp time.Time) string { + t.Helper() + return f.idTokenClaims(t, map[string]any{"iss": iss, "sub": sub, "aud": "priompt", "exp": exp.Unix(), "groups": groups}) +} + +func (f *fakeIdP) idTokenClaims(t *testing.T, claims map[string]any) string { t.Helper() h := b64(map[string]string{"alg": "RS256", "typ": "JWT", "kid": "idp1"}) - p := b64(map[string]any{"iss": iss, "sub": sub, "aud": "priompt", "exp": exp.Unix(), "groups": groups}) + p := b64(claims) sum := sha256.Sum256([]byte(h + "." + p)) sig, err := rsa.SignPKCS1v15(rand.Reader, f.priv, crypto.SHA256, sum[:]) if err != nil { @@ -193,6 +198,34 @@ func TestOIDCExchange(t *testing.T) { "grant_type": {"oidc"}, "id_token": {f.idToken(t, f.srv.URL, "x", []string{"acme-authors"}, time.Now().Add(-time.Minute))}}); code != 401 { t.Errorf("expired ID token must 401, got %d", code) } + + // A token the same IdP issued to another app in the tenant must not exchange. + other := func(claims map[string]any) int { + base := map[string]any{"iss": f.srv.URL, "sub": "x", "exp": exp.Unix(), "groups": []string{"prompt-admins"}} + for k, v := range claims { + base[k] = v + } + code, _ := getToken(t, srv, url.Values{"grant_type": {"oidc"}, "id_token": {f.idTokenClaims(t, base)}}) + return code + } + if code := other(map[string]any{"aud": "some-other-app"}); code != 401 { + t.Errorf("ID token for another audience must 401, got %d", code) + } + if code := other(map[string]any{"aud": []string{"priompt", "some-other-app"}, "azp": "some-other-app"}); code != 401 { + t.Errorf("ID token issued to another party (azp) must 401, got %d", code) + } + if code := other(map[string]any{}); code != 401 { + t.Errorf("ID token without aud must 401, got %d", code) + } +} + +// Without an audience every ID token from the tenant would be accepted, so +// the IdP cannot be configured without one. +func TestOIDCRequiresAudience(t *testing.T) { + f := newFakeIdP(t) + if _, err := newIDP(f.srv.URL, ""); err == nil { + t.Fatal("newIDP with an empty audience must fail") + } } // Trailing # comments in clients.txt / groups.txt are not fields: the README's diff --git a/cmd/priompt-auth/oidc.go b/cmd/priompt-auth/oidc.go index 17fc17f..95546b3 100644 --- a/cmd/priompt-auth/oidc.go +++ b/cmd/priompt-auth/oidc.go @@ -22,7 +22,7 @@ import ( type idp struct { issuer string - audience string // "" = don't check aud + audience string // required aud (and azp, when present) jwksURI string client *http.Client @@ -34,6 +34,9 @@ type idp struct { // newIDP discovers the issuer's JWKS endpoint via OIDC discovery. Fails fast: // a misconfigured issuer should stop `serve`, not surface per-request. func newIDP(issuer, audience string) (*idp, error) { + if audience == "" { + return nil, errors.New("an OIDC audience is required: ID tokens for other apps in the tenant would be accepted") + } i := &idp{issuer: issuer, audience: audience, client: &http.Client{Timeout: 5 * time.Second}, keys: map[string]*rsa.PublicKey{}} resp, err := i.client.Get(strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration") @@ -140,6 +143,7 @@ func (i *idp) verify(raw string) (sub string, groups []string, err error) { Sub string `json:"sub"` Iss string `json:"iss"` Aud json.RawMessage `json:"aud"` // string or []string per spec + Azp string `json:"azp"` Exp int64 `json:"exp"` Groups []string `json:"groups"` } @@ -152,9 +156,13 @@ func (i *idp) verify(raw string) (sub string, groups []string, err error) { if c.Exp == 0 || time.Now().Unix() > c.Exp { return "", nil, errors.New("ID token expired") } - if i.audience != "" && !audContains(c.Aud, i.audience) { + if !audContains(c.Aud, i.audience) { return "", nil, errors.New("audience mismatch") } + // With several audiences, azp names the party the token was issued to. + if c.Azp != "" && c.Azp != i.audience { + return "", nil, errors.New("authorized party mismatch") + } return c.Sub, c.Groups, nil }