Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Takahashi Misato authored and Takahashi Misato committed Jan 10, 2013
1 parent 3585dcd commit 0119048
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 0 deletions.
Empty file added README.md
Empty file.
99 changes: 99 additions & 0 deletions examples/deskcom/app.js
@@ -0,0 +1,99 @@
var express = require('express')
, passport = require('passport')
, util = require('util')
, DeskcomStrategy = require('passport-deskcom').Strategy;

var DESKCOM_CONSUMER_KEY = "-----";
var DESKCOM_CONSUMER_SECRET = "------";

passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(obj, done) {
done(null, obj);
});

passport.use(
new DeskcomStrategy(
{
consumerKey: DESKCOM_CONSUMER_KEY,
consumerSecret: DESKCOM_CONSUMER_SECRET,
site: "https://spotlight.desk.com",
callbackURL: "http://127.0.0.1:3000/auth/deskcom/callback"
},
function(token, tokenSecret, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
)
);

var express = require("express");
var app = express();

// configure Express
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});


app.get('/', function(req, res){
res.render('index', { user: req.user });
});

app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});

app.get('/login', function(req, res){
res.render('login', { user: req.user });
});

// GET /auth/deskcom
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Twitter authentication will involve redirecting
// the user to desk.com. After authorization, the Twitter will redirect
// the user back to this application at /auth/deskcom/callback
app.get('/auth/deskcom',
passport.authenticate('deskcom'),
function(req, res){
// The request will be redirected to Twitter for authentication, so this
// function will not be called.
});

// GET /auth/deskcom/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/deskcom/callback',
passport.authenticate('deskcom', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});

app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});

app.listen(3000);

function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
9 changes: 9 additions & 0 deletions examples/deskcom/package.json
@@ -0,0 +1,9 @@
{
"name": "passport-deskcom-examples-signin",
"version": "0.0.0",
"dependencies": {
"express": ">= 0.0.0",
"ejs": ">= 0.0.0",
"passport": ">= 0.0.0"
}
}
2 changes: 2 additions & 0 deletions examples/deskcom/views/account.ejs
@@ -0,0 +1,2 @@
<p>ID: <%= user.id %></p>
<p>Username: <%= user.name %></p>
5 changes: 5 additions & 0 deletions examples/deskcom/views/index.ejs
@@ -0,0 +1,5 @@
<% if (!user) { %>
<h2>Welcome! Please log in.</h2>
<% } else { %>
<h2>Hello, <%= user.name %>.</h2>
<% } %>
21 changes: 21 additions & 0 deletions examples/deskcom/views/layout.ejs
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Passport-Deskcom Example</title>
</head>
<body>
<% if (!user) { %>
<p>
<a href="/">Home</a> |
<a href="/login">Log In</a>
</p>
<% } else { %>
<p>
<a href="/">Home</a> |
<a href="/account">Account</a> |
<a href="/logout">Log Out</a>
</p>
<% } %>
<%- body %>
</body>
</html>
1 change: 1 addition & 0 deletions examples/deskcom/views/login.ejs
@@ -0,0 +1 @@
<a href="/auth/deskcom">login deskcom</a>
8 changes: 8 additions & 0 deletions lib/passport-deskcom/index.js
@@ -0,0 +1,8 @@
var Strategy = require('./strategy');

var pkg_info = require('pkginfo')
pkg_info(module, 'version');

exports.Strategy = Strategy;


118 changes: 118 additions & 0 deletions lib/passport-deskcom/strategy.js
@@ -0,0 +1,118 @@
/**
* Module dependencies.
*/

var util = require('util')
, _ = require('underscore')
, OAuthStrategy = require('passport-oauth').OAuthStrategy
, InternalOAuthError = require('passport-oauth').OAuthStrategy.InternalOAuthError;

/**
* Const parameters
*/
var DESK_COM_TOKEN_PATH = '/oauth/request_token';
var DESK_COM_ACCESS_TOKEN_PATH = '/oauth/access_token';
var DESK_COM_AUTH_PATH = '/oauth/authorize';
var DESK_CRETENTIAL_PATH = '/api/v1/account/verify_credentials.json';

var DESK_COM_SESSION_KEY = 'oauth:deskcom:';

/**
* `Strategy` constructor.
*
* Desk.com authentication strategy
* Oauth1.0a protocol
*
* Options:
* - `consumerKey` identifies client to Deskcom
* - `consumerSecret` secret used to establish ownership of the consumer key
* - `callbackURL` URL to which Deskcom will redirect the user after obtaining authorization
*
* Examples:
*
* passport.use(new DeskcomStrategy({
* consumerKey: '*************',
* consumerSecret: '***************',
* site: 'https://yoursite.desk.com',
* },
* function (token, tokenSecret, profile, done) {
*
* }
* });
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy(options, verify) {
options = options || {};
if (!options.site) {
throw new Error('DesckcomStrategy requires desk.com site url');
}
options.requestTokenURL = options.requestTokenURL || options.site+DESK_COM_TOKEN_PATH;
options.accessTokenURL = options.accessTokenURL || options.site+DESK_COM_ACCESS_TOKEN_PATH;
options.userAuthorizationURL = options.userAuthorizationURL || options.site+DESK_COM_AUTH_PATH;
options.sessionKey = options.sessionKey || DESK_COM_SESSION_KEY

this.DESK_CRETENTIAL_URL = options.site+DESK_CRETENTIAL_PATH;

console.log(options);
OAuthStrategy.call(this, options, verify);
this.name = 'deskcom';
}

util.inherits(Strategy, OAuthStrategy);

module.exports = Strategy;

/**
* Authenticate request by delegating to Desk.com using OAuth.
*
* @param req
* @param options
* @return {*}
* @api protected
*/
Strategy.prototype.authenticate = function(req, options) {
if (req.query && req.query.denied) {
return this.fail();
}
OAuthStrategy.prototype.authenticate.call(this, req, options);
}

/**
* Retrieve user profile from Desk.com.
*
* @param {String} token
* @param {String} tokenSecret
* @param {Object} params
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function(token, tokenSecret, params, done) {
this._oauth.get(this.DESK_CRETENTIAL_URL, token, tokenSecret, function (err, body, res) {
if (err) {
return done(new InternalOAuthError('failed to fetch user CRETENTIAL', err));
}
try {
var json = JSON.parse(body);
var profile = _.clone(json.user);
profile.provider = 'deskcom';
profile._raw = body;
profile._json = json;
done(null, profile);
} catch (e) {
done(e);
}
});
}

/**
* Expose `Strategy`.
*/
module.exports = Strategy;





37 changes: 37 additions & 0 deletions package.json
@@ -0,0 +1,37 @@
{
"name": "passport-deskcom",
"version": "0.0.1",
"description": "Desk.com authentication strategy for Passport.",
"author": {
"name": "Misato Takahashi",
"email": "misato_takahashi@spotlig.ht",
"url": "http://www.smapo.jp/"
},
"repository": {
"type": "git",
"url": "git://github.com/mistat/passport-deskcom.git"
},
"bugs": {
"url": "http://github.com/mistat/passport-deskcom/issues"
},
"main": "./lib/passport-deskcom",
"dependencies": {
"pkginfo": "0.2.x",
"passport-oauth": "0.1.x",
"underscore": ">0.0.0"
},
"devDependencies": {
"vows": "0.6.x"
},
"scripts": {
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
},
"engines": { "node": ">= 0.4.0" },
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
}
],
"keywords": ["passport", "deskcom", "auth", "authn", "authentication", "identity"]
}
12 changes: 12 additions & 0 deletions test/index-test.js
@@ -0,0 +1,12 @@
var vows = require('vows');
var assert = require('assert');
var util = require('util');
var deskcom = require('passport-deskcom');

vows.describe('passport-deskcom').addBatch({
'module': {
'should report a version' : function (x) {
assert.isString(deskcom.version);
}
}
}).export(module);
22 changes: 22 additions & 0 deletions test/strategy-test.js
@@ -0,0 +1,22 @@
var vows = require('vows');
var assert = require('assert');
var util = require('util');
var DeskcomStrategy = require('passport-deskcom/strategy');

vows.describe('DeskcomStrategy').addBatch({
'strategy': {
topic: function() {
return new DeskcomStrategy({
consumerKey: 'desk-com-test-key',
consumerSecret: 'desk-com-test-secret',
site: 'https://example.desk.com/'
},
function() {});
},

'should be name deskcom' : function (strategy) {
assert.equal(strategy.name, 'deskcom');
}

}
}).export(module);

0 comments on commit 0119048

Please sign in to comment.