Skip to content

Commit

Permalink
0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
jozzhart committed Mar 7, 2013
1 parent 13d309e commit ea0f84d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
Empty file modified LICENSE 100644 → 100755
Empty file.
29 changes: 28 additions & 1 deletion README.md 100644 → 100755
@@ -1,4 +1,31 @@
passport-youtube
================

Youtube strategy for passport
Youtube strategy for passport



## Install

$ npm install passport-youtube

## Usage

#### Configure Strategy

The Youtube authentication strategy authenticates users using a youtube
account and OAuth 2.0 tokens. The strategy requires a `verify` callback, which
accepts these credentials and calls `done` providing a user, as well as
`options` specifying a app ID, app secret, and callback URL.

passport.use(new YoutubeStrategy({
clientID: YOUTUBE_APP_ID,
clientSecret: YOUTUBE_APP_SECRET,
callbackURL: "http://localhost:3000/auth/youtube/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ userId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
Empty file modified lib/passport-youtube/index.js 100644 → 100755
Empty file.
38 changes: 19 additions & 19 deletions lib/passport-youtube/strategy.js 100644 → 100755
Expand Up @@ -4,8 +4,8 @@
var util = require('util')
, OAuth2Strategy = require('passport-oauth').OAuth2Strategy
, InternalOAuthError = require('passport-oauth').InternalOAuthError;


/**
* `Strategy` constructor.
*
Expand All @@ -20,17 +20,17 @@ function Strategy(options, verify) {
options.authorizationURL = options.authorizationURL || 'https://accounts.google.com/o/oauth2/auth';
options.tokenURL = options.tokenURL || 'https://accounts.google.com/o/oauth2/token';
options.scopeSeparator = options.scopeSeparator || ',';

OAuth2Strategy.call(this, options, verify);
this.name = 'youtube';
this._profileURL = options.profileURL || 'https://gdata.youtube.com/feeds/api/users/default?alt=json';
}

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

/**
* Retrieve user profile from Youtube.
*
Expand All @@ -49,57 +49,57 @@ util.inherits(Strategy, OAuth2Strategy);
*/
Strategy.prototype.userProfile = function(accessToken, done) {
var url = this._profileURL;

this._oauth2.getProtectedResource(url, accessToken, function (err, body, res) {

if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }

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

var youtubeProfile = json.entry;

var profile = { provider: 'youtube' };
profile.id = youtubeProfile["yt$googlePlusUserId"]["$t"];
profile.username = youtubeProfile["yt$username"]["$t"];
profile.displayName = youtubeProfile["title"]["$t"];
profile.name = { familyName: youtubeProfile["yt$lastName"]["$t"],
givenName: youtubeProfile["yt$firstName"]["$t"]};

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

done(null, profile);
} catch(e) {
done(e);
}
});
}

Strategy.prototype._convertProfileFields = function(profileFields) {
var map = {
'id': 'id',
'username': 'username',
'displayName': 'name',
'name': ['last_name', 'first_name']
};

var fields = [];

profileFields.forEach(function(f) {
if (typeof map[f] === 'undefined') return;

if (Array.isArray(map[f])) {
Array.prototype.push.apply(fields, map[f]);
} else {
fields.push(map[f]);
}
});

return fields.join(',');
}

/**
* Expose `Strategy`.
*/
module.exports = Strategy;
module.exports = Strategy;
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "passport-youtube",
"version": "0.0.4",
"version": "0.0.5",
"description": "Youtube OAuth2 authentication strategy for Passport. Based on the passport-facebook module",
"keywords": [
"passport",
Expand Down Expand Up @@ -31,7 +31,8 @@
"dependencies": {
"pkginfo": "0.2.x",
"passport": "~0.1.1",
"oauth": "0.9.x"
"oauth": "0.9.x",
"passport-oauth": ""
},
"devDependencies": {},
"scripts": {},
Expand Down

0 comments on commit ea0f84d

Please sign in to comment.