2018-04-14 19:43:58 -07:00
|
|
|
/**
|
|
|
|
* 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
|
2018-07-22 08:40:15 -07:00
|
|
|
const ConfigManager = require('./configmanager')
|
2018-08-22 11:21:49 -07:00
|
|
|
const LoggerUtil = require('./loggerutil')
|
2018-07-22 08:40:15 -07:00
|
|
|
const Mojang = require('./mojang')
|
2018-08-22 11:21:49 -07:00
|
|
|
const logger = LoggerUtil('%c[AuthManager]', 'color: #a02d2a; font-weight: bold')
|
|
|
|
const loggerSuccess = LoggerUtil('%c[AuthManager]', 'color: #209b07; font-weight: bold')
|
2017-12-03 05:12:55 -08:00
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
// 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.
|
2018-04-29 15:05:59 -07:00
|
|
|
* @returns {Promise.<Object>} Promise which resolves the resolved authenticated account object.
|
2018-04-14 19:43:58 -07:00
|
|
|
*/
|
2018-01-18 20:45:50 -08:00
|
|
|
exports.addAccount = async function(username, password){
|
2018-05-07 15:15:59 -07:00
|
|
|
try {
|
2018-05-29 18:47:55 -07:00
|
|
|
const session = await Mojang.authenticate(username, password, ConfigManager.getClientToken())
|
2019-02-05 14:26:00 -08:00
|
|
|
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 {
|
|
|
|
throw new Error('NotPaidAccount')
|
2018-11-04 00:03:55 -07:00
|
|
|
}
|
2019-02-05 14:26:00 -08:00
|
|
|
|
2018-01-28 22:23:20 -08:00
|
|
|
} catch (err){
|
|
|
|
return Promise.reject(err)
|
|
|
|
}
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
await Mojang.invalidate(authAcc.accessToken, ConfigManager.getClientToken())
|
|
|
|
ConfigManager.removeAuthAccount(uuid)
|
|
|
|
ConfigManager.save()
|
|
|
|
return Promise.resolve()
|
|
|
|
} catch (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.
|
|
|
|
*/
|
2018-01-18 20:45:50 -08:00
|
|
|
exports.validateSelected = async function(){
|
|
|
|
const current = ConfigManager.getSelectedAccount()
|
|
|
|
const isValid = await Mojang.validate(current.accessToken, ConfigManager.getClientToken())
|
|
|
|
if(!isValid){
|
|
|
|
try {
|
|
|
|
const session = await Mojang.refresh(current.accessToken, ConfigManager.getClientToken())
|
|
|
|
ConfigManager.updateAuthAccount(current.uuid, session.accessToken)
|
2017-12-03 05:12:55 -08:00
|
|
|
ConfigManager.save()
|
2018-01-18 20:45:50 -08:00
|
|
|
} catch(err) {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.debug('Error while validating selected profile:', err)
|
2018-04-28 01:16:09 -07:00
|
|
|
if(err && err.error === 'ForbiddenOperationException'){
|
|
|
|
// What do we do?
|
2018-01-18 20:45:50 -08:00
|
|
|
}
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Account access token is invalid.')
|
2018-04-28 01:16:09 -07:00
|
|
|
return false
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|
2018-08-22 11:21:49 -07:00
|
|
|
loggerSuccess.log('Account access token validated.')
|
2018-01-18 20:45:50 -08:00
|
|
|
return true
|
|
|
|
} else {
|
2018-08-22 11:21:49 -07:00
|
|
|
loggerSuccess.log('Account access token validated.')
|
2018-01-18 20:45:50 -08:00
|
|
|
return true
|
|
|
|
}
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|