mojang.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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: 'sessionserver.mojang.com',
  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. request.post(authpath + '/authenticate',
  131. {
  132. json: true,
  133. body: {
  134. agent,
  135. username,
  136. password,
  137. clientToken,
  138. requestUser
  139. }
  140. },
  141. function(error, response, body){
  142. if(error){
  143. logger.error('Error during authentication.', error)
  144. reject(error)
  145. } else {
  146. if(response.statusCode === 200){
  147. resolve(body)
  148. } else {
  149. reject(body || {code: 'ENOTFOUND'})
  150. }
  151. }
  152. })
  153. })
  154. }
  155. /**
  156. * Validate an access token. This should always be done before launching.
  157. * The client token should match the one used to create the access token.
  158. *
  159. * @param {string} accessToken The access token to validate.
  160. * @param {string} clientToken The launcher's client token.
  161. *
  162. * @see http://wiki.vg/Authentication#Validate
  163. */
  164. exports.validate = function(accessToken, clientToken){
  165. return new Promise((resolve, reject) => {
  166. request.post(authpath + '/validate',
  167. {
  168. json: true,
  169. body: {
  170. accessToken,
  171. clientToken
  172. }
  173. },
  174. function(error, response, body){
  175. if(error){
  176. logger.error('Error during validation.', error)
  177. reject(error)
  178. } else {
  179. if(response.statusCode === 403){
  180. resolve(false)
  181. } else {
  182. // 204 if valid
  183. resolve(true)
  184. }
  185. }
  186. })
  187. })
  188. }
  189. /**
  190. * Invalidates an access token. The clientToken must match the
  191. * token used to create the provided accessToken.
  192. *
  193. * @param {string} accessToken The access token to invalidate.
  194. * @param {string} clientToken The launcher's client token.
  195. *
  196. * @see http://wiki.vg/Authentication#Invalidate
  197. */
  198. exports.invalidate = function(accessToken, clientToken){
  199. return new Promise((resolve, reject) => {
  200. request.post(authpath + '/invalidate',
  201. {
  202. json: true,
  203. body: {
  204. accessToken,
  205. clientToken
  206. }
  207. },
  208. function(error, response, body){
  209. if(error){
  210. logger.error('Error during invalidation.', error)
  211. reject(error)
  212. } else {
  213. if(response.statusCode === 204){
  214. resolve()
  215. } else {
  216. reject(body)
  217. }
  218. }
  219. })
  220. })
  221. }
  222. /**
  223. * Refresh a user's authentication. This should be used to keep a user logged
  224. * in without asking them for their credentials again. A new access token will
  225. * be generated using a recent invalid access token. See Wiki for more info.
  226. *
  227. * @param {string} accessToken The old access token.
  228. * @param {string} clientToken The launcher's client token.
  229. * @param {boolean} requestUser Optional. Adds user object to the reponse.
  230. *
  231. * @see http://wiki.vg/Authentication#Refresh
  232. */
  233. exports.refresh = function(accessToken, clientToken, requestUser = true){
  234. return new Promise((resolve, reject) => {
  235. request.post(authpath + '/refresh',
  236. {
  237. json: true,
  238. body: {
  239. accessToken,
  240. clientToken,
  241. requestUser
  242. }
  243. },
  244. function(error, response, body){
  245. if(error){
  246. logger.error('Error during refresh.', error)
  247. reject(error)
  248. } else {
  249. if(response.statusCode === 200){
  250. resolve(body)
  251. } else {
  252. reject(body)
  253. }
  254. }
  255. })
  256. })
  257. }