Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor refresh tokens #305

Merged
merged 4 commits into from
Nov 9, 2023
Merged

Refactor refresh tokens #305

merged 4 commits into from
Nov 9, 2023

Conversation

slifty
Copy link
Contributor

@slifty slifty commented Nov 7, 2023

This PR moves refresh token logic into a dedicated class that is ONLY responsible for generating a non-stale token.

It also changes when in the SFTP flow those tokens are actually generated, resulting in a more simple SFTP flow (and also ensuring that all SFTP processes are working with fresh tokens).

One side effect of this PR is we are no longer using the FUSION_AUTH_SFTP_APP_ID environment variable, and are instead only using FUSION_AUTH_SFTP_CLIENT_ID. These were two names for the same value, and I felt that CLIENT_ID was a the more consistent / clear name so decided on that one.

Resolves #298
Resolves #289
Resolves #288

We are using `require-env-variable` to ensure certain environment
variables are properly set on service start.  This adds in a few more
required variables. Even though they are not directly passed into our
service classes, they are used by third party libraries and so we want
to ensure their existence.
We had organically coupled token management with the initial
authentication flow, but they don't actually belong together.

This separates token management (e.g. utilization of refresh tokens)
from the SSH authentication system.  It also refactors the sftp session
handler to use the token manager rather than the authentication session.

While doing these refactors we took out a redundant environment
variable.

Issue #289 Duplicated FusionAuth env vars
Auth tokens are now retrieved just-in-time by the permanent file
system (rather than being passed during the creation of the permanent
file system).  This is a critical fix because (1) it prevents certain
paths that would lead to stale tokens but also (2) it means that
creating a permanent file system becomes a synchronous operation.  This
also resolves a bug where the failure to generate a token could result
in a hanging sftp connection.

Issue #288 Permanent file system errors can result in hung connections
When refactoring authentication to use refresh tokens we missed the 2FA
flow. It's not clear that we should be supporting 2FA to begin with
since rclone doesn't support it, and ultimately the real solution is to
use keys instead of passwords for sftp authentication.

That being said, while it's here we should make sure it isn't broken!

Issue #298 2FA auth flow does not utilize refresh tokens
@slifty slifty force-pushed the 288-fix-hanging-risk branch from d497ef1 to cd0d162 Compare November 7, 2023 21:29
this.sftpConnection.status(reqId, SFTP_STATUS_CODE.NO_SUCH_FILE);
break;
default: {
this.openExistingFileHandler(
Copy link
Contributor

Choose a reason for hiding this comment

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

Question, is there a bottleneck to not awaiting this for example and making the handler method async?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't make the handler methods async because we don't control their signature -- the ssh2 library (which ultimately gets these handlers passed to them) expects handlers to be synchronous, which means they don't handle failed promises.

Copy link
Contributor

@nfebe nfebe left a comment

Choose a reason for hiding this comment

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

Nothing looks out of place me, test works! Great work @slifty

@slifty slifty merged commit bef391d into main Nov 9, 2023
2 checks passed
@slifty slifty deleted the 288-fix-hanging-risk branch November 9, 2023 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants