passport-centrify-oauth2

forked OAuth 2.0 authentication strategy for Passport to suit the needs of centrify

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
passport-centrify-oauth2
1.4.14 years ago4 years agoMinified + gzip package size for passport-centrify-oauth2 in KB

Readme

passport-centrify-oauth2
OAuth 2.0 authentication strategy for Passport to be used with centrify. Regular passport-oauth2 was not passing the required query parameters, so I forked it into this module.
This module lets you authenticate using OAuth 2.0 in your Node.js applications. By plugging into Passport, OAuth 2.0 authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
Note that this strategy provides generic OAuth 2.0 support. In many cases, a provider-specific strategy can be used instead, which cuts down on unnecessary configuration, and accommodates any provider-specific quirks. See the list for supported providers.
Developers who need to implement authentication against an OAuth 2.0 provider that is not already supported are encouraged to sub-class this strategy. If you choose to open source the new provider-specific strategy, please add it to the list so other people can find it.

Install

$ npm install passport-centrify-oauth2

Usage

Configure Strategy

The OAuth 2.0 authentication strategy authenticates users using a third-party account and OAuth 2.0 tokens. The provider's OAuth 2.0 endpoints, as well as the client identifer and secret, are specified as options. The strategy requires a verify callback, which receives an access token and profile, and calls cb providing a user.
passport.use(
  new CentrifyOAuth2Strategy(
    {
      // name: 'myStrategy',  // this optional parameter let's you name the strategy as you wish, defaults to 'centrify-oauth2'
      authorizationURL: 'https://www.example.com/oauth2/authorize',
      tokenURL: 'https://www.example.com/oauth2/token',
      clientID: EXAMPLE_CLIENT_ID,
      clientSecret: EXAMPLE_CLIENT_SECRET,
      callbackURL: 'http://localhost:3000/auth/example/callback'
    },
    function(req, accessToken, refreshToken, profile, cb) {
      // profile always comes back as empty object-not sure why, but that's how Centrify works.
      // So in order to get your user profile, you need to
      const userAttributesResponse = await fetch(
        `https://${centrifySubdomain}.my.centrify.com/CDirectoryService/GetUserAttributes`,
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${accessToken}`
          }
        }
      ).then((r) => {
        return r.json()
        /* example how it may look:
        { success: true,
          Result:
           { Name: 'admin@example.co',
             InEverybodyRole: true,
             OauthClient: false,
             LastPasswordChangeDate: '/Date(1532343327336)/',
             DisplayName: 'admin',
             ForcePasswordChangeNext: 'False',
             Description: 'lalala',
             Version: '1',
             Alias: 'example.com',
             Uuid: 'c2c7bcc6-9560-44e0-8dff-5be221cd37ee',
             PictureUri: '/vfslow/etc/cds/pictures/c2c7bcc6-9560-44e0-8dff-5be221cd37ee.pic',
             directoryServiceUuid: '09B9A9B0-6CE8-465F-AB03-65766D33B05E',
             PreferredCulture: '',
             CreateDate: '/Date(1532343175871)/',
             ReportsTo: 'Unassigned',
             Mail: 'admin@example.com',
             State: 'None' }
        }
        */
      })
      User.findOrCreate({ exampleId: userAttributesResponse.Result.Uuid }, function(err, user) {
        return cb(err, user)
      })
    }
  )
)

Authenticate Requests

Use passport.authenticate(), specifying the 'oauth2' strategy, to authenticate requests.
For example, as route middleware in an Express application:
app.get('/auth/example', passport.authenticate('centrify-oauth2'))

app.get(
  '/auth/example/callback',
  passport.authenticate('centrify-oauth2', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/')
  }
)

Contributing

Tests

The test suite is located in the test/ directory. All new features are expected to have corresponding test cases. Ensure that the complete test suite passes by executing:
$ make test

Coverage

All new feature development is expected to have test coverage. Patches that increse test coverage are happily accepted. Coverage reports can be viewed by executing:
$ make test-cov
$ make view-cov