<commit_before>passport-google-id-token
========================

[![Build Status](https://travis-ci.org/jmreyes/passport-google-id-token.svg?branch=master)](https://travis-ci.org/jmreyes/passport-google-id-token)

Google ID token authentication strategy for [Passport](http://passportjs.org/) and [Node.js](http://nodejs.org/).

This module lets you authenticate using Google ID tokens in your Node.js applications.
This is useful for scenarios where we don't want to perform API calls to Google on behalf of the client, but we only want to authenticate it to our server. In short, we only validate the identity of the user by token verification, so there is no server-side OAuth operation.

Official Google documentation:

- [Authenticate with a backend server](https://developers.google.com/identity/sign-in/android/backend-auth)

More information about ID token use cases:

- [Client-Server Authentication with ID tokens](http://www.riskcompletefailure.com/2013/11/client-server-authentication-with-id.html)
- [Verifying Back-End Calls from Android Apps](http://android-developers.blogspot.in/2013/01/verifying-back-end-calls-from-android.html)

## Install

    $ npm install passport-google-id-token

## Usage

#### Configure Strategy

The strategy requires a `verify` callback which accepts the `idToken` coming from the user to be authenticated, and then calls the `done` callback supplying a `parsedToken` (with all its information in visible form) and the `googleId`.

The strategy also requires the Google client ID(s) inside the passed `options`.
An optional `getGoogleCerts` function can be specified to customize the way the Google certificates are retrieved, interesting e.g. in case a caching mechanism is needed. If not specified, the default mechanism will query the Google servers every time.

```js
var GoogleTokenStrategy = require('passport-google-id-token');

passport.use(new GoogleTokenStrategy({
    clientID: GOOGLE_CLIENT_ID,
    getGoogleCerts: optionalCustomGetGoogleCerts
  },
  function(parsedToken, googleId, done) {
    User.findOrCreate({ googleId: googleId }, function (err, user) {
      return done(err, user);
    });
  }
));
```

#### Authenticate Requests

Use `passport.authenticate()`, specifying the `'google-id-token'` strategy, to authenticate requests.

```js
app.post('/auth/google',
  passport.authenticate('google-id-token'),<commit_msg>added description for jwtOptions in README<commit_after>passport-google-id-token
========================

[![Build Status](https://travis-ci.org/jmreyes/passport-google-id-token.svg?branch=master)](https://travis-ci.org/jmreyes/passport-google-id-token)

Google ID token authentication strategy for [Passport](http://passportjs.org/) and [Node.js](http://nodejs.org/).

This module lets you authenticate using Google ID tokens in your Node.js applications.
This is useful for scenarios where we don't want to perform API calls to Google on behalf of the client, but we only want to authenticate it to our server. In short, we only validate the identity of the user by token verification, so there is no server-side OAuth operation.

Official Google documentation:

- [Authenticate with a backend server](https://developers.google.com/identity/sign-in/android/backend-auth)

More information about ID token use cases:

- [Client-Server Authentication with ID tokens](http://www.riskcompletefailure.com/2013/11/client-server-authentication-with-id.html)
- [Verifying Back-End Calls from Android Apps](http://android-developers.blogspot.in/2013/01/verifying-back-end-calls-from-android.html)

## Install

    $ npm install passport-google-id-token

## Usage

#### Configure Strategy

The strategy requires a `verify` callback which accepts the `idToken` coming from the user to be authenticated, and then calls the `done` callback supplying a `parsedToken` (with all its information in visible form) and the `googleId`.

The strategy also requires the Google client ID(s) inside the passed `options`.
An optional `getGoogleCerts` function can be specified to customize the way the Google certificates are retrieved, interesting e.g. in case a caching mechanism is needed. If not specified, the default mechanism will query the Google servers every time. Also, an optional `jwtOptions` can be specified, which will be passed onto [jwt](https://github.com/auth0/node-jsonwebtoken), e.g. `jwtOptions : {"ignoreExpiration":true}` to ignore expiration time on tokens.

```js
var GoogleTokenStrategy = require('passport-google-id-token');

passport.use(new GoogleTokenStrategy({
    clientID: GOOGLE_CLIENT_ID,
    getGoogleCerts: optionalCustomGetGoogleCerts
  },
  function(parsedToken, googleId, done) {
    User.findOrCreate({ googleId: googleId }, function (err, user) {
      return done(err, user);
    });
  }
));
```

#### Authenticate Requests

Use `passport.authenticate()`, specifying the `'google-id-token'` strategy, to authenticate requests.

```js
app.post('/auth/google',
  passport.authenticate('google-id-token'),
{'fraction_non_alphanumeric': 0.11477045908183632, 'fraction_numerical': 0.00499001996007984, 'mean_word_length': 4.96547619047619, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 7, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2782728', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1382, 'n_words': 486}