authmanager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * AuthManager
  3. *
  4. * This module aims to abstract login procedures. Results from Mojang's REST api
  5. * are retrieved through our Mojang module. These results are processed and stored,
  6. * if applicable, in the config using the ConfigManager. All login procedures should
  7. * be made through this module.
  8. *
  9. * @module authmanager
  10. */
  11. // Requirements
  12. const ConfigManager = require('./configmanager')
  13. const Mojang = require('./mojang')
  14. // Functions
  15. /**
  16. * Add an account. This will authenticate the given credentials with Mojang's
  17. * authserver. The resultant data will be stored as an auth account in the
  18. * configuration database.
  19. *
  20. * @param {string} username The account username (email if migrated).
  21. * @param {string} password The account password.
  22. * @returns {Promise.<Object>} Promise which resolves the resolved authenticated account object.
  23. */
  24. exports.addAccount = async function(username, password){
  25. try {
  26. const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken())
  27. const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
  28. ConfigManager.save()
  29. return ret
  30. } catch (err){
  31. return Promise.reject(err)
  32. }
  33. }
  34. /**
  35. * Remove an account. This will invalidate the access token associated
  36. * with the account and then remove it from the database.
  37. *
  38. * @param {string} uuid The UUID of the account to be removed.
  39. * @returns {Promise.<void>} Promise which resolves to void when the action is complete.
  40. */
  41. exports.removeAccount = async function(uuid){
  42. try {
  43. const authAcc = ConfigManager.getAuthAccount(uuid)
  44. await Mojang.invalidate(authAcc.accessToken, ConfigManager.getClientToken())
  45. ConfigManager.removeAuthAccount(uuid)
  46. ConfigManager.save()
  47. return Promise.resolve()
  48. } catch (err){
  49. return Promise.reject(err)
  50. }
  51. }
  52. /**
  53. * Validate the selected account with Mojang's authserver. If the account is not valid,
  54. * we will attempt to refresh the access token and update that value. If that fails, a
  55. * new login will be required.
  56. *
  57. * **Function is WIP**
  58. *
  59. * @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
  60. * otherwise false.
  61. */
  62. exports.validateSelected = async function(){
  63. const current = ConfigManager.getSelectedAccount()
  64. const isValid = await Mojang.validate(current.accessToken, ConfigManager.getClientToken())
  65. console.log(isValid)
  66. if(!isValid){
  67. try {
  68. const session = await Mojang.refresh(current.accessToken, ConfigManager.getClientToken())
  69. console.log('ses', session)
  70. ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
  71. ConfigManager.save()
  72. } catch(err) {
  73. console.debug('Error while validating selected profile:', err)
  74. if(err && err.error === 'ForbiddenOperationException'){
  75. // What do we do?
  76. }
  77. return false
  78. }
  79. return true
  80. } else {
  81. return true
  82. }
  83. }