| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /**
- * AuthManager
- *
- * This module aims to abstract login procedures. Results from Mojang's REST api
- * are retrieved through our Mojang module. These results are processed and stored,
- * if applicable, in the config using the ConfigManager. All login procedures should
- * be made through this module.
- *
- * @module authmanager
- */
- // Requirements
- const ConfigManager = require('./configmanager')
- const { LoggerUtil } = require('helios-core')
- const { MojangRestAPI, mojangErrorDisplayable, MojangErrorCode } = require('helios-core/mojang')
- const { RestResponseStatus } = require('helios-core/common')
- const log = LoggerUtil.getLogger('AuthManager')
- // Functions
- /**
- * Add an account. This will authenticate the given credentials with Mojang's
- * authserver. The resultant data will be stored as an auth account in the
- * configuration database.
- *
- * @param {string} username The account username (email if migrated).
- * @param {string} password The account password.
- * @returns {Promise.<Object>} Promise which resolves the resolved authenticated account object.
- */
- exports.addAccount = async function(username, password){
- try {
- const response = await MojangRestAPI.authenticate(username, password, ConfigManager.getClientToken())
- console.log(response)
- if(response.responseStatus === RestResponseStatus.SUCCESS) {
- const session = response.data
- if(session.selectedProfile != null){
- const ret = ConfigManager.addAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
- if(ConfigManager.getClientToken() == null){
- ConfigManager.setClientToken(session.clientToken)
- }
- ConfigManager.save()
- return ret
- } else {
- return Promise.reject(mojangErrorDisplayable(MojangErrorCode.ERROR_NOT_PAID))
- }
- } else {
- return Promise.reject(mojangErrorDisplayable(response.mojangErrorCode))
- }
-
- } catch (err){
- log.error(err)
- return Promise.reject(mojangErrorDisplayable(MojangErrorCode.UNKNOWN))
- }
- }
- /**
- * Remove an account. This will invalidate the access token associated
- * with the account and then remove it from the database.
- *
- * @param {string} uuid The UUID of the account to be removed.
- * @returns {Promise.<void>} Promise which resolves to void when the action is complete.
- */
- exports.removeAccount = async function(uuid){
- try {
- const authAcc = ConfigManager.getAuthAccount(uuid)
- const response = await MojangRestAPI.invalidate(authAcc.accessToken, ConfigManager.getClientToken())
- if(response.responseStatus === RestResponseStatus.SUCCESS) {
- ConfigManager.removeAuthAccount(uuid)
- ConfigManager.save()
- return Promise.resolve()
- } else {
- log.error('Error while removing account', response.error)
- return Promise.reject(response.error)
- }
- } catch (err){
- log.error('Error while removing account', err)
- return Promise.reject(err)
- }
- }
- /**
- * Validate the selected account with Mojang's authserver. If the account is not valid,
- * we will attempt to refresh the access token and update that value. If that fails, a
- * new login will be required.
- *
- * **Function is WIP**
- *
- * @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
- * otherwise false.
- */
- exports.validateSelected = async function(){
- const current = ConfigManager.getSelectedAccount()
- const response = await MojangRestAPI.validate(current.accessToken, ConfigManager.getClientToken())
- if(response.responseStatus === RestResponseStatus.SUCCESS) {
- const isValid = response.data
- if(!isValid){
- const refreshResponse = await MojangRestAPI.refresh(current.accessToken, ConfigManager.getClientToken())
- if(refreshResponse.responseStatus === RestResponseStatus.SUCCESS) {
- const session = refreshResponse.data
- ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
- ConfigManager.save()
- } else {
- log.error('Error while validating selected profile:', refreshResponse.error)
- log.info('Account access token is invalid.')
- return false
- }
- log.info('Account access token validated.')
- return true
- } else {
- log.info('Account access token validated.')
- return true
- }
- }
-
- }
|