This commit is contained in:
GotPPay
2017-10-16 20:21:19 +02:00
parent 8a7f8794cf
commit a75ea978f9
2554 changed files with 804218 additions and 109 deletions

1
backend/node_modules/gtoken/.npmignore generated vendored Normal file
View File

@@ -0,0 +1 @@
node_modules

3
backend/node_modules/gtoken/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"

21
backend/node_modules/gtoken/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Ryan Seys
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

140
backend/node_modules/gtoken/README.md generated vendored Normal file
View File

@@ -0,0 +1,140 @@
# node-gtoken
Node.js Google Authentication Service Account Tokens
[![Build Status](https://travis-ci.org/ryanseys/node-gtoken.svg?branch=master)](https://travis-ci.org/ryanseys/node-gtoken)
## Installation
``` sh
npm install gtoken
```
## Usage
### Use with a `.pem` or `.p12` key file:
``` js
var GoogleToken = require('gtoken');
var gtoken = GoogleToken({
keyFile: 'path/to/key.pem', // or path to .p12 key file
email: 'my_service_account_email@developer.gserviceaccount.com',
scope: ['https://scope1', 'https://scope2'] // or space-delimited string of scopes
});
gtoken.getToken(function(err, token) {
if (err) {
console.log(err);
return;
}
console.log(token);
});
```
### Use with a service account `.json` key file:
``` js
var GoogleToken = require('gtoken');
var gtoken = GoogleToken({
keyFile: 'path/to/key.json',
scope: ['https://scope1', 'https://scope2'] // or space-delimited string of scopes
});
gtoken.getToken(function(err, token) {
if (err) {
console.log(err);
return;
}
console.log(token);
});
```
### Pass the private key as a string directly:
``` js
var key = '-----BEGIN RSA PRIVATE KEY-----\nXXXXXXXXXXX...';
var GoogleToken = require('gtoken');
var gtoken = GoogleToken({
email: 'my_service_account_email@developer.gserviceaccount.com',
scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes
key: key
});
```
## Options
> Various options that can be set when creating initializing the `gtoken` object.
- `options.email or options.iss`: The service account email address.
- `options.scope`: An array of scope strings or space-delimited string of scopes.
- `options.sub`: The email address of the user requesting delegated access.
- `options.keyFile`: The filename of `.json` key, `.pem` key or `.p12` key.
- `options.key`: The raw RSA private key value, in place of using `options.keyFile`.
### .getToken(callback)
> Returns the cached token or requests a new one and returns it.
``` js
gtoken.getToken(function(err, token) {
console.log(err || token);
// gtoken.token value is also set
});
```
### Properties
> Various properties set on the gtoken object after call to `.getToken()`.
- `gtoken.token`: The access token.
- `gtoken.expires_at`: The expiry date as milliseconds since 1970/01/01
- `gtoken.key`: The raw key value.
- `gtoken.raw_token`: Most recent raw token data received from Google.
### .hasExpired()
> Returns true if the token has expired, or token does not exist.
``` js
gtoken.getToken(function(err, token) {
if(token) {
gtoken.hasExpired(); // false
}
});
```
### .revokeToken()
> Revoke the token if set.
``` js
gtoken.revokeToken(function(err) {
if (err) {
console.log(err);
return;
}
console.log('Token revoked!');
});
```
## Downloading your private `.p12` key from Google
1. Open the [Google Developer Console][gdevconsole].
2. Open your project and under "APIs & auth", click Credentials.
3. Generate a new `.p12` key and download it into your project.
## Converting your `.p12` key to a `.pem` key
You can just specify your `.p12` file (with `.p12` extension) as the `keyFile` and it will automatically be converted to a `.pem` on the fly, however this results in a slight performance hit. If you'd like to convert to a `.pem` for use later, use OpenSSL if you have it installed.
``` sh
$ openssl pkcs12 -in key.p12 -nodes -nocerts > key.pem
```
Don't forget, the passphrase when converting these files is the string `'notasecret'`
## License
MIT
[gdevconsole]: https://console.developers.google.com

247
backend/node_modules/gtoken/lib/index.js generated vendored Normal file
View File

@@ -0,0 +1,247 @@
var gp12pem = require('google-p12-pem');
var request = require('request');
var mime = require('mime');
var jws = require('jws');
var fs = require('fs');
var GOOGLE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token';
var GOOGLE_REVOKE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/revoke?token=';
/**
* Create a GoogleToken.
*
* @param {object} options Configuration object.
*/
function GoogleToken(options) {
if (!(this instanceof GoogleToken)) {
return new GoogleToken(options);
}
this._configure(options);
}
GoogleToken.prototype._mime = mime;
/**
* Returns whether the token has expired.
*
* @return {Boolean} true if the token has expired, false otherwise.
*/
GoogleToken.prototype.hasExpired = function() {
var now = (new Date()).getTime();
if (this.token && this.expires_at) {
return now >= this.expires_at;
} else {
return true;
}
};
GoogleToken.prototype._gp12pem = gp12pem;
/**
* Returns a cached token or retrieves a new one from Google.
*
* @param {Function} callback The callback function.
*/
GoogleToken.prototype.getToken = function(callback) {
var self = this;
if (!this.hasExpired()) {
return callback(null, this.token);
} else {
if (!this.key && !this.keyFile) {
callback(new Error('No key or keyFile set.'));
return;
} else if (!this.key && this.keyFile) {
var mimeType = this._mime.lookup(this.keyFile);
if (mimeType === 'application/json') {
// json file
fs.readFile(this.keyFile, handleJSONKey);
} else {
// Must be a .p12 file or .pem key
if (!self.iss) {
var error = new Error('email is required.');
error.code = 'MISSING_CREDENTIALS';
callback(error);
return;
}
if (mimeType === 'application/x-pkcs12') {
// convert to .pem on the fly
self._gp12pem(this.keyFile, handleKey);
} else {
// assume .pem key otherwise
fs.readFile(this.keyFile, handleKey);
}
}
} else {
return this._requestToken(callback);
}
}
function handleJSONKey(err, key) {
if (err) {
callback(err);
return;
}
try {
var body = JSON.parse(key);
self.key = body.private_key;
self.iss = body.client_email;
} catch (e) {
callback(e);
return;
}
if (!self.key || !self.iss) {
var error = new Error('private_key and client_email are required.');
error.code = 'MISSING_CREDENTIALS';
callback(error);
return;
}
self._requestToken(callback);
}
function handleKey(err, key) {
if (err) {
callback(err);
return;
}
self.key = key;
self._requestToken(callback);
}
};
/**
* Revoke the token if one is set.
*
* @param {Function} callback The callback function.
*/
GoogleToken.prototype.revokeToken = function(callback) {
var self = this;
if (this.token) {
this._request(GOOGLE_REVOKE_TOKEN_URL + this.token, function(err, res) {
if (err) {
callback(err);
return;
}
self._configure({
email: self.iss,
sub: self.sub,
key: self.key,
keyFile: self.keyFile,
scope: self.scope
});
callback();
});
} else {
callback(new Error('No token to revoke.'));
}
};
/**
* Configure the GoogleToken for re-use.
* @param {object} options Configuration object.
*/
GoogleToken.prototype._configure = function(options) {
var self = this;
options = options || {};
this.keyFile = options.keyFile;
this.key = options.key;
this._request = request;
this.token = this.expires_at = this.raw_token = null;
this.iss = options.email || options.iss;
if (options.sub) {
this.sub = options.sub;
}
if (typeof options.scope === 'object') {
this.scope = options.scope.join(' ');
} else {
this.scope = options.scope;
}
};
/**
* Request the token from Google.
*
* @param {Function} callback The callback function.
*/
GoogleToken.prototype._requestToken = function(callback) {
var self = this;
var iat = Math.floor(new Date().getTime() / 1000);
var payload = {
iss: this.iss,
scope: this.scope,
aud: GOOGLE_TOKEN_URL,
exp: iat + 3600, // 3600 seconds = 1 hour
iat: iat
};
if (this.sub) {
payload.sub = this.sub;
}
var toSign = {
header: {
alg: 'RS256',
typ: 'JWT'
},
payload: payload,
secret: this.key
};
return this._signJWT(toSign, function(err, signedJWT) {
if (err) {
callback(err, null);
return;
}
return self._request({
method: 'post',
url: GOOGLE_TOKEN_URL,
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signedJWT
}
}, function(err, res, body) {
try {
body = JSON.parse(body);
} catch (e) {
body = {};
}
err = err || body.error && new Error(body.error);
if (err) {
self.token = null;
self.token_expires = null;
callback(err, null);
return;
}
self.raw_token = body;
self.token = body.access_token;
self.expires_at = (iat + body.expires_in) * 1000;
return callback(null, self.token);
});
});
};
/**
* Sign the JWT object, returning any errors in the callback.
*
* @param {object} opts The configuration object.
* @param {Function} callback The callback function.
*/
GoogleToken.prototype._signJWT = function(opts, callback) {
try {
var signedJWT = jws.sign(opts);
return callback(null, signedJWT);
} catch (err) {
callback(err, null);
}
};
module.exports = GoogleToken;

64
backend/node_modules/gtoken/package.json generated vendored Normal file
View File

@@ -0,0 +1,64 @@
{
"_from": "gtoken@^1.2.1",
"_id": "gtoken@1.2.2",
"_inBundle": false,
"_integrity": "sha1-Fyd2oanZasCfwioA9b6DzubeiCA=",
"_location": "/gtoken",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "gtoken@^1.2.1",
"name": "gtoken",
"escapedName": "gtoken",
"rawSpec": "^1.2.1",
"saveSpec": null,
"fetchSpec": "^1.2.1"
},
"_requiredBy": [
"/google-auth-library"
],
"_resolved": "https://registry.npmjs.org/gtoken/-/gtoken-1.2.2.tgz",
"_shasum": "172776a1a9d96ac09fc22a00f5be83cee6de8820",
"_spec": "gtoken@^1.2.1",
"_where": "/home/bilal/Saburly/slucajna-televizija/backend/node_modules/google-auth-library",
"author": {
"name": "Ryan Seys"
},
"bugs": {
"url": "https://github.com/ryanseys/node-gtoken/issues"
},
"bundleDependencies": false,
"dependencies": {
"google-p12-pem": "^0.1.0",
"jws": "^3.0.0",
"mime": "^1.2.11",
"request": "^2.72.0"
},
"deprecated": false,
"description": "Node.js Google Authentication Service Account Tokens",
"devDependencies": {
"mocha": "^2.0.1"
},
"homepage": "https://github.com/ryanseys/node-gtoken#readme",
"keywords": [
"google",
"service",
"account",
"api",
"token",
"api",
"auth"
],
"license": "MIT",
"main": "./lib/index.js",
"name": "gtoken",
"repository": {
"type": "git",
"url": "git+https://github.com/ryanseys/node-gtoken.git"
},
"scripts": {
"test": "mocha --timeout 5000"
},
"version": "1.2.2"
}

View File

@@ -0,0 +1,6 @@
{
"private_key_id": "exampleprivatekeyid",
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDnKHOGLUthrSiZ73RA7OGuVj4gAgw7QhLK5PecVk/jLAYgtiA+\nlZKgVOMQjGSAH3J3WDVdJNP2AjaMPb93EOIOAGVdz9Olwh0BKYHmPIgqRHjCw4Il\nyPJIkoBkeTpUXxJl46R6cdeXOzAJal2fxom3yUqvkXsJ9+owREa3WwaZdwIDAQAB\nAoGBAM6Mlq2OS4077Muf64EYKZBd/6UaL1PD9obVkWtgtIGJFPClzKoffKVHNJ3U\n1PinIOEDMKvJ/JwV3ifyBOOfclzdhwQId3Stvr2/7Y0O+sTxQ8bNPu7FuXeiEVg8\ncc2g2r1gzrieK1F+innPIzON8uwhRfnGYUBjgVlAqjSUmeIBAkEA+0Nmm2lYW8yZ\nffukkdiYOUANytg6u4u2Y8NTgKSqjdhUu2FZw+RK5mGFnT9qTR5/hJHEOl8l5GxP\nhxU+Ck1AUwJBAOuEBa+J7hyBvHMZXWFZT4sLIyF8nGnVGKMWnwoqvijm1vtxvXDN\nCh6vxbe6UK2R2wNQcU0yzaw+qVVhFXBGrc0CQAzWAKPn2c+2wBCjjmC1A/HIx+uY\n6Ec0d/jYvadQwjXE229HJeLQAtOKH91MpA2UhUcsey3QNotYzSdFeonkMGkCQBUK\na3OeT/6N/KcPyg7rarbSXOYd+t9doWDYoYbIAMsorGY0aCdOuiu5qmFarOCE2ls2\ncZjjBAryYjB1hvPL7LUCQC8d8Y1v7z6oS2tMZukAcUUlu38K5dwysdgY3dhgmNWl\nNPyXxhPIogoPsSRFbS4PjJpNUFqRY/GTNO/btmBXvts\u003d\n-----END RSA PRIVATE KEY-----\n",
"client_id": "example.apps.googleusercontent.com",
"type": "service_account"
}

7
backend/node_modules/gtoken/test/assets/key.json generated vendored Normal file
View File

@@ -0,0 +1,7 @@
{
"private_key_id": "exampleprivatekeyid",
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDnKHOGLUthrSiZ73RA7OGuVj4gAgw7QhLK5PecVk/jLAYgtiA+\nlZKgVOMQjGSAH3J3WDVdJNP2AjaMPb93EOIOAGVdz9Olwh0BKYHmPIgqRHjCw4Il\nyPJIkoBkeTpUXxJl46R6cdeXOzAJal2fxom3yUqvkXsJ9+owREa3WwaZdwIDAQAB\nAoGBAM6Mlq2OS4077Muf64EYKZBd/6UaL1PD9obVkWtgtIGJFPClzKoffKVHNJ3U\n1PinIOEDMKvJ/JwV3ifyBOOfclzdhwQId3Stvr2/7Y0O+sTxQ8bNPu7FuXeiEVg8\ncc2g2r1gzrieK1F+innPIzON8uwhRfnGYUBjgVlAqjSUmeIBAkEA+0Nmm2lYW8yZ\nffukkdiYOUANytg6u4u2Y8NTgKSqjdhUu2FZw+RK5mGFnT9qTR5/hJHEOl8l5GxP\nhxU+Ck1AUwJBAOuEBa+J7hyBvHMZXWFZT4sLIyF8nGnVGKMWnwoqvijm1vtxvXDN\nCh6vxbe6UK2R2wNQcU0yzaw+qVVhFXBGrc0CQAzWAKPn2c+2wBCjjmC1A/HIx+uY\n6Ec0d/jYvadQwjXE229HJeLQAtOKH91MpA2UhUcsey3QNotYzSdFeonkMGkCQBUK\na3OeT/6N/KcPyg7rarbSXOYd+t9doWDYoYbIAMsorGY0aCdOuiu5qmFarOCE2ls2\ncZjjBAryYjB1hvPL7LUCQC8d8Y1v7z6oS2tMZukAcUUlu38K5dwysdgY3dhgmNWl\nNPyXxhPIogoPsSRFbS4PjJpNUFqRY/GTNO/btmBXvts\u003d\n-----END RSA PRIVATE KEY-----\n",
"client_email": "email@developer.gserviceaccount.com",
"client_id": "example.apps.googleusercontent.com",
"type": "service_account"
}

BIN
backend/node_modules/gtoken/test/assets/key.p12 generated vendored Normal file

Binary file not shown.

15
backend/node_modules/gtoken/test/assets/key.pem generated vendored Normal file
View File

@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDnKHOGLUthrSiZ73RA7OGuVj4gAgw7QhLK5PecVk/jLAYgtiA+
lZKgVOMQjGSAH3J3WDVdJNP2AjaMPb93EOIOAGVdz9Olwh0BKYHmPIgqRHjCw4Il
yPJIkoBkeTpUXxJl46R6cdeXOzAJal2fxom3yUqvkXsJ9+owREa3WwaZdwIDAQAB
AoGBAM6Mlq2OS4077Muf64EYKZBd/6UaL1PD9obVkWtgtIGJFPClzKoffKVHNJ3U
1PinIOEDMKvJ/JwV3ifyBOOfclzdhwQId3Stvr2/7Y0O+sTxQ8bNPu7FuXeiEVg8
cc2g2r1gzrieK1F+innPIzON8uwhRfnGYUBjgVlAqjSUmeIBAkEA+0Nmm2lYW8yZ
ffukkdiYOUANytg6u4u2Y8NTgKSqjdhUu2FZw+RK5mGFnT9qTR5/hJHEOl8l5GxP
hxU+Ck1AUwJBAOuEBa+J7hyBvHMZXWFZT4sLIyF8nGnVGKMWnwoqvijm1vtxvXDN
Ch6vxbe6UK2R2wNQcU0yzaw+qVVhFXBGrc0CQAzWAKPn2c+2wBCjjmC1A/HIx+uY
6Ec0d/jYvadQwjXE229HJeLQAtOKH91MpA2UhUcsey3QNotYzSdFeonkMGkCQBUK
a3OeT/6N/KcPyg7rarbSXOYd+t9doWDYoYbIAMsorGY0aCdOuiu5qmFarOCE2ls2
cZjjBAryYjB1hvPL7LUCQC8d8Y1v7z6oS2tMZukAcUUlu38K5dwysdgY3dhgmNWl
NPyXxhPIogoPsSRFbS4PjJpNUFqRY/GTNO/btmBXvts=
-----END RSA PRIVATE KEY-----

378
backend/node_modules/gtoken/test/index.js generated vendored Normal file
View File

@@ -0,0 +1,378 @@
var assert = require('assert');
var fs = require('fs');
var GoogleToken = require('../lib/index.js');
var EMAIL = 'example@developer.gserviceaccount.com';
var KEYFILE = './test/assets/key.pem';
var P12FILE = './test/assets/key.p12';
var KEYFILEJSON = './test/assets/key.json';
var KEYFILENOEMAILJSON = './test/assets/key-no-email.json';
var KEYCONTENTS = fs.readFileSync(KEYFILE);
var KEYJSONCONTENTS = fs.readFileSync(KEYFILEJSON);
var SCOPE1 = 'https://www.googleapis.com/auth/urlshortener';
var SCOPE2 = 'https://www.googleapis.com/auth/drive';
var SCOPES = [SCOPE1, SCOPE2];
var GOOGLE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token';
var GOOGLE_REVOKE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/revoke?token=';
var TESTDATA = {
email: 'email@developer.gserviceaccount.com',
scope: 'scope123', // or space-delimited string of scopes
key: 'abc123key'
};
var TESTDATA_KEYFILE = {
email: 'email@developer.gserviceaccount.com',
scope: 'scope123', // or space-delimited string of scopes
keyFile: KEYFILE
};
var TESTDATA_KEYFILENOEMAIL = {
scope: 'scope123', // or space-delimited string of scopes
keyFile: KEYFILE
};
var TESTDATA_KEYFILEJSON = {
scope: 'scope123', // or space-delimited string of scopes
keyFile: KEYFILEJSON
};
var TESTDATA_KEYFILENOEMAILJSON = {
scope: 'scope123', // or space-delimited string of scopes
keyFile: KEYFILENOEMAILJSON
};
var TESTDATA_P12 = {
email: 'email@developer.gserviceaccount.com',
scope: 'scope123', // or space-delimited string of scopes
keyFile: P12FILE
};
var TESTDATA_P12_NO_EMAIL = {
scope: 'scope123', // or space-delimited string of scopes
keyFile: P12FILE
};
var MIME = {
lookup: function(filename) {
if (filename === P12FILE) {
return 'application/x-pkcs12';
} else if (filename === KEYFILEJSON) {
return 'application/json';
} else {
return '';
}
}
};
var noop = function() {};
describe('gtoken', function() {
it('should exist', function() {
assert.equal(typeof GoogleToken, 'function');
});
it('should work without new or options', function() {
var gtoken = GoogleToken();
assert(gtoken);
});
describe('.iss', function() {
it('should be set from email option', function() {
var gtoken = GoogleToken({
email: EMAIL
});
assert.equal(gtoken.iss, EMAIL);
assert.equal(gtoken.email, undefined);
});
it('should be set from iss option', function() {
var gtoken = GoogleToken({
iss: EMAIL
});
assert.equal(gtoken.iss, EMAIL);
});
it('should be set from email option over iss option', function() {
var gtoken = GoogleToken({
iss: EMAIL,
email: 'another' + EMAIL
});
assert.equal(gtoken.iss, 'another' + EMAIL);
});
});
describe('.scope', function() {
it('should accept strings', function() {
var gtoken = GoogleToken({
scope: 'hello world'
});
assert.equal(gtoken.scope, 'hello world');
});
it('should accept array of strings', function() {
var gtoken = GoogleToken({
scope: ['hello', 'world']
});
assert.equal(gtoken.scope, 'hello world');
});
});
describe('.hasExpired()', function() {
it('should exist', function() {
var gtoken = GoogleToken();
assert.equal(typeof gtoken.hasExpired, 'function');
});
it('should detect expired tokens', function() {
var gtoken = GoogleToken();
assert(gtoken.hasExpired(), 'should be expired without token');
gtoken.token = 'hello';
assert(gtoken.hasExpired(), 'should be expired without expires_at');
gtoken.expires_at = (new Date().getTime()) + 10000;
assert(!gtoken.hasExpired(), 'shouldnt be expired with future date');
gtoken.expires_at = (new Date().getTime()) - 10000;
assert(gtoken.hasExpired(), 'should be expired with past date');
gtoken.expires_at = (new Date().getTime()) + 10000;
gtoken.token = null;
assert(gtoken.hasExpired(), 'should be expired with no token');
});
});
describe('.revokeToken()', function() {
it('should exist', function() {
var gtoken = GoogleToken();
assert.equal(typeof gtoken.revokeToken, 'function');
});
it('should run ._configure()', function(done) {
var gtoken = GoogleToken();
gtoken.token = 'woot';
gtoken._request = function(opts, cb) {
assert.equal(opts, GOOGLE_REVOKE_TOKEN_URL + 'woot');
cb();
};
gtoken._configure = function(options) {
assert(options);
};
gtoken.revokeToken(done);
});
it('should return error when no token set', function(done) {
var gtoken = GoogleToken();
gtoken.token = null;
gtoken.revokeToken(function(err) {
assert(err && err.message);
done();
});
});
});
describe('.getToken()', function() {
it('should exist', function() {
var gtoken = GoogleToken();
assert.equal(typeof gtoken.getToken, 'function');
});
it('should run jws.sign() with correct object', function(done) {
var gtoken = GoogleToken(TESTDATA);
gtoken._signJWT = function(data) {
assert.deepEqual(data.header, {
alg: 'RS256',
typ: 'JWT'
});
assert(data.payload);
assert.equal(data.payload.iss, 'email@developer.gserviceaccount.com');
assert.equal(data.payload.scope, 'scope123');
assert.equal(data.payload.aud, GOOGLE_TOKEN_URL);
assert.equal(data.secret, 'abc123key');
done();
};
gtoken.getToken(noop);
});
it('should read .pem keyFile from file', function(done) {
var gtoken = GoogleToken(TESTDATA_KEYFILE);
gtoken._mime = MIME;
gtoken._signJWT = function(opts, cb) {
cb();
};
gtoken._request = function(opts, cb) {
cb();
};
gtoken.getToken(function(err, token) {
assert.deepEqual(gtoken.key, KEYCONTENTS);
done();
});
});
it('should return error if iss is not set with .pem', function(done) {
var gtoken = GoogleToken(TESTDATA_KEYFILENOEMAIL);
gtoken.getToken(function(err) {
assert.strictEqual(err.code, 'MISSING_CREDENTIALS');
done();
});
});
it('should read .json key from file', function(done) {
var gtoken = GoogleToken(TESTDATA_KEYFILEJSON);
gtoken._mime = MIME;
gtoken._signJWT = function(opts, cb) {
cb();
};
gtoken._request = function(opts, cb) {
cb();
};
gtoken.getToken(function(err, token) {
var parsed = JSON.parse(KEYJSONCONTENTS);
assert.deepEqual(gtoken.key, parsed.private_key);
assert.deepEqual(gtoken.iss, parsed.client_email);
done();
});
});
it('should return error if iss is not set with .json', function(done) {
var gtoken = GoogleToken(TESTDATA_KEYFILENOEMAILJSON);
gtoken.getToken(function(err) {
assert.strictEqual(err.code, 'MISSING_CREDENTIALS');
done();
});
});
it('should return cached token if not expired', function(done) {
var gtoken = GoogleToken(TESTDATA);
gtoken.token = 'mytoken';
gtoken.expires_at = new Date().getTime() + 10000;
gtoken.getToken(function(err, token) {
assert.equal(token, 'mytoken');
done();
});
});
it('should run mime.lookup if keyFile given', function(done) {
var gtoken = GoogleToken(TESTDATA_KEYFILE);
gtoken._mime = {
lookup: function(filename) {
assert.equal(filename, KEYFILE);
done();
}
};
gtoken._request = function(opts, callback) {
callback();
};
gtoken.getToken(noop);
});
it('should run gp12pem if .p12 file is given', function(done) {
var gtoken = GoogleToken(TESTDATA_P12);
gtoken._gp12pem = function(filename, callback) {
assert.equal(filename, P12FILE);
done();
};
gtoken._mime = MIME;
gtoken.getToken(noop);
});
it('should return error if iss is not set with .p12', function(done) {
var gtoken = GoogleToken(TESTDATA_P12_NO_EMAIL);
gtoken.getToken(function(err) {
assert.strictEqual(err.code, 'MISSING_CREDENTIALS');
done();
});
});
describe('request', function() {
it('should be run with correct options', function(done) {
var gtoken = GoogleToken(TESTDATA);
gtoken._signJWT = function sign(opts, callback) {
callback(null, 'signedJWT123');
};
gtoken._request = function(options, callback) {
assert.deepEqual(options, {
method: 'post',
url: GOOGLE_TOKEN_URL,
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: 'signedJWT123'
}
});
callback();
};
gtoken.getToken(done);
});
it('should set and return correct properties on success', function(done) {
var gtoken = GoogleToken(TESTDATA);
var RESPBODY = JSON.stringify({
access_token: 'accesstoken123',
expires_in: 3600,
token_type: 'Bearer'
});
gtoken._request = function(options, callback) {
callback(null, 'res', RESPBODY);
};
gtoken._signJWT = function(opts, callback) {
callback(null, 'signedJWT123');
};
gtoken.getToken(function(err, token) {
assert.deepEqual(gtoken.raw_token, JSON.parse(RESPBODY));
assert.equal(gtoken.token, 'accesstoken123');
assert.equal(gtoken.token, token);
assert.equal(err, null);
assert(gtoken.expires_at >= (new Date()).getTime());
assert(gtoken.expires_at <= (new Date()).getTime() + (3600 * 1000));
done();
});
});
it('should set and return correct properties on error', function(done) {
var ERROR = new Error('An error occurred.');
var gtoken = GoogleToken(TESTDATA);
var RESPBODY = JSON.stringify({
access_token: 'accesstoken123',
expires_in: 3600,
token_type: 'Bearer'
});
gtoken._request = function(options, callback) {
callback(ERROR);
};
gtoken._signJWT = function sign(opts, callback) {
callback(null, 'signedJWT123');
};
gtoken.getToken(function(err, token) {
assert.equal(gtoken.raw_token, null);
assert.equal(gtoken.token, null);
assert.equal(gtoken.token, token);
assert.equal(err, ERROR);
assert.equal(gtoken.expires_at, null);
done();
});
});
});
});
});