Skip to content

feat(auth): add scoped personal access tokens - #1686

Open
hgaol wants to merge 4 commits into
apache:devfrom
hgaol:feat/pat-user-access
Open

hgaol wants to merge 4 commits into
apache:devfrom
hgaol:feat/pat-user-access

Conversation

@hgaol

@hgaol hgaol commented Nov 23, 2026

Copy link
Copy Markdown
Member

Fixes #1555

Proposed Changes

  • Add scoped, expiring personal access tokens for existing user accounts, including revocation and account settings management.
  • Add Bearer token authentication with route scopes for core actions and an admin-controlled security toggle.
  • Update API documentation and add backend and UI test coverage.

Add user-owned expiring and revocable credentials, strict Bearer authentication, semantic route scopes, current account resolution, recent-authentication checks, account lifecycle hooks, and default-off admin controls. Include account-settings management, HTTP documentation, and end-to-end coverage for two eligible users creating content and voting through PATs.
@LinkinStars

Copy link
Copy Markdown
Member

The PAT lookup selects an external login by user_id without filtering by provider, then passes its ExternalID to the active UserCenter plugin.

For example, a local account linked only to GitHub is correctly exempt from UserCenter status checks during password login, but its PAT sends the GitHub ID to UserCenter, which may produce an incorrect authentication result.

Please align this with CheckUserStatusInUserCenter: resolve the binding for the active UserCenter provider, and leave ExternalID empty when no matching binding exists.

Filter external login lookup by both user ID and the active UserCenter provider. Leave ExternalID empty when no UserCenter or matching binding exists, so unrelated OAuth identities are never sent to the plugin.

Add regression coverage for disabled UserCenter, missing bindings, unrelated and inactive providers, matching bindings, and multiple providers, including another user's binding. Addresses the review comment on apache#1686.
@hgaol

hgaol commented Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

Fixed in 138b297. GetUserCacheInfoByUserID now looks up the external-login binding for the active UserCenter provider and leaves ExternalID empty if no matching binding exists. Regression tests cover disabled UserCenter, missing/unrelated/inactive bindings, matching and multiple providers, and a binding owned by another user. The focused regression test passes.

@LinkinStars

Copy link
Copy Markdown
Member

@robinv8 There are no issues on the backend. Please check if there are any changes that need to be made to the front end.

@robinv8 robinv8 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the PR. I’m requesting changes for the password form, duplicate submissions, and revocation while token access is disabled. I’ve left the details inline.

old_pass: formData.old_pass.value,
pass: formData.pass.value,
revoke_personal_access_tokens:
formData.revoke_personal_access_tokens.value,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Blocking: Reset the revoke-all option when closing the password form

This submits the checkbox value from the previous form session. If a user checks “Revoke all personal access tokens,” cancels, and reopens the form, the box is still checked. Saving a new password can then revoke every token unintentionally. Please clear the form on cancel and after a successful save, and add a regression test that checks the next submission sends false.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 20dbcc7: the password form now resets via initialFormData()/closeForm() on both cancel and successful save, so revoke_personal_access_tokens (and the rest of the form) no longer carries over between sessions. Added regression coverage in ModifyPass/index.test.tsx for both the cancel and save paths.

<Button variant="link" onClick={resetCreate}>
{t('cancel', { keyPrefix: 'btns' })}
</Button>
<Button disabled={!canCreate} onClick={createToken}>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Blocking: Prevent duplicate token creation

This button remains enabled while creation is pending. Two clicks can create two tokens, and a later response can replace the first token in the one-time display. The Continue button in the reauthentication dialog has the same issue. Please guard both handlers against duplicate calls and disable the buttons while their requests are pending. Keep the reauthentication flow guarded until the subsequent token creation request finishes. Please also clear the password and pending create data when dismissing the reauthentication dialog.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 20dbcc7: createToken/confirmReauthentication are now guarded by a submitting ref + isSubmitting state, with the Create/Continue buttons disabled while a request is in flight. The reauthentication flow stays guarded through the follow-up creation call, and dismissCreate/dismissReauthentication clear the password and pending create payload when either dialog is dismissed. Added tests covering double-click guarding and dialog dismissal.

</Badge>
</td>
<td className="text-end">
{item.status === 'active' && (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Blocking: Keep revocation available when token access is disabled

The disabled banner says users can still revoke existing tokens, but the API marks them temporarily_unavailable. Those rows are hidden by default and have no Revoke action even when shown. Please make them visible and revocable without requiring the administrator to turn token access back on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 20dbcc7: added isRevocable() (active or temporarily_unavailable) so those tokens stay visible by default and keep their Revoke action even while PAT access is disabled site-wide. Covered by a new regression test.

const security = siteSecurityStore();
const user = loggedUserInfoStore((state) => state.user);
const reauthenticationCaptcha = useCaptchaPlugin('edit_userinfo');
const { data = [], mutate } = usePersonalAccessTokens();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-blocking: Distinguish a failed fetch from an empty list

Defaulting data to an empty array makes a failed request, such as a timeout, look like the user has no tokens. The hook already exposes isLoading and error. Please use them to show loading and a retryable error, and reserve the empty state for a successful response.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 20dbcc7: the component now reads isLoading/error from usePersonalAccessTokens and renders a loading state, a retryable error alert, or the empty-list message accordingly, instead of defaulting data to []. Added a test asserting all three states render distinctly.

),
).toBe(true);
const createButtons = screen.getAllByRole('button', { name: 'create' });
expect(createButtons[createButtons.length - 1].hasAttribute('disabled')).toBe(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Test coverage: Isolate the scope requirement

The name is blank here, so Create would still be disabled if the scope check were removed. Please enter a valid name first, verify that Create is disabled with no scopes, then select one scope and verify it becomes enabled.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 20dbcc7: the test now fills in a valid name before toggling scopes, so the Create button's disabled state is isolated to the scope requirement rather than also being gated by an empty name.

Reset the password form after cancellation and successful updates. Guard PAT creation and reauthentication through the subsequent creation request, and discard passwords and pending payloads when the dialog is dismissed.

Keep temporarily unavailable tokens visible and revocable while PAT access is disabled. Distinguish loading and retryable fetch errors from successful empty results. Add regression coverage and isolate the explicit-scope requirement, addressing robinv8's review on apache#1686.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants