├── LICENSE ├── README.md ├── lib └── passport-wordpress │ ├── index.js │ └── strategy.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Passport-Wordpress 2 | 3 | [Passport](https://github.com/jaredhanson/passport) strategy for authenticating 4 | with [Wordpress](http://wordpress.com) using the OAuth 2.0 API. 5 | 6 | ## Install 7 | 8 | $ npm install passport-wordpress 9 | 10 | ## Usage 11 | 12 | #### Configure Strategy 13 | 14 | The Wordpress authentication strategy authenticates users using a Wordpress 15 | account and OAuth 2.0 tokens. The strategy requires a `verify` callback, which 16 | accepts these credentials and calls `done` providing a user, as well as 17 | `options` specifying a client ID, client secret, and callback URL. 18 | 19 | passport.use(new WordpressStrategy({ 20 | clientID: CLIENT_ID, 21 | clientSecret: CLIENT_SECRET 22 | }, 23 | function(accessToken, refreshToken, profile, done) { 24 | User.findOrCreate({ WordpressId: profile.id }, function (err, user) { 25 | return done(err, user); 26 | }); 27 | } 28 | )); 29 | 30 | #### Authenticate Requests 31 | 32 | Use `passport.authorize()`, specifying the `'Wordpress'` strategy, to 33 | authenticate requests. 34 | 35 | For example, as route middleware in an [Express](http://expressjs.com/) 36 | application: 37 | 38 | app.get('/auth/wordpress', 39 | passport.authorize('wordpress')); 40 | 41 | app.get('/auth/wordpress/callback', 42 | passport.authorize('wordpress', { failureRedirect: '/login' }), 43 | function(req, res) { 44 | // Successful authentication, redirect home. 45 | res.redirect('/'); 46 | }); 47 | 48 | ## Thanks 49 | 50 | - [Jared Hanson](http://github.com/jaredhanson) 51 | 52 | ## License 53 | 54 | [The MIT License](http://opensource.org/licenses/MIT) 55 | 56 | Copyright (c) 2013 Michael Pearson <[http://github.com/mjpearson](http://github.com/mjpearson)> -------------------------------------------------------------------------------- /lib/passport-wordpress/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies. 3 | */ 4 | var Strategy = require('./strategy'); 5 | 6 | 7 | /** 8 | * Framework version. 9 | */ 10 | require('pkginfo')(module, 'version'); 11 | 12 | /** 13 | * Expose constructors. 14 | */ 15 | exports.Strategy = Strategy; 16 | -------------------------------------------------------------------------------- /lib/passport-wordpress/strategy.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies. 3 | */ 4 | var util = require('util') 5 | , OAuth2Strategy = require('passport-oauth').OAuth2Strategy; 6 | 7 | 8 | /** 9 | * `Strategy` constructor. 10 | * 11 | * The Wordpress authentication strategy authenticates requests by delegating 12 | * to Wordpress using the OAuth 2.0 protocol. 13 | * 14 | * Applications must supply a `verify` callback which accepts an `accessToken`, 15 | * `refreshToken` and service-specific `profile`, and then calls the `done` 16 | * callback supplying a `user`, which should be set to `false` if the 17 | * credentials are not valid. If an exception occured, `err` should be set. 18 | * 19 | * Options: 20 | * - `clientID` your Wordpress application's client id 21 | * - `clientSecret` your Wordpress application's client secret 22 | * - `callbackURL` URL to which Wordpress will redirect the user after granting authorization 23 | * 24 | * Examples: 25 | * 26 | * passport.use(new WordpressStrategy({ 27 | * clientID: '123-456-789', 28 | * clientSecret: 'shhh-its-a-secret' 29 | * callbackURL: 'https://www.example.net/auth/wordpress/callback' 30 | * }, 31 | * function(accessToken, refreshToken, profile, done) { 32 | * User.findOrCreate(..., function (err, user) { 33 | * done(err, user); 34 | * }); 35 | * } 36 | * )); 37 | * 38 | * @param {Object} options 39 | * @param {Function} verify 40 | * @api public 41 | */ 42 | function Strategy(options, verify) { 43 | options = options || {}; 44 | options.authorizationURL = options.authorizationURL || 'https://public-api.wordpress.com/oauth2/authorize'; 45 | options.tokenURL = options.tokenURL || 'https://public-api.wordpress.com/oauth2/token'; 46 | this.profileUrl = options.profileUrl || "https://public-api.wordpress.com/rest/v1/me"; 47 | 48 | OAuth2Strategy.call(this, options, verify); 49 | this.name = 'wordpress'; 50 | } 51 | 52 | /** 53 | * Inherit from `OAuth2Strategy`. 54 | */ 55 | util.inherits(Strategy, OAuth2Strategy); 56 | 57 | /** 58 | * Retrieve user profile from Wordpress. 59 | * 60 | * This function constructs a normalized profile, with the following properties: 61 | * 62 | * - `provider` always set to `wordpress` 63 | * - `id` the user's ID 64 | * - `displayName` the user's username 65 | * 66 | * @param {String} accessToken 67 | * @param {Function} done 68 | * @api protected 69 | */ 70 | Strategy.prototype.userProfile = function(accessToken, done) { 71 | this._oauth2.useAuthorizationHeaderforGET(true); 72 | 73 | this._oauth2.get(this.profileUrl, accessToken, function (err, body, res) { 74 | if (err) { return done(err); } 75 | 76 | try { 77 | var json = JSON.parse(body); 78 | 79 | var profile = { provider: 'Wordpress' }; 80 | profile.id = json.ID || json.id; 81 | profile.displayName = json.username; 82 | 83 | profile._raw = body; 84 | profile._json = json; 85 | done(null, profile); 86 | } catch(e) { 87 | done(e); 88 | } 89 | }); 90 | } 91 | 92 | /** The default oauth2 strategy puts the access_token into Authorization: header AND query string 93 | * witch is a violation of the RFC so lets override and not add the header and supply only the token for qs. 94 | */ 95 | Strategy.prototype.get = function(url, access_token, callback) { 96 | this._oauth2._request("GET", url, {}, "", access_token, callback ); 97 | }; 98 | 99 | /** 100 | * Expose `Strategy`. 101 | */ 102 | module.exports = Strategy; 103 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "passport-wordpress", 3 | "version": "0.0.4", 4 | "description": "Wordpress OAuth2 strategy for Passport.", 5 | "keywords": [ 6 | "passport", 7 | "wordpress", 8 | "auth", 9 | "authn", 10 | "authentication", 11 | "identity" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/mjpearson/passport-wordpress.git" 16 | }, 17 | "bugs": { 18 | "url": "http://github.com/mjpearson/passport-wordpress/issues" 19 | }, 20 | "licenses": [ 21 | { 22 | "type": "MIT", 23 | "url": "http://www.opensource.org/licenses/MIT" 24 | } 25 | ], 26 | "main": "./lib/passport-wordpress", 27 | "dependencies": { 28 | "pkginfo": "0.2.x", 29 | "passport-oauth": "1.x.x" 30 | }, 31 | "engines": { 32 | "node": ">= 0.4.0" 33 | }, 34 | "_id": "passport-wordpress@0.0.3", 35 | "_from": "passport-wordpress@>=0.0.1" 36 | } 37 | --------------------------------------------------------------------------------