authmanager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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('helios-core')
  14. const { MojangRestAPI, mojangErrorDisplayable, MojangErrorCode } = require('helios-core/mojang')
  15. const { RestResponseStatus } = require('helios-core/common')
  16. const log = LoggerUtil.getLogger('AuthManager')
  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 response = await MojangRestAPI.authenticate(username, password, ConfigManager.getClientToken())
  30. console.log(response)
  31. if(response.responseStatus === RestResponseStatus.SUCCESS) {
  32. const session = response.data
  33. if(session.selectedProfile != null){
  34. const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
  35. if(ConfigManager.getClientToken() == null){
  36. ConfigManager.setClientToken(session.clientToken)
  37. }
  38. ConfigManager.save()
  39. return ret
  40. } else {
  41. return Promise.reject(mojangErrorDisplayable(MojangErrorCode.ERROR_NOT_PAID))
  42. }
  43. } else {
  44. return Promise.reject(mojangErrorDisplayable(response.mojangErrorCode))
  45. }
  46. } catch (err){
  47. log.error(err)
  48. return Promise.reject(mojangErrorDisplayable(MojangErrorCode.UNKNOWN))
  49. }
  50. }
  51. /**
  52. * Remove an account. This will invalidate the access token associated
  53. * with the account and then remove it from the database.
  54. *
  55. * @param {string} uuid The UUID of the account to be removed.
  56. * @returns {Promise.<void>} Promise which resolves to void when the action is complete.
  57. */
  58. exports.removeAccount = async function(uuid){
  59. try {
  60. const authAcc = ConfigManager.getAuthAccount(uuid)
  61. const response = await MojangRestAPI.invalidate(authAcc.accessToken, ConfigManager.getClientToken())
  62. if(response.responseStatus === RestResponseStatus.SUCCESS) {
  63. ConfigManager.removeAuthAccount(uuid)
  64. ConfigManager.save()
  65. return Promise.resolve()
  66. } else {
  67. log.error('Error while removing account', response.error)
  68. return Promise.reject(response.error)
  69. }
  70. } catch (err){
  71. log.error('Error while removing account', err)
  72. return Promise.reject(err)
  73. }
  74. }
  75. /**
  76. * Validate the selected account with Mojang's authserver. If the account is not valid,
  77. * we will attempt to refresh the access token and update that value. If that fails, a
  78. * new login will be required.
  79. *
  80. * **Function is WIP**
  81. *
  82. * @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
  83. * otherwise false.
  84. */
  85. exports.validateSelected = async function(){
  86. const current = ConfigManager.getSelectedAccount()
  87. const response = await MojangRestAPI.validate(current.accessToken, ConfigManager.getClientToken())
  88. if(response.responseStatus === RestResponseStatus.SUCCESS) {
  89. const isValid = response.data
  90. if(!isValid){
  91. const refreshResponse = await MojangRestAPI.refresh(current.accessToken, ConfigManager.getClientToken())
  92. if(refreshResponse.responseStatus === RestResponseStatus.SUCCESS) {
  93. const session = refreshResponse.data
  94. ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
  95. ConfigManager.save()
  96. } else {
  97. log.error('Error while validating selected profile:', refreshResponse.error)
  98. log.info('Account access token is invalid.')
  99. return false
  100. }
  101. log.info('Account access token validated.')
  102. return true
  103. } else {
  104. log.info('Account access token validated.')
  105. return true
  106. }
  107. }
  108. }