Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Passport-Yahoo-OAuth

[Passport](http://passportjs.org/) strategies for authenticating with [Yahoo!](http://www.yahoo.com/)
using the OAuth 1.0a API.
using the OAuth 1.0a API and Oauth 2.0.

This module lets you authenticate using Yahoo! in your Node.js applications.
By plugging into Passport, Yahoo! authentication can be easily and
Expand All @@ -10,13 +10,18 @@ unobtrusively integrated into any application or framework that supports
[Express](http://expressjs.com/).

## Installation

- Original version
$ npm install passport-yahoo-oauth
- Forked version
$ npm install passport-yahoo-oauth2

## Usage

#### Configure Strategy

#### OAuth 1
var YahooStrategy = require('passport-yahoo-oauth').Strategy;

The Yahoo authentication strategy authenticates users using a Yahoo account
and OAuth tokens. The strategy requires a `verify` callback, which accepts
these credentials and calls `done` providing a user, as well as `options`
Expand All @@ -34,6 +39,22 @@ specifying a consumer key, consumer secret, and callback URL.
}
));

#### OAuth2 (forked version)
var YahooStrategy = require('passport-yahoo-oauth2').OAuth2Strategy;

passport.use(new YahooStrategy({
consumerKey: YAHOO_CONSUMER_KEY,
consumerSecret: YAHOO_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/yahoo/callback"
},
function(token, tokenSecret, profile, done) {
User.findOrCreate({ yahooId: profile.id }, function (err, user) {
return done(err, user);
});
}
));


#### Authenticate Requests

Use `passport.authenticate()`, specifying the `'yahoo'` strategy, to
Expand Down Expand Up @@ -73,6 +94,8 @@ properly.
## Credits

- [Jared Hanson](http://github.com/jaredhanson)
- [Sahat Yalkabov](https://twitter.com/EvNowAndForever)
- [Eugene Obrezkov](https://github.com/ghaiklor)

## License

Expand Down
2 changes: 2 additions & 0 deletions lib/passport-yahoo-oauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Module dependencies.
*/
var Strategy = require('./strategy');
var OAuth2Strategy = require('./oauth2');


/**
Expand All @@ -13,3 +14,4 @@ require('pkginfo')(module, 'version');
* Expose constructors.
*/
exports.Strategy = Strategy;
exports.OAuth2Strategy = OAuth2Strategy;
130 changes: 130 additions & 0 deletions lib/passport-yahoo-oauth/oauth2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
'use strict';

/**
* Module dependencies.
*/
var util = require('util'),
OAuth2Strategy = require('passport-oauth2').Strategy,
InternalOAuthError = require('passport-oauth2').InternalOAuthError,
request = require('request');

/**
* `Strategy` constructor.
*
* The Yahoo authentication strategy authenticates requests by delegating to
* Yahoo using the OAuth protocol.
*
* Applications must supply a `verify` callback which accepts a `token`,
* `tokenSecret` and service-specific `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `consumerKey` identifies client to Yahoo
* - `consumerSecret` secret used to establish ownership of the consumer key
* - `callbackURL` URL to which Yahoo will redirect the user after obtaining authorization
*
* Examples:
*
* passport.use(new YahooStrategy({
* consumerKey: '123-456-789',
* consumerSecret: 'shhh-its-a-secret'
* callbackURL: 'https://www.example.net/auth/yahoo/callback'
* },
* function(token, tokenSecret, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user);
* });
* }
* ));
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy(options, verify) {
//https://api.login.yahoo.com/oauth2/request_auth
options = options || {};

options.authorizationURL = options.authorizationURL || 'https://api.login.yahoo.com/oauth2/request_auth';
options.tokenURL = options.tokenURL || 'https://api.login.yahoo.com/oauth2/get_token';
options.profileURL = options.profileURL || 'https://social.yahooapis.com/v1/user/:xoauthYahooGuid/profile?format=json';

OAuth2Strategy.call(this, options, verify);

this._options = options;
this.name = 'yahoo';
}

/**
* Inherit from `OAuthStrategy`.
*/
util.inherits(Strategy, OAuth2Strategy);

/**
* Retrieve user profile from Yahoo.
* inpired from post: http://yahoodevelopers.tumblr.com/post/105969451213/implementing-yahoo-oauth2-authentication
* other code from : passport-yahoo-token repo
* This function constructs a normalized profile, with the following properties:
*
* - `id`
* - `displayName`
*
* @param {String} token
* @param {String} tokenSecret
* @param {Object} params
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function (accessToken, done) {
var xoauthYahooGuid = 'me';

this._oauth2._useAuthorizationHeaderForGET = true;
var request_options = {
url: this._options.profileURL.replace(':xoauthYahooGuid', xoauthYahooGuid),
headers: {
Authorization: this._oauth2.buildAuthHeader(accessToken)
},
rejectUnauthorized: false,
json: true
};

request.get(request_options, function(err, response, body) {
if(err) {
return done(new InternalOAuthError('Failed to fetch user profile', err));
}

try{
var json = body.profile;
json.id = json.guid;

var profile = {
provider: 'yahoo',
id: json.id,
displayName: [json.givenName || '', json.familyName || ''].join(' '),
name: {
familyName: json.familyName || '',
givenName: json.givenName || ''
},
emails: [{
value: (json.emails && json.emails[0].handle) || '',
type: (json.emails && json.emails[0].type) || ''
}],
photos: [{
value: (json.image && json.image.imageUrl) || ''
}],
_raw: body,
_json: json
};
done(null, profile);
}catch(e) {
done(e);
}
});
};


/**
* Expose `Strategy`.
*/
module.exports = Strategy;
14 changes: 7 additions & 7 deletions lib/passport-yahoo-oauth/strategy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Module dependencies.
*/
var util = require('util')
, OAuthStrategy = require('passport-oauth').OAuthStrategy
, InternalOAuthError = require('passport-oauth').InternalOAuthError;
var util = require('util');
var OAuthStrategy = require('passport-oauth2').Strategy;
var InternalOAuthError = require('passport-oauth2').InternalOAuthError;


/**
Expand Down Expand Up @@ -71,18 +71,18 @@ util.inherits(Strategy, OAuthStrategy);
* @api protected
*/
Strategy.prototype.userProfile = function(token, tokenSecret, params, done) {
this._oauth.get('http://social.yahooapis.com/v1/user/' + params.xoauth_yahoo_guid + '/profile?format=json', token, tokenSecret, function (err, body, res) {
this._oauth.get('https://social.yahooapis.com/v1/user/' + params.xoauth_yahoo_guid + '/profile?format=json', token, tokenSecret, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }

try {
var json = JSON.parse(body);

var profile = { provider: 'yahoo' };
profile.id = json.profile.guid;
profile.displayName = json.profile.givenName + ' ' + json.profile.familyName;
profile.name = { familyName: json.profile.familyName,
givenName: json.profile.givenName };

profile._raw = body;
profile._json = json;

Expand Down
40 changes: 28 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
{
"name": "passport-yahoo-oauth",
"version": "0.1.2",
"description": "Yahoo! (OAuth) authentication strategy for Passport.",
"keywords": ["passport", "yahoo", "auth", "authn", "authentication", "identity"],
"name": "passport-yahoo-oauth2",
"version": "0.2.6",
"description": "This is a forked version of Yahoo passport! (OAuth1 and OAuth2) authentication strategy for Passport.",
"keywords": [
"passport",
"yahoo",
"auth",
"authn",
"authentication",
"identity"
],
"repository": {
"type": "git",
"url": "git://github.com/jaredhanson/passport-yahoo-oauth.git"
"url": "git://github.com/francisalvinbarretto/passport-yahoo-oauth.git"
},
"bugs": {
"url": "http://github.com/jaredhanson/passport-yahoo-oauth/issues"
"url": "http://github.com/francisalvinbarretto/passport-yahoo-oauth/issues"
},
"author": {
"name": "Jared Hanson",
"email": "[email protected]",
"url": "http://www.jaredhanson.net/"
},
"licenses": [ {
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
} ],
"contributors": {
"name": "Francis Alvin Barretto",
"email": "[email protected]"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
}
],
"main": "./lib/passport-yahoo-oauth",
"dependencies": {
"passport-oauth2": "^1.4.0",
"pkginfo": "0.2.x",
"passport-oauth": "0.1.x"
"request": "^2.56.0"
},
"devDependencies": {
"vows": "0.6.x"
},
"scripts": {
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
},
"engines": { "node": ">= 0.4.0" }
"engines": {
"node": ">= 0.4.0"
}
}