smolnar82 opened a new pull request, #1557:
URL: https://github.com/apache/knox/pull/1557

   [KNOX-3626](https://issues.apache.org/jira/browse/KNOX-3626) - Let public 
PKCE clients refresh without a client_secret
   
   ## What changes were proposed in this pull request?
   
   The KnoxIDF discovery document advertises `none` in 
`token_endpoint_auth_methods_supported`, and a public client can redeem an 
authorization code with PKCE and no `client_secret`. The `refresh_token` grant, 
however, always required a `client_secret`, so a public client (e.g. an MCP 
connector) got a refresh token it could never use and went invalid when the 
access token expired, about an hour after connecting.
   
   The token endpoint now records how the client authenticated when it started 
the token chain, and stores it server-side as `client_auth_method` metadata on 
the issued refresh token:
   - `none` when the code was redeemed with PKCE and no `client_secret`.
   - `client_secret_post` otherwise (including PKCE plus a `client_secret`).
   - Every rotation carries the presented token's value forward to the new 
refresh token.
   
   On the `refresh_token` grant:
   - The `client_secret` may be omitted only when the stored value is `none`. 
The value comes from the stored metadata, never from the request, so a 
confidential chain cannot be downgraded.
   - A `client_secret` that is presented is always verified, also on a public 
chain.
   - A refresh token without the metadata (issued before this change) still 
requires the `client_secret`. Public clients holding such a token have to 
re-authorize once after the upgrade.
   - `client_id` binding and single-use rotation are unchanged.
   
   Docs updated: `knoxidf/security.md` and `knoxidf/endpoints.md`.
   
   ## How was this patch tested?
   
   ### Unit tests
   
   `mvn -o -pl gateway-service-knoxidf verify -DskipITs` is green (354 tests, 
checkstyle, PMD, SpotBugs).
   - `TokenResourceRefreshTokenClientAuthTest`: a confidential chain without a 
secret is rejected; a public chain refreshes without a secret, rejects a wrong 
secret and still requires the matching `client_id`; PKCE without a secret 
starts a public chain, PKCE with a secret and plain secret redemption start a 
confidential chain; the refresh grant carries the method forward; a legacy 
token records none.
   - `TokenResourceRefreshTokenRotationTest`: rotation of a public chain 
carries `none` forward to the new refresh token.
   
   I also checked that disabling the new early return makes the public-chain 
tests fail.
   
   ### Manual tests
   
   Ran against a local gateway backed by PostgreSQL, using the 
`knoxidf-auth-clientregistration` (Basic auth), `knoxidf-auth-sso` (KnoxSSO 
cookie) and `knoxidf-auth-token` (JWT) topologies.
   
   Setup: register a client, log in through KnoxSSO and get an authorization 
code with an S256 challenge (consent accepted).
   
   ```bash
   GW=https://www.local.com:8443/gateway
   
   curl -sk -u admin:admin-password -X POST \
     "$GW/knoxidf-auth-clientregistration/knoxidf/api/v1/client/register" \
     --data-urlencode redirect_uris=https://localhost/callback \
     --data-urlencode allowed_scopes=openid,profile,offline_access > reg.json
   CID=$(jq -r .client_id reg.json)
   SECRET=$(jq -r .client_secret reg.json)
   
   VERIFIER=$(openssl rand -base64 48 | tr '+/' '-_' | tr -d '=\n')
   CHALLENGE=$(printf %s "$VERIFIER" | openssl dgst -sha256 -binary | base64 | 
tr '+/' '-_' | tr -d '=\n')
   
   curl -sk -u admin:admin-password -c cookies -o /dev/null \
     "$GW/knoxsso/api/v1/websso?originalUrl=$GW/knoxidf-auth-sso/"
   
   curl -sk -b cookies -G "$GW/knoxidf-auth-sso/knoxidf/api/v1/authorize" \
     --data-urlencode response_type=code \
     --data-urlencode client_id=$CID \
     --data-urlencode redirect_uri=https://localhost/callback \
     --data-urlencode "scope=openid profile offline_access" \
     --data-urlencode state=xyz \
     --data-urlencode code_challenge=$CHALLENGE \
     --data-urlencode code_challenge_method=S256
   # follow the redirect to the consent page, then:
   curl -sk -b cookies -X POST \
     "$GW/knoxidf-auth-sso/knoxidf/api/v1/authorize/consentAccepted" \
     -d state=$STATE \
     -d action=accept
   # CODE = the code parameter of the Location header
   ```
   
   The refresh token's id is its `knox.id` claim; its metadata is checked with:
   
   ```sql
   select token_id, md_name, md_value
     from knox_token_metadata
    where token_id = '<refresh token knox.id>'
      and md_name in ('client_auth_method', 'type', 'client_id');
   ```
   
   #### Positive case: public client (PKCE, no client_secret)
   
   Redeem the code with PKCE only:
   
   ```bash
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=authorization_code \
     --data-urlencode code=$CODE \
     --data-urlencode client_id=$CID \
     --data-urlencode redirect_uri=https://localhost/callback \
     --data-urlencode code_verifier=$VERIFIER
   ```
   
   Refresh without a `client_secret`, then replay the old refresh token:
   
   ```bash
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=refresh_token \
     --data-urlencode client_id=$CID \
     --data-urlencode refresh_token=$RT
   ```
   
   | Step | Result |
   |---|---|
   | Redeem code with PKCE, no `client_secret` | 200; refresh token `d757165e…` 
stored with `client_auth_method=none` |
   | Refresh, no `client_secret` | 200; rotated refresh token `969ca1dd…` 
stored with `client_auth_method=none` |
   | Replay the old refresh token | 401 `Unknown token` (consumed by rotation) |
   
   #### Negative cases
   
   Redeem a new code with PKCE **and** the `client_secret`, to start a 
confidential chain:
   
   ```bash
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=authorization_code \
     --data-urlencode code=$CODE \
     --data-urlencode client_id=$CID \
     --data-urlencode client_secret=$SECRET \
     --data-urlencode redirect_uri=https://localhost/callback \
     --data-urlencode code_verifier=$VERIFIER
   ```
   
   Refresh with no, a wrong, and the correct `client_secret`. The wrong secret 
has the wire format of a real one 
(`base64(base64(client_id)::base64(passcode))`) with a bogus passcode:
   
   ```bash
   WRONG=$(printf '%s::%s' "$(printf %s $CID | base64)" "$(printf %s 
not-the-passcode | base64)" | base64 | tr -d '\n')
   
   curl -sk -X POST "$GW/knoxidf-auth-token/knoxidf/api/v1/token" \
     -d grant_type=refresh_token \
     --data-urlencode client_id=$CID \
     --data-urlencode refresh_token=$RT \
     --data-urlencode client_secret=$WRONG   # or $SECRET, or omitted
   ```
   
   To simulate a refresh token issued before this change, the 
`client_auth_method` row of the public-chain token `969ca1dd…` was deleted:
   
   ```sql
   delete from knox_token_metadata
    where token_id = '969ca1dd-735a-4f34-a9a9-cedd63f24c97'
      and md_name = 'client_auth_method';
   ```
   
   | # | Step | Result |
   |---|---|---|
   | 0 | Redeem code with PKCE **and** `client_secret` | 200; refresh token 
`d4ec0432…` stored with `client_auth_method=client_secret_post` |
   | 1 | Confidential chain, no `client_secret` | 400 `invalid_grant` ("Invalid 
grant: client authentication failed") |
   | 2 | Confidential chain, wrong `client_secret` | 400 `invalid_grant` |
   | 3 | Confidential chain, correct `client_secret` | 200 (audit 
`reason=rotated`); rotated refresh token `5256b09a…` keeps 
`client_auth_method=client_secret_post` |
   | 4 | Public chain, wrong `client_secret` | 400 `invalid_grant` |
   | 5 | Pre-change refresh token (no `client_auth_method`), no `client_secret` 
| 400 `invalid_grant` |
   
   The gateway audit log showed the matching `validation_failed` entries for 1, 
2 and 4 and the `rotated` entry for 3.
   
   ## Integration Tests
   
   None added. The existing `test_knoxidf.py` refresh tests use a 
`client_secret`, so they are unaffected (not re-run locally); the public-client 
flow is covered by the unit tests and the manual run above. No opt-in label 
suites are needed.
   
   ## Performance implications
   
   None. Issuing a refresh token stores one more metadata entry with it, and 
the refresh grant reads it from metadata it already loads. No new remote or DB 
calls.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to