Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ curl -s -X POST localhost:8444/token -d grant_type=oidc -d id_token=<IdP ID toke
```

The service checks the ID token's signature (via the issuer's JWKS), issuer,
expiry, and — if `-oidc-audience` is set — audience, then maps the caller's
`groups` claim through `groups.txt`.
expiry and audience (`-oidc-audience`, required with `-oidc-issuer`: the client
ID of priompt's app at the IdP), then maps the caller's `groups` claim through
`groups.txt`.

## Service accounts

Expand Down
8 changes: 7 additions & 1 deletion cmd/priompt-auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func serveCmd(args []string) {
clientsFile := fs.String("clients-file", "", "service accounts: `client_id sha256:<hex> [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)

Expand All @@ -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 {
Expand Down
35 changes: 34 additions & 1 deletion cmd/priompt-auth/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions cmd/priompt-auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down Expand Up @@ -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"`
}
Expand All @@ -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
}

Expand Down
Loading