Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

hangouts chat: allow to consume auth data from env var instead of file #1543

Merged
merged 3 commits into from
Dec 20, 2018

Conversation

julbrs
Copy link
Contributor

@julbrs julbrs commented Dec 12, 2018

Instead of setting GOOGLE_APPLICATION_CREDENTIALS with the path of the json google auth file, this PR allow to use GOOGLE_APPLICATION_CREDENTIALS_DATA that contains the actual json data. This idea come from here.

It will allow to use botkit as a hangout chat bot on Heroku like systems.

You need to start botkit like that:

$ GOOGLE_APPLICATION_CREDENTIALS_DATA='{
  "type": "service_account",
  "project_id": "your-project-id",
  "private_key_id": "your-private-key-id",
  "private_key": "your-private-key",
  "client_email": "your-client-email",
  "client_id": "your-client-id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "your-cert-url"
}' npm start

See issue #1533

@julbrs
Copy link
Contributor Author

julbrs commented Dec 12, 2018

I have also create a pull request on the dedicated starter project: howdyai/botkit-starter-googlehangouts#1

@ouadie-lahdioui ouadie-lahdioui self-requested a review December 13, 2018 16:38
Copy link
Collaborator

@ouadie-lahdioui ouadie-lahdioui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your contribution and for sharing your use case 👍

However, google auth library already offers a smart mechanism to load credentials from environment variables without using .fromJSON() directly on our code to get a JWT, it did that for us inside auth.getClient method.

.getClient() method can create the correct credential type for us, depending upon the environment our code is running under. For example : a JWT auth client will be created when our code is running on ourr local developer machine, and a compute client will be created when the same code is running on Google Cloud Platform.

I propose something like that if you don't mind:

    google_hangouts_botkit.middleware.spawn.use(function(worker, next) {

        let params = {
            scopes: 'https://www.googleapis.com/auth/chat.bot',
            ...configuration.google_auth_params
        };

        google
            .auth
            .getClient(params)
            .then(client => {
                ...
            })
            .catch(err => {
                ...
            });
    });
var controller = Botkit.googlehangoutsbot({
    endpoint: 'Axjn86rTGRQwisaYFyT0XZyiOCh7rZUPGx1A',
    token: "YOUR_TOKEN",
    debug: true,
    google_auth_params: {
        credentials: process.env['CREDS']
    }
});

WDYT ?

@julbrs
Copy link
Contributor Author

julbrs commented Dec 15, 2018

Hello,

will test that ASAP !

@julbrs
Copy link
Contributor Author

julbrs commented Dec 18, 2018

Great it work well ! I have also updated the PR of howdyai/botkit-starter-googlehangouts#1

Copy link
Collaborator

@ouadie-lahdioui ouadie-lahdioui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thank you @bobman38

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants