authmanager.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 LoggerUtil = require('./loggerutil')
  14. const Mojang = require('./mojang')
  15. const logger = LoggerUtil('%c[AuthManager]', 'color: #a02d2a; font-weight: bold')
  16. const loggerSuccess = LoggerUtil('%c[AuthManager]', 'color: #209b07; font-weight: bold')
  17. // Functions
  18. /**
  19. * Add an account. This will authenticate the given credentials with Mojang's
  20. * authserver. The resultant data will be stored as an auth account in the
  21. * configuration database.
  22. *
  23. * @param {string} username The account username (email if migrated).
  24. * @param {string} password The account password.
  25. * @returns {Promise.<Object>} Promise which resolves the resolved authenticated account object.
  26. */
  27. exports.addAccount = async function(username, password){
  28. try {
  29. const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken())
  30. const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
  31. ConfigManager.save()
  32. return ret
  33. } catch (err){
  34. return Promise.reject(err)
  35. }
  36. }
  37. /**
  38. * Remove an account. This will invalidate the access token associated
  39. * with the account and then remove it from the database.
  40. *
  41. * @param {string} uuid The UUID of the account to be removed.
  42. * @returns {Promise.<void>} Promise which resolves to void when the action is complete.
  43. */
  44. exports.removeAccount = async function(uuid){
  45. try {
  46. const authAcc = ConfigManager.getAuthAccount(uuid)
  47. await Mojang.invalidate(authAcc.accessToken, ConfigManager.getClientToken())
  48. ConfigManager.removeAuthAccount(uuid)
  49. ConfigManager.save()
  50. return Promise.resolve()
  51. } catch (err){
  52. return Promise.reject(err)
  53. }
  54. }
  55. /**
  56. * Validate the selected account with Mojang's authserver. If the account is not valid,
  57. * we will attempt to refresh the access token and update that value. If that fails, a
  58. * new login will be required.
  59. *
  60. * **Function is WIP**
  61. *
  62. * @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
  63. * otherwise false.
  64. */
  65. exports.validateSelected = async function(){
  66. const current = ConfigManager.getSelectedAccount()
  67. const isValid = await Mojang.validate(current.accessToken, ConfigManager.getClientToken())
  68. if(!isValid){
  69. try {
  70. const session = await Mojang.refresh(current.accessToken, ConfigManager.getClientToken())
  71. ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
  72. ConfigManager.save()
  73. } catch(err) {
  74. logger.debug('Error while validating selected profile:', err)
  75. if(err && err.error === 'ForbiddenOperationException'){
  76. // What do we do?
  77. }
  78. logger.log('Account access token is invalid.')
  79. return false
  80. }
  81. loggerSuccess.log('Account access token validated.')
  82. return true
  83. } else {
  84. loggerSuccess.log('Account access token validated.')
  85. return true
  86. }
  87. }