Skip to content

Implement token exchange from OCM#57234

Merged
mickenordin merged 9 commits into
nextcloud:masterfrom
enriquepablo:master
Jun 18, 2026
Merged

Implement token exchange from OCM#57234
mickenordin merged 9 commits into
nextcloud:masterfrom
enriquepablo:master

Conversation

@enriquepablo

@enriquepablo enriquepablo commented Dec 23, 2025

Copy link
Copy Markdown
Contributor

See #57152 #57166

Summary

This PR implements the token exchange flow from OCM, allowing Nextcloud to use bearer auth with short lived tokens. This opens up interoperability with OpenCloud/ownCloud OCIS and the possibility to implement webapp shares in the future, which requires this.

Discussion

This is a preliminary PR for discussion. If this goes through there are some things missing with which we'll be grateful to get some guidance:

  • docs and tests
  • keeping retrieved access tokens in the db
  • removing tokens when removing shares

There is also the question that for access tokens, we are keeping a reference to the corresponding refresh token in the uid field of the access token db record.

@github-actions

github-actions Bot commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

@enriquepablo

Copy link
Copy Markdown
Contributor Author

Disclaimer: Some of the code in this PR (mainly regarding the tests) was generated by Claude Opus 4.5. I have provided it with very specific prompts, and thoroughly reviewed the output.

@mickenordin

mickenordin commented May 28, 2026

Copy link
Copy Markdown
Contributor

I have grouped all changes into 8 commits9 commits that should be easier to review than 82 commits now @susnux @provokateurin

@mickenordin

Copy link
Copy Markdown
Contributor
screencast-2026-06-07T11.18.mp4

Here is a screen cast of a webapp share, using the token exchange and new http signatures

@mickenordin

Copy link
Copy Markdown
Contributor
screencast-2026-06-07T11.33.mp4

Here is a screen cast of the same server sharing to a vanilla nextcloud server, and you can see that the fallback to basic auth works without any token exchange, so backwards compatibility is maintained

@nextcloud-bot nextcloud-bot mentioned this pull request Jun 8, 2026
enriquepablo and others added 9 commits June 17, 2026 10:44
Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
…ccess tokens

Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
… validation

Accept both the legacy options.sharedSecret envelope and the new
protocol[name].sharedSecret form. Preserve the original cloud ID so the
factory can discover capabilities, then reset shareWith to the local
username for user lookup.

Delegate per-protocol validation to providers via the new
IValidationAwareCloudFederationProvider interface, with split exception
handling: BadRequestException -> 400, ProviderCouldNotAddShareException
-> the exception's own HTTP status (501 fallback).

In the notification handler, fall back to looking up the refresh token
via OcmTokenMapMapper when the access token cannot identify the federation.

Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
…ange

Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
… shares

Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
Co-authored-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Micke Nordin <kano@sunet.se>
Signed-off-by: Enrique Pérez Arnaud <enrique@cazalla.net>
Shares using the OCM multi-protocol envelope (name multi, with the secret carried in a sibling protocol entry such as webdav) were rejected with Missing sharedSecret in protocol. Scan every protocol entry for the shared secret during validation, resolve the secret from the matching entry, and let the files provider serve the webdav entry of a multi envelope. Covers the file and folder resource types.

Signed-off-by: Micke Nordin <kano@sunet.se>
@mickenordin

Copy link
Copy Markdown
Contributor

@provokateurin @susnux do you think we can get this reviewed and merged now that NC34 is out? It takes a significant amount of work to keep the pr fresh...

Comment on lines +41 to +42
*
* @since 32.0.0

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.

Suggested change
*
* @since 32.0.0

Only needed for ocp apis

@welcome

welcome Bot commented Jun 18, 2026

Copy link
Copy Markdown

Thanks for your first pull request and welcome to the community! Feel free to keep them coming! If you are looking for issues to tackle then have a look at this selection: https://github.com/nextcloud/server/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22

@nickvergessen

Copy link
Copy Markdown
Member

This PR is missing a version bump and therefore breaks all daily/master based instances

Comment on lines +46 to +48
$db = Server::get(IDBConnection::class);
$tokenProvider = Server::get(PublicKeyTokenProvider::class);
$userManager = Server::get(IUserManager::class);

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.

Should use DI?

@enriquepablo enriquepablo Jun 19, 2026

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.

I have opened a (#61439) #61441 addressing this. I can see you took care of the rest of your comments.

* Shares created by this fork (32-char tokens) that are somehow missing from
* oc_authtoken are silently repaired.
*/
class Version1012Date20260306120000 extends SimpleMigrationStep {

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.

Should bump the version of the app since a migration was added

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.

I can see you already addressed this in #61428 . Thanks!

}

if (!$table->hasColumn('access_token_expires')) {
$table->addColumn('access_token_expires', Types::INTEGER, [

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.

If this is supposed to be a timestamp, the column type is wrong at least on 32bits

Should have lenght 11 or more, or BIGINT or type datetime instead

@enriquepablo

Copy link
Copy Markdown
Contributor Author

@nickvergessen I will fix these issues and open a new PR and link it here

@mickenordin

Copy link
Copy Markdown
Contributor

@nickvergessen I will fix these issues and open a new PR and link it here

Check what was already fixed here first: #61428 (review) @enriquepablo

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

Labels

3. to review Waiting for reviews community pull requests from community feedback-requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants