lib/strategy.js
function OAuth2Strategy(options, verify) {
  if (typeof options == 'function') {
    verify = options;
    options = undefined;
  }
  options = options || {};

  if (!verify) { throw new TypeError('OAuth2Strategy requires a verify callback'); }
  if (!options.authorizationURL) { throw new TypeError('OAuth2Strategy requires a authorizationURL option'); }
  if (!options.tokenURL) { throw new TypeError('OAuth2Strategy requires a tokenURL option'); }
  if (!options.clientID) { throw new TypeError('OAuth2Strategy requires a clientID option'); }

  passport.Strategy.call(this);
  this.name = 'oauth2';
  this._verify = verify;

  // NOTE: The _oauth2 property is considered "protected".  Subclasses are
  //       allowed to use it when making protected resource requests to retrieve
  //       the user profile.
  this._oauth2 = new OAuth2(options.clientID,  options.clientSecret,
                            '', options.authorizationURL, options.tokenURL, options.customHeaders);

  this._callbackURL = options.callbackURL;
  this._scope = options.scope;
  this._scopeSeparator = options.scopeSeparator || ' ';
  this._pkceMethod = (options.pkce === true) ? 'S256' : options.pkce;
  this._key = options.sessionKey || ('oauth2:' + url.parse(options.authorizationURL).hostname);

  if (options.store) {
    this._stateStore = options.store;
  } else {
    if (options.state) {
      this._stateStore = options.pkce ? new PKCESessionStateStore({ key: this._key }) : new SessionStateStore({ key: this._key });
    } else {
      if (options.pkce) { throw new TypeError('OAuth2Strategy requires `state: true` option when PKCE is enabled'); }
      this._stateStore = new NullStateStore();
    }
  }
  this._trustProxy = options.proxy;
  this._passReqToCallback = options.passReqToCallback;
  this._skipUserProfile = (options.skipUserProfile === undefined) ? false : options.skipUserProfile;
}
function OAuth2Strategy(options, verify) {
  if (typeof options == 'function') {
    verify = options;
    options = undefined;
  }
  options = options || {};

  if (!verify) { throw new TypeError('OAuth2Strategy requires a verify callback'); }
  if (!options.authorizationURL) { throw new TypeError('OAuth2Strategy requires a authorizationURL option'); }
  if (!options.tokenURL) { throw new TypeError('OAuth2Strategy requires a tokenURL option'); }
  if (!options.clientID) { throw new TypeError('OAuth2Strategy requires a clientID option'); }

  passport.Strategy.call(this);
  this.name = 'oauth2';
  this._verify = verify;

  // NOTE: The _oauth2 property is considered "protected".  Subclasses are
  //       allowed to use it when making protected resource requests to retrieve
  //       the user profile.
  this._oauth2 = new OAuth2(options.clientID,  options.clientSecret,
                            '', options.authorizationURL, options.tokenURL, options.customHeaders);

  this._callbackURL = options.callbackURL;
  this._scope = options.scope;
  this._scopeSeparator = options.scopeSeparator || ' ';
  this._pkceMethod = (options.pkce === true) ? 'S256' : options.pkce;
  this._key = options.sessionKey || ('oauth2:' + url.parse(options.authorizationURL).hostname);

  if (options.store) {
    this._stateStore = options.store;
  } else {
    if (options.state) {
      this._stateStore = options.pkce ? new PKCESessionStateStore({ key: this._key }) : new SessionStateStore({ key: this._key });
    } else {
      if (options.pkce) { throw new TypeError('OAuth2Strategy requires `state: true` option when PKCE is enabled'); }
      this._stateStore = new NullStateStore();
    }
  }
  this._trustProxy = options.proxy;
  this._passReqToCallback = options.passReqToCallback;
  this._skipUserProfile = (options.skipUserProfile === undefined) ? false : options.skipUserProfile;
}
function OAuth2Strategy ( options , verify ) { if ( typeof options == 'function' ) { verify = options ; options = undefined ; } options = options || { } ; if ( ! verify ) { throw new TypeError ( 'OAuth2Strategy requires a verify callback' ) ; } if ( ! options . authorizationURL ) { throw new TypeError ( 'OAuth2Strategy requires a authorizationURL option' ) ; } if ( ! options . tokenURL ) { throw new TypeError ( 'OAuth2Strategy requires a tokenURL option' ) ; } if ( ! options . clientID ) { throw new TypeError ( 'OAuth2Strategy requires a clientID option' ) ; } passport . Strategy . call ( this ) ; this . name = 'oauth2' ; this . _verify = verify ; // NOTE: The _oauth2 property is considered "protected".  Subclasses are //       allowed to use it when making protected resource requests to retrieve //       the user profile. this . _oauth2 = new OAuth2 ( options . clientID , options . clientSecret , '' , options . authorizationURL , options . tokenURL , options . customHeaders ) ; this . _callbackURL = options . callbackURL ; this . _scope = options . scope ; this . _scopeSeparator = options . scopeSeparator || ' ' ; this . _pkceMethod = ( options . pkce === true ) ? 'S256' : options . pkce ; this . _key = options . sessionKey || ( 'oauth2:' + url . parse ( options . authorizationURL ) . hostname ) ; if ( options . store ) { this . _stateStore = options . store ; } else { if ( options . state ) { this . _stateStore = options . pkce ? new PKCESessionStateStore ( { key : this . _key } ) : new SessionStateStore ( { key : this . _key } ) ; } else { if ( options . pkce ) { throw new TypeError ( 'OAuth2Strategy requires `state: true` option when PKCE is enabled' ) ; } this . _stateStore = new NullStateStore ( ) ; } } this . _trustProxy = options . proxy ; this . _passReqToCallback = options . passReqToCallback ; this . _skipUserProfile = ( options . skipUserProfile === undefined ) ? false : options . skipUserProfile ; }
Creates an instance of `OAuth2Strategy`.

The OAuth 2.0 authentication strategy authenticates requests using the OAuth
2.0 framework.

OAuth 2.0 provides a facility for delegated authentication, whereby users can
authenticate using a third-party service such as Facebook.  Delegating in
this manner involves a sequence of events, including redirecting the user to
the third-party service for authorization.  Once authorization has been
granted, the user is redirected back to the application and an authorization
code can be used to obtain credentials.

Applications must supply a `verify` callback, for which the function
signature is:

function(accessToken, refreshToken, profile, done) { ... }

The verify callback is responsible for finding or creating the user, and
invoking `done` with the following arguments:

done(err, user, info);

`user` should be set to `false` to indicate an authentication failure.
Additional `info` can optionally be passed as a third argument, typically
used to display informational messages.  If an exception occured, `err`
should be set.

Options:

- `authorizationURL`  URL used to obtain an authorization grant
- `tokenURL`          URL used to obtain an access token
- `clientID`          identifies client to service provider
- `clientSecret`      secret used to establish ownership of the client identifer
- `callbackURL`       URL to which the service provider will redirect the user after obtaining authorization
- `passReqToCallback` when `true`, `req` is the first argument to the verify callback (default: `false`)

Examples:

passport.use(new OAuth2Strategy({
authorizationURL: 'https://www.example.com/oauth2/authorize',
tokenURL: 'https://www.example.com/oauth2/token',
clientID: '123-456-789',
clientSecret: 'shhh-its-a-secret'
callbackURL: 'https://www.example.net/auth/example/callback'
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate(..., function (err, user) {
done(err, user);
});
}
));

@constructor
@param {Object} options
@param {Function} verify
@api public
Creates an instance of OAuth2Strategy .