mojang.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import request from 'request'
  2. import { LoggerUtil } from '../loggerutil'
  3. import { Agent } from '../model/mojang/auth/Agent'
  4. import { AuthPayload } from '../model/mojang/auth/AuthPayload'
  5. import { Session } from '../model/mojang/auth/Session'
  6. import { Status } from './type/Status'
  7. export class Mojang {
  8. private static readonly logger = new LoggerUtil('%c[Mojang]', 'color: #a02d2a; font-weight: bold')
  9. public static readonly AUTH_ENDPOINT = 'https://authserver.mojang.com'
  10. public static readonly MINECRAFT_AGENT: Agent = {
  11. name: 'Minecraft',
  12. version: 1
  13. }
  14. protected static statuses: Status[] = [
  15. {
  16. service: 'sessionserver.mojang.com',
  17. status: 'grey',
  18. name: 'Multiplayer Session Service',
  19. essential: true
  20. },
  21. {
  22. service: 'authserver.mojang.com',
  23. status: 'grey',
  24. name: 'Authentication Service',
  25. essential: true
  26. },
  27. {
  28. service: 'textures.minecraft.net',
  29. status: 'grey',
  30. name: 'Minecraft Skins',
  31. essential: false
  32. },
  33. {
  34. service: 'api.mojang.com',
  35. status: 'grey',
  36. name: 'Public API',
  37. essential: false
  38. },
  39. {
  40. service: 'minecraft.net',
  41. status: 'grey',
  42. name: 'Minecraft.net',
  43. essential: false
  44. },
  45. {
  46. service: 'account.mojang.com',
  47. status: 'grey',
  48. name: 'Mojang Accounts Website',
  49. essential: false
  50. }
  51. ]
  52. /**
  53. * Converts a Mojang status color to a hex value. Valid statuses
  54. * are 'green', 'yellow', 'red', and 'grey'. Grey is a custom status
  55. * to our project which represents an unknown status.
  56. *
  57. * @param {string} status A valid status code.
  58. * @returns {string} The hex color of the status code.
  59. */
  60. public static statusToHex(status: string){
  61. switch(status.toLowerCase()){
  62. case 'green':
  63. return '#a5c325'
  64. case 'yellow':
  65. return '#eac918'
  66. case 'red':
  67. return '#c32625'
  68. case 'grey':
  69. default:
  70. return '#848484'
  71. }
  72. }
  73. /**
  74. * Retrieves the status of Mojang's services.
  75. * The response is condensed into a single object. Each service is
  76. * a key, where the value is an object containing a status and name
  77. * property.
  78. *
  79. * @see http://wiki.vg/Mojang_API#API_Status
  80. */
  81. public static status(): Promise<Status[]>{
  82. return new Promise((resolve, reject) => {
  83. request.get('https://status.mojang.com/check',
  84. {
  85. json: true,
  86. timeout: 2500
  87. },
  88. function(error, response, body: {[service: string]: 'red' | 'yellow' | 'green'}[]){
  89. if(error || response.statusCode !== 200){
  90. Mojang.logger.warn('Unable to retrieve Mojang status.')
  91. Mojang.logger.debug('Error while retrieving Mojang statuses:', error)
  92. //reject(error || response.statusCode)
  93. for(let i=0; i<Mojang.statuses.length; i++){
  94. Mojang.statuses[i].status = 'grey'
  95. }
  96. resolve(Mojang.statuses)
  97. } else {
  98. for(let i=0; i<body.length; i++){
  99. const key = Object.keys(body[i])[0]
  100. inner:
  101. for(let j=0; j<Mojang.statuses.length; j++){
  102. if(Mojang.statuses[j].service === key) {
  103. Mojang.statuses[j].status = body[i][key]
  104. break inner
  105. }
  106. }
  107. }
  108. resolve(Mojang.statuses)
  109. }
  110. })
  111. })
  112. }
  113. /**
  114. * Authenticate a user with their Mojang credentials.
  115. *
  116. * @param {string} username The user's username, this is often an email.
  117. * @param {string} password The user's password.
  118. * @param {string} clientToken The launcher's Client Token.
  119. * @param {boolean} requestUser Optional. Adds user object to the reponse.
  120. * @param {Object} agent Optional. Provided by default. Adds user info to the response.
  121. *
  122. * @see http://wiki.vg/Authentication#Authenticate
  123. */
  124. public static authenticate(
  125. username: string,
  126. password: string,
  127. clientToken: string | null,
  128. requestUser: boolean = true,
  129. agent: Agent = Mojang.MINECRAFT_AGENT
  130. ): Promise<Session> {
  131. return new Promise((resolve, reject) => {
  132. const body: AuthPayload = {
  133. agent,
  134. username,
  135. password,
  136. requestUser
  137. }
  138. if(clientToken != null){
  139. body.clientToken = clientToken
  140. }
  141. request.post(Mojang.AUTH_ENDPOINT + '/authenticate',
  142. {
  143. json: true,
  144. body
  145. },
  146. function(error, response, body){
  147. if(error){
  148. Mojang.logger.error('Error during authentication.', error)
  149. reject(error)
  150. } else {
  151. if(response.statusCode === 200){
  152. resolve(body)
  153. } else {
  154. reject(body || {code: 'ENOTFOUND'})
  155. }
  156. }
  157. })
  158. })
  159. }
  160. /**
  161. * Validate an access token. This should always be done before launching.
  162. * The client token should match the one used to create the access token.
  163. *
  164. * @param {string} accessToken The access token to validate.
  165. * @param {string} clientToken The launcher's client token.
  166. *
  167. * @see http://wiki.vg/Authentication#Validate
  168. */
  169. public static validate(accessToken: string, clientToken: string): Promise<boolean> {
  170. return new Promise((resolve, reject) => {
  171. request.post(Mojang.AUTH_ENDPOINT + '/validate',
  172. {
  173. json: true,
  174. body: {
  175. accessToken,
  176. clientToken
  177. }
  178. },
  179. function(error, response, body){
  180. if(error){
  181. Mojang.logger.error('Error during validation.', error)
  182. reject(error)
  183. } else {
  184. if(response.statusCode === 403){
  185. resolve(false)
  186. } else {
  187. // 204 if valid
  188. resolve(true)
  189. }
  190. }
  191. })
  192. })
  193. }
  194. /**
  195. * Invalidates an access token. The clientToken must match the
  196. * token used to create the provided accessToken.
  197. *
  198. * @param {string} accessToken The access token to invalidate.
  199. * @param {string} clientToken The launcher's client token.
  200. *
  201. * @see http://wiki.vg/Authentication#Invalidate
  202. */
  203. public static invalidate(accessToken: string, clientToken: string): Promise<void>{
  204. return new Promise((resolve, reject) => {
  205. request.post(Mojang.AUTH_ENDPOINT + '/invalidate',
  206. {
  207. json: true,
  208. body: {
  209. accessToken,
  210. clientToken
  211. }
  212. },
  213. function(error, response, body){
  214. if(error){
  215. Mojang.logger.error('Error during invalidation.', error)
  216. reject(error)
  217. } else {
  218. if(response.statusCode === 204){
  219. resolve()
  220. } else {
  221. reject(body)
  222. }
  223. }
  224. })
  225. })
  226. }
  227. /**
  228. * Refresh a user's authentication. This should be used to keep a user logged
  229. * in without asking them for their credentials again. A new access token will
  230. * be generated using a recent invalid access token. See Wiki for more info.
  231. *
  232. * @param {string} accessToken The old access token.
  233. * @param {string} clientToken The launcher's client token.
  234. * @param {boolean} requestUser Optional. Adds user object to the reponse.
  235. *
  236. * @see http://wiki.vg/Authentication#Refresh
  237. */
  238. public static refresh(accessToken: string, clientToken: string, requestUser: boolean = true): Promise<Session> {
  239. return new Promise((resolve, reject) => {
  240. request.post(Mojang.AUTH_ENDPOINT + '/refresh',
  241. {
  242. json: true,
  243. body: {
  244. accessToken,
  245. clientToken,
  246. requestUser
  247. }
  248. },
  249. function(error, response, body){
  250. if(error){
  251. Mojang.logger.error('Error during refresh.', error)
  252. reject(error)
  253. } else {
  254. if(response.statusCode === 200){
  255. resolve(body)
  256. } else {
  257. reject(body)
  258. }
  259. }
  260. })
  261. })
  262. }
  263. }