Skip to content

skymarshal: behaviour: handle RFC 8693 token-exchange responses - #9638

Merged
taylorsilva merged 1 commit into
concourse:masterfrom
ibaileyStingray:fix-token-exchange-id-token
Jul 14, 2026
Merged

taylorsilva merged 1 commit into
concourse:masterfrom
ibaileyStingray:fix-token-exchange-id-token

Conversation

@ibaileyStingray

@ibaileyStingray ibaileyStingray commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Changes proposed by this PR

closes #9636

Make sure to follow all PR requirements.

  • Dex's token-exchange grant returns the issued token in access_token and, per RFC 8693 section 2.2.1, has no id_token member. StoreAccessToken assumed every 2xx from /sky/issuer/token carried an id_token, so it parsed an empty string, failed, and converted Dex's successful response into an empty 500 - making token exchange against the embedded Dex unusable. Fall back to parsing claims from access_token so exchanged tokens are converted to stored Concourse access tokens like every other grant.

Notes to reviewer

  • Why rewrite rather than pass through? Simply skipping the middleware for
    the exchange grant would hand the client a raw Dex JWT, which the ATC no
    longer accepts (Concourse >= 7 validates opaque DB-backed tokens). Rewriting
    keeps exchange clients on the same token model as fly login, including the
    user row creation (CreateOrUpdateUser) and the short-header benefit that
    motivated opaque tokens in the first place.
  • Why not gate on grant_type from the request? The response-side check
    (id_token empty) is more robust — the request body has already been
    consumed by the Dex handler, and any 2xx response lacking id_token
    previously produced a guaranteed 500, so the fallback is strictly an
    improvement.
  • The two struct additions are cosmetic but keep the rewritten response
    honest: omitempty stops an empty "id_token": "" appearing in exchange
    responses, and issued_token_type is preserved instead of silently dropped
    on re-marshal.
  • A test belongs in skymarshal/token/access_token_test.go: feed the
    middleware a fake Dex 200 with access_token = signed JWT and no
    id_token, assert a 200 with an opaque token and stored claims.

Dex's token-exchange grant returns the issued token in access_token and,
per RFC 8693 section 2.2.1, has no id_token member. StoreAccessToken
assumed every 2xx from /sky/issuer/token carried an id_token, so it
parsed an empty string, failed, and converted Dex's successful response
into an empty 500 - making token exchange against the embedded Dex
unusable. Fall back to parsing claims from access_token so exchanged
tokens are converted to stored Concourse access tokens like every
other grant.

concourse#9636

Signed-off-by: ibaileyStingray <[email protected]>
@ibaileyStingray
ibaileyStingray requested a review from a team July 9, 2026 17:19
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: ibaileyStingray / name: ibaileyStingray (9eb8666)

@taylorsilva

Copy link
Copy Markdown
Member

Thanks for opening an issue and opening a PR to fix it. I'll spend some time validating the issue and your proposed fix. I like to be extra cautious around any auth changes. The proposed change does seem good and I appreciate you adding tests for it as well!

When you get a chance can you please do the EasyCLA sign-off by clicking the link in the previous comment. TY!

@taylorsilva taylorsilva moved this from Todo to In Progress in Pull Requests Jul 9, 2026
@ibaileyStingray

Copy link
Copy Markdown
Contributor Author

Thanks you for you feedback. Being an corporate contributor, the easyCLA should take a few days. But I am actively working for it to reach the good person desk :). Meanwhile, I'll stay available for any question or suggestion from your side.
thanks again.

@ibaileyStingray

Copy link
Copy Markdown
Contributor Author

easyCLA step approved. thanks for your patience

@taylorsilva

Copy link
Copy Markdown
Member

I'm going to need your help reproducing the original issue and verifying that this PR fixes it. I tried puzzling it out on my own (and with Claude) but couldn't get this working.

In the issue you opened, you mention getting an ${UPSTREAM_TOKEN}. I tried doing this by logging into a local Concourse and intercepting the http://web:8080/sky/issuer/callback?code=SOME_CODE&state=xyz URL from the OIDC provider and pasting it into the provided curl command:

curl -i http://localhost:8080/sky/issuer/token \
-u 'fly:Zmx5' \
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
--data-urlencode 'connector_id=oidc' \
--data-urlencode 'subject_token_type=urn:ietf:params:oauth:token-type:id_token' \
--data-urlencode "subject_token=SOME_CODE" \
--data-urlencode 'requested_token_type=urn:ietf:params:oauth:token-type:access_token' \
--data-urlencode 'scope=openid profile email federated:id groups'

But I got back HTTP 401 and {"error":"access_denied"}. The web logs also showed a slightly different but similar error to the one in the issue:

{"timestamp":"2026-07-13T20:09:26.215517544Z","level":"error","source":"atc","message":"atc.dex.failed to verify subject token",
  "data":{"err":"oidc: failed to verify token: oidc: malformed jwt: go-jose/go-jose: compact JWS format must have three parts","session":"17"}}

Not sure what you're using for your local OIDC. The one we used to use in the ./hack/overrides/oidc.yml is outdated and doesn't work for me at the moment. I tried another one I found. General auth with Concourse works, but can't get this to work.

More precise steps to reproduce would be helpful.

@taylorsilva

Copy link
Copy Markdown
Member

I pushed the local oidc setup I had to master. You can run it with:

docker compose -f docker-compose.yml -f ./hack/overrides/oidc.yml up -d

@taylorsilva taylorsilva moved this from In Progress to Waiting on Contributor in Pull Requests Jul 13, 2026
@ibaileyStingray

Copy link
Copy Markdown
Contributor Author

Thanks for pushing the local OIDC setup — I reproduced the issue and verified the fix with it. Two things explain what you saw:

  1. The subject_token must be a JWT from the upstream IdP, not the callback code. The code=SOME_CODE value from the callback URL is an opaque authorization code. Dex's OIDC connector tries to verify the subject token as a JWT, fails (compact JWS format must have three parts — your error came from atc.dex, i.e. inside Dex), and returns 401 access_denied before the bug in the Concourse middleware is ever reached. The bug only triggers when Dex succeeds: the middleware then discards Dex's successful response and returns an empty 500.

  2. One env var is needed for the ID-token flow. With getUserInfo enabled (the default), Dex also sends the subject token to the IdP's UserInfo endpoint, which rejects an ID token with Invalid token use. This matches our production setup (Entra with CONCOURSE_OIDC_DISABLE_GET_USER_INFO=true, as described in RFC 8693 token exchange against embedded Dex always returns an empty 500 — StoreAccessToken middleware assumes every token response contains id_token #9636). Add under web.environment in hack/overrides/oidc.yml:

CONCOURSE_OIDC_DISABLE_GET_USER_INFO: "true"

Repro steps (from a checkout of master, then again with this PR applied):

docker compose -f docker-compose.yml -f ./hack/overrides/oidc.yml up -d --build

Mint an upstream ID token headlessly via local-idp's login API (no browser needed):

CHALLENGE_ID=$(curl -s http://localhost:9000/login/init \
  -H 'Content-Type: application/json' \
  -d '{"username":"user1","password":"password1","client_id":"client1","scopes":"openid profile email"}' \
  | python3 -c 'import json,sys; print(json.load(sys.stdin)["challenge_id"])')
UPSTREAM_TOKEN=$(curl -s http://localhost:9000/login/complete \
  -H 'Content-Type: application/json' \
  -d "{\"challenge_id\":\"${CHALLENGE_ID}\",\"challenge_data\":\"x\"}" \
  | python3 -c 'import json,sys; print(json.load(sys.stdin)["identity_token"])')

Then run the exchange from the issue:

curl -i http://localhost:8080/sky/issuer/token \
  -u 'fly:Zmx5' \
  --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
  --data-urlencode 'connector_id=oidc' \
  --data-urlencode 'subject_token_type=urn:ietf:params:oauth:token-type:id_token' \
  --data-urlencode "subject_token=${UPSTREAM_TOKEN}" \
  --data-urlencode 'requested_token_type=urn:ietf:params:oauth:token-type:access_token' \
  --data-urlencode 'scope=openid profile email federated:id groups'

On current master: HTTP 500 with an empty body, and the web log shows the failure in the Concourse middleware (not Dex):

atc.dex-server.token-request.parse-id-token:
go-jose/go-jose: compact JWS format must have three parts

With this PR applied: HTTP 200 with the RFC 8693 response intact and an opaque Concourse token:

{
  "access_token": "<opaque>",
  "token_type": "bearer",
  "expires_in": 86399,
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token"
}

and the returned token authenticates successfully against GET /api/v1/user.

@taylorsilva taylorsilva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY for the detailed steps. Was able to reproduce it locally and verify the output is what you said it would be. Code changes and tests look good to me as well. Thanks again for the PR!

@taylorsilva
taylorsilva merged commit 583450f into concourse:master Jul 14, 2026
11 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting on Contributor to Done in Pull Requests Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

RFC 8693 token exchange against embedded Dex always returns an empty 500 — StoreAccessToken middleware assumes every token response contains id_token

2 participants