Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2. var https = require('https');
  3.  
  4. /**
  5.  * Module for verifying users to EVE characters. Hint: chain the successCallback of verifyCode
  6.  * to a call to getCharacterInfo.
  7.  * config: {
  8.  *    ccpSsoHostname: "sisilogin.testeveonline.com",
  9.  *    ccpSecretKey: '{client_id}:{client_secret}'// Base64 encoded use https://www.base64encode.org/
  10.  * }
  11.  */
  12. var ccpVerify = function (config) {
  13.     // some static properties used in the requests
  14.     this.basicAuthHeader = 'Basic ' + config.ccpSecretKey
  15.     this.formUrlEncodedHeader = 'application/x-www-form-urlencoded';
  16.     this.config = config;
  17. }
  18.  
  19. /**
  20.  * Verifies a given authorization code as coming from CCP.
  21.  * authCode: the code to verify
  22.  * successCallback: function, taking the resultant acess token as parameter
  23.  * failCallback: function, taking an error message as parameter
  24.  */
  25. ccpVerify.prototype.verifyCode = function (authCode, successCallback, failCallback) {
  26.  
  27.     var options = {
  28.         hostname: this.config.ccpSsoHostname,
  29.         path: '/oauth/token',
  30.         method: 'POST',
  31.         headers: {
  32.             'Authorization': this.basicAuthHeader,
  33.             'Content-Type': this.formUrlEncodedHeader
  34.         }
  35.     }
  36.  
  37.     var requestBody = 'grant_type=authorization_code&code=' + authCode;
  38.  
  39.     var req = https.request(options, function (res) {
  40.         var statusCode = res.statusCode;
  41.  
  42.         res.on('data', function (d) {
  43.             var data;
  44.             try {
  45.                 data = JSON.parse(d);
  46.             }
  47.             catch (e) {
  48.                 console.error(e);
  49.                 data = { error_description: 'Unknown error' };
  50.             }
  51.  
  52.             if (!data.access_token) {
  53.                 failCallback(data.error_description);
  54.             }
  55.             else {
  56.                 successCallback(data.access_token);
  57.             }
  58.         });
  59.     });
  60.     req.on('error', function (e) {
  61.         failCallback({ error_description: 'Unknown error' });
  62.     });
  63.  
  64.     req.write(requestBody);
  65.     req.end();
  66. };
  67.  
  68. /**
  69.  * Gets character info associated with a given access token.
  70.  * accessToken: an access token linked to a user
  71.  * successCallback: function, takes an object representing the character as parameter
  72.  * failCallback: function, takes an error message as parameter
  73.  */
  74. ccpVerify.prototype.getCharacterInfo = function (accessToken, successCallback, failCallback) {
  75.     var config = this.config;
  76.  
  77.     var options = {
  78.         hostname: this.config.ccpSsoHostname,
  79.         path: '/oauth/verify',
  80.         method: 'GET',
  81.         headers: {
  82.             'Authorization': 'Bearer ' + accessToken
  83.         }
  84.     }
  85.  
  86.     var req = https.request(options, function (res) {
  87.         var statusCode = res.statusCode;
  88.  
  89.         res.on('data', function (d) {
  90.             var data;
  91.             try {
  92.                 data = JSON.parse(d);
  93.             }
  94.             catch (e) {
  95.                 data = { error_description: 'Unknown error' };
  96.             }
  97.  
  98.             if (!data.CharacterID) {
  99.                 failCallback(data.error_description);
  100.             }
  101.             else {
  102.                 successCallback(data);
  103.             }
  104.         });
  105.     });
  106.     req.on('error', function (e) {
  107.         failCallback({ error_description: 'Unknown error' });
  108.     });
  109.  
  110.     req.end();
  111. }
  112.  
  113.  
  114.  
  115. module.exports = ccpVerify;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement