authmanager.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if(ConfigManager.getClientToken() == null){
  32. ConfigManager.setClientToken(session.clientToken)
  33. }
  34. ConfigManager.save()
  35. return ret
  36. } catch (err){
  37. return Promise.reject(err)
  38. }
  39. }
  40. /**
  41. * Remove an account. This will invalidate the access token associated
  42. * with the account and then remove it from the database.
  43. *
  44. * @param {string} uuid The UUID of the account to be removed.
  45. * @returns {Promise.<void>} Promise which resolves to void when the action is complete.
  46. */
  47. exports.removeAccount = async function(uuid){
  48. try {
  49. const authAcc = ConfigManager.getAuthAccount(uuid)
  50. await Mojang.invalidate(authAcc.accessToken, ConfigManager.getClientToken())
  51. ConfigManager.removeAuthAccount(uuid)
  52. ConfigManager.save()
  53. return Promise.resolve()
  54. } catch (err){
  55. return Promise.reject(err)
  56. }
  57. }
  58. /**
  59. * Validate the selected account with Mojang's authserver. If the account is not valid,
  60. * we will attempt to refresh the access token and update that value. If that fails, a
  61. * new login will be required.
  62. *
  63. * **Function is WIP**
  64. *
  65. * @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
  66. * otherwise false.
  67. */
  68. exports.validateSelected = async function(){
  69. const current = ConfigManager.getSelectedAccount()
  70. const isValid = await Mojang.validate(current.accessToken, ConfigManager.getClientToken())
  71. if(!isValid){
  72. try {
  73. const session = await Mojang.refresh(current.accessToken, ConfigManager.getClientToken())
  74. ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
  75. ConfigManager.save()
  76. } catch(err) {
  77. logger.debug('Error while validating selected profile:', err)
  78. if(err && err.error === 'ForbiddenOperationException'){
  79. // What do we do?
  80. }
  81. logger.log('Account access token is invalid.')
  82. return false
  83. }
  84. loggerSuccess.log('Account access token validated.')
  85. return true
  86. } else {
  87. loggerSuccess.log('Account access token validated.')
  88. return true
  89. }
  90. }