mojang.js 8.0 KB

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