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
|
2022-02-11 16:51:28 -08:00
|
|
|
const ConfigManager = require('./configmanager')
|
|
|
|
const { LoggerUtil } = require('helios-core')
|
2022-02-06 15:23:44 -08:00
|
|
|
const { RestResponseStatus } = require('helios-core/common')
|
2022-02-11 16:51:28 -08:00
|
|
|
const { MojangRestAPI, mojangErrorDisplayable, MojangErrorCode } = require('helios-core/mojang')
|
|
|
|
const { MicrosoftAuth, microsoftErrorDisplayable, MicrosoftErrorCode } = require('helios-core/microsoft')
|
|
|
|
const { AZURE_CLIENT_ID } = require('./ipcconstants')
|
2022-02-06 15:23:44 -08:00
|
|
|
|
|
|
|
const log = LoggerUtil.getLogger('AuthManager')
|
2017-12-03 05:12:55 -08:00
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
// Functions
|
|
|
|
|
|
|
|
/**
|
2022-02-11 16:51:28 -08:00
|
|
|
* Add a Mojang account. This will authenticate the given credentials with Mojang's
|
2018-04-14 19:43:58 -07:00
|
|
|
* 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
|
|
|
*/
|
2022-02-11 16:51:28 -08:00
|
|
|
exports.addMojangAccount = async function(username, password) {
|
2018-05-07 15:15:59 -07:00
|
|
|
try {
|
2022-02-06 15:23:44 -08:00
|
|
|
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){
|
2022-02-11 16:51:28 -08:00
|
|
|
const ret = ConfigManager.addMojangAuthAccount(session.selectedProfile.id, session.accessToken, username, session.selectedProfile.name)
|
2022-02-06 15:23:44 -08:00
|
|
|
if(ConfigManager.getClientToken() == null){
|
|
|
|
ConfigManager.setClientToken(session.clientToken)
|
|
|
|
}
|
|
|
|
ConfigManager.save()
|
|
|
|
return ret
|
|
|
|
} else {
|
|
|
|
return Promise.reject(mojangErrorDisplayable(MojangErrorCode.ERROR_NOT_PAID))
|
2019-02-05 14:26:00 -08:00
|
|
|
}
|
2022-02-06 15:23:44 -08:00
|
|
|
|
2019-02-05 14:26:00 -08:00
|
|
|
} else {
|
2022-02-06 15:23:44 -08:00
|
|
|
return Promise.reject(mojangErrorDisplayable(response.mojangErrorCode))
|
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){
|
2022-02-06 15:23:44 -08:00
|
|
|
log.error(err)
|
|
|
|
return Promise.reject(mojangErrorDisplayable(MojangErrorCode.UNKNOWN))
|
2018-01-28 22:23:20 -08:00
|
|
|
}
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:28 -08:00
|
|
|
const AUTH_MODE = { FULL: 0, MS_REFRESH: 1, MC_REFRESH: 2 }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the full MS Auth flow in a given mode.
|
|
|
|
*
|
|
|
|
* AUTH_MODE.FULL = Full authorization for a new account.
|
|
|
|
* AUTH_MODE.MS_REFRESH = Full refresh authorization.
|
|
|
|
* AUTH_MODE.MC_REFRESH = Refresh of the MC token, reusing the MS token.
|
|
|
|
*
|
|
|
|
* @param {string} entryCode FULL-AuthCode. MS_REFRESH=refreshToken, MC_REFRESH=accessToken
|
|
|
|
* @param {*} authMode The auth mode.
|
|
|
|
* @returns An object with all auth data. AccessToken object will be null when mode is MC_REFRESH.
|
|
|
|
*/
|
|
|
|
async function fullMicrosoftAuthFlow(entryCode, authMode) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
let accessTokenRaw
|
|
|
|
let accessToken
|
|
|
|
if(authMode !== AUTH_MODE.MC_REFRESH) {
|
|
|
|
const accessTokenResponse = await MicrosoftAuth.getAccessToken(entryCode, authMode === AUTH_MODE.MS_REFRESH, AZURE_CLIENT_ID)
|
|
|
|
if(accessTokenResponse.responseStatus === RestResponseStatus.ERROR) {
|
|
|
|
return Promise.reject(microsoftErrorDisplayable(accessTokenResponse.microsoftErrorCode))
|
|
|
|
}
|
|
|
|
accessToken = accessTokenResponse.data
|
|
|
|
accessTokenRaw = accessToken.access_token
|
|
|
|
} else {
|
|
|
|
accessTokenRaw = entryCode
|
|
|
|
}
|
|
|
|
|
|
|
|
const xblResponse = await MicrosoftAuth.getXBLToken(accessTokenRaw)
|
|
|
|
if(xblResponse.responseStatus === RestResponseStatus.ERROR) {
|
|
|
|
return Promise.reject(microsoftErrorDisplayable(xblResponse.microsoftErrorCode))
|
|
|
|
}
|
|
|
|
const xstsResonse = await MicrosoftAuth.getXSTSToken(xblResponse.data)
|
|
|
|
if(xstsResonse.responseStatus === RestResponseStatus.ERROR) {
|
|
|
|
return Promise.reject(microsoftErrorDisplayable(xstsResonse.microsoftErrorCode))
|
|
|
|
}
|
|
|
|
const mcTokenResponse = await MicrosoftAuth.getMCAccessToken(xstsResonse.data)
|
|
|
|
if(mcTokenResponse.responseStatus === RestResponseStatus.ERROR) {
|
|
|
|
return Promise.reject(microsoftErrorDisplayable(mcTokenResponse.microsoftErrorCode))
|
|
|
|
}
|
|
|
|
const mcProfileResponse = await MicrosoftAuth.getMCProfile(mcTokenResponse.data.access_token)
|
|
|
|
if(mcProfileResponse.responseStatus === RestResponseStatus.ERROR) {
|
|
|
|
return Promise.reject(microsoftErrorDisplayable(mcProfileResponse.microsoftErrorCode))
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
accessToken,
|
|
|
|
accessTokenRaw,
|
|
|
|
xbl: xblResponse.data,
|
|
|
|
xsts: xstsResonse.data,
|
|
|
|
mcToken: mcTokenResponse.data,
|
|
|
|
mcProfile: mcProfileResponse.data
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
log.error(err)
|
|
|
|
return Promise.reject(microsoftErrorDisplayable(MicrosoftErrorCode.UNKNOWN))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
/**
|
2022-02-11 16:51:28 -08:00
|
|
|
* Calculate the expiry date. Advance the expiry time by 10 seconds
|
|
|
|
* to reduce the liklihood of working with an expired token.
|
|
|
|
*
|
|
|
|
* @param {number} nowMs Current time milliseconds.
|
|
|
|
* @param {number} epiresInS Expires in (seconds)
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-02-15 06:09:26 -08:00
|
|
|
function calculateExpiryDate(nowMs, epiresInS) {
|
2022-02-11 16:51:28 -08:00
|
|
|
return nowMs + ((epiresInS-10)*1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a Microsoft account. This will pass the provided auth code to Mojang's OAuth2.0 flow.
|
|
|
|
* The resultant data will be stored as an auth account in the configuration database.
|
|
|
|
*
|
|
|
|
* @param {string} authCode The authCode obtained from microsoft.
|
|
|
|
* @returns {Promise.<Object>} Promise which resolves the resolved authenticated account object.
|
|
|
|
*/
|
|
|
|
exports.addMicrosoftAccount = async function(authCode) {
|
|
|
|
|
|
|
|
const fullAuth = await fullMicrosoftAuthFlow(authCode, AUTH_MODE.FULL)
|
|
|
|
|
|
|
|
// Advance expiry by 10 seconds to avoid close calls.
|
|
|
|
const now = new Date().getTime()
|
|
|
|
|
|
|
|
const ret = ConfigManager.addMicrosoftAuthAccount(
|
|
|
|
fullAuth.mcProfile.id,
|
|
|
|
fullAuth.mcToken.access_token,
|
|
|
|
fullAuth.mcProfile.name,
|
|
|
|
calculateExpiryDate(now, fullAuth.mcToken.expires_in),
|
|
|
|
fullAuth.accessToken.access_token,
|
|
|
|
fullAuth.accessToken.refresh_token,
|
|
|
|
calculateExpiryDate(now, fullAuth.accessToken.expires_in)
|
|
|
|
)
|
|
|
|
ConfigManager.save()
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a Mojang account. This will invalidate the access token associated
|
2018-04-14 19:43:58 -07:00
|
|
|
* 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.
|
|
|
|
*/
|
2022-02-11 16:51:28 -08:00
|
|
|
exports.removeMojangAccount = async function(uuid){
|
2018-04-14 19:43:58 -07:00
|
|
|
try {
|
|
|
|
const authAcc = ConfigManager.getAuthAccount(uuid)
|
2022-02-06 15:23:44 -08:00
|
|
|
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)
|
|
|
|
}
|
2018-04-14 19:43:58 -07:00
|
|
|
} catch (err){
|
2022-02-06 15:23:44 -08:00
|
|
|
log.error('Error while removing account', err)
|
2018-04-14 19:43:58 -07:00
|
|
|
return Promise.reject(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:51:28 -08:00
|
|
|
/**
|
|
|
|
* Remove a Microsoft account. It is expected that the caller will invoke the OAuth logout
|
|
|
|
* through the ipc renderer.
|
|
|
|
*
|
|
|
|
* @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.removeMicrosoftAccount = async function(uuid){
|
|
|
|
try {
|
|
|
|
ConfigManager.removeAuthAccount(uuid)
|
|
|
|
ConfigManager.save()
|
|
|
|
return Promise.resolve()
|
|
|
|
} catch (err){
|
|
|
|
log.error('Error while removing account', err)
|
|
|
|
return Promise.reject(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:43:58 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
|
|
|
|
* otherwise false.
|
|
|
|
*/
|
2022-02-11 16:51:28 -08:00
|
|
|
async function validateSelectedMojangAccount(){
|
2018-01-18 20:45:50 -08:00
|
|
|
const current = ConfigManager.getSelectedAccount()
|
2022-02-06 15:23:44 -08:00
|
|
|
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
|
2022-02-11 16:51:28 -08:00
|
|
|
ConfigManager.updateMojangAuthAccount(current.uuid, session.accessToken)
|
2022-02-06 15:23:44 -08:00
|
|
|
ConfigManager.save()
|
|
|
|
} else {
|
|
|
|
log.error('Error while validating selected profile:', refreshResponse.error)
|
|
|
|
log.info('Account access token is invalid.')
|
|
|
|
return false
|
2018-01-18 20:45:50 -08:00
|
|
|
}
|
2022-02-06 15:23:44 -08:00
|
|
|
log.info('Account access token validated.')
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
log.info('Account access token validated.')
|
|
|
|
return true
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|
2018-01-18 20:45:50 -08:00
|
|
|
}
|
2022-02-06 15:23:44 -08:00
|
|
|
|
2022-02-11 16:51:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the selected account with Microsoft'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.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
|
|
|
|
* otherwise false.
|
|
|
|
*/
|
|
|
|
async function validateSelectedMicrosoftAccount(){
|
|
|
|
const current = ConfigManager.getSelectedAccount()
|
|
|
|
const now = new Date().getTime()
|
|
|
|
const mcExpiresAt = Date.parse(current.expiresAt)
|
|
|
|
const mcExpired = now >= mcExpiresAt
|
|
|
|
|
|
|
|
if(!mcExpired) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MC token expired. Check MS token.
|
|
|
|
|
|
|
|
const msExpiresAt = Date.parse(current.microsoft.expires_at)
|
|
|
|
const msExpired = now >= msExpiresAt
|
|
|
|
|
|
|
|
if(msExpired) {
|
|
|
|
// MS expired, do full refresh.
|
|
|
|
try {
|
|
|
|
const res = await fullMicrosoftAuthFlow(current.microsoft.refresh_token, AUTH_MODE.MS_REFRESH)
|
|
|
|
|
|
|
|
ConfigManager.updateMicrosoftAuthAccount(
|
|
|
|
current.uuid,
|
|
|
|
res.mcToken.access_token,
|
|
|
|
res.accessToken.access_token,
|
|
|
|
res.accessToken.refresh_token,
|
|
|
|
calculateExpiryDate(now, res.accessToken.expires_in),
|
|
|
|
calculateExpiryDate(now, res.mcToken.expires_in)
|
|
|
|
)
|
|
|
|
ConfigManager.save()
|
|
|
|
return true
|
|
|
|
} catch(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Only MC expired, use existing MS token.
|
|
|
|
try {
|
|
|
|
const res = await fullMicrosoftAuthFlow(current.microsoft.access_token, AUTH_MODE.MC_REFRESH)
|
|
|
|
|
|
|
|
ConfigManager.updateMicrosoftAuthAccount(
|
|
|
|
current.uuid,
|
|
|
|
res.mcToken.access_token,
|
|
|
|
current.microsoft.access_token,
|
|
|
|
current.microsoft.refresh_token,
|
|
|
|
current.microsoft.expires_at,
|
|
|
|
calculateExpiryDate(now, res.mcToken.expires_in)
|
|
|
|
)
|
|
|
|
ConfigManager.save()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the selected auth account.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<boolean>} Promise which resolves to true if the access token is valid,
|
|
|
|
* otherwise false.
|
|
|
|
*/
|
|
|
|
exports.validateSelected = async function(){
|
|
|
|
const current = ConfigManager.getSelectedAccount()
|
|
|
|
|
|
|
|
if(current.type === 'microsoft') {
|
|
|
|
return await validateSelectedMicrosoftAccount()
|
|
|
|
} else {
|
|
|
|
return await validateSelectedMojangAccount()
|
|
|
|
}
|
|
|
|
|
2017-12-03 05:12:55 -08:00
|
|
|
}
|