Advertisement
Guest User

Node JS CCP SSO Imp

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