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

Add token authentication #780

Merged
merged 20 commits into from Dec 5, 2020
Merged

Add token authentication #780

merged 20 commits into from Dec 5, 2020

Conversation

stenlan
Copy link
Contributor

@stenlan stenlan commented Nov 30, 2020

(this assumes PrismarineJS/node-yggdrasil#51)
Fixes #519. It allows the user to specify a profilesFolder property. If given, a launcher_profiles.json file will be read or created if it doesn't yet exist, and it will then act approximately like the minecraft launcher. The given username is searched within the launcher profiles and if found, it attempts to use this information to login. If not found, or the login fails, it can optionally use a password if it's given. If not, it will fail. On any successful login, if the profilesFolder property is given, it will save it properly to the file. This works for preexisting and non-preexisting profiles and is compatible with the official launcher. It will only save it if the clientToken matches so as to not corrupt the existing launcher_profiles.json. This will match by default, and might only mismatch if another clientToken or session with clientToken is passed as an options property.


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


Copy link
Contributor

@DeltaEvo DeltaEvo left a comment

Choose a reason for hiding this comment

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

Nice feature !
In your code a lot of let can be transformed to const
And I don't think we need theses extra dependencies

src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
|| Object.values(auths.authenticationDatabase[key].profiles)[0].displayName.toLowerCase() == lowerUsername
)
if (err) {
if (profile) { // profile is invalid, remove
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure that the profile is invalid here ? We should check the error since a lot of kind of error can happen here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have thought about this, and it should be fine like this IMO. The minecraft launcher itself does the same thing I think, if your login doesn't succeed for whatever reason, it invalidates it

src/client/auth.js Outdated Show resolved Hide resolved
@stenlan
Copy link
Contributor Author

stenlan commented Dec 1, 2020

@DeltaEvo I think I fixed most of the stuff you mentioned, any thoughts?

src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
src/client/auth.js Outdated Show resolved Hide resolved
@DeltaEvo
Copy link
Contributor

DeltaEvo commented Dec 1, 2020

Yep it seems better, only

In your code a lot of let can be transformed to const

is still valid :)

@wvffle
Copy link
Member

wvffle commented Dec 1, 2020

You should resolve things that you've already done

@stenlan
Copy link
Contributor Author

stenlan commented Dec 1, 2020

Okay I resolved most, the remaining ones have to do with error handling. That's mainly because I'm not sure what the preferred way to go about that is in this project. If I know this I can properly implement it

@stenlan
Copy link
Contributor Author

stenlan commented Dec 1, 2020

And transforming let to const I guess...

@stenlan
Copy link
Contributor Author

stenlan commented Dec 1, 2020

Ok I changed that. I still left it in an async function because I prefer the await operator over ugly .then chaining. If you ask me, this pr is ready to merge now

@stenlan
Copy link
Contributor Author

stenlan commented Dec 4, 2020

Welp, idk why the java env setup is failling in the tests, but lmk what needs to be done to get this merged. Seems like it would be a useful feature, and it's also on the "big prismarine projects" list I think. If you don't want it just tell me and I'll close this

@Gjum
Copy link
Member

Gjum commented Dec 4, 2020

The java env setup seems to be failling because of a recent (Oct. 1) security vulnerability fix on Github's CI. It's not really in-scope of this PR, so I've attempted a fix in #785. If that fix works, just re-run the CI (if that's possible? or just push an empty commit to trigger it).

return JSON.parse(await fs.readFile(options.profilesFolder + '/launcher_profiles.json', 'utf8'))
} catch (err) {
await fs.mkdir(options.profilesFolder, { recursive: true })
await fs.writeFile(options.profilesFolder + '/launcher_profiles.json', '{}')
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this mess with the launcher at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, 'mess' as in it will modify that file. It's compatible with the official launcher, which means it properly deletes and also saves new accounts to the file for the launcher to use

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, one could just specify a path that is not the launcher folder and it will just use its own isolated file instead

Merge in original repo again
@stenlan
Copy link
Contributor Author

stenlan commented Dec 4, 2020

Thanks a lot @Gjum, the tests are all passing now

@rom1504
Copy link
Member

rom1504 commented Dec 5, 2020

looks good.
Missing:

@Heath123
Copy link
Contributor

Heath123 commented Dec 5, 2020

I have a report of this error:

(node:22920) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at module.exports (C:\Users\*****\Desktop\pakkit\node_modules\minecraft-protocol\src\client\auth.js:81:30)

(happens at https://github.com/ph0t0shop/node-minecraft-protocol/blob/master/src/client/auth.js#L81)

@stenlan
Copy link
Contributor Author

stenlan commented Dec 5, 2020

why not use the home folder by default so it's enabled by default ? this would remove hundred of opened issues and discord complaints (ie people getting temporarily banned by mojang due to too often auth requests)

@rom1504 I was a bit hesitant to do this, firstly because then we need to find the minecraft folder on the user's system, and secondly because then how would one specify that they just don't want to save credentials? Also, it might be unexpected for the user that it suddenly uses their local minecraft folder

@rom1504
Copy link
Member

rom1504 commented Dec 5, 2020

https://github.com/PrismarineJS/node-minecraft-wrap/blob/master/lib/wrap_client.js#L11 works pretty well to find the path
To specify not to save the user could put this option as false

About it being unexpected that it would use their folder, I think it's actually what most people would expect by default.
We can add a note in the readme that we're doing that and how to disable it.

I think it's really beneficial to enable this by default, as the current behavior is to call the mojang server too many times which is actually not great.

@rom1504
Copy link
Member

rom1504 commented Dec 5, 2020 via email

@Karang
Copy link
Contributor

Karang commented Dec 5, 2020

What needs to be enabled by default is saving the token, not necessary the credentials. What I would do is:

  • if the user specifies mc directory: use it to load/store credentials and token
  • by default: save/load the token only in the local or tmp directory

@stenlan
Copy link
Contributor Author

stenlan commented Dec 5, 2020

Okay. I will do this in a bit, and also add some error handling for invalid launcher profile files

@rom1504
Copy link
Member

rom1504 commented Dec 5, 2020

Hmmm does the vanilla client save the credentials ? I think it doesn't
Saving the token is good but saving the password not so much

@rom1504
Copy link
Member

rom1504 commented Dec 5, 2020

Yeah and I see you also don't store the password.
What did you mean by credentials @Karang ?

@Karang
Copy link
Contributor

Karang commented Dec 5, 2020

Yeah and I see you also don't store the password.
What did you mean by credentials @Karang ?

I was thinking about username + password but yeah thats probably not a good idea.

@stenlan
Copy link
Contributor Author

stenlan commented Dec 5, 2020

Oh yeah you definitely don't wanna do that. Currently, the password can be specified, and it will be used to login only if it can't use the launcher_profiles for the account, and then saves it as well. This IMO is the most intuitive and mimics the launcher behavior pretty pragmatically

@rom1504 rom1504 merged commit 0277091 into PrismarineJS:master Dec 5, 2020
rom1504 added a commit that referenced this pull request Dec 6, 2020
This reverts commit 0277091.
This commits introduced a bug in offline mode.
We don't know yet how to fix it, so better revert first and fix later
rom1504 added a commit that referenced this pull request Dec 6, 2020
This reverts commit 14c4886.

Adds back token auth now that mineflayed is fixed
MrGeorgen added a commit to themoonisacheese/2bored2wait that referenced this pull request Jan 8, 2021
matthi4s pushed a commit to aternosorg/node-minecraft-protocol that referenced this pull request Jun 9, 2021
* Add token authentication

* Update package.json

* Update src/client/auth.js

Co-authored-by: David Duarte <deltaduartedavid@gmail.com>

* Remove some deps

* Fix merge conflict + fix some token stuff

* Fix even more stuff + testing

* Change back echo example

* Let -> const + bugfix

* Fixed error handling

* Revert client_echo again...

* Fix linter errors

* Profile improvements

* Stupid linter errors

* Update yggdrasil, remove token auth example, move to client_echo instead

* Update client_echo

* Linter error...

Co-authored-by: David Duarte <deltaduartedavid@gmail.com>
matthi4s pushed a commit to aternosorg/node-minecraft-protocol that referenced this pull request Jun 9, 2021
This reverts commit 0277091.
This commits introduced a bug in offline mode.
We don't know yet how to fix it, so better revert first and fix later
matthi4s pushed a commit to aternosorg/node-minecraft-protocol that referenced this pull request Jun 9, 2021
This reverts commit 14c4886.

Adds back token auth now that mineflayed is fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add an option to store and use auth token directly in nmp
7 participants