Skip to content

feat(oauth): add refresh token and expiry support to OAuth token flow - #1047

Merged
svor merged 14 commits into
mainfrom
CRW-4121
Sep 30, 2026
Merged

svor merged 14 commits into
mainfrom
CRW-4121

Conversation

@vinokurig

@vinokurig vinokurig commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Persist OAuth refresh tokens and expiration times in Kubernetes secrets alongside access tokens, enabling token refresh without re-authorization after server restarts.
Adds a POST /oauth/refresh endpoint, updates the OAuthToken DTO, PersonalAccessToken, and PersonalAccessTokenParams with refreshToken/expiresIn fields, and restores in-memory credentials from persisted secrets when the credential store is empty.

Screenshot/screencast of this PR

What issues does this PR fix or reference?

https://redhat.atlassian.net/browse/CRW-4121

How to test this PR?

  1. Start Che from the che-server pull request image: quay.io/eclipse/che-server:pr-1047
  2. Configure an oauth, e.g. for gitlab: https://eclipse.dev/che/docs/stable/administration-guide/configuring-oauth-2-for-gitlab/
  3. Start a workspace from a Gitlab repository url, an Oauth token must be added to the user-preferences -> Personal Access Tokens tab
  4. Restart the che-server pod to clear the oauth data from the che-server pod memory.
  5. Execute the POST API request: <che host url>/api/oauth/refresh?oauth_provider=gitlab&provider_url=https://gitlab.com
  6. Check the token secret, the token and the refresh-token data must be refreshed.

PR Checklist

As the author of this Pull Request I made sure that:

Release Notes

Reviewers

Reviewers, please comment how you tested the PR when approving it.

Summary by CodeRabbit

  • New Features

    • OAuth credentials now retain refresh tokens and expiration details, allowing expired tokens to refresh automatically, including after credentials are restored from storage.
    • Token refresh can be requested through the authentication API.
    • OAuth provider URLs are available through the authentication API.
    • Stored credentials can be retrieved without triggering refresh or validation.
  • Bug Fixes

    • Improved fallback behavior when token refresh fails, including validation of stored credentials.
    • Corrected matching of stored credentials by OAuth provider.

@github-actions

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

@vinokurig
vinokurig force-pushed the CRW-4121 branch 3 times, most recently from fb69994 to 7a2aa30 Compare September 7, 2026 12:56
Persist OAuth refresh tokens and expiration times in Kubernetes secrets
alongside access tokens, enabling token refresh without re-authorization
after server restarts. Adds a POST /oauth/refresh endpoint, updates the
OAuthToken DTO, PersonalAccessToken, and PersonalAccessTokenParams with
refreshToken/expiresIn fields, and restores in-memory credentials from
persisted secrets when the credential store is empty.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

public static final String REFRESH_TOKEN_DATA_FIELD = "refresh-token";

/** Kubernetes secret data field key for the token expiration time in seconds. */
public static final String EXPIRES_IN_DATA_FIELD = "expires-in";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it better to move to annotation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

*/
@POST
@Path("refresh")
public void refresh(@Required @QueryParam("oauth_provider") String oauthProvider)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can see that provider url is not considered.
What if we have different urls for a single provider, like GitHub

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, switched to provider_url parameter

token.getRefreshToken(),
token.getExpiresIn());
personalAccessTokenManager.store(personalAccessToken);
gitCredentialManager.createOrReplace(personalAccessToken);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think removePreviousTokenSecretsIfPresent is missed.
BTW, can we resue forceRefreshPersonalAccessToken(String scmServerUrl) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nice catch, switched to forceRefreshPersonalAccessToken(String scmServerUrl)

@github-actions

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 11, 2026
@github-actions

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 14, 2026
…en flow

Handle providers that omit `expires_in` and issue no refresh token:

* `OAuthAuthenticator`: extract `newOAuthToken(Credential)` and set the
  expiration only when the credential provides one, since
  `OAuthToken#withExpiresIn` takes a primitive.
* `EmbeddedOAuthAPI`: fall back to 0 when the token response has no
  `expires_in`.
* `KubernetesPersonalAccessTokenManager`: only write the `refresh-token`
  secret field when a refresh token is present.

Add tests for each case, including a new `OAuthAuthenticatorTest` for the
OAuth2 authenticator.

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 14, 2026
@svor

svor commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

/retest

personalAccessToken.getScmTokenName())
.put(
ANNOTATION_SCM_TOKEN_EXPIRES_IN,
String.valueOf(personalAccessToken.getExpiresIn()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it updates every PAT and most all of them will have 0
It misleads since 0 means expires immediately.
Should we check if personalAccessToken.getExpiresIn()) > 0 before adding annotation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

new TokenResponse()
.setAccessToken(token.getToken())
.setRefreshToken(token.getRefreshToken())
.setExpiresInSeconds(token.getExpiresIn());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wouldn't put 0 which is a default value.
Instead it is better to check if value > 0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, added the check and covered it with tests

@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 25, 2026
@vinokurig

Copy link
Copy Markdown
Contributor Author

/retest

@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 28, 2026
@Nullable private final String refreshToken;

/** Token expiration time in seconds. 0 if the token does not expire. */
@Nullable private final long expiresIn;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
@Nullable private final long expiresIn;
@Nullable private final Long expiresIn;

}

@Nullable
public long getExpiresIn() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
public long getExpiresIn() {
public Long getExpiresIn() {

Instant expiresAt = Instant.parse(creationTimestamp).plusSeconds(params.getExpiresIn());
// A token that is about to expire is treated as expired, so that it does not run out in the
// middle of the operation it is handed out for.
return !Instant.now().isBefore(expiresAt.minusSeconds(TOKEN_EXPIRATION_LEEWAY_SECONDS));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am afraid if we compare Kubernetes API server time with che-server instance time

@github-actions

Copy link
Copy Markdown

Docker image build succeeded: quay.io/eclipse/che-server:pr-1047

kubectl patch command
kubectl patch -n eclipse-che "checluster/eclipse-che" --type=json -p="[{"op": "replace", "path": "/spec/components/cheServer/deployment", "value": {containers: [{image: "quay.io/eclipse/che-server:pr-1047", name: che}]}}]"

@openshift-ci openshift-ci Bot added the lgtm label Sep 28, 2026
@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 29, 2026
@svor

svor commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

/retest

1 similar comment
@svor

svor commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

/retest

@svor svor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

verified together with eclipse-che/che-dashboard#1680
works as expected:

  • Authorized oauth token is displayed on Git Services tab
  • When the token is expired, opening Git Services tab on the dashboard automatically refreshes the token and git commands work as expected
  • Pre-existed OAuth tokens are moved to Git Services after applying current changes
  • The mechanism to delete token works as expected

@svor

svor commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

/retest

@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 29, 2026
@eclipse-che eclipse-che deleted a comment from openshift-ci Bot Sep 29, 2026
@svor

svor commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

/retest

2 similar comments
@svor

svor commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

/retest

@svor

svor commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

/retest

@openshift-ci

openshift-ci Bot commented Sep 30, 2026

Copy link
Copy Markdown

@vinokurig: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/v19-github-no-pat-oauth-flow-raw-devfile-url 4945cb4 link true /test v19-github-no-pat-oauth-flow-raw-devfile-url
ci/prow/v19-che-smoke-test 4945cb4 link true /test v19-che-smoke-test
ci/prow/v22-gitea-with-pat-setup-flow 4945cb4 link true /test v22-gitea-with-pat-setup-flow
ci/prow/v22-che-smoke-test 4945cb4 link true /test v22-che-smoke-test
ci/prow/v22-github-no-pat-oauth-flow 4945cb4 link true /test v22-github-no-pat-oauth-flow
ci/prow/v22-gitlab-with-pat-setup-flow 4945cb4 link true /test v22-gitlab-with-pat-setup-flow
ci/prow/v22-gitlab-no-pat-oauth-flow 4945cb4 link true /test v22-gitlab-no-pat-oauth-flow
ci/prow/v22-bitbucket-no-pat-oauth-flow-raw-devfile-url 4945cb4 link true /test v22-bitbucket-no-pat-oauth-flow-raw-devfile-url
ci/prow/v22-bitbucket-no-pat-oauth-flow-ssh-url 4945cb4 link true /test v22-bitbucket-no-pat-oauth-flow-ssh-url
ci/prow/v22-github-with-pat-setup-flow 4945cb4 link true /test v22-github-with-pat-setup-flow
ci/prow/v22-gitea-no-pat-oauth-flow 4945cb4 link true /test v22-gitea-no-pat-oauth-flow
ci/prow/v22-gitlab-with-oauth-setup-flow 4945cb4 link true /test v22-gitlab-with-oauth-setup-flow
ci/prow/v22-azure-with-pat-setup-flow 4945cb4 link true /test v22-azure-with-pat-setup-flow
ci/prow/v22-gitlab-no-pat-oauth-flow-raw-devfile-url 4945cb4 link true /test v22-gitlab-no-pat-oauth-flow-raw-devfile-url
ci/prow/v22-azure-no-pat-oauth-flow-raw-devfile-url 4945cb4 link true /test v22-azure-no-pat-oauth-flow-raw-devfile-url
ci/prow/v22-azure-no-pat-oauth-flow-ssh-url 4945cb4 link true /test v22-azure-no-pat-oauth-flow-ssh-url
ci/prow/v22-gitlab-no-pat-oauth-flow-ssh-url 4945cb4 link true /test v22-gitlab-no-pat-oauth-flow-ssh-url
ci/prow/v22-azure-no-pat-oauth-flow 4945cb4 link true /test v22-azure-no-pat-oauth-flow
ci/prow/v22-bitbucket-no-pat-oauth-flow 4945cb4 link true /test v22-bitbucket-no-pat-oauth-flow
ci/prow/v22-github-no-pat-oauth-flow-raw-devfile-url 4945cb4 link true /test v22-github-no-pat-oauth-flow-raw-devfile-url
ci/prow/v22-github-no-pat-oauth-flow-ssh-url 4945cb4 link true /test v22-github-no-pat-oauth-flow-ssh-url

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@artaleks9

artaleks9 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor
  • Verified on Eclipse Che deployed on OpenShift version 4.22.14 with quay.io/eclipse/che-server:pr-1047:
    The functionality works properly

  • Also all ci-tests on old configuration (prefix ci/prow/v19 ) are passed.

  • Failure ci-tests on new configuration ( prefix ci/prow/v22) is due to the known restriction HyperShift infrastructure:

Acquiring leases for test gitlab-with-pat-setup-flow: [che-aws-quota-slice hypershift-hive-quota-slice]
error: Failed to acquire resource, current capacity: 0 free, 20 leased

@openshift-ci

openshift-ci Bot commented Sep 30, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: artaleks9, svor, tolusha, vinokurig

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@svor
svor merged commit 77d0e49 into main Sep 30, 2026
29 of 48 checks passed
@svor
svor deleted the CRW-4121 branch September 30, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants