Permalink
Browse files

Added salesforce.js. Fixed github.js

  • Loading branch information...
1 parent ba1e354 commit adfeaf25182e87a9632882739a2977ba0907db3e @rrojas-chariot committed Aug 7, 2012
Showing with 100 additions and 4 deletions.
  1. +24 −4 lib/modules/github.js
  2. +76 −0 lib/modules/salesforce.js
View
@@ -7,7 +7,7 @@ oauthModule.submodule('github')
})
.oauthHost('https://github.com')
- .apiHost('https://github.com/api/v2/json')
+ .apiHost('https://api.github.com')
.authPath('/login/oauth/authorize')
.accessTokenPath('/login/oauth/access_token')
@@ -21,10 +21,30 @@ oauthModule.submodule('github')
.fetchOAuthUser( function (accessToken) {
var p = this.Promise();
- this.oauth.get(this.apiHost() + '/user/show', accessToken, function (err, data) {
+ this.oauth.get(this.apiHost() + '/user', accessToken, function (err, data) {
if (err) return p.fail(err);
- var oauthUser = JSON.parse(data).user;
+ var oauthUser = JSON.parse(data);
p.fulfill(oauthUser);
})
return p;
- });
+ })
+ .moduleErrback( function (err, seqValues) {
+ if (err instanceof Error) {
+ var next = seqValues.next;
+ return next(err);
+ } else if (err.extra) {
+ var ghResponse = err.extra.res
+ , serverResponse = seqValues.res;
+ serverResponse.writeHead(
+ ghResponse.statusCode
+ , ghResponse.headers);
+ serverResponse.end(err.extra.data);
+ } else if (err.statusCode) {
+ var serverResponse = seqValues.res;
+ serverResponse.writeHead(err.statusCode);
+ serverResponse.end(err.data);
+ } else {
+ console.error(err);
+ throw new Error('Unsupported error type');
+ }
+ });
View
@@ -0,0 +1,76 @@
+var oauthModule = require('./oauth2')
+ , https = require('https');
+
+var salesforce = module.exports =
+oauthModule.submodule('salesforce')
+ .configurable({
+ scope: 'specify types of access: (no scope), user, public_repo, repo, gist'
+
+ })
+
+ .oauthHost('https://login.salesforce.com')
+ .apiHost('https://na9.salesforce.com')
+
+ .authPath('/services/oauth2/authorize')
+
+ .entryPath('/auth/salesforce')
+ .callbackPath('/auth/salesforce/callback')
+ .authQueryParam('response_type', 'code')
+
+ .accessTokenPath('/services/oauth2/token')
+ .accessTokenHttpMethod('post')
+ .accessTokenParam('grant_type','authorization_code')
+ .accessTokenParam('format', 'json')
+
+ .authQueryParam('scope', function () {
+ return this._scope && this.scope();
+ })
+
+ .fetchOAuthUser( function (accessToken) {
+
+ var p = this.Promise();
+ var data ='';
+ //parse the url
+ var url = (require('url').parse(this.apiHost()));
+ var options = {
+ host: url['host'],
+ port: 443,
+ path: '/services/data/v22.0/query?q='+escape('SELECT id, username, firstname, lastname, email from User limit 1'),
+ method: 'GET',
+ headers: {
+ 'Host': url['host'],
+ 'Authorization': 'OAuth '+ accessToken,
+ 'Accept':'application/jsonrequest',
+ 'Cache-Control':'no-cache,no-store,must-revalidate',
+ 'Content-type':'application/json; charset=UTF-8'
+ }
+ }
+
+ var req = https.request(options, function(res) {
+ res.setEncoding('utf8');
+
+ res.on('data', function (chunk) {
+ data+=chunk;
+ });
+
+ res.on('end', function(d) {
+ console.log('Data: '+ data);
+ var oauthUser = JSON.parse(data);
+ p.fulfill(oauthUser);
+ });
+ }).on('error', function(e) {
+ console.log('Error: '+ e);
+ return p.fail(e);
+ });
+
+ //console.log(req);
+ req.end();
+ return p;
+
+ })
+
+ .convertErr( function (err) {
+ console.log(JSON.stringify(err));
+ return new Error(err.data);
+ });
+

0 comments on commit adfeaf2

Please sign in to comment.