Skip to content

Commit aad01bd

Browse files
Andy PritchardAndy Pritchard
authored andcommitted
initial commit
1 parent e4d062a commit aad01bd

6 files changed

Lines changed: 268 additions & 2 deletions

File tree

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2012-2013 Jared Hanson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SOURCES = lib/**/*.js
2+
3+
# ==============================================================================
4+
# Node Tests
5+
# ==============================================================================
6+
7+
VOWS = ./node_modules/.bin/vows
8+
TESTS ?= test/*-test.js
9+
10+
test:
11+
@NODE_ENV=test NODE_PATH=lib $(VOWS) $(TESTS)
12+
13+
# ==============================================================================
14+
# Static Analysis
15+
# ==============================================================================
16+
17+
JSHINT = jshint
18+
19+
hint: lint
20+
lint:
21+
$(JSHINT) $(SOURCES)
22+
23+
24+
.PHONY: test hint lint

README.md

100644100755
Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
passport-chalkable-oauth2
2-
=========================
1+
# Passport-Chalkable-OAuth2
2+
3+
[Passport](http://passportjs.org/) strategies for authenticating with [Chalkable](http://www.chalkable.com/)
4+
using OAuth 1.0a and OAuth 2.0.
5+
6+
This module lets you authenticate using Chalkable in your Node.js applications.
7+
By plugging into Passport, Chalkable authentication can be easily and
8+
unobtrusively integrated into any application or framework that supports
9+
[Connect](http://www.senchalabs.org/connect/)-style middleware, including
10+
[Express](http://expressjs.com/).
11+
12+
## Install
13+
14+
$ npm install passport-chalkable-oauth2
15+
16+
## Usage
17+
18+
#### Configure Strategy
19+
20+
The Chalkable OAuth 2.0 authentication strategy authenticates users using a Chalkable
21+
account and OAuth 2.0 tokens. The strategy requires a `verify` callback, which
22+
accepts these credentials and calls `done` providing a user, as well as
23+
`options` specifying a client ID, client secret, and callback URL.
24+
25+
var ChalkableStrategy = require('passport-chalkable-oauth2').OAuth2Strategy;
26+
27+
passport.use(new ChalkableStrategy({
28+
clientID: CHALKABLE_CLIENT_ID,
29+
clientSecret: CHALKABLE_CLIENT_SECRET,
30+
callbackURL: "http://127.0.0.1:3000/auth/chalkable/callback"
31+
},
32+
function(accessToken, refreshToken, profile, done) {
33+
User.findOrCreate({ chalkableId: profile.id }, function (err, user) {
34+
return done(err, user);
35+
});
36+
}
37+
));
38+
39+
#### Authenticate Requests
40+
41+
Use `passport.authenticate()`, specifying the `'chalkable'` strategy, to
42+
authenticate requests.
43+
44+
For example, as route middleware in an [Express](http://expressjs.com/)
45+
application:
46+
47+
app.get('/auth/chalkable/callback',
48+
passport.authenticate('chalkable', { failureRedirect: '/login' }),
49+
function(req, res) {
50+
// Successful authentication, redirect home.
51+
res.redirect('/');
52+
});
53+
54+
## Credits
55+
56+
- [Jared Hanson](http://github.com/jaredhanson) for the orignial passport module, and the Google OAuth module from which this is derived.
57+
58+
## License
59+
60+
[The MIT License](http://opensource.org/licenses/MIT)
61+
62+
Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
var OAuth2Strategy = require('./oauth2');
5+
6+
7+
/**
8+
* Framework version.
9+
*/
10+
require('pkginfo')(module, 'version');
11+
12+
/**
13+
* Expose constructors.
14+
*/
15+
exports.OAuth2Strategy = OAuth2Strategy;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
var util = require('util')
5+
, OAuth2Strategy = require('passport-oauth').OAuth2Strategy
6+
, InternalOAuthError = require('passport-oauth').InternalOAuthError;
7+
8+
9+
/**
10+
* `Strategy` constructor.
11+
*
12+
* The Chalkable authentication strategy authenticates requests by delegating to
13+
* Chalkable using the OAuth 2.0 protocol.
14+
*
15+
* Applications must supply a `verify` callback which accepts an `accessToken`,
16+
* `refreshToken` and service-specific `profile`, and then calls the `done`
17+
* callback supplying a `user`, which should be set to `false` if the
18+
* credentials are not valid. If an exception occured, `err` should be set.
19+
*
20+
* Options:
21+
* - `clientID` your Google application's client id
22+
* - `clientSecret` your Google application's client secret
23+
* - `callbackURL` URL to which Google will redirect the user after granting authorization
24+
*
25+
* Examples:
26+
*
27+
* passport.use(new ChalkableStrategy({
28+
* clientID: 'example.net',
29+
* clientSecret: 'shhh-its-a-secret'
30+
* callbackURL: 'https://www.example.net/auth/chalkable/callback'
31+
* },
32+
* function(accessToken, refreshToken, profile, done) {
33+
* User.findOrCreate(..., function (err, user) {
34+
* done(err, user);
35+
* });
36+
* }
37+
* ));
38+
*
39+
* @param {Object} options
40+
* @param {Function} verify
41+
* @api public
42+
*/
43+
function Strategy(options, verify) {
44+
options = options || {};
45+
options.authorizationURL = options.authorizationURL || 'https://chalkable-access-control.accesscontrol.windows.net/v2/OAuth2-13';
46+
options.tokenURL = options.tokenURL || 'https://chalkable-access-control.accesscontrol.windows.net/v2/OAuth2-13';
47+
48+
OAuth2Strategy.call(this, options, verify);
49+
this.name = 'chalkable';
50+
}
51+
52+
/**
53+
* Inherit from `OAuth2Strategy`.
54+
*/
55+
util.inherits(Strategy, OAuth2Strategy);
56+
57+
58+
/**
59+
* Retrieve user profile from Chalkable.
60+
*
61+
* This function constructs a normalized profile, with the following properties:
62+
*
63+
* - `provider` always set to `chalkable`
64+
* - `id`
65+
* - `username`
66+
* - `displayName`
67+
*
68+
* @param {String} accessToken
69+
* @param {Function} done
70+
* @api protected
71+
*/
72+
Strategy.prototype.userProfile = function(accessToken, done) {
73+
this._oauth2.get('https://chalkable.com/User/Me.json', accessToken, function (err, body, res) {
74+
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
75+
76+
try {
77+
var json = JSON.parse(body);
78+
79+
var profile = { provider: 'chalkable' };
80+
profile.id = json.id;
81+
profile.displayName = json.displayname;
82+
profile.name = { familyName: json.lastname,
83+
givenName: json.firstname };
84+
profile.emails = [{ value: json.email }];
85+
profile.gender = json.gender;
86+
profile.picture = json.pictureid;
87+
88+
profile._raw = body;
89+
profile._json = json;
90+
91+
done(null, profile);
92+
} catch(e) {
93+
done(e);
94+
}
95+
});
96+
}
97+
98+
/**
99+
* Return extra Chalkable-specific parameters to be included in the authorization
100+
* request.
101+
*
102+
* @param {Object} options
103+
* @return {Object}
104+
* @api protected
105+
*/
106+
Strategy.prototype.authorizationParams = function(options) {
107+
var params = {};
108+
return params;
109+
}
110+
111+
112+
/**
113+
* Expose `Strategy`.
114+
*/
115+
module.exports = Strategy;

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "passport-chalkable-oauth2",
3+
"version": "0.0.1",
4+
"description": "Chalkable (OAuth2) authentication strategies for Passport.",
5+
"keywords": ["passport", "chalkable", "auth", "authn", "authentication", "identity"],
6+
"repository": {
7+
"type": "git",
8+
"url": "git://github.com/headlessme/passport-chalkable-oauth2.git"
9+
},
10+
"bugs": {
11+
"url": "http://github.com/headlessme/passport-chalkable-oauth2/issues"
12+
},
13+
"author": {
14+
"name": "Andy Pritchard",
15+
"email": "contact@coggle.it",
16+
"url": "http://coggle.it/"
17+
},
18+
"licenses": [ {
19+
"type": "MIT",
20+
"url": "http://www.opensource.org/licenses/MIT"
21+
} ],
22+
"main": "./lib/passport-chalkable-oauth2",
23+
"dependencies": {
24+
"pkginfo": "0.2.x",
25+
"passport-oauth": "~0.1.4"
26+
},
27+
"devDependencies": {
28+
},
29+
"scripts": {
30+
},
31+
"engines": { "node": ">= 0.4.0" }
32+
}

0 commit comments

Comments
 (0)