hubot-googledrive-search
Advanced tools
Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "hubot-googledrive-search", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Google drive search for hubot!", | ||
@@ -28,4 +28,5 @@ "main": "index.coffee", | ||
"coffee-script": "^1.10.0", | ||
"googleapis": "^2.1.7" | ||
"googleapis": "^2.1.7", | ||
"hubot-google-auth": "^1.0.3" | ||
} | ||
} |
@@ -9,3 +9,3 @@ // Description: | ||
// Commands: | ||
// hubot drive code <code> | ||
// hubot drive set code <code> | ||
// - used to authenticate the bot initially (see setCode and generateAuthUrl) | ||
@@ -28,24 +28,15 @@ // | ||
var google = require('googleapis'); | ||
var drive = google.drive('v2'); | ||
var CLIENT_ID = process.env.HUBOT_DRIVE_CLIENT_ID, | ||
CLIENT_SECRET = process.env.HUBOT_DRIVE_CLIENT_SECRET, | ||
REDIRECT_URL = process.env.HUBOT_DRIVE_REDIRECT_URL, | ||
SCOPES = 'https://www.googleapis.com/auth/drive'; | ||
// Make sure that these keys do not conflict with things that are already in your hubot's brain | ||
var TOKEN_KEY = 'HUBOT_DRIVE_AUTH_TOKEN', | ||
REFRESH_KEY = 'HUBOT_DRIVE_REFRESH_TOKEN', | ||
EXPIRY_KEY = 'HUBOT_DRIVE_EXPIRE_TIME'; | ||
// We'll initialize the google auth in the module.exports functions | ||
// since this is when we have access to the brain | ||
var HubotGoogleAuth = require('hubot-google-auth'); | ||
var auth; | ||
var CLIENT_ID = process.env.DRIVE_CLIENT_ID, | ||
CLIENT_SECRET = process.env.DRIVE_CLIENT_SECRET, | ||
REDIRECT_URL = process.env.DRIVE_REDIRECT_URL; | ||
// Initialize the oauthClient | ||
var OAuth2 = google.auth.OAuth2; | ||
var oauthClient = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); | ||
google.options({ | ||
auth: oauthClient | ||
}); | ||
/** | ||
* Lists files and links for the given queryString | ||
* For more info on constructing query string see: https://developers.google.com/drive/v2/web/search-parameters | ||
* For more info on constructing query strings see: https://developers.google.com/drive/v2/web/search-parameters | ||
* This will not handle any paging and will only return the top search results given by the drive api | ||
@@ -58,7 +49,22 @@ * | ||
validateToken(function(err, resp) { | ||
var tokens = auth.getTokens(); | ||
// If there are no existing refresh tokens then the user will need to manually authenticate | ||
if (!tokens.refresh_token) { | ||
var authUrl = auth.generateAuthUrl(), | ||
authMsg = `Authorize this app by visiting this url :\n ${authUrl}` + | ||
'\nThen use @hubot drive set code <code>'; | ||
cb({ | ||
err: null, | ||
msg: authMsg | ||
}); | ||
return; | ||
} | ||
auth.validateToken(robot.brain, function(err, resp) { | ||
if (err) { | ||
cb({ | ||
err: err, | ||
msg: err.msg | ||
msg: authMsg | ||
}); | ||
@@ -68,3 +74,3 @@ return; | ||
drive.files.list({ | ||
auth.google.drive('v2').files.list({ | ||
q: queryString | ||
@@ -113,3 +119,3 @@ }, function(err, resp) { | ||
for (var i = 0; i < 5 && i < results.length; i++) { | ||
res += `Name: ${results[i].title} Link: ${results[i].alternateLink}\n`; | ||
res += `#{i+1}.) ${results[i].title} \n${results[i].alternateLink}\n\n`; | ||
} | ||
@@ -120,105 +126,2 @@ | ||
/** | ||
* Stores the token and expire time into the robot brain and | ||
* Sets it in the oauthClient | ||
* | ||
* @param token the token object returned from google oauth2 | ||
*/ | ||
var storeToken = function(token) { | ||
oauthClient.setCredentials(token); | ||
robot.brain.set(TOKEN_KEY, token.access_token); | ||
if (token.refresh_token) { | ||
robot.brain.set(REFRESH_KEY, token.refresh_token); | ||
} | ||
robot.brain.set(EXPIRY_KEY, +token.expiry_date); | ||
robot.brain.save(); | ||
robot.brain.resetSaveInterval(60); | ||
} | ||
/** | ||
* Initially tokens must be created from the command line. | ||
* This requires a user manually inputting a code so it cannot be done by the bot alone. | ||
* This generates the url where the code can be obtained | ||
*/ | ||
var generateAuthUrl = function() { | ||
var scopes = [ | ||
'https://www.googleapis.com/auth/drive' | ||
]; | ||
var authUrl = oauthClient.generateAuthUrl({ | ||
access_type: 'offline', //offline means that we get a refresh token | ||
scope: scopes | ||
}); | ||
return authUrl; | ||
} | ||
/** | ||
* Used to set the code provided by the generated auth url. | ||
* This code is generated for a user and is needed to initiate the oauth2 handshake. | ||
* | ||
* @param code the code obtained by a user from the auth url | ||
*/ | ||
var setCode = function(code, cb) { | ||
oauthClient.getToken(code, function(err, token) { | ||
if (err) { | ||
console.log(err); | ||
cb({ | ||
err: err, | ||
msg: 'Error while trying to retrieve access token' | ||
}); | ||
return; | ||
} | ||
storeToken(token); | ||
cb(null, { | ||
resp: token, | ||
msg: "Drive code successfully set" | ||
}); | ||
}); | ||
} | ||
/** | ||
* Checks the current expire time and determines if the token is valid. | ||
* Refreshes the token if it is not valid. | ||
* | ||
* @param cb the callback function (err, resp), use this to make api calls | ||
*/ | ||
var validateToken = function(cb) { | ||
var at = robot.brain.get(TOKEN_KEY), | ||
rt = robot.brain.get(REFRESH_KEY); | ||
if (at == null || rt == null) { | ||
var authMsg = `Authorize this app by visiting this url :\n ${generateAuthUrl()}` + | ||
'\nThen use @hubot drive set code <code>'; | ||
cb({ | ||
err: null, | ||
msg: authMsg | ||
}); | ||
return; | ||
} | ||
var expirTime = robot.brain.get(EXPIRY_KEY), | ||
curTime = (new Date()) / 1; | ||
if (expirTime < curTime) { | ||
oauthClient.refreshAccessToken(function(err, token) { | ||
if (err != null) { | ||
cb({ | ||
err: err, | ||
msg: 'Drive Authentication Error: error refreshing token' | ||
}, null); | ||
return; | ||
} | ||
storeToken(token); | ||
cb(null, { | ||
resp: token, | ||
msg: 'Token refreshed' | ||
}); | ||
}); | ||
} else { | ||
cb(null); | ||
} | ||
} | ||
// Export robot functions | ||
@@ -228,2 +131,4 @@ var initialBrainLoad = true; | ||
auth = new HubotGoogleAuth(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL, SCOPES, robot.brain); | ||
robot.respond(/drive(\s+set)?\s+code\s+([^\s]+)/i, { | ||
@@ -234,3 +139,3 @@ id: 'drive.set-code' | ||
msg.send('Attempting to set code...') | ||
setCode(code, function(err, resp) { | ||
auth.setCode(code, function(err, resp) { | ||
if (err) { | ||
@@ -248,9 +153,6 @@ msg.send(err.msg); | ||
}, function(msg) { | ||
var tok = robot.brain.get(TOKEN_KEY), | ||
ref_tok = robot.brain.get(REFRESH_KEY), | ||
expire = robot.brain.get(EXPIRY_KEY); | ||
msg.send('token: ' + tok); | ||
msg.send('refresh token: ' + ref_tok); | ||
msg.send('expire date: ' + expire); | ||
var tokens = auth.getTokens(); | ||
for (var name in tokens) { | ||
msg.send(`#{name}: ${tokens[name]}`); | ||
} | ||
}); | ||
@@ -301,18 +203,2 @@ | ||
}); | ||
// Set credentials on load. Does not validate/refresh tokens | ||
robot.brain.on('loaded', function() { | ||
if (!initialBrainLoad) { | ||
return; | ||
} | ||
initialBrainLoad = false; | ||
var at = robot.brain.get(TOKEN_KEY), | ||
rt = robot.brain.get(REFRESH_KEY); | ||
oauthClient.setCredentials({ | ||
access_token: at, | ||
refresh_token: rt | ||
}); | ||
}); | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
11561
3
167
+ Addedhubot-google-auth@^1.0.3
+ Addedhubot-google-auth@1.2.0(transitive)