Passport-Coursera-OAuth
Passport strategies for authenticating with Coursera using OAuth 2.0.
Coursera APIs are quite basic. The user profile information only includes a id (user id), timezone, locale and privacy.
I personally found the APIs only useful for checking the users enrollments (https://api.coursera.org/api/users/v1/me/enrollments).
This module lets you authenticate using Coursera in your Node.js applications. By plugging into Passport, Coursera authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
The client id and client secret needed to authenticate with Coursera can be set up from the developer's console Coursera Developer's Console.
Install
$ npm install passport-coursera-oauth
Authenticate Requests
Use passport.authenticate()
, specifying the 'coursera'
strategy, to
authenticate requests.
Authentication with Coursera requires an extra scope parameter. For information, go here.
For example, as route middleware in an Express application:
app.get('/auth/coursera',
passport.authenticate('coursera', { scope: ['view_profile'] }));
app.get('/auth/coursera/callback',
passport.authenticate('coursera', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
Usage of OAuth 2.0
Configure Strategy
The Coursera OAuth 2.0 authentication strategy authenticates users using a Coursera
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 client ID, client secret, and callback URL.
var CourseraStrategy = require('passport-coursera-oauth').OAuth2Strategy;
passport.use(new CourseraStrategy({
clientID: COURSERA_CLIENT_ID,
clientSecret: COURSERA_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/coursera/callback",
profileFields: [ 'timezone', 'locale', 'privacy']
},
function(accessToken, profile, done) {
User.findOrCreate({ courseraId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
Authenticate Requests
Use passport.authenticate()
, specifying the 'coursera'
strategy, to
authenticate requests.
Authentication with Coursera requires an extra scope parameter. For information, go here.
For example, as route middleware in an Express application:
app.get('/auth/coursera',
passport.authenticate('coursera', { scope: ['view_profile'] }));
app.get('/auth/coursera/callback',
passport.authenticate('coursera', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
Examples
For a complete, working example, refer to the OAuth 2.0 example.
Tests
npm install --dev
make test
Credits
License
Copyright (c) 2015-2016 Naval Saini <http://flipflopapp.com/>