diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 94e7b2e..3144dd6 --- a/README.md +++ b/README.md @@ -1,4 +1,31 @@ passport-youtube ================ -Youtube strategy for passport \ No newline at end of file +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); + }); + } + )); diff --git a/lib/passport-youtube/index.js b/lib/passport-youtube/index.js old mode 100644 new mode 100755 diff --git a/lib/passport-youtube/strategy.js b/lib/passport-youtube/strategy.js old mode 100644 new mode 100755 index c9ef914..7d297f1 --- a/lib/passport-youtube/strategy.js +++ b/lib/passport-youtube/strategy.js @@ -4,8 +4,8 @@ var util = require('util') , OAuth2Strategy = require('passport-oauth').OAuth2Strategy , InternalOAuthError = require('passport-oauth').InternalOAuthError; - - + + /** * `Strategy` constructor. * @@ -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. * @@ -49,33 +49,33 @@ 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', @@ -83,23 +83,23 @@ Strategy.prototype._convertProfileFields = function(profileFields) { '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; \ No newline at end of file +module.exports = Strategy; diff --git a/package.json b/package.json index d0e4f99..4c7af4f 100644 --- a/package.json +++ b/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", @@ -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": {},