Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Auth through Service Account in env variable #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var colors = require('colors');
var OAuth2Client = require('googleapis').auth.OAuth2;
var ServiceToken = require('googleapis').auth.JWT;

var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
Expand All @@ -15,6 +16,12 @@ module.exports = function authenticate() {
};

function getCredentials() {
if (process.env.GOOGLE_APPS_PRIVATE_KEY) {
const buffer = Buffer.from(process.env.GOOGLE_APPS_PRIVATE_KEY, 'base64').toString();
const json = JSON.parse(buffer);
json.serviceAccount = true;
return Promise.resolve(json);
}
return fs.readFileAsync(defaults.STORAGE_FILE)
.then(JSON.parse)
.catch(SyntaxError, function(e) {
Expand All @@ -27,6 +34,34 @@ function getCredentials() {
}

function createAuthClient(credentials) {
if (credentials.serviceAccount == true) {
var auth = new ServiceToken(
credentials.client_email,
null, // 'path/to/key.pem',

// Contents of private_key.pem if you want to load the pem file yourself
// (do not use the path parameter above if using this param)
credentials.private_key,

// Scopes can be specified either as an array or as a single, space-delimited string
// ['https://spreadsheets.google.com/feeds'], // TODO: check if this is correct
[
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.apps.readonly',
],

// User to impersonate (leave empty if no impersonation needed)
null
);

return Promise.promisify(auth.authorize.bind(auth))()
.then(function(res) {
return auth;
})
}

var auth = new OAuth2Client(
credentials.client_id,
credentials.client_secret,
Expand Down