Passport-CloudFoundry
Passport strategy for authenticating with Cloud Foundry open PaaS using the OAuth 2.0 API.
Installation
$ npm install passport-cloudfoundry
Usage
Configure Strategy
The CloudFoundry authentication strategy authenticates users using a CloudFoundry
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 cfStrategy = clientID: CF_CLIENT_ID clientSecret: CF_CLIENT_SECRET callbackURL: CF_CALLBACK_URL { //verify callback // asynchronous verification, for effect... process;}; //****************************************// Examples 2 (w/ 'state' parameter)://**************************************** var CloudFoundryStrategy = Strategy;var cfStrategy = clientID: '123-456-789' clientSecret: 'shhh-its-a-secret' callbackURL: 'https://myapp.cloudfoundry.com/auth/cloudfoundry/callback' passReqToCallback: true //<---- Pass this to get req (as 1st param)from CF.com to callback { //Verify things like 'state' in req.query (be sure to set: passReqToCallback=true) ifreqquerystate === 'stateValueIpreviouslySent' User; else ; }; // Set a callback to generate 'state' value. cfStrategy; Where 'generateState' generates and stores is somwhere and returns that random value back // Temporarily store `state` ids var states = {}; // Generates a random value to be used as 'state' param during authorization { var state = uuid; statesstate = true; return state; } // Finally, in your 'finalCallback',check if that state exists ifreqquerystate && statesreqquerystate ; //delete it from memory delete statesreqquerystate; else
Express middlewares
In Express apps, you need to add the below middlewares. Also make sure to add them after Express' session middleware
// Initialize Passport! Also use passport.session() middleware, to support// persistent login sessions (recommended). app; app;
Express routes
For passport to work, you need to add two routes.
app.get('/auth/cloudfoundry'…
to start OAuthapp.get('/auth/cloudfoundry/callback'…
to receive OAuth callback back from Cloud Foundry.
* NOTE: This must match your callback url's path that you used to register the app.
For example, as route middleware in an Express application:
app; app; w/ 'state' app;
Logging out
To logout, call req.logout()
AND cfStrategy.reset()
like below:
app;
An Example OAuth2 App
The example app is located in the /examples folder. But here are the details.
Running the example app
- Git clone this repo to
passport-cloudfoundry
folder (notenpm install
won't download examples, docs etc). cd passport-cloudfoundry/examples/login
npm install
- Open
login/app.js
and setCF_CLIENT_ID
,CF_CLIENT_SECRET
&CF_CALLBACK_URL
- You will get these info when you register your app (see below).
//Set Cloud Foundry app's clientID
var CF_CLIENT_ID = '--insert-cloudfoundry-client-id-here--';
//Set Cloud Foundry app's clientSecret
var CF_CLIENT_SECRET = '--insert-cloudfoundry-client-secret-here--';
// Note: You should have a app.get(..) for this callback to receive callback from Cloud Foundry
// For example: If your callback url is: https://myKoolapp.cloudfoundry.com/auth/cloudfoundry/callback
// then, you should have a HTTP GET endpoint like: app.get('/auth/cloudfoundry/callback', callback))
//
var CF_CALLBACK_URL = '--insert-cloudfoundry--url--/auth/cloudfoundry/callback';
node app.js
to run app onlocalhost:3000
- PS: You should run this app at a pre-registered location that you provided during registering your app.
- Open up browser & open the app.
Registering your app
- Register your app at Cloud Foundry at: http://support.cloudfoundry.com/
- Provide your app name. e.g. MyCoolApp
- Provide a app's server domain for callback. e.g. mycoolapp.cloudfoundry.com
App Workflow
Once the app is up and running, here is how it works:
1. You will see login page like below (Click on 'Login with Cloud Foundry' link):
https://login.cloudfoundry.com/login (default auth url):
2. Your browser will be redirected to
3. If you Sign-in using your cloudfoundry.com account, you will see
4. If you Approve, browser will redirect you to your app w/ access_token & user info. And you will see:
Tests
$ npm install --dev
$ make test
Credits
License
(MIT) VMware
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.