| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- import { LoggerUtil } from '../logging/loggerutil'
- import { Agent } from './model/auth/Agent'
- import { Status, StatusColor } from './model/internal/Status'
- import got, { RequestError, HTTPError, TimeoutError, GotError, ParseError } from 'got'
- import { Session } from './model/auth/Session'
- import { AuthPayload } from './model/auth/AuthPayload'
- import { MojangResponse, MojangResponseCode, deciperResponseCode, isInternalError, MojangErrorBody } from './model/internal/Response'
- export class Mojang {
- private static readonly logger = LoggerUtil.getLogger('Mojang')
- private static readonly TIMEOUT = 2500
- public static readonly AUTH_ENDPOINT = 'https://authserver.mojang.com'
- public static readonly STATUS_ENDPOINT = 'https://status.mojang.com'
- private static authClient = got.extend({
- prefixUrl: Mojang.AUTH_ENDPOINT,
- responseType: 'json',
- retry: 0
- })
- private static statusClient = got.extend({
- prefixUrl: Mojang.STATUS_ENDPOINT,
- responseType: 'json',
- retry: 0
- })
- public static readonly MINECRAFT_AGENT: Agent = {
- name: 'Minecraft',
- version: 1
- }
- protected static statuses: Status[] = [
- {
- service: 'sessionserver.mojang.com',
- status: StatusColor.GREY,
- name: 'Multiplayer Session Service',
- essential: true
- },
- {
- service: 'authserver.mojang.com',
- status: StatusColor.GREY,
- name: 'Authentication Service',
- essential: true
- },
- {
- service: 'textures.minecraft.net',
- status: StatusColor.GREY,
- name: 'Minecraft Skins',
- essential: false
- },
- {
- service: 'api.mojang.com',
- status: StatusColor.GREY,
- name: 'Public API',
- essential: false
- },
- {
- service: 'minecraft.net',
- status: StatusColor.GREY,
- name: 'Minecraft.net',
- essential: false
- },
- {
- service: 'account.mojang.com',
- status: StatusColor.GREY,
- name: 'Mojang Accounts Website',
- essential: false
- }
- ]
- /**
- * Converts a Mojang status color to a hex value. Valid statuses
- * are 'green', 'yellow', 'red', and 'grey'. Grey is a custom status
- * to our project which represents an unknown status.
- */
- public static statusToHex(status: string){
- switch(status.toLowerCase()){
- case StatusColor.GREEN:
- return '#a5c325'
- case StatusColor.YELLOW:
- return '#eac918'
- case StatusColor.RED:
- return '#c32625'
- case StatusColor.GREY:
- default:
- return '#848484'
- }
- }
- private static handleGotError<T>(operation: string, error: GotError, dataProvider: () => T): MojangResponse<T> {
- const response: MojangResponse<T> = {
- data: dataProvider(),
- responseCode: MojangResponseCode.ERROR,
- error
- }
-
- if(error instanceof HTTPError) {
- response.responseCode = deciperResponseCode(error.response.body as MojangErrorBody)
- Mojang.logger.error(`Error during ${operation} request (HTTP Response ${error.response.statusCode})`, error)
- Mojang.logger.debug('Response Details:')
- Mojang.logger.debug('Body:', error.response.body)
- Mojang.logger.debug('Headers:', error.response.headers)
- } else if(error instanceof RequestError) {
- Mojang.logger.error(`${operation} request recieved no response (${error.code}).`, error)
- } else if(error instanceof TimeoutError) {
- Mojang.logger.error(`${operation} request timed out (${error.timings.phases.total}ms).`)
- } else if(error instanceof ParseError) {
- Mojang.logger.error(`${operation} request recieved unexepected body (Parse Error).`)
- } else {
- // CacheError, ReadError, MaxRedirectsError, UnsupportedProtocolError, CancelError
- Mojang.logger.error(`Error during ${operation} request.`, error)
- }
- response.isInternalError = isInternalError(response.responseCode)
- return response
- }
- private static expectSpecificSuccess(operation: string, expected: number, actual: number) {
- if(actual !== expected) {
- Mojang.logger.warn(`${operation} expected ${expected} response, recieved ${actual}.`)
- }
- }
- /**
- * Retrieves the status of Mojang's services.
- * The response is condensed into a single object. Each service is
- * a key, where the value is an object containing a status and name
- * property.
- *
- * @see http://wiki.vg/Mojang_API#API_Status
- */
- public static async status(): Promise<MojangResponse<Status[]>>{
- try {
- const res = await Mojang.statusClient.get<{[service: string]: StatusColor}[]>('check')
- Mojang.expectSpecificSuccess('Mojang Status', 200, res.statusCode)
- res.body.forEach(status => {
- const entry = Object.entries(status)[0]
- for(let i=0; i<Mojang.statuses.length; i++) {
- if(Mojang.statuses[i].service === entry[0]) {
- Mojang.statuses[i].status = entry[1]
- break
- }
- }
- })
- return {
- data: Mojang.statuses,
- responseCode: MojangResponseCode.SUCCESS
- }
- } catch(error) {
- return Mojang.handleGotError('Mojang Status', error as GotError, () => {
- for(let i=0; i<Mojang.statuses.length; i++){
- Mojang.statuses[i].status = StatusColor.GREY
- }
- return Mojang.statuses
- })
- }
-
- }
- /**
- * Authenticate a user with their Mojang credentials.
- *
- * @param {string} username The user's username, this is often an email.
- * @param {string} password The user's password.
- * @param {string} clientToken The launcher's Client Token.
- * @param {boolean} requestUser Optional. Adds user object to the reponse.
- * @param {Object} agent Optional. Provided by default. Adds user info to the response.
- *
- * @see http://wiki.vg/Authentication#Authenticate
- */
- public static async authenticate(
- username: string,
- password: string,
- clientToken: string | null,
- requestUser: boolean = true,
- agent: Agent = Mojang.MINECRAFT_AGENT
- ): Promise<MojangResponse<Session | null>> {
- try {
- const json: AuthPayload = {
- agent,
- username,
- password,
- requestUser
- }
- if(clientToken != null){
- json.clientToken = clientToken
- }
- const res = await Mojang.authClient.post<Session>('authenticate', { json })
- Mojang.expectSpecificSuccess('Mojang Authenticate', 200, res.statusCode)
- return {
- data: res.body,
- responseCode: MojangResponseCode.SUCCESS
- }
- } catch(err) {
- return Mojang.handleGotError('Mojang Authenticate', err as GotError, () => null)
- }
- }
- /**
- * Validate an access token. This should always be done before launching.
- * The client token should match the one used to create the access token.
- *
- * @param {string} accessToken The access token to validate.
- * @param {string} clientToken The launcher's client token.
- *
- * @see http://wiki.vg/Authentication#Validate
- */
- public static async validate(accessToken: string, clientToken: string): Promise<MojangResponse<boolean>> {
- try {
- const json = {
- accessToken,
- clientToken
- }
- const res = await Mojang.authClient.post('validate', { json })
- Mojang.expectSpecificSuccess('Mojang Validate', 204, res.statusCode)
- return {
- data: res.statusCode === 204,
- responseCode: MojangResponseCode.SUCCESS
- }
- } catch(err) {
- if(err instanceof HTTPError && err.response.statusCode === 403) {
- return {
- data: false,
- responseCode: MojangResponseCode.SUCCESS
- }
- }
- return Mojang.handleGotError('Mojang Validate', err as GotError, () => false)
- }
- }
- /**
- * Invalidates an access token. The clientToken must match the
- * token used to create the provided accessToken.
- *
- * @param {string} accessToken The access token to invalidate.
- * @param {string} clientToken The launcher's client token.
- *
- * @see http://wiki.vg/Authentication#Invalidate
- */
- public static async invalidate(accessToken: string, clientToken: string): Promise<MojangResponse<undefined>> {
- try {
- const json = {
- accessToken,
- clientToken
- }
- const res = await Mojang.authClient.post('invalidate', { json })
- Mojang.expectSpecificSuccess('Mojang Invalidate', 204, res.statusCode)
- return {
- data: undefined,
- responseCode: MojangResponseCode.SUCCESS
- }
- } catch(err) {
- return Mojang.handleGotError('Mojang Invalidate', err as GotError, () => undefined)
- }
- }
- /**
- * Refresh a user's authentication. This should be used to keep a user logged
- * in without asking them for their credentials again. A new access token will
- * be generated using a recent invalid access token. See Wiki for more info.
- *
- * @param {string} accessToken The old access token.
- * @param {string} clientToken The launcher's client token.
- * @param {boolean} requestUser Optional. Adds user object to the reponse.
- *
- * @see http://wiki.vg/Authentication#Refresh
- */
- public static async refresh(accessToken: string, clientToken: string, requestUser: boolean = true): Promise<MojangResponse<Session | null>> {
- try {
- const json = {
- accessToken,
- clientToken,
- requestUser
- }
- const res = await Mojang.authClient.post<Session>('refresh', { json })
- Mojang.expectSpecificSuccess('Mojang Refresh', 200, res.statusCode)
- return {
- data: res.body,
- responseCode: MojangResponseCode.SUCCESS
- }
- } catch(err) {
- return Mojang.handleGotError('Mojang Refresh', err as GotError, () => null)
- }
- }
- }
|