Skip to content

Instantly share code, notes, and snippets.

@unknowndomain
Last active May 16, 2018 15:42
Show Gist options
  • Select an option

  • Save unknowndomain/e7944e17494b71de9f84106033f42f69 to your computer and use it in GitHub Desktop.

Select an option

Save unknowndomain/e7944e17494b71de9f84106033f42f69 to your computer and use it in GitHub Desktop.
var querystring = require( 'querystring' );
var Buffer = require( 'buffer' ).Buffer;
var request = require( 'request' );
var EVE = {
client_id: null,
client_secret: null,
redirect_url: null,
scope: [],
access_token: null,
refresh_token: null,
api_url: 'https://esi.evetech.net',
authorize_url: 'https://login.eveonline.com/oauth',
generateAuthoriseURL: function( state ) {
var query = {
response_type: 'code',
redirect_uri: EVE.redirect_url,
client_id: EVE.client_id,
scope: EVE.scope.join( ' ' ),
state: state
};
return EVE.authorize_url + '/authorize?' + querystring.stringify( query );
},
getToken: function( authorisation_code, cb ) {
var options = {
url: EVE.authorize_url + '/token',
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from( EVE.client_id + ':' + EVE.client_secret, 'ascii' ).toString( 'base64' ),
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'login.eveonline.com',
},
body: querystring.stringify ( {
'grant_type': 'authorization_code',
'code': authorisation_code
} )
}
request( options, function( err, res, body ) {
if ( res.statusCode == 200 ) {
console.log( 'Authorised:' );
console.log( body );
EVE.access_token = body.access_token;
EVE.refresh_token = body.refresh_token;
if ( cb ) cb( body );
} else if ( res.statusCode == 401 ) {
console.log( 'Unauthorised:' );
console.log( '\n\n\nRES: ' );
console.log( res );
if ( cb ) cb( 401 );
} else {
console.log( 'Unknown Error: ' );
console.log( '\n\n\nERR: ' );
console.log( err );
console.log( '\n\n\nRES: ' );
console.log( res );
console.log( "Status: " + res.statusCode );
if ( cb ) cb( res.statusCode );
}
} );
},
refreshToken: function( cb ) {
var options = {
url: EVE.authorize_url + '/token',
method: 'POST',
headers: {
'Authorization': 'Basic ' + Buffer.from( EVE.client_id + ':' + EVE.client_secret, 'ascii' ).toString( 'base64' ),
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'login.eveonline.com',
},
body: querystring.stringify ( {
'grant_type': 'refresh_token',
'refresh_token': EVE.refresh_token
} )
}
request( options, function( err, res, body ) {
if ( res.statusCode == 200 ) {
body = JSON.parse( body );
EVE.access_token = body.access_token;
EVE.refresh_token = body.refresh_token;
if ( cb ) cb( body );
} else if ( res.statusCode == 401 ) {
console.log( 'Unauthorised:' );
console.log( '\n\n\nRES: ' );
console.log( res );
if ( cb ) cb( 401 );
} else {
console.log( 'Unknown Error: ' );
console.log( '\n\n\nERR: ' );
console.log( err );
console.log( '\n\n\nRES: ' );
console.log( res );
console.log( "Status: " + res.statusCode );
if ( cb ) cb( res.statusCode );
}
} );
},
getESI: function( path, cb ) {
var options = {
url: EVE.api_url + path,
method: 'GET',
headers: {
'Authorization': 'Bearer ' + EVE.access_token
}
}
request( options, function( err, res, body ) {
if ( res.statusCode == 200 ) {
if ( cb ) cb( JSON.parse( body ) );
} else {
if ( cb ) cb( false, res );
}
} );
},
postESI: function( path, body, cb ) {
var options = {
url: EVE.api_url + path,
method: 'POST',
headers: {
'Authorization': 'Bearer ' + EVE.access_token
}
}
request( options, function( err, res, body ) {
if ( res.statusCode == 200 ) {
if ( cb ) cb( JSON.parse( body ) );
} else {
if ( cb ) cb( false, res );
}
} );
},
verify: function( cb ) {
EVE.getESI( '/verify', cb );
},
getCharacter: function( character_id, cb ) {
EVE.getESI( '/v4/characters/' + character_id, cb );
},
getCorporationAssets: function( corporation_id, cb ) {
EVE.getESI( '/v3/corporations/' + corporation_id + '/assets', cb );
},
getMarketPrices: function( cb ) {
EVE.getESI( '/v1/markets/prices', cb );
},
};
module.exports = function( config ) {
if ( config.client_id ) EVE.client_id = config.client_id;
if ( config.client_secret ) EVE.client_secret = config.client_secret;
if ( config.redirect_url ) EVE.redirect_url = config.redirect_url;
if ( config.scope ) EVE.scope = config.scope;
if ( config.access_token ) EVE.access_token = config.access_token;
if ( config.refresh_token ) EVE.refresh_token = config.refresh_token;
if ( config.api_url ) EVE.api_url = config.api_url;
return EVE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment