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

How do you sign out of Scratch? #80

Open
hueychen27 opened this issue Apr 11, 2024 · 7 comments
Open

How do you sign out of Scratch? #80

hueychen27 opened this issue Apr 11, 2024 · 7 comments

Comments

@hueychen27
Copy link
Contributor

How can I sign out of Scratch with the Scratch API? I heard of this link https://scratch.mit.edu/accounts/logout/, but how do I use it?

@towerofnix
Copy link
Owner

The simplest way to sign out is just clearing / discarding your session ID cookie, but of course that would still leave the session valid, just lost to you. (If someone else managed to intercept the session ID and stole it, they would still be able to keep acting as you.)

I've never used that endpoint before, but here is how scratch-www does it, which you can model your own code off of:

// POST to /accounts/logout using a dummy form instead of XHR. This ensures
// logout only happens AFTER onbeforeunload has the chance to prevent nagivation.
jar.use('scratchcsrftoken', '/csrf_token/', (err, csrftoken) => {
    if (err) return log.error('Error while retrieving CSRF token', err);
    const form = document.createElement('form');
    form.setAttribute('method', 'POST');
    form.setAttribute('action', '/accounts/logout/');
    const csrfField = document.createElement('input');
    csrfField.setAttribute('type', 'hidden');
    csrfField.setAttribute('name', 'csrfmiddlewaretoken');
    csrfField.setAttribute('value', csrftoken);
    form.appendChild(csrfField);
    document.body.appendChild(form);
    form.submit();
});

I don't know if it's necessary to provide a valid CSRF token here (by fetching /csrf_token/ like the official code does), or if you can just provide 'a' like other code.

If we add this to the documentation (as we should!), it would be great to check if the session ID really is invalidated, i.e. if it's impossible to reuse (if you kept track of it separately from your cookies, or someone else stole it).

@hueychen27
Copy link
Contributor Author

You do need to provide a valid CSRF token, at least in my experiences (not "a".)

@hueychen27
Copy link
Contributor Author

By the way, I adapted the code to work for node-fetch, and it will not sign out all sessions using the account, only your session. Maybe I need to use the browser's csrftoken.

@towerofnix
Copy link
Owner

I don't think it's possible to sign out all sessions (short of resetting your password, probably). Even if you got the browser's cookies and used those, you would only be signing out on that browser and that system, not any other browsers/computers which are signed in.

@hueychen27
Copy link
Contributor Author

If someone gets your session cookie, they can use your account without your password. Changing your password does not help. I know this because I have been hacked as I gave away the essential cookies... As a conclusion, the Scratch Team banned my account, got it back from a repeal request, and the hacker was no more.

@towerofnix
Copy link
Owner

That's interesting. I assumed resetting password might log all sessions out, since the page reads "After changing your password, you will be prompted to log back in." — but it looks like that's mistaken, or only affects the current session.

@hueychen27
Copy link
Contributor Author

When it says "you," it really means only you...

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

No branches or pull requests

2 participants