authmanager.js 3.6 KB

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