Permalink
Browse files

clean up

  • Loading branch information...
1 parent b93dba4 commit ef7f4b49bce95b7d7510afadc281d6ac1685b3b9 @jozzhart committed Mar 1, 2013
Showing with 252 additions and 3 deletions.
  1. +8 −3 .gitignore
  2. +14 −0 .npmignore
  3. +20 −0 LICENSE
  4. +15 −0 lib/passport-youtube/index.js
  5. +151 −0 lib/passport-youtube/strategy.js
  6. +44 −0 package.json
View
11 .gitignore
@@ -1,3 +1,10 @@
+# Mac OS X
+.DS_Store
+
+# Node.js
+node_modules
+npm-debug.log
+
lib-cov
*.seed
*.log
@@ -9,6 +16,4 @@ lib-cov
pids
logs
-results
-
-npm-debug.log
+results
View
14 .npmignore
@@ -0,0 +1,14 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+npm-debug.log
View
20 LICENSE
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (c) 2011-2013 Jared Hanson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
15 lib/passport-youtube/index.js
@@ -0,0 +1,15 @@
+/**
+ * Module dependencies.
+ */
+var Strategy = require('./strategy');
+
+
+/**
+ * Framework version.
+ */
+require('pkginfo')(module, 'version');
+
+/**
+ * Expose constructors.
+ */
+exports.Strategy = Strategy;
View
151 lib/passport-youtube/strategy.js
@@ -0,0 +1,151 @@
+/**
+ * Module dependencies.
+ */
+var util = require('util')
+ , OAuth2Strategy = require('passport-oauth').OAuth2Strategy
+ , InternalOAuthError = require('passport-oauth').InternalOAuthError;
+
+
+/**
+ * `Strategy` constructor.
+ *
+ * Youtube authentication strategy authenticates requests using the OAuth 2.0 protocol.
+ *
+ * @param {Object} options
+ * @param {Function} verify
+ * @api public
+ */
+function Strategy(options, verify) {
+ options = options || {};
+ 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://graph.facebook.com/me';
+ this._profileFields = options.profileFields || null;
+}
+
+/**
+ * Inherit from `OAuth2Strategy`.
+ */
+util.inherits(Strategy, OAuth2Strategy);
+
+/**
+ * Return extra Facebook-specific parameters to be included in the authorization
+ * request.
+ *
+ * Options:
+ * - `display` Display mode to render dialog, { `page`, `popup`, `touch` }.
+ *
+ * @param {Object} options
+ * @return {Object}
+ * @api protected
+ */
+Strategy.prototype.authorizationParams = function (options) {
+ var params = {},
+ display = options.display;
+
+ if (display) {
+ params['display'] = display;
+ }
+
+ return params;
+};
+
+/**
+ * Retrieve user profile from Youtube.
+ *
+ * This function constructs a normalized profile, with the following properties:
+ *
+ * - `provider` always set to `youtube`
+ * - `id` the user's Facebook ID
+ * - `username` the user's Facebook username
+ * - `displayName` the user's full name
+ * - `name.familyName` the user's last name
+ * - `name.givenName` the user's first name
+ * - `name.middleName` the user's middle name
+ * - `gender` the user's gender: `male` or `female`
+ * - `profileUrl` the URL of the profile for the user on Facebook
+ * - `emails` the proxied or contact email address granted by the user
+ *
+ * @param {String} accessToken
+ * @param {Function} done
+ * @api protected
+ */
+Strategy.prototype.userProfile = function(accessToken, done) {
+ var url = this._profileURL;
+ if (this._profileFields) {
+ var fields = this._convertProfileFields(this._profileFields);
+ url += (fields !== '' ? '?fields=' + fields : '');
+ }
+
+ 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 profile = { provider: 'youtube' };
+ profile.id = json.id;
+ profile.username = json.username;
+ profile.displayName = json.name;
+ profile.name = { familyName: json.last_name,
+ givenName: json.first_name,
+ middleName: json.middle_name };
+
+ profile.gender = json.gender;
+ profile.profileUrl = json.link;
+ profile.emails = [{ value: json.email }];
+
+ if (json.picture) {
+ if (typeof json.picture == 'object' && json.picture.data) {
+ // October 2012 Breaking Changes
+ profile.photos = [{ value: json.picture.data.url }];
+ } else {
+ profile.photos = [{ value: json.picture }];
+ }
+ }
+
+ 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', 'middle_name'],
+ 'gender': 'gender',
+ 'profileUrl': 'link',
+ 'emails': 'email',
+ 'photos': 'picture'
+ };
+
+ 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;
View
44 package.json
@@ -0,0 +1,44 @@
+{
+ "name": "passport-youtube",
+ "version": "0.0.1",
+ "description": "Youtube OAuth2 authentication strategy for Passport. Based on the passport-facebook module",
+ "keywords": [
+ "passport",
+ "oauth2",
+ "auth",
+ "authentication",
+ "youtube"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jozzhart/passport-youtube.git"
+ },
+ "bugs": {
+ "url": "http://github.com/jozzhart/passport-youtube/issues"
+ },
+ "author": {
+ "name": "Jozz Hart",
+ "email": "me@jozzhart.com",
+ "url": "http://www.jozzhart.com/"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/MIT"
+ }
+ ],
+ "main": "./lib/passport-youtube",
+ "dependencies": {
+ "pkginfo": "0.2.x",
+ "passport": "~0.1.1",
+ "oauth": "0.9.x"
+ },
+ "devDependencies": {},
+ "scripts": {},
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "readme": "passport-youtube\n================\n\nYoutube strategy for passport",
+ "_id": "passport-youtube@0.0.1",
+ "_from": "passport-youtube@"
+}

0 comments on commit ef7f4b4

Please sign in to comment.