Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var https = require('https');
- var ccpVerify = function (config) {
- // some static properties used in the requests
- this.basicAuthHeader = 'Basic ' + config.ccpSecretKey
- this.formUrlEncodedHeader = 'application/x-www-form-urlencoded';
- this.config = config;
- }
- /**
- * Verifies a given authorization code as coming from CCP.
- * authCode: the code to verify
- * successCallback: function, taking the resultant acess token as parameter
- * failCallback: function, taking an error message as parameter
- */
- ccpVerify.prototype.verifyCode = function (authCode, successCallback, failCallback) {
- var options = {
- hostname: this.config.ccpSsoHostname,
- path: '/oauth/token',
- method: 'POST',
- headers: {
- 'Authorization': this.basicAuthHeader,
- 'Content-Type': this.formUrlEncodedHeader
- }
- }
- var requestBody = 'grant_type=authorization_code&code=' + authCode;
- var req = https.request(options, function (res) {
- var statusCode = res.statusCode;
- res.on('data', function (d) {
- var data;
- try {
- data = JSON.parse(d);
- }
- catch (e) {
- console.error(e);
- data = { error_description: 'Unknown error' };
- }
- if (!data.access_token) {
- failCallback(data.error_description);
- }
- else {
- successCallback(data.access_token);
- }
- });
- });
- req.on('error', function (e) {
- failCallback({ error_description: 'Unknown error' });
- });
- req.write(requestBody);
- req.end();
- };
- /**
- * Gets character info associated with a given access token.
- * accessToken: an access token linked to a user
- * successCallback: function, takes an object representing the character as parameter
- * failCallback: function, takes an error message as parameter
- */
- ccpVerify.prototype.getCharacterInfo = function (accessToken, successCallback, failCallback) {
- var config = this.config;
- var options = {
- hostname: this.config.ccpSsoHostname,
- path: '/oauth/verify',
- method: 'GET',
- headers: {
- 'Authorization': 'Bearer ' + accessToken
- }
- }
- var req = https.request(options, function (res) {
- var statusCode = res.statusCode;
- res.on('data', function (d) {
- var data;
- try {
- data = JSON.parse(d);
- }
- catch (e) {
- data = { error_description: 'Unknown error' };
- }
- if (!data.CharacterID) {
- failCallback(data.error_description);
- }
- else {
- successCallback(data);
- }
- });
- });
- req.on('error', function (e) {
- failCallback({ error_description: 'Unknown error' });
- });
- req.end();
- }
- module.exports = ccpVerify;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement